gpt4 book ai didi

java - JFreeChart 日期轴格式化问题

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:56:06 26 4
gpt4 key购买 nike

我有一个时间序列图表。我将 x 轴作为日期,Y 轴只是数字。我正在尝试格式化 x 轴上的日期,但我不断收到异常。我的代码如下:

        TimeSeries trueSeries = new TimeSeries("True Data");
TimeSeries regressionSeries = new TimeSeries("Regression Line");
TimeSeries averageSeries = new TimeSeries("Moving Average");

for (Date date : regression.keySet()) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
int year = cal.get(Calendar.YEAR);
regressionSeries.add(new Day(day, month, year),
regression.get(date));
averageSeries.add(new Day(day, month, year),
movingAverage.get(date));
trueSeries.add(new Day(day, month, year), trueData.get(date));
}
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(trueSeries);
dataset.addSeries(regressionSeries);
dataset.addSeries(averageSeries);
JFreeChart chart = ChartFactory.createXYLineChart(
stock.getCompanyName() + " (" + stock.getTicker() + ")",
"Date", // x-axis Label
"Close Price", // y-axis Label
dataset, // Dataset
PlotOrientation.VERTICAL, // Plot Orientation
true, // Show Legend
true, // Use tooltips
false // Configure chart to generate URLs?
);

然后我尝试将 x 轴转换为简单的日期格式,如下所示:

XYPlot plot = (XYPlot) chart.getPlot();
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("dd-MM-yyyy"));

当我运行它时,出现以下异常:

java.lang.ClassCastException: org.jfree.chart.axis.NumberAxis 无法转换为 org.jfree.chart.axis.DateAxis

有人可以告诉我我做错了什么吗?

最佳答案

根据 Adding date/time to JFreeChart graph :

"…you're using ChartFactory.createXYLineChart(), which creates a NumberAxis for the domain. Instead, use ChartFactory.createTimeSeriesChart(), which creates a DateAxis for the domain."—trashgod

关于java - JFreeChart 日期轴格式化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21503426/

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