Scaling the "Y" axis upon Mouse dragging...

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
aselaneli
Posts: 6
Joined: Wed Dec 21, 2011 11:09 am
antibot: No, of course not.
Location: Colombo

Scaling the "Y" axis upon Mouse dragging...

Post by aselaneli » Wed Dec 21, 2011 11:55 am

Hello everyone,

I'm fairly new to JFreeCharts.
I was given a task to auto-scale the graph when the Y axis is dragged up or down.

After going through some online resources, I was able to develop a simple test class along with all necessary mouse listeners.
However I couldn't find an online resource where I can learn how to do the scaling of the Y axis.
It would be much appreciated if someone could guide me through this one.
Following is my sample code.

Code: Select all

import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.AxisEntity;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.EntityCollection;
import org.jfree.chart.entity.XYItemEntity;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;

public class TestChart extends ApplicationFrame implements MouseListener, MouseMotionListener{

	private static final long serialVersionUID = -4907154254780517431L;
	
	private JFreeChart chart = null;
	private ChartPanel panel = null;
	private TimeSeries xAxisValues = new TimeSeries("Test Series");
	private TimeSeriesCollection xAxisValueCollection = new TimeSeriesCollection();
	private ChartRenderingInfo info = null;
	

	public TestChart(String title) {
		super(title);
		
		chart = ChartFactory.createTimeSeriesChart("Y axis drag demo", 
				"Date", 
				"Price Per Unit",
				getDataset(), 
				true, 
				true, 
				false);
		panel = new ChartPanel(chart);
		panel.addMouseMotionListener(this);
		panel.addMouseListener(this);
		panel.setPreferredSize(new Dimension(750, 500));
		panel.setAutoscrolls(false);
		panel.setMouseZoomable(false);
		
		info = panel.getChartRenderingInfo();
		
		XYPlot localXYPlot = (XYPlot)chart.getPlot();
		
		ValueAxis yAxis = localXYPlot.getRangeAxis();
		yAxis.setLowerBound(0);
		yAxis.setUpperBound(700);
		
		setContentPane(panel);
	}
	
	
	public static void main(String[] args) {
		TestChart dragYaxis = new TestChart("Y axis drag demo");
		dragYaxis.pack();
		dragYaxis.setVisible(true);
	}
	
	
	private XYDataset getDataset() {
		xAxisValues.add(new Month(1, 2011), 100.27765D);
		xAxisValues.add(new Month(2, 2011), 514.10002D);
		xAxisValues.add(new Month(3, 2011), 234.31998D);
		xAxisValues.add(new Month(4, 2011), 453.19959D);
		xAxisValues.add(new Month(5, 2011), 200.14897D);
		xAxisValues.add(new Month(6, 2011), 345.60002D);
		xAxisValueCollection.addSeries(xAxisValues);
		return xAxisValueCollection;
	}

	
	@Override
	public void mouseDragged(MouseEvent arg0) {
		int x = arg0.getX();
		int y = arg0.getY();
		EntityCollection entities = this.info.getEntityCollection();
		ChartMouseEvent event = new ChartMouseEvent(chart, arg0, entities.getEntity(x, y));
		ChartEntity entity = event.getEntity();
		if ((entity != null) && (entity instanceof XYItemEntity)) {
			System.out.println("Mouse dragged on XYItemEntity...");
		} 
		else if((entity != null) && (entity instanceof AxisEntity)){
			System.out.println("Mouse dragged on Axis..."); // This is where the auto-scaling comes into play
		}
		else if (!(entity instanceof XYItemEntity)){
			System.out.println("Mouse dragged on Canvas...");
		}
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		
	}

}
Thanks in advance,
Asela.

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Re: Scaling the "Y" axis upon Mouse dragging...

Post by matinh » Wed Dec 21, 2011 2:06 pm

Hi!

This really depends on the axis you are using. Most likely you have a ValueAxis or subtype of it. Documentation for the axis can be found at http://www.jfree.org/jfreechart/api/jav ... eAxis.html.
Have a look at these methods:
  • setRange(double lower, double upper)
  • setLowerMargin(double margin)
  • setUpperMargin(double margin)
  • setAutoRange(boolean auto, boolean notify)
hth,
- martin

aselaneli
Posts: 6
Joined: Wed Dec 21, 2011 11:09 am
antibot: No, of course not.
Location: Colombo

Re: Scaling the "Y" axis upon Mouse dragging...

Post by aselaneli » Thu Dec 22, 2011 12:28 pm

Hi Martin,

Thank you so much for your reply & it helped me a lot.
However there's one more small problem that I'm facing.
When I click & drag on the axis, I need to get the VALUE of the Y axis where I clicked. (it's not the coordinates)

It's as follows.
If I click near "8" & start dragging up or down, is there a way that I can get the "Y" axis value as "8" by any chance..??

Code: Select all

    10-|
     9-|
     8-|
     7-|
     6-|
     5-|
     4-|
     3-|
     2-|
     1-|_______________________________
Thanks,
Asela.

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Re: Scaling the "Y" axis upon Mouse dragging...

Post by matinh » Thu Dec 22, 2011 12:37 pm

There is no such feature that I know of. If you have got the coordinates, you can calculate the value, but that might be a bit tricky. You have to take into account the axis-range, the margin, the plot dimensions and eventually more.

But there is the panning feature (as I learned just recently), which does a similar thing. Have a look at the Pannable interface and the pan* methods in the plot classes. There is also a pan() Method in the ValueAxis. Maybe you can use that or at least get some usefull information from looking at it.

You can try out the panning feature when running the JFreeChart demo from the website. Hold down the Ctrl key, click in a chart and drag it around. It is not available for all demos. It is enabled for example on LineChartDemo2.

hth,
- martin


Locked