I've got the following problem. I want to zoom-in a Scatterplot and then select all the displayed elements.
It would be sufficient to somehow get the displayed area in the zoomed-in Scatterplot. From the range of this area i could determine which elements are displayed in the area and which are not.
\edit: Found the Solution (Implementing AxisChangeListener Interface)
import java.awt.Color;
import java.awt.Dimension;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.event.AxisChangeEvent;
import org.jfree.chart.event.AxisChangeListener;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
public class ScatterExample extends ApplicationFrame implements AxisChangeListener {
/**
* Creates a new demo instance.
* @param title the frame title.
*/
private XYSeriesCollection dataset;
private JFreeChart chart;
public ScatterExample(String title) {
super(title);
dataset = createSampleXYDataset();
chart = ChartFactory.createScatterPlot(
"Scatterplot Demo", // chart title
"X", // domain axis label
"Y", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips?
false // URLs?
);
//customize chart
chart.setBackgroundPaint(Color.white);
XYPlot plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.white);
plot.getRangeAxis().setRange(0.0, 10.0);
plot.getRangeAxis().addChangeListener(this);
plot.getDomainAxis().setRange(0.0, 10.0);
plot.getDomainAxis().addChangeListener(this);
ChartPanel chartPanel = new ChartPanel(chart, false);
chartPanel.setPreferredSize(new Dimension(500, 500));
setContentPane(chartPanel);
}
private XYSeriesCollection createSampleXYDataset() {
XYSeriesCollection d = new XYSeriesCollection();
XYSeries ser = new XYSeries("Series 1");
XYSeries ser2 = new XYSeries("Series 2");
ser.add(1.0,2.0);
ser.add(2.0,2.0);
ser2.add(3.0,2.0);
ser.add(3.0,3.0);
ser2.add(2.0,5.0);
ser.add(1.0,2.0);
ser.add(3.0,7.0);
ser2.add(4.0,4.0);
d.addSeries(ser);
d.addSeries(ser2);
return d;
}
/**
* Starting point for the demonstration application.
* @param args ignored.
*/
public static void main(String[] args) {
ScatterExample demo = new ScatterExample("Scatterplot Demo");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
@Override
public void axisChanged(AxisChangeEvent event) {
if (event.getAxis().equals(chart.getXYPlot().getRangeAxis())){
double rangeLow = chart.getXYPlot().getRangeAxis().getLowerBound();
double rangeUp = chart.getXYPlot().getRangeAxis().getUpperBound();
System.out.println("RangeAxis new range from "+rangeLow+" to "+rangeUp);
}
else {
if (event.getAxis().equals(chart.getXYPlot().getDomainAxis())){
double domainLow = chart.getXYPlot().getDomainAxis().getLowerBound();
double domainUp = chart.getXYPlot().getDomainAxis().getUpperBound();
System.out.println("DomainAxis new range from "+domainLow+" to "+domainUp);
}
}
}
}