Example usage for org.jfree.data.xy XYSeriesCollection addChangeListener

List of usage examples for org.jfree.data.xy XYSeriesCollection addChangeListener

Introduction

In this page you can find the example usage for org.jfree.data.xy XYSeriesCollection addChangeListener.

Prototype

@Override
public void addChangeListener(DatasetChangeListener listener) 

Source Link

Document

Registers an object to receive notification of changes to the dataset.

Usage

From source file:com.imaging100x.tracker.TrackerUtils.java

/**
* Create a frame with a plot of the data given in XYSeries
*//*from w  w  w .j av  a2s.c  om*/
public static void plotData(String title, final XYSeries data, String xTitle, String yTitle, int xLocation,
        int yLocation) {
    // JFreeChart code
    XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(data);
    JFreeChart chart = ChartFactory.createScatterPlot(title, // Title
            xTitle, // x-axis Label
            yTitle, // y-axis Label
            dataset, // Dataset
            PlotOrientation.VERTICAL, // Plot Orientation
            false, // Show Legend
            true, // Use tooltips
            false // Configure chart to generate URLs?
    );
    final XYPlot plot = (XYPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.white);
    plot.setRangeGridlinePaint(Color.lightGray);
    XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
    renderer.setBaseShapesVisible(true);
    renderer.setSeriesPaint(0, Color.black);
    renderer.setSeriesFillPaint(0, Color.white);
    renderer.setSeriesLinesVisible(0, true);
    Shape circle = new Ellipse2D.Float(-2.0f, -2.0f, 4.0f, 4.0f);
    renderer.setSeriesShape(0, circle, false);
    renderer.setUseFillPaint(true);

    ChartFrame graphFrame = new ChartFrame(title, chart);
    graphFrame.getChartPanel().setMouseWheelEnabled(true);
    graphFrame.setPreferredSize(new Dimension(SIZE, SIZE));
    graphFrame.setResizable(true);
    graphFrame.pack();
    graphFrame.setLocation(xLocation, yLocation);
    graphFrame.setVisible(true);

    dataset.addChangeListener(new DatasetChangeListener() {

        public void datasetChanged(DatasetChangeEvent dce) {
            double xRange = data.getMaxX() - data.getMinX();
            double yRange = data.getMaxY() - data.getMinY();
            double xAvg = (data.getMaxX() + data.getMinX()) / 2;
            double yAvg = (data.getMaxY() + data.getMinY()) / 2;
            double range = xRange;
            if (yRange > range) {
                range = yRange;
            }
            double offset = 0.55 * range;
            plot.getDomainAxis().setRange(xAvg - offset, xAvg + offset);
            plot.getRangeAxis().setRange(yAvg - offset, yAvg + offset);
        }

    });

}