2

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

一切都很好,如果只有一个间隔,或者我没有放大。

有什么想法吗?

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;
    }
}
4

1 回答 1

4

可悲的是,我不知道为什么,但这种变化可能有助于表征异常。单击并拖动以选择略多于所有范围和域的除n天之外的所有天,其中n > 3。当您向左拖动时,向未来平移,您将看到最后一段在第 3 天后消失,并且然后在大约n天后重新出现。该按钮允许重新开始;请注意,刻度标记中午。Reset

附录:在这个论坛帖子中,作者检查了问题并提出了补丁

附录:在错误 ID 3561093中修复。

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);
    }
}
于 2012-08-22T02:07:37.647 回答