How to get the step size of Y Axis [Range Axis]

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
deepujain
Posts: 21
Joined: Sat Jun 02, 2007 10:52 am
Location: Bangalore

How to get the step size of Y Axis [Range Axis]

Post by deepujain » Tue Mar 25, 2008 11:39 am

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.

deepujain
Posts: 21
Joined: Sat Jun 02, 2007 10:52 am
Location: Bangalore

Post by deepujain » Fri Mar 28, 2008 10:57 am

David And Others please reply to this question.
Zzzzz....

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Post by paradoxoff » Sat Mar 29, 2008 8:47 am

NumberAxis.getTickUnit().getSize()

deepujain
Posts: 21
Joined: Sat Jun 02, 2007 10:52 am
Location: Bangalore

Post by deepujain » Mon Mar 31, 2008 9:40 am

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?
Zzzzz....

deepujain
Posts: 21
Joined: Sat Jun 02, 2007 10:52 am
Location: Bangalore

Post by deepujain » Thu Apr 03, 2008 5:37 pm

David can you please let me know how to do this?
Zzzzz....

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Post by paradoxoff » Thu Apr 03, 2008 7:00 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?

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.

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Post by skunk » Thu Apr 03, 2008 7:00 pm

Did you call getTickUnit() AFTER the plot has been drawn?

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.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Post by paradoxoff » Fri Apr 04, 2008 8:13 am

paradoxoff wrote:Is it a timing issue?
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:

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());
				};
			});
		}
	}
}

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Post by skunk » Fri Apr 04, 2008 1:19 pm

You would be able to avoid these timing issues if you added a ChartProgressListener to the chart and waited for the DRAWING_FINISHED notification instead.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Post by paradoxoff » Fri Apr 04, 2008 5:30 pm

skunk wrote:You would be able to avoid these timing issues if you added a ChartProgressListener to the chart and waited for the DRAWING_FINISHED notification instead.
Tried it and it is working without further prerequisites. Thanks a lot!

deepujain
Posts: 21
Joined: Sat Jun 02, 2007 10:52 am
Location: Bangalore

Post by deepujain » Thu Apr 10, 2008 9:49 am

Thanks, the solution worked.
Zzzzz....

Locked