4

I am having problems creating a timeseries-chart using the JFreechart-Api. In the timeseries Graph i want the x-Axis to display DAYS-MONTH. It does so, however i cannot set the days properly when creating the timeseries-data, due to a SeriesException.

I can provide for a minimal example which can be compile to see how the error happens.

I know that the Month-Class can take a Date as argument

Whats the problem with using Month(Date date)-Consturctor the way i use it? And how can i set the days in timeseries-data, so that they show up in the plot?

(Note: The imports are not included.)

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
        }
    }
}
Katie Hurley
  • 179
  • 9
kiltek
  • 3,183
  • 6
  • 47
  • 70

1 Answers1

5

The exception is :

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.

You are attempting to add twice the same point in the series. Both:

new Month(new Date(2002, 1, 1, 12, 45, 23))  and
new Month(new Date(2002, 1, 2, 12, 45, 23))

represents the same month.

If you want to have two values, one for 1st of january and one for 2nd of January, use org.jfree.data.time.Day :

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

By the way, new Month(new Date(2002, 1, 1, 12, 45, 23)) is February 3902 and not January 2002, as the java.util.Date contructor take as an argument : the year minus 1900 and the month between 0-11

obourgain
  • 8,856
  • 6
  • 42
  • 57