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) {
}
}
Asela.