java 如何使用 Jfreechart 在折线图的 X 轴中显示日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12837986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display Date in a X-axis of Line Graph using Jfreechart
提问by Prashant
I am trying to display a Line Graph with Time(HH:MM:SS) as X-axis and Number(as Y-Axis). The read data from "Time" column is of the format HH:MM:SS. The way i am populating dataset from which chart is construted is as follows
我正在尝试以时间(HH:MM:SS)作为X轴和数字(作为Y轴)显示折线图。从“时间”列读取的数据格式为 HH:MM:SS。我填充从中构建图表的数据集的方式如下
for (Row row : sheet)
{
Double sar_Val = poiGetCellValue(sar);
Double date_val = poiGetCellValue(date);
if(sar_Val != null && date_val != null)
{
series1.add(date_val,sar_Val);
}
dataset.addSeries(series1);
}
//poiGetCellValue in the above code returns a double based on the data type
//上面代码中的poiGetCellValue根据数据类型返回一个double
Problem is that i have to convert the data under "Time" column which is in format HH:MM:SS to some double value and populate the series1 since add function take only double values. How to display the time in X-Axis once i have converted the value to double Or is there any other method to add to XYseries?
问题是我必须将格式为 HH:MM:SS 的“时间”列下的数据转换为一些双精度值并填充 series1,因为添加函数只采用双精度值。将值转换为 double 后如何在 X 轴中显示时间 或者是否有任何其他方法可以添加到 XYseries?
回答by GrahamA
Use a org.jfree.data.time.TimeSeries
to store the values rather that an XYSeries
and a TimeSeriesCollection
for the Dataset.
使用org.jfree.data.time.TimeSeries
到,而存储的值,一个XYSeries
和一个TimeSeriesCollection
为数据集。
This will allow you to add a RegularTimePeriod
and a double rather than two doubles. RegularTimePeriod
is implemented by Day
so you final code would look like this:
这将允许您添加一个RegularTimePeriod
和一个双,而不是两个双。 RegularTimePeriod
是由实现的,Day
因此您的最终代码如下所示:
private XYDataset createDataset() {
TimeSeries series1 = new TimeSeries("Data");
Date date = new Date();
series1.add(new Day(date),46.6);
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(series1);
return dataset;
}