gpt4 book ai didi

java - Jfreechart 时间序列图

转载 作者:行者123 更新时间:2023-12-04 02:52:15 24 4
gpt4 key购买 nike

我在创建 时遇到问题时间序列图 使用 JFreechart -阿皮。
在时间序列图中,我希望 x 轴显示 DAYS-MONTH。它这样做了,但是由于 ,我在创建时间序列数据时无法正确设置日期。系列异常 .

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

我知道 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
}
}
}

最佳答案

异常(exception)是:

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 年 2 月而不是 2002 年 1 月,如 java.util.Date构造函数作为参数:年份减去 1900 和 0-11 之间的月份

关于java - Jfreechart 时间序列图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17444501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com