Example usage for org.jfree.chart.annotations XYPolygonAnnotation XYPolygonAnnotation

List of usage examples for org.jfree.chart.annotations XYPolygonAnnotation XYPolygonAnnotation

Introduction

In this page you can find the example usage for org.jfree.chart.annotations XYPolygonAnnotation XYPolygonAnnotation.

Prototype

public XYPolygonAnnotation(double[] polygon, Stroke stroke, Paint outlinePaint) 

Source Link

Document

Creates a new annotation where the box is drawn as an outline using the specified stroke and outlinePaint.

Usage

From source file:projects.wdlf47tuc.ProcessAllSwathcal.java

/**
 * Plots the CMD using the existing dataset. Used whenever chart annotations change, without the
 * underlying plot data changing. This method identifies all sources lying within the boxed region
 * and loads them into the secondary list {@link #boxedSources}.
 * /*from  w  w  w. java 2 s .co m*/
 * @param allSources
 *    The {@link Source}s to plot
 * @return
 *    A JFreeChart presenting the colour-magnitude diagram for the current selection criteria and colours.
 */
private JFreeChart plotCmd() {

    XYSeries outside = new XYSeries("Outside");
    XYSeries inside = new XYSeries("Inside");

    // Use a Path2D.Double instance to determine polygon intersection
    Path2D.Double path = new Path2D.Double();
    boxedSources.clear();

    boolean performBoxSelection = (points.size() > 2);

    if (performBoxSelection) {
        // Initialise Path2D object
        path.moveTo(points.get(0)[0], points.get(0)[1]);
        for (double[] point : points) {
            path.lineTo(point[0], point[1]);
        }
    }

    for (Source source : selectedSources) {
        double magnitude = source.getMag(magFilter);
        double col1 = source.getMag(col1Filter);
        double col2 = source.getMag(col2Filter);

        double x = col1 - col2;
        double y = magnitude;

        if (performBoxSelection) {
            Point2D.Double point = new Point2D.Double(x, y);
            if (path.contains(point)) {
                inside.add(x, y);
                boxedSources.add(source);
            } else {
                outside.add(x, y);
            }
        } else {
            outside.add(x, y);
        }
    }

    final XYSeriesCollection data = new XYSeriesCollection();
    data.addSeries(outside);
    data.addSeries(inside);

    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    renderer.setSeriesLinesVisible(0, false);
    renderer.setSeriesShapesVisible(0, true);
    renderer.setSeriesShape(0, new Ellipse2D.Float(-0.5f, -0.5f, 1, 1));
    renderer.setSeriesPaint(0, ChartColor.BLACK);

    renderer.setSeriesLinesVisible(1, false);
    renderer.setSeriesShapesVisible(1, true);
    renderer.setSeriesShape(1, new Ellipse2D.Float(-0.5f, -0.5f, 1, 1));
    renderer.setSeriesPaint(1, ChartColor.RED);

    NumberAxis xAxis = new NumberAxis(col1Filter.toString() + " - " + col2Filter.toString());
    xAxis.setRange(getXRange());

    NumberAxis yAxis = new NumberAxis(magFilter.toString());
    yAxis.setRange(getYRange());
    yAxis.setInverted(true);

    // Configure plot
    XYPlot xyplot = new XYPlot(data, xAxis, yAxis, renderer);
    xyplot.setBackgroundPaint(Color.lightGray);
    xyplot.setDomainGridlinePaint(Color.white);
    xyplot.setDomainGridlinesVisible(true);
    xyplot.setRangeGridlinePaint(Color.white);

    // Specify selection box, if points have been specified
    if (!points.isEmpty()) {

        double[] coords = new double[points.size() * 2];

        for (int i = 0; i < points.size(); i++) {
            double[] point = points.get(i);
            coords[2 * i + 0] = point[0];
            coords[2 * i + 1] = point[1];
        }
        XYPolygonAnnotation box = new XYPolygonAnnotation(coords, new BasicStroke(2.0f), Color.BLUE);
        xyplot.addAnnotation(box);
    }

    // Configure chart
    JFreeChart chart = new JFreeChart("47 Tuc CMD", xyplot);
    chart.setBackgroundPaint(Color.white);
    chart.setTitle("47 Tuc colour-magnitude diagram");
    chart.removeLegend();

    return chart;
}