2013-07-03 157 views
2

我在创建时间序列图使用JFreechart -Api时出现问题。 在时间序列图中我想让x轴显示DAYS-MONTH。它是这样做的,但是由于SeriesException,我创建时间序列数据时无法正确设置日期。

Jfreechart时间序列图

我可以提供一个可以编译以查看错误如何发生的最小示例。

我知道一个月的类可以采取一个日期作为参数

请告诉我用月(日期为准)-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 
     } 
    } 
} 
+0

编译示例的+1。 – obourgain

回答

3

唯一的例外是:

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日,一个用于月份的第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))是2月3902,而不是2002年1月,作为java.util.Date构造者以参数为例:年份减去1900年和0-11年之间的月份