Java – How to display Date in a X-axis of Line Graph using Jfreechart

datasetgraphjavajfreechart

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

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

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?

Best Answer

Use a org.jfree.data.time.TimeSeries to store the values rather that an XYSeries and a TimeSeriesCollection for the Dataset.

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:

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;
}
Related Topic