Hi,
Is it possible to use numbers instead of date for the XValues (horizontal axis) ? I'd like to pass incrementing numbers (1 to N) and plot hi/low or candlestick charts. Later on I want to create my own x-tics and print them on the horizontal axis.
Thanks
Mudit
Possible to use numbers instead of dates in HighLowDataset?
Re: Possible to use numbers instead of dates in HighLowDatas
Some progress.
I create a new class similar to DefaultHighLowDataset. In that class, I replaced Date[] with Number[] array. And changed the method where it gets Xvalue.
Then I modified createSampleHighLowDataset to increment the "date" by 1 everytime createdate is called. So, the date array is int[] now and going 1, 2, 3 and so on !
Next was to create a new method for creating HighLowCharts. Created a new method fomr the ChartFactory.createHighLowChart. Here is the new method .
----------------------------------
public static JFreeChart mycreateHighLowChart(String title, String xAxisLabel,
String valueAxisLabel, HighLowDataset data,
boolean legend) {
NumberAxis xAxis = new HorizontalNumberAxis(timeAxisLabel);
NumberAxis valueAxis = new VerticalNumberAxis(valueAxisLabel);
valueAxis.setAutoRangeIncludesZero(false);
XYPlot plot = new XYPlot(xAxis, valueAxis);
plot.setXYItemRenderer(new HighLowRenderer());
JFreeChart chart = new JFreeChart(data, plot, title, JFreeChart.DEFAULT_TITLE_FONT, legend);
return chart;
}
-------------------------------------
Now, I've hi/low charts with numbers on the x-axis and not dates !!!!!
Only thing is if I've fewer than 5 or so bars, I see null points on the x-axis, such as 0.25, 0.50, 0.75, 1.25 etc.
I believe that these should not be even plotted. Anyway, I dont think its an issue for me.
Now the next step is that I'd like to print my own x-tics instead of 1,2,3 on the x-axis. I can create dates using SimpleDateFormat and want to print them on the x-axis. I'll create an array like
1 Apr-10-2002
2 Apr-11-2002
etc etc . means on 1st bar, x-tics is Apr-10-2002, 2nd bar is Apr-11-2002 etc etc.
I'd like some help. In the meantime, I'll explore Horiztontal*.Axis and see if its already there somehow.
Thanks
Mudit
I create a new class similar to DefaultHighLowDataset. In that class, I replaced Date[] with Number[] array. And changed the method where it gets Xvalue.
Then I modified createSampleHighLowDataset to increment the "date" by 1 everytime createdate is called. So, the date array is int[] now and going 1, 2, 3 and so on !
Next was to create a new method for creating HighLowCharts. Created a new method fomr the ChartFactory.createHighLowChart. Here is the new method .
----------------------------------
public static JFreeChart mycreateHighLowChart(String title, String xAxisLabel,
String valueAxisLabel, HighLowDataset data,
boolean legend) {
NumberAxis xAxis = new HorizontalNumberAxis(timeAxisLabel);
NumberAxis valueAxis = new VerticalNumberAxis(valueAxisLabel);
valueAxis.setAutoRangeIncludesZero(false);
XYPlot plot = new XYPlot(xAxis, valueAxis);
plot.setXYItemRenderer(new HighLowRenderer());
JFreeChart chart = new JFreeChart(data, plot, title, JFreeChart.DEFAULT_TITLE_FONT, legend);
return chart;
}
-------------------------------------
Now, I've hi/low charts with numbers on the x-axis and not dates !!!!!
Only thing is if I've fewer than 5 or so bars, I see null points on the x-axis, such as 0.25, 0.50, 0.75, 1.25 etc.
I believe that these should not be even plotted. Anyway, I dont think its an issue for me.
Now the next step is that I'd like to print my own x-tics instead of 1,2,3 on the x-axis. I can create dates using SimpleDateFormat and want to print them on the x-axis. I'll create an array like
1 Apr-10-2002
2 Apr-11-2002
etc etc . means on 1st bar, x-tics is Apr-10-2002, 2nd bar is Apr-11-2002 etc etc.
I'd like some help. In the meantime, I'll explore Horiztontal*.Axis and see if its already there somehow.
Thanks
Mudit
Re: Possible to use numbers instead of dates in HighLowDatas
I have to do the same thing using numbers across the X axis using Days on Stream... example Days online. I will then chart using a line chart showing Avg temperatures for example on a Daily basis.
Re: Possible to use numbers instead of dates in HighLowDatas
Mudit Wahal wrote:
>
> Now, I've hi/low charts with numbers on the x-axis and not
> dates !!!!!
You are on the right track. The HorizontalNumberAxis and the HorizontalDateAxis classes are interchangeable.
> Only thing is if I've fewer than 5 or so bars, I see null
> points on the x-axis, such as 0.25, 0.50, 0.75, 1.25 etc.
> I believe that these should not be even plotted. Anyway, I
> dont think its an issue for me.
This is the auto tick unit selection mechanism. You can change the standard tick sizes by calling the setStandardTickUnits(...) method in the NumberAxis class.
> Now the next step is that I'd like to print my own x-tics
> instead of 1,2,3 on the x-axis. I can create dates using
> SimpleDateFormat and want to print them on the x-axis. I'll
> create an array like
>
> 1 Apr-10-2002
> 2 Apr-11-2002
>
> etc etc . means on 1st bar, x-tics is Apr-10-2002, 2nd bar is
> Apr-11-2002 etc etc.
You need to do something similar to what the date axis does now. It receives a number like 9,895,434,344 and interprets it as the number of milliseconds since 1 Jan 1970 and formats it as a date (on the axis). But as far as the plotting goes, it is just plotting a number.
Hope that helps.
DG.
>
> Now, I've hi/low charts with numbers on the x-axis and not
> dates !!!!!
You are on the right track. The HorizontalNumberAxis and the HorizontalDateAxis classes are interchangeable.
> Only thing is if I've fewer than 5 or so bars, I see null
> points on the x-axis, such as 0.25, 0.50, 0.75, 1.25 etc.
> I believe that these should not be even plotted. Anyway, I
> dont think its an issue for me.
This is the auto tick unit selection mechanism. You can change the standard tick sizes by calling the setStandardTickUnits(...) method in the NumberAxis class.
> Now the next step is that I'd like to print my own x-tics
> instead of 1,2,3 on the x-axis. I can create dates using
> SimpleDateFormat and want to print them on the x-axis. I'll
> create an array like
>
> 1 Apr-10-2002
> 2 Apr-11-2002
>
> etc etc . means on 1st bar, x-tics is Apr-10-2002, 2nd bar is
> Apr-11-2002 etc etc.
You need to do something similar to what the date axis does now. It receives a number like 9,895,434,344 and interprets it as the number of milliseconds since 1 Jan 1970 and formats it as a date (on the axis). But as far as the plotting goes, it is just plotting a number.
Hope that helps.
DG.
Re: Possible to use numbers instead of dates in HighLowDatas
David,
Seems like I need to create a new HorizontalDateTimeLabelAxis class on the lines of HorizontalNumberAxis. I'm storing the index and the date for that index in my new HighLowDataset. So, when plotting the index, instead of the index, I need to get the date stored in the index and plot it.
Will let you know how it goes !
Thanks
Mudit
Seems like I need to create a new HorizontalDateTimeLabelAxis class on the lines of HorizontalNumberAxis. I'm storing the index and the date for that index in my new HighLowDataset. So, when plotting the index, instead of the index, I need to get the date stored in the index and plot it.
Will let you know how it goes !
Thanks
Mudit
Re: Possible to use numbers instead of dates in HighLowDatas
Hmmm.. may be more like HorizontalDateAxis and not HorizontalNumberAxis. You were right !
thanks
mudit
thanks
mudit
Re: Possible to use numbers instead of dates in HighLowDatas
okay, i've an idea. Let me know if it will work.
I'm extending HorizontalNumberAxis and overriding the refreshTicks method. Everything else is inherited from HorizontalNumberAxis.
For every currentTickValue it computes, try to get the label from the data. For some values (such as 2.5 etc etc), the value will be null. Need to handle the exception in the data.
// get the data object
data = plot.getChart().getDataset()
int count = this.calculateVisibleTickCount();
double lowestTickValue = this.calculateLowestVisibleTickValue();
for (int i=0; i<count; i++) {
double currentTickValue = lowestTickValue+(i*size);
/** new stuff here **/
String tickLabel= data.getDateLabel(currentTickValue);
If (ticklabel == null)
continue; // plot the next label
double xx = this.translateValueToJava2D(currentTickValue, plotArea);
-------------------------------------
let me know
thanks
mudit
I'm extending HorizontalNumberAxis and overriding the refreshTicks method. Everything else is inherited from HorizontalNumberAxis.
For every currentTickValue it computes, try to get the label from the data. For some values (such as 2.5 etc etc), the value will be null. Need to handle the exception in the data.
// get the data object
data = plot.getChart().getDataset()
int count = this.calculateVisibleTickCount();
double lowestTickValue = this.calculateLowestVisibleTickValue();
for (int i=0; i<count; i++) {
double currentTickValue = lowestTickValue+(i*size);
/** new stuff here **/
String tickLabel= data.getDateLabel(currentTickValue);
If (ticklabel == null)
continue; // plot the next label
double xx = this.translateValueToJava2D(currentTickValue, plotArea);
-------------------------------------
let me know
thanks
mudit