I am trying to data from our database onto a Time Series chart. Currently the data I am trying to chart is all from the same day (this is test data won't always by that way).
Can a time series chart multiple points from the same day?
Also is the a way on the horizontal date axis so that it does not attempt to print the entire date for every point. I need a way to compact this because now I am only showing about 4 points on the chart.
Thanks,
Erich
Time series chart questions
Re: Time series chart questions
The data for a time series chart is from the XYDataset interface. The X values are interpreted by the date axis to be milliseconds since midnight GMT, 1 Jan 1970 (the same encoding used by java.util.Date). Your dataset can return any date-time in that format.
If you use the TimeSeriesCollection class as an XYDataset (a good idea, but certainly not compulsory) then you can only have one data value per time period in each time series - that's a constraint applied by the BasicTimeSeries class, not the XYDataset interface or the chart itself. When you construct a BasicTimeSeries, it defaults to daily data...one value per day. But you can specify the period in the constructor as Hour, Minute, Second or Millisecond.
I'm not sure what you mean about the date axis printing dates for every point. If you have the auto-tick-selection ON (the default) then you most likely won't see labels for every date. If it is still a problem, e-mail me a (small) image file showing the problem.
Regards,
DG.
If you use the TimeSeriesCollection class as an XYDataset (a good idea, but certainly not compulsory) then you can only have one data value per time period in each time series - that's a constraint applied by the BasicTimeSeries class, not the XYDataset interface or the chart itself. When you construct a BasicTimeSeries, it defaults to daily data...one value per day. But you can specify the period in the constructor as Hour, Minute, Second or Millisecond.
I'm not sure what you mean about the date axis printing dates for every point. If you have the auto-tick-selection ON (the default) then you most likely won't see labels for every date. If it is still a problem, e-mail me a (small) image file showing the problem.
Regards,
DG.
Re: Time series chart questions
David,
I've similar question. I'm getting data from mysql server in YYYY-MM-DD HH:MM:SS format. Using SimpeDateFormat, I converted it into "Date" (which is seconds from Jan 1, 1970).
Then I created a BasicTimeSeries series and used Day(Date) to create a new Day. This Date is the object returned from the SimpleDateFormat().
Since each bar from the database has a unique timestamp, each Date will be different by atleast 1 second.
When adding new elements to the BasicTimeSeries, I get exception
"TimeSeries.add(...): time period already exists."
Here is a snipped of the code
String date=rset.getString(1).trim();
String time=rset.getString(2).trim();
double price=rset.getDouble(3);
String str_sqldatetime = date + " " + time;
Date sqldatetime = SQLDateTime.parseDay(str_sqldatetime);
Date mydate = new Day(sqldatetime);
series.add(mydate, price); <== Exception is here ===
---------------here is output from the program -----
2002-04-10 09:30:00 1355.91 1 Wed Apr 10 09:30:00 PDT 2002
2002-04-10 09:31:11 1357.76 1 Wed Apr 10 09:31:11 PDT 2002
StocksMy: JDBC exception TimeSeries.add(...): time period already exist
--------------------------------------------------------------
As we can see the first two columns (date and time) are unique. Third column is the price and 4th is the volume. 1-4 columns is retrieved from the sqlserver. remaining is the printing of the sqldatetime object to make sure that the parsing of 1st column is accurate.
I'm using 0.8.1 version of JFreeChart.
Thanks
Mudit
I've similar question. I'm getting data from mysql server in YYYY-MM-DD HH:MM:SS format. Using SimpeDateFormat, I converted it into "Date" (which is seconds from Jan 1, 1970).
Then I created a BasicTimeSeries series and used Day(Date) to create a new Day. This Date is the object returned from the SimpleDateFormat().
Since each bar from the database has a unique timestamp, each Date will be different by atleast 1 second.
When adding new elements to the BasicTimeSeries, I get exception
"TimeSeries.add(...): time period already exists."
Here is a snipped of the code
String date=rset.getString(1).trim();
String time=rset.getString(2).trim();
double price=rset.getDouble(3);
String str_sqldatetime = date + " " + time;
Date sqldatetime = SQLDateTime.parseDay(str_sqldatetime);
Date mydate = new Day(sqldatetime);
series.add(mydate, price); <== Exception is here ===
---------------here is output from the program -----
2002-04-10 09:30:00 1355.91 1 Wed Apr 10 09:30:00 PDT 2002
2002-04-10 09:31:11 1357.76 1 Wed Apr 10 09:31:11 PDT 2002
StocksMy: JDBC exception TimeSeries.add(...): time period already exist
--------------------------------------------------------------
As we can see the first two columns (date and time) are unique. Third column is the price and 4th is the volume. 1-4 columns is retrieved from the sqlserver. remaining is the printing of the sqldatetime object to make sure that the parsing of 1st column is accurate.
I'm using 0.8.1 version of JFreeChart.
Thanks
Mudit
Re: Time series chart questions
I just found my answer.
Day class only uses year, month and date from the "Date" and not hh/mm/ss when it constructs a calendar !
-----------------------------------------------------------------------
public Day(Date time, TimeZone zone) {
Calendar calendar = Calendar.getInstance(zone);
calendar.setTime(time);
int d = calendar.get(Calendar.DAY_OF_MONTH);
int m = calendar.get(Calendar.MONTH)+1;
int y = calendar.get(Calendar.YEAR);
this.serialDate = SerialDate.createInstance(d, m, y);
}
-----------------------------------------------------------------------
So, how do I store the "Date" in the BasicTimeSeries so that it takes into account the date and time of the day. I dont think BasicTimeSeries is adequate for this purpose.
Thanks
Mudit
Day class only uses year, month and date from the "Date" and not hh/mm/ss when it constructs a calendar !
-----------------------------------------------------------------------
public Day(Date time, TimeZone zone) {
Calendar calendar = Calendar.getInstance(zone);
calendar.setTime(time);
int d = calendar.get(Calendar.DAY_OF_MONTH);
int m = calendar.get(Calendar.MONTH)+1;
int y = calendar.get(Calendar.YEAR);
this.serialDate = SerialDate.createInstance(d, m, y);
}
-----------------------------------------------------------------------
So, how do I store the "Date" in the BasicTimeSeries so that it takes into account the date and time of the day. I dont think BasicTimeSeries is adequate for this purpose.
Thanks
Mudit
(Done) Re: Time series chart questions
Okay,
Once again I found answers for all my questions. Should have read DG's constructor's sentence again and again. Now using Second.class in the constructor. Also, Second.parseSecond() is doing the work for me to parse the yyyy-mm-dd hh:mm:ss, so I dont need to invent a new class. Thats great.
Now my new question, I want to print the date on the x-axis whenever it changes. For eg, say I've 1 hour bar data for last 5 days. Each day has 8 bars of data. Say the data starts from April 1st, 2002.
So, I want to see Apr 1 on the 1st x-tic and then 10:30am, 11:30am etc etc till the 9th bar. On the 9th bar, I want to see Apr 2. Then again I want to see 10:30am, and so on. If space is issue on x-axis then 0401, 0402 will do.
Also, if not hour bars, then just how about printing only on the bars when the date changes. So, it'll be easy to see the data for each day. Right now by default, its printing the hour and min in hh:mm. I'd rather see just the bars where the day is different from the previous bar.
Thanks
Mudit
Once again I found answers for all my questions. Should have read DG's constructor's sentence again and again. Now using Second.class in the constructor. Also, Second.parseSecond() is doing the work for me to parse the yyyy-mm-dd hh:mm:ss, so I dont need to invent a new class. Thats great.
Now my new question, I want to print the date on the x-axis whenever it changes. For eg, say I've 1 hour bar data for last 5 days. Each day has 8 bars of data. Say the data starts from April 1st, 2002.
So, I want to see Apr 1 on the 1st x-tic and then 10:30am, 11:30am etc etc till the 9th bar. On the 9th bar, I want to see Apr 2. Then again I want to see 10:30am, and so on. If space is issue on x-axis then 0401, 0402 will do.
Also, if not hour bars, then just how about printing only on the bars when the date changes. So, it'll be easy to see the data for each day. Right now by default, its printing the hour and min in hh:mm. I'd rather see just the bars where the day is different from the previous bar.
Thanks
Mudit
Re: (Done) Re: Time series chart questions
Hi Mudit,
This is another feature that JFreeChart doesn't yet support. I need to find a general way to change the tick format for the first tick within a major group.
Your requirement is to display the date for the first tick in every day, and just the time for all other ticks (if I understand correctly). Similar requirements would be to:
- display the year for the first tick, and just the month (or quarter or week) for subsequent ticks;
- display the month for the first tick, and just the day of the month for the remaining ticks;
...and there are probably others.
I'll add it to the to-do list.
Regards,
DG.
This is another feature that JFreeChart doesn't yet support. I need to find a general way to change the tick format for the first tick within a major group.
Your requirement is to display the date for the first tick in every day, and just the time for all other ticks (if I understand correctly). Similar requirements would be to:
- display the year for the first tick, and just the month (or quarter or week) for subsequent ticks;
- display the month for the first tick, and just the day of the month for the remaining ticks;
...and there are probably others.
I'll add it to the to-do list.
Regards,
DG.