我在使用JFreechart -Api 创建时间序列图时遇到问题。在时间序列图中,我希望 x 轴显示 DAYS-MONTH。确实如此,但是由于SeriesException,我无法在创建时间序列数据时正确设置日期。
我可以提供一个可以编译的最小示例,以查看错误是如何发生的。
我知道 Month-Class 可以将 Date 作为参数
以我使用 Month(Date date)-Consturctor 的方式使用它有什么问题?以及如何设置时间序列数据中的日期,以便它们显示在图中?
(注:不包括进口。)
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
        }
    }
}