How to get the step size of Y Axis [Range Axis]
How to get the step size of Y Axis [Range Axis]
I am newbie.
I have a graph where the range is set to auto and the step size can vary depending on the data. For example if the Y Axis [Range Axis] is
50,100,150,200 ........
Here the step size is 50. I need to get this data. Is it possible. Please help.
I searched the forum on how to get the step size of Y Axis and did not find anything useful to me.
I have a graph where the range is set to auto and the step size can vary depending on the data. For example if the Y Axis [Range Axis] is
50,100,150,200 ........
Here the step size is 50. I need to get this data. Is it possible. Please help.
I searched the forum on how to get the step size of Y Axis and did not find anything useful to me.
Thanks,
But it did not work, The value returned by NumberAxis.getTickUnit().getSize() is always 1.0. Currently my Y Axis reads as 1575, 1600, 1625, 1650, ...... 2250,2275 and hence the step size here is 25.
Is it possible to get this value. The Y axis will vary depending on the data.
Please let me know if its possible to get this data?
But it did not work, The value returned by NumberAxis.getTickUnit().getSize() is always 1.0. Currently my Y Axis reads as 1575, 1600, 1625, 1650, ...... 2250,2275 and hence the step size here is 25.
Is it possible to get this value. The Y axis will vary depending on the data.
Please let me know if its possible to get this data?
Zzzzz....
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Sorry for my earlier post.
I reproduced deepujain's finding that NumberAxis.getTickUnit().getSize() indeed returns the default (which happens to be 1.0d), and I am not sure why.
NumberAxis.draw(...) calls NumberAxis.refreshTicks(...) which calls NUmberAxis.refreshTicksHorizontal or -Vertical. In the refreshTicksXXX methods is checked whether auto ticks should be used (default is yes), and selectAutoTickUnit()...) is called which delegates the work to select[Horizontal or Vertical]AutoTickUnit which are calculating the best tick unit and storing it by calling setTickUnit(...). In principle it should be possible to retrieve the tick unit from there by calling .getTickUnit().
Here is a demo that shows the issue. Interestingly when you zoom a little bit so that the tick unit is not changed you are getting the real value in the axisChanged method. Is it a timing issue? Is the tickUnit requested in the code below before it has actually been set or what else is going on?
I reproduced deepujain's finding that NumberAxis.getTickUnit().getSize() indeed returns the default (which happens to be 1.0d), and I am not sure why.
NumberAxis.draw(...) calls NumberAxis.refreshTicks(...) which calls NUmberAxis.refreshTicksHorizontal or -Vertical. In the refreshTicksXXX methods is checked whether auto ticks should be used (default is yes), and selectAutoTickUnit()...) is called which delegates the work to select[Horizontal or Vertical]AutoTickUnit which are calculating the best tick unit and storing it by calling setTickUnit(...). In principle it should be possible to retrieve the tick unit from there by calling .getTickUnit().
Here is a demo that shows the issue. Interestingly when you zoom a little bit so that the tick unit is not changed you are getting the real value in the axisChanged method. Is it a timing issue? Is the tickUnit requested in the code below before it has actually been set or what else is going on?
Code: Select all
package demo;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.Axis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.event.AxisChangeEvent;
import org.jfree.chart.event.AxisChangeListener;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.DefaultXYDataset;
public class ChartComponentDemo4 implements AxisChangeListener{
public ChartComponentDemo4(){
DefaultXYDataset set = new DefaultXYDataset();
double[][] data = new double[][]{{10.0d,20.0d,30.0d,40.0d},{1.0d,2.0d,3.0d,4.0d}};
set.addSeries("One",data);
NumberAxis xAxis = new NumberAxis("x axis");
xAxis.addChangeListener(this);
//xAxis.set
//xAxis.setAutoTickUnitSelection(false);
boolean auto = xAxis.getAutoRangeIncludesZero();
xAxis.setAutoRangeIncludesZero(true ^ auto);
NumberAxis yAxis = new NumberAxis("yaxis");
XYPlot xyplot = new XYPlot(set,xAxis,yAxis,new XYLineAndShapeRenderer(true,true));
JFreeChart chart = new JFreeChart(" The Chart",null,xyplot,true);
JFrame frame = new JFrame();
System.out.println("Number Tick Unit size for xAxis before drawing: "+xAxis.getTickUnit().getSize());
frame.getContentPane().add(new ChartPanel(chart));
frame.pack();
frame.setVisible(true);
xAxis.setAutoRangeIncludesZero(true ^ auto);
xAxis.setAutoRangeIncludesZero(auto);
System.out.println("Number Tick Unit size for xAxis after drawing: "+xAxis.getTickUnit().getSize());
}
public static void main(String[] args){
ChartComponentDemo4 demo = new ChartComponentDemo4();
}
public void axisChanged(AxisChangeEvent ace){
Axis axis = ace.getAxis();
if(axis instanceof NumberAxis){
System.out.println("Number Tick Unit size for axis "+axis.getLabel()+" is :" +((NumberAxis)axis).getTickUnit().getSize());
//System.out.println("Number Tick Auto Tick Index "+axis.getLabel()+" is :" +((NumberAxis)axis).getAutoTickIndex());
}
}
}
Last edited by paradoxoff on Thu Apr 03, 2008 7:01 pm, edited 1 time in total.
Did you call getTickUnit() AFTER the plot has been drawn?
This is from the javadoc
This is from the javadoc
Note: if the autoTickUnitSelection flag is true the tick unit may be changed while the axis is being drawn, so in that case the return value from this method may be irrelevant if the method is called before the axis has been drawn.
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Yes. After some thinking and reading skunk's hint, I wondered what the result of NumberAxis.getTickUnit().getSize() would be if it is ensured that this value is requested after the drawing is finished. This can be accomplished by running the request in the EDT. It turned out that this procedure gives the correct results. All that was necessary was a slight modification of the axisChanged method. However, to get the correct value right after drawing it also seems to be required to "manually" trigger an AxisChangeEvent. If the calls to xAxis.setAutoRangeIncludesZero(boolean auto) in the constructor are commented out, the value is sometimes correct but mostly not. Here is the code that I have used:paradoxoff wrote:Is it a timing issue?
Code: Select all
package demo;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.Axis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.event.AxisChangeEvent;
import org.jfree.chart.event.AxisChangeListener;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.DefaultXYDataset;
public class ChartComponentDemo4 implements AxisChangeListener{
public ChartComponentDemo4(){
DefaultXYDataset set = new DefaultXYDataset();
double[][] data = new double[][]{{10.0d,20.0d,30.0d,40.0d},{1.0d,2.0d,3.0d,4.0d}};
set.addSeries("One",data);
final NumberAxis xAxis = new NumberAxis("x axis");
xAxis.addChangeListener(this);
NumberAxis yAxis = new NumberAxis("yaxis");
XYPlot xyplot = new XYPlot(set,xAxis,yAxis,new XYLineAndShapeRenderer(true,true));
JFreeChart chart = new JFreeChart(" The Chart",null,xyplot,true);
JFrame frame = new JFrame();
frame.getContentPane().add(new ChartPanel(chart));
frame.pack();
frame.setVisible(true);
boolean auto = xAxis.getAutoRangeIncludesZero();
xAxis.setAutoRangeIncludesZero(true ^ auto);//"manual" trigger...
xAxis.setAutoRangeIncludesZero(auto);//.. of an AxisChangeEvent
SwingUtilities.invokeLater(new Runnable(){
public void run(){
System.out.println("Number Tick Unit size for xAxis after drawing in EDT, Constructor: "+xAxis.getTickUnit().getSize());
};
});
}
public static void main(String[] args){
ChartComponentDemo4 demo = new ChartComponentDemo4();
}
public void axisChanged(AxisChangeEvent ace){
final Axis axis = ace.getAxis();
if(axis instanceof NumberAxis){
System.out.println("Number Tick Unit size for axis "+axis.getLabel()+" is :" +((NumberAxis)axis).getTickUnit().getSize());
SwingUtilities.invokeLater(new Runnable(){
public void run(){
System.out.println("Number Tick Unit size for xAxis after drawing in EDT, axisChanged: "+((NumberAxis)axis).getTickUnit().getSize());
};
});
}
}
}
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm