How to display Date in a X-axis of Line Graph using Jfreechart
Asked Answered
D

1

8

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?

Deegan answered 11/10, 2012 at 10:57 Comment(0)
D
11

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;
}
Dramatization answered 11/10, 2012 at 11:35 Comment(7)
Thanks. But now facing problem with conversion. As i mentioned earlier the time is in HH:MM:SS format. When i do cell.getDateCellValue() it returns me "Sun Dec 31 15:45:01 IST 1899" but all i need is just 15:45:01. Any conversion needed?Deegan
@user1122891: Please edit your question to show new code, where it will be easier to read.Babbler
@user1122891 Take a look at the Day constructor as HSSFCell#getDateCellValue() returns a java.util.Date you can use new Day(date.getDateCellValue())Dramatization
I tried that but as i have mentioned above , it gave me following exception java.lang.IllegalArgumentException: The 'year' argument must be in range 1900 to 9999. at org.jfree.date.SpreadsheetDate.<init>(Unknown Source) at org.jfree.date.SerialDate.createInstance(Unknown Source) at org.jfree.data.time.Day.<init>(Day.java:142) at org.jfree.data.time.Day.<init>(Day.java:126) at Main.createDataset(Main.java:168) at Main.<init>(Main.java:53) at Main.main(Main.java:225)Deegan
@Babbler i tried following one series1. add(new Day(date.getDateCellValue()),29.0) But as i said it gives me an exception saying java.lang.IllegalArgumentException: The 'year' argument must be in range 1900 to 9999.Deegan
Please edit your question to show new code and errors, where it will be easier to read. Are you using org.apache.poi.ss.usermodel.DateUtil?Babbler
i meet the same problem now , the value is HH.MM.SS format ,and i just want to put it in x axis as the value shows;Disinfest

© 2022 - 2024 — McMap. All rights reserved.