JFreeChart时间序列图表

4
我在使用JFreechart-API创建时间序列图时遇到了问题。我希望x轴显示“日-月”格式的日期,但是由于SeriesException错误,无法正确设置日期。

我可以提供一个最小的示例代码,以便查看错误信息。

我知道Month类可以接受Date类型的参数。

请问我使用Month(Date date)构造函数的方式有什么问题?如何在时间序列数据中设置日期,以便它们显示在图表中?

(注:导入部分未包含在内。)
public class MyTimeSeriesGraphMinimalExample {
    public static void main(String args[]) {
        TimeSeries timeseries = new TimeSeries("Series 1");
        //works not
        timeseries.add(new Month(new Date(2002, 1, 1, 12, 45, 23)),
            100.10000000000002D);//day 1
        timeseries.add(new Month(new Date(2002, 1, 2, 12, 45, 23)),
            694.10000000000002D);// day 2

        // works timeseries.add(new Month(3, 2002), 734.39999999999998D);
        // works timeseries.add(new Month(4, 2002), 453.19999999999999D);

        TimeSeries timeseries1 = new TimeSeries("Series 2");

                    //works not
        timeseries1.addOrUpdate(new Month(new Date(2002, 1, 1, 12, 45, 23)),
                234.09999999999999D);// day 1
        timeseries1.addOrUpdate(new Month(new Date(2002, 1, 2, 12, 45, 23)),
                623.70000000000005D);// day 2

        //works timeseries1.add(new Month(3, 2002), 642.5D);
        //works timeseries1.add(new Month(4, 2002), 700.39999999999998D);

        TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
        timeseriescollection.addSeries(timeseries);
        timeseriescollection.addSeries(timeseries1);
        XYDataset xydataset = timeseriescollection;

    //chart-visual-property-settings
        JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(
            "Time Series Demo 3", "Time", "Value", xydataset, true, true,
            false);
        XYPlot xyplot = (XYPlot) jfreechart.getPlot();
        DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
        dateaxis.setTickUnit(new DateTickUnit(DateTickUnitType.MONTH, 1,
            new SimpleDateFormat("dd-MMM")));
        dateaxis.setVerticalTickLabels(true);
        XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot
            .getRenderer();
        xylineandshaperenderer.setBaseShapesVisible(true);
        xylineandshaperenderer.setSeriesFillPaint(0, Color.red);
        xylineandshaperenderer.setSeriesFillPaint(1, Color.green);
        xylineandshaperenderer.setSeriesPaint(0, Color.red);
        xylineandshaperenderer.setSeriesPaint(1, Color.green);
        xylineandshaperenderer.setUseFillPaint(true);
        xylineandshaperenderer
            .setLegendItemToolTipGenerator(new StandardXYSeriesLabelGenerator(
                    "Tooltip {0}"));

    //draw
        try {
            ChartUtilities.saveChartAsJPEG(new File("C:/series.jpeg"),
                jfreechart, 600, 500);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}
1个回答

5
异常是:
org.jfree.data.general.SeriesException: 
You are attempting to add an observation for the time period February 3902 but 
the series already contains an observation for that time period. Duplicates 
are not permitted. Try using the addOrUpdate() method.

您正在尝试在系列中添加相同的点两次。它们分别是:
new Month(new Date(2002, 1, 1, 12, 45, 23))  and
new Month(new Date(2002, 1, 2, 12, 45, 23))

表示相同的月份。

如果您想要两个值,一个是1月1日的值,另一个是1月2日的值,则可以使用org.jfree.data.time.Day

  timeseries.add(new Day(1, 1, 2002), 100.10000000000002D);
  timeseries.add(new Day(2, 1, 2002), 694.10000000000002D);

顺便提一下,new Month(new Date(2002, 1, 1, 12, 45, 23)) 表示的是 3902 年的二月份,而不是 2002 年的一月份。因为 java.util.Date 构造函数接受的参数格式为:年份减去 1900 年,月份从 0 到 11。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接