Example usage for org.jfree.chart.plot XYPlot addAnnotation

List of usage examples for org.jfree.chart.plot XYPlot addAnnotation

Introduction

In this page you can find the example usage for org.jfree.chart.plot XYPlot addAnnotation.

Prototype

public void addAnnotation(XYAnnotation annotation) 

Source Link

Document

Adds an annotation to the plot and sends a PlotChangeEvent to all registered listeners.

Usage

From source file:ascensionxyplot.AscensionXYPlot.java

/**
 * Creates a combined chart./*from ww  w  .  j  a v  a  2s.  c  o m*/
 *
 * @return The combined chart.
 */
private JFreeChart createCombinedChart(File dataFile) {

    // create subplot 1...
    final XYDataset data1 = createDataset1(dataFile);
    final XYItemRenderer renderer1 = new StandardXYItemRenderer();
    final NumberAxis rangeAxis1 = new NumberAxis("Range 1");
    final XYPlot subplot1 = new XYPlot(data1, null, rangeAxis1, renderer1);
    subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);

    // add secondary axis
    //        subplot1.setDataset(1, createDataset2());
    //        final NumberAxis axis2 = new NumberAxis("Range Axis 2");
    //        axis2.setAutoRangeIncludesZero(false);
    //        subplot1.setRangeAxis(1, axis2);
    //        subplot1.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
    //        subplot1.setRenderer(1, new StandardXYItemRenderer());       
    //        subplot1.mapDatasetToRangeAxis(1, 1);

    final XYTextAnnotation annotation1 = new XYTextAnnotation("x value", 20.0, 120000.0);
    final XYTextAnnotation annotation2 = new XYTextAnnotation("y value", 20.0, 110000.0);
    final XYTextAnnotation annotation3 = new XYTextAnnotation("z value", 20.0, 100000.0);
    final XYTextAnnotation annotation4 = new XYTextAnnotation("timepoint", 20.0, 10000.0);
    annotation1.setFont(new Font("SansSerif", Font.PLAIN, 9));
    annotation2.setFont(new Font("SansSerif", Font.PLAIN, 9));
    annotation3.setFont(new Font("SansSerif", Font.PLAIN, 9));
    annotation4.setFont(new Font("SansSerif", Font.PLAIN, 9));
    //        annotation.setRotationAngle(Math.PI / 4.0);
    subplot1.addAnnotation(annotation1);
    subplot1.addAnnotation(annotation2);
    subplot1.addAnnotation(annotation3);
    subplot1.addAnnotation(annotation4);

    // create subplot 2...
    final XYDataset data2 = createDataset2();
    final XYItemRenderer renderer2 = new StandardXYItemRenderer();
    final NumberAxis rangeAxis2 = new NumberAxis("Range 2");
    rangeAxis2.setAutoRangeIncludesZero(false);
    final XYPlot subplot2 = new XYPlot(data2, null, rangeAxis2, renderer2);
    subplot2.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);

    // parent plot...
    final CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new NumberAxis("Domain"));
    plot.setGap(10.0);

    // add the subplots...
    plot.add(subplot1, 1);
    plot.add(subplot2, 1);
    plot.setOrientation(PlotOrientation.VERTICAL);

    // return a new chart containing the overlaid plot...
    return new JFreeChart("Is this the title?", JFreeChart.DEFAULT_TITLE_FONT, plot, true);

}

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 ww.  j av a  2s . c  o  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;
}

From source file:org.trade.ui.chart.CandlestickChart.java

/**
 * Method seriesChanged.//from ww w. j a  v a  2s.  c o  m
 * 
 * @param event
 *            SeriesChangeEvent
 * @see org.jfree.data.general.SeriesChangeListener#seriesChanged(SeriesChangeEvent)
 */
public void seriesChanged(SeriesChangeEvent event) {

    Object series = event.getSource();
    if (series instanceof CandleSeries) {

        CandleSeries candleSeries = (CandleSeries) series;
        if (!candleSeries.isEmpty()) {
            CombinedDomainXYPlot combinedXYplot = (CombinedDomainXYPlot) this.chart.getPlot();
            @SuppressWarnings("unchecked")
            List<XYPlot> subplots = combinedXYplot.getSubplots();
            XYPlot xyplot = subplots.get(0);

            CandleItem candleItem = (CandleItem) candleSeries.getDataItem(candleSeries.getItemCount() - 1);
            String msg = "Time: " + dateFormat.format(candleItem.getLastUpdateDate()) + " Open: "
                    + new Money(candleItem.getOpen()) + " High: " + new Money(candleItem.getHigh()) + " Low: "
                    + new Money(candleItem.getLow()) + " Close: " + new Money(candleItem.getClose()) + " Vwap: "
                    + new Money(candleItem.getVwap());
            titleLegend2.setText(msg);
            valueMarker.setValue(candleItem.getClose());

            double x = TradingCalendar
                    .getSpecificTime(candleSeries.getStartTime(), candleItem.getPeriod().getStart()).getTime();
            String annotationText = "(" + dateFormat.format(candleItem.getLastUpdateDate()) + ", "
                    + new Money(candleItem.getClose()) + ")";
            if (null == closePriceLine) {
                closePriceLine = new XYTextAnnotation(annotationText, x, candleItem.getY());
                closePriceLine.setTextAnchor(TextAnchor.BOTTOM_RIGHT);
                xyplot.addAnnotation(closePriceLine);
                xyplot.addRangeMarker(valueMarker);
            } else {
                closePriceLine.setText(annotationText);
                closePriceLine.setX(x);
                closePriceLine.setY(candleItem.getY());
            }

            this.chart.fireChartChanged();
        }
    }
}

From source file:org.pf.midea.SimulationController.java

@Override
public void run() {
    textAreaNumeric.setText("");
    XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
    double progress = 0;
    double progressStep = 100.0 / (planCells.length * (hSquareHigh - hSquareLow + hSquareStep) / hSquareStep);
    progressBar.setValue(0);//  ww  w .  ja  va 2  s .  c o  m
    for (int i = 0; i < planCells.length; i++) {
        XYSeries xySeries;
        if (showLineNumbers)
            xySeries = new XYSeries(String.valueOf(i + 1) + ") " + planCells[i].getShortDescription());
        else
            xySeries = new XYSeries(planCells[i].getShortDescription());
        textAreaNumeric.append(planCells[i].getDescription() + "\n");
        textAreaNumeric.setCaretPosition(textAreaNumeric.getDocument().getLength());

        PlanStates.SourceType currentSourceType = planCells[i].getSourceCell().getSourceType();
        PlanStates.CodeType currentCodeType = planCells[i].getCodeCell().getCodeType();
        PlanStates.ModulationType currentModulationType = planCells[i].getModulationCell().getModulationType();
        PlanStates.ChannelType currentChannelType = planCells[i].getChannelCell().getChannelType();
        PlanStates.ErrorsType currentErrorsType = planCells[i].getErrorsCell().getErrorsType();

        BinaryNumber[] sourcePoints = null;
        switch (currentSourceType) {
        case ST_TEST:
            sourcePoints = ConstellationPointsGenerator.getTestPoints(currentModulationType, iterationsCount);
            break;
        case ST_RANDOM:
            sourcePoints = ConstellationPointsGenerator.getRandomPoints(currentModulationType, iterationsCount);
            break;
        }

        Coder coder = null;
        Decoder decoder = null;
        switch (currentCodeType) {
        case CT_NONE:
            coder = new CoderNone();
            decoder = new DecoderNone();
            break;
        case CT_HAMMING74:
            coder = new CoderHamming74();
            decoder = new DecoderHamming74();
            break;
        case CT_CYCLIC:
            coder = new CoderCyclic85();
            decoder = new DecoderCyclic85();
            break;
        case CT_BCH155:
            coder = new CoderBCH155();
            decoder = new DecoderBCH155();
            break;
        }

        Modulator modulator = null;
        Demodulator demodulator = null;
        switch (currentModulationType) {
        case MT_ASK:
            modulator = new ModulatorASK();
            demodulator = new DemodulatorASK();
            break;
        case MT_FSK:
            modulator = new ModulatorFSK();
            demodulator = new DemodulatorFSK();
            break;
        case MT_BPSK:
            modulator = new ModulatorBPSK();
            demodulator = new DemodulatorBPSK();
            break;
        case MT_QPSK:
            modulator = new ModulatorQPSK();
            demodulator = new DemodulatorQPSK();
            break;
        case MT_8PSK:
            modulator = new Modulator8PSK();
            demodulator = new Demodulator8PSK();
            break;
        case MT_16PSK:
            modulator = new Modulator16PSK();
            demodulator = new Demodulator16PSK();
            break;
        case MT_32PSK:
            modulator = new Modulator32PSK();
            demodulator = new Demodulator32PSK();
            break;
        case MT_16QAM:
            modulator = new Modulator16QAM();
            demodulator = new Demodulator16QAM();
            break;
        case MT_32QAM:
            modulator = new Modulator32QAM();
            demodulator = new Demodulator32QAM();
            break;
        case MT_64QAM:
            modulator = new Modulator64QAM();
            demodulator = new Demodulator64QAM();
            break;
        case MT_256QAM:
            modulator = new Modulator256QAM();
            demodulator = new Demodulator256QAM();
            break;
        }

        Channel channel = null;
        switch (currentChannelType) {
        case CHT_AWGN:
            channel = new ChannelAWGN();
            break;
        case CHT_RAYLEIGH:
            channel = new ChannelRayleigh();
            break;
        }

        BinaryNumber[] encoded = coder.encode(sourcePoints);
        Signal[] modulated = modulator.modulate(encoded);

        double error = 0;

        for (double h = hSquareLow; h <= hSquareHigh; h += hSquareStep) {
            double realH;
            if (dBs)
                realH = StatisticsTools.decibelsToTimes(h);
            else
                realH = h;

            Signal[] noised = channel.noise(realH, modulated);
            BinaryNumber[] demodulated = demodulator.demodulate(noised);
            noised = null;
            System.gc();
            BinaryNumber[] decoded = decoder.decode(demodulated);
            demodulated = null;
            System.gc();
            switch (currentErrorsType) {
            case ET_SER:
                error = StatisticsTools.getSER(sourcePoints, decoded);
                break;
            case ET_BER:
                error = StatisticsTools.getBER(sourcePoints, decoded);
                break;
            }
            decoded = null;
            System.gc();

            if (error > 0) {
                xySeries.add(h, error);
                textAreaNumeric.append(String.valueOf(h) + "\t" + String.valueOf(error) + "\n");
                textAreaNumeric.setCaretPosition(textAreaNumeric.getDocument().getLength());
            }

            progress += progressStep;
            progressBar.setValue((int) Math.round(progress));
        }
        xySeriesCollection.addSeries(xySeries);
        textAreaNumeric.append("\n");
        textAreaNumeric.setCaretPosition(textAreaNumeric.getDocument().getLength());
    }

    JFreeChart chart = ChartFactory.createXYLineChart("", dBs ? "" : "", "?",
            xySeriesCollection, PlotOrientation.VERTICAL, true, true, false);
    chart.getLegend().setPosition(RectangleEdge.RIGHT);
    XYPlot xyPlot = chart.getXYPlot();

    for (int i = 0; i < planCells.length; i++) {
        xyPlot.getRenderer().setSeriesStroke(i, new BasicStroke(planCells[i].getLineWidth()));
        if (planCells[i].getLineColor() != null)
            xyPlot.getRenderer().setSeriesPaint(i, planCells[i].getLineColor());

        if (showLineNumbers) {
            XYSeries currentSeries = xySeriesCollection.getSeries(i);
            double annotationY = currentSeries.getY(0).doubleValue();
            double annotationX = currentSeries.getX(0).doubleValue();
            for (int j = 1; j < currentSeries.getItemCount(); j++)
                if (currentSeries.getY(j).doubleValue() == 0) {
                    annotationY = currentSeries.getY(j - 1).doubleValue();
                    annotationX = currentSeries.getX(j - 1).doubleValue();
                    break;
                } else {
                    annotationY = currentSeries.getY(j).doubleValue();
                    annotationX = currentSeries.getX(j).doubleValue();
                }
            XYTextAnnotation annotation = new XYTextAnnotation(String.valueOf(i + 1), annotationX, annotationY);
            annotation.setBackgroundPaint(Color.WHITE);
            annotation.setFont(new Font("Dialog", 0, 14));
            xyPlot.addAnnotation(annotation);
        }
    }

    xyPlot.setBackgroundPaint(Color.WHITE);
    xyPlot.setDomainGridlinePaint(Color.GRAY);
    xyPlot.setRangeGridlinePaint(Color.GRAY);
    NumberAxis domainAxis = new NumberAxis("h, " + (dBs ? "" : ""));
    LogAxis rangeAxis = new LogAxis("?");
    rangeAxis.setNumberFormatOverride(new HumanNumberFormat(1));
    domainAxis.setTickLabelFont(new Font("Dialog", 0, 14));
    rangeAxis.setTickLabelFont(new Font("Dialog", 0, 14));
    xyPlot.setDomainAxis(domainAxis);
    xyPlot.setRangeAxis(rangeAxis);

    ChartPanel nestedPanel = new ChartPanel(chart);
    chartPanel.removeAll();
    chartPanel.add(nestedPanel, new CellConstraints());
    chartPanel.updateUI();
}

From source file:org.operamasks.faces.render.graph.ChartRenderer.java

private void addXYTextAnnotations(JFreeChart chart, UIChart comp) {
    XYPlot plot = (XYPlot) chart.getPlot();

    UIDataSeries data = comp.getDataSeries();
    if (data instanceof UITimeSeries) {
        Class timePeriodClass = getTimePeriodClass(((UITimeSeries) data).getTimePeriod());

        for (UIComponent kid : comp.getChildren()) {
            if (kid.isRendered() && (kid instanceof UITextAnnotation)) {
                XYTextAnnotation a = createTimeSeriesTextAnnotation((UITextAnnotation) kid, timePeriodClass);
                if (a != null) {
                    plot.addAnnotation(a);
                }//  w w  w  .  j  a va2  s  .  c o  m
            }
        }
    } else {
        for (UIComponent kid : comp.getChildren()) {
            if (kid.isRendered() && (kid instanceof UITextAnnotation)) {
                XYTextAnnotation a = createXYTextAnnotation((UITextAnnotation) kid);
                if (a != null) {
                    plot.addAnnotation(a);
                }
            }
        }
    }
}

From source file:org.micromanager.asidispim.Utils.PlotUtils.java

/**
 * Create a frame with a plot of the data given in XYSeries overwrite any
 * previously created frame with the same title
 *
        // w w w . j av  a  2s  .  com
 * @param title shown in the top of the plot
 * @param data array with data series to be plotted
 * @param xTitle Title of the X axis
 * @param yTitle Title of the Y axis
 * @param showShapes whether or not to draw shapes at the data points
 * @param annotation to be shown in plot
 * @return Frame that displays the data
 */
public Frame plotDataN(String title, XYSeries[] data, String xTitle, String yTitle, boolean[] showShapes,
        String annotation) {

    // if we already have a plot open with this title, close it, but remember
    // its position
    Frame[] gfs = ChartFrame.getFrames();
    for (Frame f : gfs) {
        if (f.getTitle().equals(title)) {
            f.dispose();
        }
    }

    // JFreeChart code
    XYSeriesCollection dataset = new XYSeriesCollection();
    // calculate min and max to scale the graph
    double minX, minY, maxX, maxY;
    minX = data[0].getMinX();
    minY = data[0].getMinY();
    maxX = data[0].getMaxX();
    maxY = data[0].getMaxY();
    for (XYSeries d : data) {
        dataset.addSeries(d);
        if (d.getMinX() < minX) {
            minX = d.getMinX();
        }
        if (d.getMaxX() > maxX) {
            maxX = d.getMaxX();
        }
        if (d.getMinY() < minY) {
            minY = d.getMinY();
        }
        if (d.getMaxY() > maxY) {
            maxY = d.getMaxY();
        }
    }

    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?
    );
    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.white);
    plot.setRangeGridlinePaint(Color.lightGray);

    XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
    renderer.setBaseShapesVisible(true);

    for (int i = 0; i < data.length; i++) {
        renderer.setSeriesFillPaint(i, Color.white);
        renderer.setSeriesLinesVisible(i, true);
    }

    renderer.setSeriesPaint(0, Color.blue);
    Shape circle = new Ellipse2D.Float(-2.0f, -2.0f, 4.0f, 4.0f);
    renderer.setSeriesShape(0, circle, false);

    if (data.length > 1) {
        renderer.setSeriesPaint(1, Color.red);
        Shape square = new Rectangle2D.Float(-2.0f, -2.0f, 4.0f, 4.0f);
        renderer.setSeriesShape(1, square, false);
    }
    if (data.length > 2) {
        renderer.setSeriesPaint(2, Color.darkGray);
        Shape rect = new Rectangle2D.Float(-2.0f, -1.0f, 4.0f, 2.0f);
        renderer.setSeriesShape(2, rect, false);
    }
    if (data.length > 3) {
        renderer.setSeriesPaint(3, Color.magenta);
        Shape rect = new Rectangle2D.Float(-1.0f, -2.0f, 2.0f, 4.0f);
        renderer.setSeriesShape(3, rect, false);
    }

    for (int i = 0; i < data.length; i++) {
        if (showShapes.length > i && !showShapes[i]) {
            renderer.setSeriesShapesVisible(i, false);
        }
    }

    XYAnnotation an = new XYTextAnnotation(annotation, maxX - 0.01, maxY);
    plot.addAnnotation(an);

    renderer.setUseFillPaint(true);

    final MyChartFrame graphFrame = new MyChartFrame(title, chart);
    graphFrame.getChartPanel().setMouseWheelEnabled(true);
    graphFrame.pack();
    graphFrame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent arg0) {
            graphFrame.dispose();
        }
    });

    graphFrame.setVisible(true);

    return graphFrame;
}

From source file:edu.ucsf.valelab.spotintensityanalysis.plot.PlotUtils.java

/**
 * Create a frame with a plot of the data given in XYSeries overwrite any
 * previously created frame with the same title
 *
        /* w  w w  .j  a  va 2  s.  c om*/
 * @param title shown in the top of the plot
 * @param data array with data series to be plotted
 * @param xTitle Title of the X axis
 * @param yTitle Title of the Y axis
 * @param showShapes whether or not to draw shapes at the data points
 * @param annotation to be shown in plot
 * @return Frame that displays the data
 */
public Frame plotDataN(String title, XYSeries[] data, String xTitle, String yTitle, boolean[] showShapes,
        String annotation) {

    // JFreeChart code
    XYSeriesCollection dataset = new XYSeriesCollection();
    // calculate min and max to scale the graph
    double minX, minY, maxX, maxY;
    minX = data[0].getMinX();
    minY = data[0].getMinY();
    maxX = data[0].getMaxX();
    maxY = data[0].getMaxY();
    for (XYSeries d : data) {
        dataset.addSeries(d);
        if (d.getMinX() < minX) {
            minX = d.getMinX();
        }
        if (d.getMaxX() > maxX) {
            maxX = d.getMaxX();
        }
        if (d.getMinY() < minY) {
            minY = d.getMinY();
        }
        if (d.getMaxY() > maxY) {
            maxY = d.getMaxY();
        }
    }

    JFreeChart chart = ChartFactory.createScatterPlot(title, // Title
            xTitle, // x-axis Label
            yTitle, // y-axis Label
            dataset, // Dataset
            PlotOrientation.VERTICAL, // Plot Orientation
            true, // Show Legend
            true, // Use tooltips
            false // Configure chart to generate URLs?
    );
    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.white);
    plot.setRangeGridlinePaint(Color.lightGray);

    XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
    renderer.setBaseShapesVisible(true);

    for (int i = 0; i < data.length; i++) {
        renderer.setSeriesFillPaint(i, Color.white);
        renderer.setSeriesLinesVisible(i, true);
    }

    renderer.setSeriesPaint(0, Color.blue);
    Shape circle = new Ellipse2D.Float(-2.0f, -2.0f, 4.0f, 4.0f);
    renderer.setSeriesShape(0, circle, false);

    if (data.length > 1) {
        renderer.setSeriesPaint(1, Color.red);
        Shape square = new Rectangle2D.Float(-2.0f, -2.0f, 4.0f, 4.0f);
        renderer.setSeriesShape(1, square, false);
    }
    if (data.length > 2) {
        renderer.setSeriesPaint(2, Color.darkGray);
        Shape rect = new Rectangle2D.Float(-2.0f, -1.0f, 4.0f, 2.0f);
        renderer.setSeriesShape(2, rect, false);
    }
    if (data.length > 3) {
        renderer.setSeriesPaint(3, Color.magenta);
        Shape rect = new Rectangle2D.Float(-1.0f, -2.0f, 2.0f, 4.0f);
        renderer.setSeriesShape(3, rect, false);
    }

    for (int i = 0; i < data.length; i++) {
        if (showShapes.length > i && !showShapes[i]) {
            renderer.setSeriesShapesVisible(i, false);
        }
    }

    // place annotation at 80 % of max X, maxY
    XYAnnotation an = new XYTextAnnotation(annotation, maxX - 0.2 * (maxX - minX), maxY);
    plot.addAnnotation(an);

    renderer.setUseFillPaint(true);

    if (graphFrame_ == null)
        graphFrame_ = new MyChartFrame(title, chart);
    else
        graphFrame_.getChartPanel().setChart(chart);
    graphFrame_.getChartPanel().setMouseWheelEnabled(true);
    graphFrame_.pack();
    final MyChartFrame privateFrame = graphFrame_;
    graphFrame_.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent arg0) {
            privateFrame.dispose();
        }
    });

    graphFrame_.setVisible(true);

    return graphFrame_;
}

From source file:edu.ucsf.valelab.saim.plot.PlotUtils.java

/**
 * Create a frame with a plot of the data given in XYSeries overwrite any
 * previously created frame with the same title
 *
        //  w  w  w  .  j  ava  2  s  .  com
 * @param title shown in the top of the plot
 * @param data array with data series to be plotted
 * @param xTitle Title of the X axis
 * @param yTitle Title of the Y axis
 * @param showShapes whether or not to draw shapes at the data points
 * @param annotation to be shown in plot
 * @return Frame that displays the data
 */
public Frame plotDataN(String title, XYSeries[] data, String xTitle, String yTitle, boolean[] showShapes,
        String annotation) {

    // Close existing frames
    // Frame[] gfs = ChartFrame.getFrames();
    // for (Frame f : gfs) {
    //if (f.getTitle().equals(title)) {
    //   f.dispose();
    //}
    // }

    // JFreeChart code
    XYSeriesCollection dataset = new XYSeriesCollection();
    // calculate min and max to scale the graph
    double minX, minY, maxX, maxY;
    minX = data[0].getMinX();
    minY = data[0].getMinY();
    maxX = data[0].getMaxX();
    maxY = data[0].getMaxY();
    for (XYSeries d : data) {
        dataset.addSeries(d);
        if (d.getMinX() < minX) {
            minX = d.getMinX();
        }
        if (d.getMaxX() > maxX) {
            maxX = d.getMaxX();
        }
        if (d.getMinY() < minY) {
            minY = d.getMinY();
        }
        if (d.getMaxY() > maxY) {
            maxY = d.getMaxY();
        }
    }

    JFreeChart chart = ChartFactory.createScatterPlot(title, // Title
            xTitle, // x-axis Label
            yTitle, // y-axis Label
            dataset, // Dataset
            PlotOrientation.VERTICAL, // Plot Orientation
            true, // Show Legend
            true, // Use tooltips
            false // Configure chart to generate URLs?
    );
    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.white);
    plot.setRangeGridlinePaint(Color.lightGray);

    XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
    renderer.setBaseShapesVisible(true);

    for (int i = 0; i < data.length; i++) {
        renderer.setSeriesFillPaint(i, Color.white);
        renderer.setSeriesLinesVisible(i, true);
    }

    renderer.setSeriesPaint(0, Color.blue);
    Shape circle = new Ellipse2D.Float(-2.0f, -2.0f, 4.0f, 4.0f);
    renderer.setSeriesShape(0, circle, false);

    if (data.length > 1) {
        renderer.setSeriesPaint(1, Color.red);
        Shape square = new Rectangle2D.Float(-2.0f, -2.0f, 4.0f, 4.0f);
        renderer.setSeriesShape(1, square, false);
    }
    if (data.length > 2) {
        renderer.setSeriesPaint(2, Color.darkGray);
        Shape rect = new Rectangle2D.Float(-2.0f, -1.0f, 4.0f, 2.0f);
        renderer.setSeriesShape(2, rect, false);
    }
    if (data.length > 3) {
        renderer.setSeriesPaint(3, Color.magenta);
        Shape rect = new Rectangle2D.Float(-1.0f, -2.0f, 2.0f, 4.0f);
        renderer.setSeriesShape(3, rect, false);
    }

    for (int i = 0; i < data.length; i++) {
        if (showShapes.length > i && !showShapes[i]) {
            renderer.setSeriesShapesVisible(i, false);
        }
    }

    // place annotation at 80 % of max X, maxY
    XYAnnotation an = new XYTextAnnotation(annotation, maxX - 0.2 * (maxX - minX), maxY);
    plot.addAnnotation(an);

    renderer.setUseFillPaint(true);

    final MyChartFrame graphFrame = new MyChartFrame(title, chart);
    graphFrame.getChartPanel().setMouseWheelEnabled(true);
    graphFrame.pack();
    graphFrame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent arg0) {
            graphFrame.dispose();
        }
    });

    graphFrame.setVisible(true);

    return graphFrame;
}

From source file:org.micromanager.saim.plot.PlotUtils.java

/**
 * Create a frame with a plot of the data given in XYSeries overwrite any
 * previously created frame with the same title
 *
        /*w w w. ja  v a 2s.c o m*/
 * @param title shown in the top of the plot
 * @param data array with data series to be plotted
 * @param xTitle Title of the X axis
 * @param yTitle Title of the Y axis
 * @param showShapes whether or not to draw shapes at the data points
 * @param annotation to be shown in plot
 * @return Frame that displays the data
 */
public Frame plotDataN(String title, XYSeries[] data, String xTitle, String yTitle, boolean[] showShapes,
        String annotation) {

    //Close existing frames
    Frame[] gfs = ChartFrame.getFrames();
    for (Frame f : gfs) {
        if (f.getTitle().equals(title)) {
            f.dispose();
        }
    }

    // JFreeChart code
    XYSeriesCollection dataset = new XYSeriesCollection();
    // calculate min and max to scale the graph
    double minX, minY, maxX, maxY;
    minX = data[0].getMinX();
    minY = data[0].getMinY();
    maxX = data[0].getMaxX();
    maxY = data[0].getMaxY();
    for (XYSeries d : data) {
        dataset.addSeries(d);
        if (d.getMinX() < minX) {
            minX = d.getMinX();
        }
        if (d.getMaxX() > maxX) {
            maxX = d.getMaxX();
        }
        if (d.getMinY() < minY) {
            minY = d.getMinY();
        }
        if (d.getMaxY() > maxY) {
            maxY = d.getMaxY();
        }
    }

    JFreeChart chart = ChartFactory.createScatterPlot(title, // Title
            xTitle, // x-axis Label
            yTitle, // y-axis Label
            dataset, // Dataset
            PlotOrientation.VERTICAL, // Plot Orientation
            true, // Show Legend
            true, // Use tooltips
            false // Configure chart to generate URLs?
    );
    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.white);
    plot.setRangeGridlinePaint(Color.lightGray);

    XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
    renderer.setBaseShapesVisible(true);

    for (int i = 0; i < data.length; i++) {
        renderer.setSeriesFillPaint(i, Color.white);
        renderer.setSeriesLinesVisible(i, true);
    }

    renderer.setSeriesPaint(0, Color.blue);
    Shape circle = new Ellipse2D.Float(-2.0f, -2.0f, 4.0f, 4.0f);
    renderer.setSeriesShape(0, circle, false);

    if (data.length > 1) {
        renderer.setSeriesPaint(1, Color.red);
        Shape square = new Rectangle2D.Float(-2.0f, -2.0f, 4.0f, 4.0f);
        renderer.setSeriesShape(1, square, false);
    }
    if (data.length > 2) {
        renderer.setSeriesPaint(2, Color.darkGray);
        Shape rect = new Rectangle2D.Float(-2.0f, -1.0f, 4.0f, 2.0f);
        renderer.setSeriesShape(2, rect, false);
    }
    if (data.length > 3) {
        renderer.setSeriesPaint(3, Color.magenta);
        Shape rect = new Rectangle2D.Float(-1.0f, -2.0f, 2.0f, 4.0f);
        renderer.setSeriesShape(3, rect, false);
    }

    for (int i = 0; i < data.length; i++) {
        if (showShapes.length > i && !showShapes[i]) {
            renderer.setSeriesShapesVisible(i, false);
        }
    }

    // place annotation at 80 % of max X, maxY
    XYAnnotation an = new XYTextAnnotation(annotation, maxX - 0.2 * (maxX - minX), maxY);
    plot.addAnnotation(an);

    renderer.setUseFillPaint(true);

    final MyChartFrame graphFrame = new MyChartFrame(title, chart);
    graphFrame.getChartPanel().setMouseWheelEnabled(true);
    graphFrame.pack();
    graphFrame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent arg0) {
            graphFrame.dispose();
        }
    });

    graphFrame.setVisible(true);

    return graphFrame;
}

From source file:gov.nih.nci.cma.web.graphing.CMAPrincipalComponentAnalysisPlot.java

/**
 * This chart uses the XYAnnotation as a glyph to represent
 * a single pca data point. Glyph shape is determined by survival time.
 * Survival of more than 10 months is represented by a circle. 10 months or less
 * is represented by a square. Component1 values are represented by X
 * Component2 values are represented by Y
 *///from w w w  .  j a va2s .co m

private void createGlyphsAndAddToPlot(XYPlot plot) { //, double glyphScaleFactor) {
    XYShapeAnnotation glyph;
    Shape glyphShape = null;
    Color glyphColor;
    NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
    Range r = domainAxis.getRange();
    double upperBound = r.getUpperBound();

    double dotSize = 0.7;
    if (upperBound >= 50 && upperBound <= 100)
        dotSize = 1d;
    else if (upperBound > 100 && upperBound <= 150)
        dotSize = 2d;
    else if (upperBound > 150 && upperBound <= 200)
        dotSize = 3d;
    else if (upperBound > 200)
        dotSize = 5d;

    //PrincipalComponentAnalysisDataPoint pcaPoint;
    double x, y;
    //for (Iterator i=dataPoints.iterator(); i.hasNext(); ) {
    for (CMAPCADataPoint pcaPoint : dataPoints) {
        x = pcaPoint.getComponentValue(component1);
        y = pcaPoint.getComponentValue(component2);

        String sampleGroupName = pcaPoint.getSampleGroupName();
        String order = sampleGroupNames.get(sampleGroupName);
        glyphShape = getShapeForDataPoint(order, x, y, dotSize);

        glyphColor = colorMap.get(order);
        if (glyphColor == null)
            glyphColor = Color.black;
        glyph = new XYShapeAnnotation(glyphShape, new BasicStroke(1.0f), Color.BLACK, glyphColor);
        String tooltip = "";

        tooltip = pcaPoint.getSampleId() + " " + pcaPoint.getSampleGroupName();

        glyph.setToolTipText(tooltip);
        plot.addAnnotation(glyph);
    }
}