JFreeChart:如何在平移时自动调整范围轴最小/最大值(具有固定域轴范围)

时间:2017-02-09 08:38:25

标签: jfreechart

我有一个时间序列图表,其中域范围是自动修复的,因此,一次只能在图表中看到大部分数据集。我只启用了域名平移。

我正在寻找一种功能,当用户在任一方向(即向左或向右)平移时,范围轴最小/最大值应根据图表中可见的数据自动调整(而不是根据完整的数据集) 。 JFreeChart-1.0.19是否提供开箱即用的任何此类功能?

另一方面,我知道当按时间序列添加动态数据(具有固定的域轴范围)时,范围轴上的setAutoAdjust将根据图表上可见的数据自动调整最小值/最大值。

类似的问题被问到here

请在下面找到示例代码: TimeSeriesDemo.java

import java.text.SimpleDateFormat;
import javax.swing.JPanel;
import org.jfree.ui.ApplicationFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.XYDataset;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.axis.DateAxis;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.Day;
import org.jfree.chart.ChartPanel;
import org.jfree.ui.RefineryUtilities;

public class TimeSeriesDemo extends ApplicationFrame {
    public TimeSeriesDemo1(String title) {
        super(title);
        JPanel chartPanel = createDemoPanel();
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);
    }

    private static JFreeChart createChart(XYDataset dataset) {
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
            "Nifty 50 Close",  // title
            "Date",             // x-axis label
            "Value",   // y-axis label
            dataset,            // data
            true,               // create legend?
            true,               // generate tooltips?
            false               // generate URLs?
        );

        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setDomainPannable(true);
        plot.setRangePannable(false);

        XYItemRenderer r = plot.getRenderer();
        if (r instanceof XYLineAndShapeRenderer) {
            XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
            renderer.setBaseShapesVisible(false);
        }

        DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("dd-MMM-yyyy"));
        axis.setFixedAutoRange(6.0 * 24 * 60 * 60 * 1000);

        return chart;
    }

    private static XYDataset createDataset() {
        TimeSeries s1 = new TimeSeries("Nifty 50");
        s1.add(new Day(11,1,2017),8380.65);
        s1.add(new Day(12,1,2017),8407.2);
        s1.add(new Day(13,1,2017),8400.35);
        s1.add(new Day(16,1,2017),8412.8);
        s1.add(new Day(17,1,2017),8398);
        s1.add(new Day(18,1,2017),8417);
        s1.add(new Day(19,1,2017),8435.1);
        s1.add(new Day(20,1,2017),8349.35);
        s1.add(new Day(23,1,2017),8391.5);
        s1.add(new Day(24,1,2017),8475.8);
        s1.add(new Day(25,1,2017),8602.75);
        s1.add(new Day(27,1,2017),8641.25);
        s1.add(new Day(30,1,2017),8632.75);
        s1.add(new Day(31,1,2017),8561.3);
        s1.add(new Day(1,2,2017),8716.4);
        s1.add(new Day(2,2,2017),8734.25);
        s1.add(new Day(3,2,2017),8740.95);
        s1.add(new Day(6,2,2017),8801.05);
        s1.add(new Day(7,2,2017),8768.3);
        s1.add(new Day(8,2,2017),8769.05);

        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(s1);

        return dataset;
    }

    public static JPanel createDemoPanel() {
        JFreeChart chart = createChart(createDataset());
        ChartPanel cp = new ChartPanel(chart);
        cp.setDomainZoomable(false);
        cp.setRangeZoomable(false);
        return cp;
    }

    public static void main(String[] args) {
        TimeSeriesDemo1 demo = new TimeSeriesDemo1("Time Series Demo 1");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }

}

0 个答案:

没有答案