2013-07-03 186 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年之間的月份