Time Series Problem
Time Series Problem
Hello,
i'll be thankfull to you if any one can get a solution to this problem.
i'm using "TimeSeries" graph to generate a graph which has time on the x-axis. well,i would like to have time say for 9.30,10.00,10.30,11.00.......etc i.e, half hour spacing, on my x-axis and print all the values falling between this range in the graph.
i could generate the graph for days in the x-axis,but unsuccessful in genarating the time on the x-axis. i hope some can get me the solution
thanks in advance. truly
RaviTeja Bellamkonda
Fn Star.
i'll be thankfull to you if any one can get a solution to this problem.
i'm using "TimeSeries" graph to generate a graph which has time on the x-axis. well,i would like to have time say for 9.30,10.00,10.30,11.00.......etc i.e, half hour spacing, on my x-axis and print all the values falling between this range in the graph.
i could generate the graph for days in the x-axis,but unsuccessful in genarating the time on the x-axis. i hope some can get me the solution
thanks in advance. truly
RaviTeja Bellamkonda
Fn Star.
Re: Time Series Problem
Ravi,
Dont use timeseries. Extended the HorizontalNumberAxis and override refreshTicks method. Also implement the dataset you are using and your own method for providing labels to the newly extended HorizontalNumberAxis.
You must have an array where you are storing the time for each bar. The index of the bar (1,2, 3, etc etc) is the X value, an int[] array. In your overriding getXValue, you return this value. When the newHorizontalNumberAxis refreshTicks method is making a new label, call your method to return the time ("11:30" etc etc) based on this X value.
So, you treat the xaxis as a simple number array and provide your own labels.
I had to extend almost every single class in JFreeChart. For eg, in order to draw trend lines, I had to extended JFreeChartPanel so that I can catch mouse*listeners.
Thanks
Mudit
Dont use timeseries. Extended the HorizontalNumberAxis and override refreshTicks method. Also implement the dataset you are using and your own method for providing labels to the newly extended HorizontalNumberAxis.
You must have an array where you are storing the time for each bar. The index of the bar (1,2, 3, etc etc) is the X value, an int[] array. In your overriding getXValue, you return this value. When the newHorizontalNumberAxis refreshTicks method is making a new label, call your method to return the time ("11:30" etc etc) based on this X value.
So, you treat the xaxis as a simple number array and provide your own labels.
I had to extend almost every single class in JFreeChart. For eg, in order to draw trend lines, I had to extended JFreeChartPanel so that I can catch mouse*listeners.
Thanks
Mudit
Re: Time Series Problem
Hi Ravi,
Try this sample code (against 0.8.1) with and without the four lines that I have commented out...sorry for the formatting!
package com.jrefinery.chart.demo;
import java.awt.Stroke;
import java.awt.BasicStroke;
import java.util.Calendar;
import com.jrefinery.data.BasicTimeSeries;
import com.jrefinery.data.Hour;
import com.jrefinery.data.Day;
import com.jrefinery.data.TimeSeriesCollection;
import com.jrefinery.ui.ApplicationFrame;
import com.jrefinery.chart.JFreeChart;
import com.jrefinery.chart.ChartFactory;
import com.jrefinery.chart.JFreeChartPanel;
import com.jrefinery.chart.HorizontalDateAxis;
import com.jrefinery.chart.DateUnit;
public class TimeSeriesDemo extends ApplicationFrame {
protected BasicTimeSeries series;
/**
* A demonstration application showing a quarterly time series containing a null value.
*/
public TimeSeriesDemo() {
this.series = new BasicTimeSeries("Hourly Data", Hour.class);
this.series.add(new Hour(9, new Day(16, 5, 2002)), 500.2);
this.series.add(new Hour(10, new Day(16, 5, 2002)), 694.1);
this.series.add(new Hour(11, new Day(16, 5, 2002)), 734.4);
this.series.add(new Hour(12, new Day(16, 5, 2002)), 453.2);
this.series.add(new Hour(13, new Day(16, 5, 2002)), 500.2);
this.series.add(new Hour(14, new Day(16, 5, 2002)), null);
this.series.add(new Hour(15, new Day(16, 5, 2002)), 734.4);
this.series.add(new Hour(16, new Day(16, 5, 2002)), 453.2);
TimeSeriesCollection dataset = new TimeSeriesCollection(series);
JFreeChart chart = ChartFactory.createTimeSeriesChart("Time Series Demo", "Time", "Value",
dataset, true);
HorizontalDateAxis axis = (HorizontalDateAxis)chart.getPlot().getHorizontalAxis();
//axis.setAutoTickUnitSelection(false);
//DateUnit unit = new DateUnit(Calendar.MINUTE, 30);
//axis.getTickLabelFormatter().applyPattern("HH:mm");
//axis.setTickUnit(unit);
// set the stroke for each series...
Stroke[] seriesStrokeArray = new Stroke[1];
seriesStrokeArray[0] = new BasicStroke(4.0f);
chart.getPlot().setSeriesStroke(seriesStrokeArray);
JFreeChartPanel chartPanel = new JFreeChartPanel(chart);
this.setContentPane(chartPanel);
}
/**
* Starting point for the demonstration application.
*/
public static void main(String[] args) {
TimeSeriesDemo demo = new TimeSeriesDemo();
demo.pack();
demo.setVisible(true);
}
}
Try this sample code (against 0.8.1) with and without the four lines that I have commented out...sorry for the formatting!
package com.jrefinery.chart.demo;
import java.awt.Stroke;
import java.awt.BasicStroke;
import java.util.Calendar;
import com.jrefinery.data.BasicTimeSeries;
import com.jrefinery.data.Hour;
import com.jrefinery.data.Day;
import com.jrefinery.data.TimeSeriesCollection;
import com.jrefinery.ui.ApplicationFrame;
import com.jrefinery.chart.JFreeChart;
import com.jrefinery.chart.ChartFactory;
import com.jrefinery.chart.JFreeChartPanel;
import com.jrefinery.chart.HorizontalDateAxis;
import com.jrefinery.chart.DateUnit;
public class TimeSeriesDemo extends ApplicationFrame {
protected BasicTimeSeries series;
/**
* A demonstration application showing a quarterly time series containing a null value.
*/
public TimeSeriesDemo() {
this.series = new BasicTimeSeries("Hourly Data", Hour.class);
this.series.add(new Hour(9, new Day(16, 5, 2002)), 500.2);
this.series.add(new Hour(10, new Day(16, 5, 2002)), 694.1);
this.series.add(new Hour(11, new Day(16, 5, 2002)), 734.4);
this.series.add(new Hour(12, new Day(16, 5, 2002)), 453.2);
this.series.add(new Hour(13, new Day(16, 5, 2002)), 500.2);
this.series.add(new Hour(14, new Day(16, 5, 2002)), null);
this.series.add(new Hour(15, new Day(16, 5, 2002)), 734.4);
this.series.add(new Hour(16, new Day(16, 5, 2002)), 453.2);
TimeSeriesCollection dataset = new TimeSeriesCollection(series);
JFreeChart chart = ChartFactory.createTimeSeriesChart("Time Series Demo", "Time", "Value",
dataset, true);
HorizontalDateAxis axis = (HorizontalDateAxis)chart.getPlot().getHorizontalAxis();
//axis.setAutoTickUnitSelection(false);
//DateUnit unit = new DateUnit(Calendar.MINUTE, 30);
//axis.getTickLabelFormatter().applyPattern("HH:mm");
//axis.setTickUnit(unit);
// set the stroke for each series...
Stroke[] seriesStrokeArray = new Stroke[1];
seriesStrokeArray[0] = new BasicStroke(4.0f);
chart.getPlot().setSeriesStroke(seriesStrokeArray);
JFreeChartPanel chartPanel = new JFreeChartPanel(chart);
this.setContentPane(chartPanel);
}
/**
* Starting point for the demonstration application.
*/
public static void main(String[] args) {
TimeSeriesDemo demo = new TimeSeriesDemo();
demo.pack();
demo.setVisible(true);
}
}
Re: Time Series Problem
David,
The issue with any timeseries class is that when the next day starts or there is weekend, it tends to put the missing time. We had discussed it before and there has to be some way from JFreeChart side to take that into account. One day intraday bars will be fine. The problem is when you try to plot two days of intraday data using Hour class. Then you will see a big gap between 4:00pm and 9:30am next day.
Extending the numberAxis and providing your own methods avoids these problems. May be you can add something similar Axis class where the dataset provides the labels for x/y axis. Something like labelAxis.
Thanks
Mudit
The issue with any timeseries class is that when the next day starts or there is weekend, it tends to put the missing time. We had discussed it before and there has to be some way from JFreeChart side to take that into account. One day intraday bars will be fine. The problem is when you try to plot two days of intraday data using Hour class. Then you will see a big gap between 4:00pm and 9:30am next day.
Extending the numberAxis and providing your own methods avoids these problems. May be you can add something similar Axis class where the dataset provides the labels for x/y axis. Something like labelAxis.
Thanks
Mudit
Re: Time Series Problem
OK, I've misunderstood the original question. At some point I'll put together a segmented date axis that allows you to skip weekends and non-trading hours...but that is a fairly big undertaking so it won't be happening really soon.
Regards,
DG.
Regards,
DG.
Re: Time Series Problem
Hello David & Mudit ,
i 'm really thankful to David for the code provided by him .
Infact we are intially trying to generate a intraday chart(for a single day).
i have one more problem which i belive u can get me the solution.
As i said earlier,i have on my x-axis the time say 9:00,9:30,10:00,10:30 ......
etc .But i may have values falling in between this ranges ,and i would like to have my points plotted in the graph.
To be more clear i may have a value at 9:01,9:02,9:05 .....hours.
i'll have to print these values on the graph,but my x-axis must be showing the points with half hour spacing
is this possible in this version.
i hope you can get me the solution.
thanking you once again.
bye
Ravi Teja Bellamkonda
fnstar
i 'm really thankful to David for the code provided by him .
Infact we are intially trying to generate a intraday chart(for a single day).
i have one more problem which i belive u can get me the solution.
As i said earlier,i have on my x-axis the time say 9:00,9:30,10:00,10:30 ......
etc .But i may have values falling in between this ranges ,and i would like to have my points plotted in the graph.
To be more clear i may have a value at 9:01,9:02,9:05 .....hours.
i'll have to print these values on the graph,but my x-axis must be showing the points with half hour spacing
is this possible in this version.
i hope you can get me the solution.
thanking you once again.
bye
Ravi Teja Bellamkonda
fnstar
Re: Time Series Problem
Here is another variation of the sample application...I've just changed the import statements and the way the time series is constructed (it now uses Minute instead of Hour as the time period, you could try Second or Millisecond if your data is that fine-grained).
Note that the date axis is independent from the data...all it sees are numbers that it interprets as the number of milliseconds since midnight GMT 1-Jan-1970. It doesn't care if the x-values from the XYDataset interface are regular or irregular in their spacing, it just works out where along the line they should be plotted.
Regards,
DG.
package com.jrefinery.chart.demo;
import java.awt.Stroke;
import java.awt.BasicStroke;
import java.util.Calendar;
import com.jrefinery.data.BasicTimeSeries;
import com.jrefinery.data.Minute;
import com.jrefinery.data.Hour;
import com.jrefinery.data.Day;
import com.jrefinery.data.TimeSeriesCollection;
import com.jrefinery.ui.ApplicationFrame;
import com.jrefinery.chart.JFreeChart;
import com.jrefinery.chart.ChartFactory;
import com.jrefinery.chart.JFreeChartPanel;
import com.jrefinery.chart.HorizontalDateAxis;
import com.jrefinery.chart.DateUnit;
public class TimeSeriesDemo extends ApplicationFrame {
protected BasicTimeSeries series;
/**
* A demonstration application showing a quarterly time series containing a null value.
*/
public TimeSeriesDemo() {
this.series = new BasicTimeSeries("Per Minute Data", Minute.class);
Day today = new Day(17, 5, 2002);
this.series.add(new Minute( 1, new Hour(9, today)), 500.2);
this.series.add(new Minute( 5, new Hour(9, today)), 694.1);
this.series.add(new Minute(34, new Hour(9, today)), 734.4);
this.series.add(new Minute(47, new Hour(9, today)), 453.2);
this.series.add(new Minute(27, new Hour(10, today)), 500.2);
this.series.add(new Minute( 5, new Hour(12, today)), 400.5);
this.series.add(new Minute(34, new Hour(14, today)), 734.4);
this.series.add(new Minute(49, new Hour(15, today)), 453.2);
TimeSeriesCollection dataset = new TimeSeriesCollection(series);
JFreeChart chart = ChartFactory.createTimeSeriesChart("Time Series Demo", "Time", "Value",
dataset, true);
HorizontalDateAxis axis = (HorizontalDateAxis)chart.getPlot().getHorizontalAxis();
//axis.setAutoTickUnitSelection(false);
//DateUnit unit = new DateUnit(Calendar.MINUTE, 30);
//axis.getTickLabelFormatter().applyPattern("HH:mm");
//axis.setTickUnit(unit);
// set the stroke for each series...
Stroke[] seriesStrokeArray = new Stroke[1];
seriesStrokeArray[0] = new BasicStroke(4.0f);
chart.getPlot().setSeriesStroke(seriesStrokeArray);
JFreeChartPanel chartPanel = new JFreeChartPanel(chart);
this.setContentPane(chartPanel);
}
/**
* Starting point for the demonstration application.
*/
public static void main(String[] args) {
TimeSeriesDemo demo = new TimeSeriesDemo();
demo.pack();
demo.setVisible(true);
}
}
Note that the date axis is independent from the data...all it sees are numbers that it interprets as the number of milliseconds since midnight GMT 1-Jan-1970. It doesn't care if the x-values from the XYDataset interface are regular or irregular in their spacing, it just works out where along the line they should be plotted.
Regards,
DG.
package com.jrefinery.chart.demo;
import java.awt.Stroke;
import java.awt.BasicStroke;
import java.util.Calendar;
import com.jrefinery.data.BasicTimeSeries;
import com.jrefinery.data.Minute;
import com.jrefinery.data.Hour;
import com.jrefinery.data.Day;
import com.jrefinery.data.TimeSeriesCollection;
import com.jrefinery.ui.ApplicationFrame;
import com.jrefinery.chart.JFreeChart;
import com.jrefinery.chart.ChartFactory;
import com.jrefinery.chart.JFreeChartPanel;
import com.jrefinery.chart.HorizontalDateAxis;
import com.jrefinery.chart.DateUnit;
public class TimeSeriesDemo extends ApplicationFrame {
protected BasicTimeSeries series;
/**
* A demonstration application showing a quarterly time series containing a null value.
*/
public TimeSeriesDemo() {
this.series = new BasicTimeSeries("Per Minute Data", Minute.class);
Day today = new Day(17, 5, 2002);
this.series.add(new Minute( 1, new Hour(9, today)), 500.2);
this.series.add(new Minute( 5, new Hour(9, today)), 694.1);
this.series.add(new Minute(34, new Hour(9, today)), 734.4);
this.series.add(new Minute(47, new Hour(9, today)), 453.2);
this.series.add(new Minute(27, new Hour(10, today)), 500.2);
this.series.add(new Minute( 5, new Hour(12, today)), 400.5);
this.series.add(new Minute(34, new Hour(14, today)), 734.4);
this.series.add(new Minute(49, new Hour(15, today)), 453.2);
TimeSeriesCollection dataset = new TimeSeriesCollection(series);
JFreeChart chart = ChartFactory.createTimeSeriesChart("Time Series Demo", "Time", "Value",
dataset, true);
HorizontalDateAxis axis = (HorizontalDateAxis)chart.getPlot().getHorizontalAxis();
//axis.setAutoTickUnitSelection(false);
//DateUnit unit = new DateUnit(Calendar.MINUTE, 30);
//axis.getTickLabelFormatter().applyPattern("HH:mm");
//axis.setTickUnit(unit);
// set the stroke for each series...
Stroke[] seriesStrokeArray = new Stroke[1];
seriesStrokeArray[0] = new BasicStroke(4.0f);
chart.getPlot().setSeriesStroke(seriesStrokeArray);
JFreeChartPanel chartPanel = new JFreeChartPanel(chart);
this.setContentPane(chartPanel);
}
/**
* Starting point for the demonstration application.
*/
public static void main(String[] args) {
TimeSeriesDemo demo = new TimeSeriesDemo();
demo.pack();
demo.setVisible(true);
}
}
Re: Time Series Problem
Ravi,
If you want to have more control above the labels and tie the x-axis time to your values, you need to extend the numberAxis and the highlowdataset (or any other dataset you are using). From your extended numberAxis call the method in the dataset which returns you the label.
In this way, your time (x-axis) and value (y-axis) are tied together and you can handle any missing data. Say, you dont have data from 11:30am to 1:30pm. If you use timeseries->Hour/Minute class, it will still put blank spaces from 11:30am to 1:30pm. Even though, you many not want to see it.
When you create the charts, use your extended numberAxis as the horizontalNumberAxis.
BTW, if you ever create daily charts and use Day class, you will have same problems for data more than 5 days or going over a weekend. The Day and Time classes cant handle breaks in time yet as they are not tied to the y-axis.
Thanks
Mudit
If you want to have more control above the labels and tie the x-axis time to your values, you need to extend the numberAxis and the highlowdataset (or any other dataset you are using). From your extended numberAxis call the method in the dataset which returns you the label.
In this way, your time (x-axis) and value (y-axis) are tied together and you can handle any missing data. Say, you dont have data from 11:30am to 1:30pm. If you use timeseries->Hour/Minute class, it will still put blank spaces from 11:30am to 1:30pm. Even though, you many not want to see it.
When you create the charts, use your extended numberAxis as the horizontalNumberAxis.
BTW, if you ever create daily charts and use Day class, you will have same problems for data more than 5 days or going over a weekend. The Day and Time classes cant handle breaks in time yet as they are not tied to the y-axis.
Thanks
Mudit