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?

Prashant
  • 1,144
  • 8
  • 17
  • 28

1 Answers1

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;
}
GrahamA
  • 5,875
  • 29
  • 39
  • 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? – Prashant Oct 11 '12 at 15:25
  • @user1122891: Please edit your question to show new code, where it will be easier to read. – trashgod Oct 11 '12 at 16:03
  • @user1122891 Take a look at the Day constructor as `HSSFCell#getDateCellValue()` returns a `java.util.Date` you can use new `Day(date.getDateCellValue())` – GrahamA Oct 11 '12 at 17:00
  • 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.(Unknown Source) at org.jfree.date.SerialDate.createInstance(Unknown Source) at org.jfree.data.time.Day.(Day.java:142) at org.jfree.data.time.Day.(Day.java:126) at Main.createDataset(Main.java:168) at Main.(Main.java:53) at Main.main(Main.java:225) – Prashant Oct 11 '12 at 17:11
  • @trashgod 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. – Prashant Oct 11 '12 at 17:20
  • 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`](http://stackoverflow.com/a/2079316/230513)? – trashgod Oct 11 '12 at 23:22
  • 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; – iknow34languages Oct 13 '19 at 11:05