本文介绍了JFreeChart:数据在缩放和平移后消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JFreeChart时间序列图表,显示TimePeriodValuesCollection。数据集包含两个间隔。数据显示正确,我可以平移(使用Ctrl-拖动)视图。问题是如果我放大,并且我在缩放视图中向右平移视图,第二个间隔在第一个间隔不再可见后突然消失。

I have a JFreeChart time series chart, which displays a TimePeriodValuesCollection. The dataset contains two intervals. The data is appearing correctly and I am able to pan (with Ctrl-drag) the view. The problem is that if I zoom in, and I pan the view to the right in the zoomed view, the second interval suddenly disappears after the first interval is no longer visible.

一切正常,如果只有一个间隔,或者我没有放大。

Everything is OK, if there is only one interval, or if I don't zoom in.

有什么想法吗?

SSCCE:

public class DisappearingTest {
    public static final SimpleDateFormat oracleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                buildFrame();
            }
        });
    }

    private static void buildFrame()  {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JPanel chartPanel = null;
        try {
            chartPanel = createChartPanel();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        f.add(chartPanel);

        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    static JPanel createChartPanel() throws ParseException {
        TimePeriodValues timePeriodValues = new TimePeriodValues("Test");

        Date startDate1 = oracleDateFormat.parse("2011-01-01");
        Date endDate1 = oracleDateFormat.parse("2011-12-31");

        timePeriodValues.add(new Second(startDate1), 0.3);
        timePeriodValues.add(new Second(endDate1), 0.3);

        Date startDate2 = oracleDateFormat.parse("2012-01-01");
        Date endDate2 = oracleDateFormat.parse("2015-12-31");

        timePeriodValues.add(new Second(startDate2), 0.5);
        timePeriodValues.add(new Second(endDate2), 0.5);

        TimePeriodValuesCollection dataSet = new TimePeriodValuesCollection();
        dataSet.addSeries(timePeriodValues);

        JFreeChart chart = ChartFactory.createTimeSeriesChart("Title", "Time", "Value", dataSet, true, true, false);
        XYPlot plot = chart.getXYPlot();
        plot.setDomainPannable(true);
        plot.setRangePannable(true);

        ChartPanel chartPanel = new ChartPanel(chart);

        return chartPanel;
    }
}


推荐答案

可悲,我不知道为什么,但这种变化可能有助于表征异常。单击并拖动以选择稍微超过所有范围和除 n 以外的所有范围,其中 n > 3.当您控制 - 向左拖动时,向其平移未来,您会看到最后一段在第3天后消失,然后重新出现 n 天后。 按钮允许重新开始;请注意,中午标记为。

Sadly, I have no idea why, but this variation may help characterize the anomaly. Click and drag to select slightly more than all of the range and all but n days of the domain, where n > 3. As you control-drag left, panning toward the future, you'll see the last segment disappear after day 3 and then re-appear about n days later. The button allows starting over; note that ticks mark noon.

附录:在此,作者检查问题并提出。

Addendum: In this forum thread, the author examines the problem and proposes a patch.

附录:修正于。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimePeriodValues;
import org.jfree.data.time.TimePeriodValuesCollection;

/**
 * @see http://stackoverflow.com/a/12065474/230513
 */
public class DisappearingTest {

    private static ChartPanel chartPanel;
    private static ValueAxis range;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                buildFrame();
            }
        });
    }

    private static void buildFrame() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        initChartPanel();
        f.add(chartPanel, BorderLayout.CENTER);
        JPanel panel = new JPanel();
        panel.add(new JButton(new AbstractAction("Reset") {

            @Override
            public void actionPerformed(ActionEvent e) {
                chartPanel.restoreAutoBounds();
                range.setLowerBound(0);
                range.setUpperBound(1);
            }
        }));
        f.add(panel, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    static void initChartPanel() {
        TimePeriodValues values = new TimePeriodValues("Test");
        values.add(new Day(1, 1, 2012), 0.2);
        values.add(new Day(2, 1, 2012), 0.2);
        values.add(new Day(3, 1, 2012), 0.8);
        values.add(new Day(1, 2, 2012), 0.8);
        TimePeriodValuesCollection data = new TimePeriodValuesCollection();
        data.addSeries(values);
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
            "Title", "Time", "Value", data, true, true, false);
        XYPlot plot = chart.getXYPlot();
        plot.setDomainPannable(true);
        range = plot.getRangeAxis();
        range.setLowerBound(0);
        range.setUpperBound(1);
        chartPanel = new ChartPanel(chart);
        chartPanel.setMouseWheelEnabled(true);
    }
}

这篇关于JFreeChart:数据在缩放和平移后消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 07:52