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:Transistor.JFETGraphViewer.java

public JFreeChart getResultChart() {
    this.makeNewSeries();

    this.chart = ChartFactory.createXYLineChart("Output characteristic V-I curves", "V_ds", "I_d", data,
            PlotOrientation.VERTICAL, true, true, false);
    XYPlot plot = (XYPlot) chart.getPlot();
    plot.getRangeAxis().setRange(0, 2100);
    pointer = new XYPointerAnnotation("V_ds", info.getVds(), getDrainCurrent(info.getVds()), PI / 4.0);
    plot.getAnnotations().clear();// ww  w  .  j  av a  2 s. c  o m
    plot.addAnnotation(pointer);

    return this.chart;
}

From source file:org.jfree.chart.demo.NormalDistributionDemo2.java

public static JFreeChart createChart(XYDataset xydataset) {
    JFreeChart jfreechart = ChartFactory.createXYLineChart("Normal Distribution Demo 2", "X", "Y", xydataset,
            PlotOrientation.VERTICAL, true, true, false);
    XYPlot xyplot = (XYPlot) jfreechart.getPlot();
    xyplot.setDomainZeroBaselineVisible(true);
    xyplot.setRangeZeroBaselineVisible(true);
    xyplot.setDomainPannable(true);/* w w  w.j  a va  2s .  co  m*/
    xyplot.setRangePannable(true);
    ValueAxis valueaxis = xyplot.getDomainAxis();
    valueaxis.setLowerMargin(0.0D);
    valueaxis.setUpperMargin(0.0D);
    XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot.getRenderer();
    xylineandshaperenderer.setDrawSeriesLineAsPath(true);
    xylineandshaperenderer.setSeriesStroke(0, new BasicStroke(1.5F));
    xylineandshaperenderer.setSeriesStroke(1, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] { 6F, 4F }, 0.0F));
    xylineandshaperenderer.setSeriesStroke(2,
            new BasicStroke(2.0F, 1, 1, 1.0F, new float[] { 6F, 4F, 3F, 3F }, 0.0F));
    xylineandshaperenderer.setSeriesStroke(3, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] { 4F, 4F }, 0.0F));
    XYPointerAnnotation xypointerannotation = new XYPointerAnnotation(" = -2.0, \262 = 0.5", -2D,
            0.56399999999999995D, 3.9269908169872414D);
    xypointerannotation.setLabelOffset(4D);
    xypointerannotation.setTextAnchor(TextAnchor.BOTTOM_RIGHT);
    xypointerannotation.setBackgroundPaint(Color.yellow);
    xyplot.addAnnotation(xypointerannotation);
    XYPointerAnnotation xypointerannotation1 = new XYPointerAnnotation(" = 0.0, \262 = 0.2",
            0.22500000000000001D, 0.80000000000000004D, 0.0D);
    xypointerannotation1.setLabelOffset(4D);
    xypointerannotation1.setTextAnchor(TextAnchor.CENTER_LEFT);
    xypointerannotation1.setBackgroundPaint(new Color(0, 0, 255, 63));
    xyplot.addAnnotation(xypointerannotation1);
    XYPointerAnnotation xypointerannotation2 = new XYPointerAnnotation(" = 0.0, \262 = 1.0", 0.75D,
            0.29999999999999999D, 5.497787143782138D);
    xypointerannotation2.setLabelOffset(4D);
    xypointerannotation2.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
    xypointerannotation2.setBackgroundPaint(new Color(255, 0, 0, 63));
    xyplot.addAnnotation(xypointerannotation2);
    XYPointerAnnotation xypointerannotation3 = new XYPointerAnnotation(" = 0.0, \262 = 5.0", 3D,
            0.074999999999999997D, 4.7123889803846897D);
    xypointerannotation3.setLabelOffset(4D);
    xypointerannotation3.setTextAnchor(TextAnchor.BOTTOM_CENTER);
    xypointerannotation3.setBackgroundPaint(new Color(0, 255, 0, 63));
    xyplot.addAnnotation(xypointerannotation3);
    return jfreechart;
}

From source file:org.jfree.chart.demo.PlotOrientationDemo2.java

/**
 * Creates a new demo instance./*from w  w  w. j  a  v a  2 s .c o m*/
 * 
 * @param title  the frame title.
 */
public PlotOrientationDemo2(String title) {

    super(title);
    JPanel panel = new JPanel(new GridLayout(2, 4));
    for (int i = 0; i < CHART_COUNT; i++) {
        this.datasets[i] = createDataset(i);
        this.charts[i] = createChart(i, this.datasets[i]);
        XYPlot plot = this.charts[i].getXYPlot();
        XYShapeAnnotation a1 = new XYShapeAnnotation(new Rectangle2D.Double(1.0, 2.0, 2.0, 3.0),
                new BasicStroke(1.0f), Color.blue);
        XYLineAnnotation a2 = new XYLineAnnotation(0.0, -5.0, 10.0, -5.0);

        plot.addAnnotation(a1);
        plot.addAnnotation(a2);
        plot.addDomainMarker(new IntervalMarker(5.0, 10.0), Layer.BACKGROUND);
        plot.addRangeMarker(new IntervalMarker(-2.0, 0.0), Layer.BACKGROUND);

        this.panels[i] = new ChartPanel(this.charts[i]);
    }
    this.charts[1].getXYPlot().getDomainAxis().setInverted(true);
    this.charts[2].getXYPlot().getRangeAxis().setInverted(true);
    this.charts[3].getXYPlot().getDomainAxis().setInverted(true);
    this.charts[3].getXYPlot().getRangeAxis().setInverted(true);

    this.charts[5].getXYPlot().getDomainAxis().setInverted(true);
    this.charts[6].getXYPlot().getRangeAxis().setInverted(true);
    this.charts[4].getXYPlot().getDomainAxis().setInverted(true);
    this.charts[4].getXYPlot().getRangeAxis().setInverted(true);

    this.charts[4].getXYPlot().setOrientation(PlotOrientation.HORIZONTAL);
    this.charts[5].getXYPlot().setOrientation(PlotOrientation.HORIZONTAL);
    this.charts[6].getXYPlot().setOrientation(PlotOrientation.HORIZONTAL);
    this.charts[7].getXYPlot().setOrientation(PlotOrientation.HORIZONTAL);

    panel.add(this.panels[0]);
    panel.add(this.panels[1]);
    panel.add(this.panels[4]);
    panel.add(this.panels[5]);
    panel.add(this.panels[2]);
    panel.add(this.panels[3]);
    panel.add(this.panels[6]);
    panel.add(this.panels[7]);

    panel.setPreferredSize(new Dimension(800, 600));
    setContentPane(panel);

}

From source file:org.jfree.chart.demo.OverlaidXYPlotDemo.java

/**
 * Creates an overlaid chart.//from  w  w  w  .ja  va  2 s.com
 *
 * @return The chart.
 */
private JFreeChart createOverlaidChart() {

    // create plot ...
    final IntervalXYDataset data1 = createDataset1();
    final XYItemRenderer renderer1 = new XYBarRenderer(0.20);
    renderer1.setToolTipGenerator(
            new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
                    new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
    final DateAxis domainAxis = new DateAxis("Date");
    domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    final ValueAxis rangeAxis = new NumberAxis("Value");
    final XYPlot plot = new XYPlot(data1, domainAxis, rangeAxis, renderer1);
    final double x = new Day(9, SerialDate.MARCH, 2002).getMiddleMillisecond();
    final XYTextAnnotation annotation = new XYTextAnnotation("Hello!", x, 10000.0);
    annotation.setFont(new Font("SansSerif", Font.PLAIN, 9));
    plot.addAnnotation(annotation);

    // add a second dataset and renderer...
    final XYDataset data2 = createDataset2();
    final XYItemRenderer renderer2 = new StandardXYItemRenderer();
    renderer2.setToolTipGenerator(
            new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
                    new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
    plot.setDataset(1, data2);
    plot.setRenderer(1, renderer2);

    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

    // return a new chart containing the overlaid plot...
    return new JFreeChart("Overlaid Plot Example", JFreeChart.DEFAULT_TITLE_FONT, plot, true);

}

From source file:jimaginary.machine.set.viewer.SetViewerTopComponent.java

private void createMidiChart(Set set) {
    if (set == null) {
        return;/*ww  w  . j av a  2 s.com*/
    }
    XYSeries series = new XYSeries("Distrbution");
    float setValues[] = set.getValues();
    for (int i = 0; i < setValues.length; i++) {
        if ((int) setValues[i] != 0) {
            series.add(i, (int) setValues[i]);
        }
    }
    XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(series);

    // Generate the graph
    JFreeChart chart = ChartFactory.createScatterPlot(set.getName(), "Position", "Note", dataset,
            PlotOrientation.VERTICAL, // Plot Orientation
            true, // Show Legend
            true, // Use tooltips
            false // Configure chart to generate URLs?
    );
    // add annotations if we have them
    XYPlot plot = chart.getXYPlot();
    for (int i = 0; i < setValues.length; i++) {
        XYTextAnnotation an = new XYTextAnnotation(MidiCodec.noteValToString((int) setValues[i]), i,
                (int) setValues[i]);
        plot.addAnnotation(an);
    }

    if (chartPanel != null) {
        jPanelChart.remove(chartPanel);
    }
    chartPanel = new ChartPanel(chart);
    jPanelChart.setLayout(new java.awt.BorderLayout());
    jPanelChart.add(chartPanel, BorderLayout.CENTER);
    jPanelChart.validate();
}

From source file:OverlaidXYPlotDemo2.java

/**
 * Creates an overlaid chart./*from w w w  .ja  v  a 2 s.  c  o  m*/
 *
 * @return The chart.
 */
private JFreeChart createOverlaidChart() {

    final DateAxis domainAxis = new DateAxis("Date");
    domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    final ValueAxis rangeAxis = new NumberAxis("Value");

    // create plot...
    final IntervalXYDataset data1 = createDataset1();
    final XYItemRenderer renderer1 = new XYBarRenderer(0.20);
    renderer1.setToolTipGenerator(
            new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
                    new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
    final XYPlot plot = new XYPlot(data1, domainAxis, rangeAxis, renderer1);
    final double x = new Day(9, SerialDate.MARCH, 2002).getMiddleMillisecond();
    final XYTextAnnotation annotation = new XYTextAnnotation("Hello!", x, 10000.0);
    annotation.setFont(new Font("SansSerif", Font.PLAIN, 9));
    plot.addAnnotation(annotation);

    final ValueAxis rangeAxis2 = new NumberAxis("Value 2");
    plot.setRangeAxis(1, rangeAxis2);

    // create subplot 2...
    final XYDataset data2A = createDataset2A();
    final XYItemRenderer renderer2A = new StandardXYItemRenderer();
    renderer2A.setToolTipGenerator(
            new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
                    new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
    plot.setDataset(1, data2A);
    plot.setRenderer(1, renderer2A);

    final XYDataset data2B = createDataset2B();
    plot.setDataset(2, data2B);
    plot.setRenderer(2, new StandardXYItemRenderer());
    plot.mapDatasetToRangeAxis(2, 1);

    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
    plot.setOrientation(PlotOrientation.VERTICAL);

    // return a new chart containing the overlaid plot...
    return new JFreeChart("Overlaid Plot Example", JFreeChart.DEFAULT_TITLE_FONT, plot, true);

}

From source file:org.jfree.chart.demo.CombinedXYPlotDemo1.java

/**
 * Creates a combined chart.// w w  w .  j  a  va2  s .c  o  m
 *
 * @return the combined chart.
 */
private JFreeChart createCombinedChart() {

    // create subplot 1...
    final XYDataset data1 = createDataset1();
    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);

    final XYTextAnnotation annotation = new XYTextAnnotation("Hello!", 50.0, 10000.0);
    annotation.setFont(new Font("SansSerif", Font.PLAIN, 9));
    annotation.setRotationAngle(Math.PI / 4.0);
    subplot1.addAnnotation(annotation);

    // 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("CombinedDomainXYPlot Demo", JFreeChart.DEFAULT_TITLE_FONT, plot, true);

}

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

/**
 * Method addBuySellTradeArrow./*from  w  w  w  . j  a v a2s  . c  om*/
 * 
 * @param action
 *            String
 * @param price
 *            Money
 * @param time
 *            Date
 * @param quantity
 *            Integer
 * @throws ValueTypeException
 */
public void addBuySellTradeArrow(String action, Money price, Date time, Integer quantity)
        throws ValueTypeException {
    String label = Action.newInstance(action) + " " + quantity + "@" + price;
    XYPointerAnnotation arrow = new XYPointerAnnotation(label, time.getTime(), price.doubleValue(), 90d);
    arrow.setLabelOffset(5.0);
    arrow.setBackgroundPaint(Color.GREEN);
    if (action.equals(Action.SELL)) {
        arrow.setAngle(-90d);
        arrow.setBackgroundPaint(Color.RED);
    }
    CombinedDomainXYPlot combinedXYplot = (CombinedDomainXYPlot) this.chart.getPlot();
    @SuppressWarnings("unchecked")
    List<XYPlot> subplots = combinedXYplot.getSubplots();
    XYPlot xyplot = subplots.get(0);
    xyplot.addAnnotation(arrow);
    this.chart.fireChartChanged();
}

From source file:de.bfs.radon.omsimulation.gui.data.OMCharts.java

/**
 * Creates a chart displaying the radon concentration of a single room. Uses
 * red for normal rooms, blue for cellar rooms and green for misc rooms.
 * /*  w w w  . j  a  v  a 2s. c  om*/
 * @param title
 *          The headline of the chart. Will be hidden if set to null.
 * @param room
 *          The room object containing the radon data.
 * @param preview
 *          Will hide annotations, labels and headlines if true.
 * @return A chart displaying the radon concentration of a single room.
 */
public static JFreeChart createRoomChart(String title, OMRoom room, boolean preview) {
    Color lineColor = new Color(0, 0, 0, 128);
    Color rangeColor = new Color(222, 222, 222, 128);
    if (room.getType() == OMRoomType.Room) {
        lineColor = new Color(255, 0, 0, 128);
        rangeColor = new Color(255, 222, 222, 128);
    } else {
        if (room.getType() == OMRoomType.Cellar) {
            lineColor = new Color(0, 0, 255, 128);
            rangeColor = new Color(222, 222, 255, 128);
        } else {
            lineColor = new Color(0, 128, 0, 255);
            rangeColor = new Color(222, 255, 222, 128);
        }
    }
    double[] values = room.getValues();
    XYSeriesCollection dataSet = new XYSeriesCollection();
    XYSeries series = new XYSeries("Radon");
    int count = room.getCount();
    double maxPointerKey = 0;
    for (int i = 0; i < count; i++) {
        series.add(i, values[i]);
        if (values[i] == room.getMaximum()) {
            maxPointerKey = i;
        }
    }
    dataSet.addSeries(series);
    title = title + ": " + room.getType().toString() + " " + room.getId();
    JFreeChart chart = ChartFactory.createXYLineChart(title, "T [h]", "Rn [Bq/m\u00B3]", dataSet,
            PlotOrientation.VERTICAL, false, true, false);
    XYPlot plot = (XYPlot) chart.getPlot();
    double positiveDeviation = room.getAverage() + room.getDeviation();
    double negativeDeviation = room.getAverage() - room.getDeviation();
    IntervalMarker deviation = new IntervalMarker(negativeDeviation, positiveDeviation);
    float[] dash = { 5, 3 };
    deviation.setPaint(rangeColor);
    deviation.setStroke(new BasicStroke(2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1, dash, 0));
    plot.addRangeMarker(deviation, Layer.BACKGROUND);
    ValueMarker arithMarker = new ValueMarker(room.getAverage(), lineColor,
            new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1, dash, 0));
    plot.addRangeMarker(arithMarker);
    ValueMarker maxiMarker = new ValueMarker(room.getMaximum(), lineColor,
            new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1, dash, 0));
    plot.addRangeMarker(maxiMarker);
    XYTextAnnotation amLabel = new XYTextAnnotation("AM=" + (int) room.getAverage(), count,
            room.getAverage() * 1.01);
    plot.addAnnotation(amLabel);
    XYTextAnnotation sdLabel = new XYTextAnnotation("SD=" + (int) room.getDeviation(), count,
            (room.getAverage() + room.getDeviation()) * 1.01);
    plot.addAnnotation(sdLabel);
    XYTextAnnotation maxLabel = new XYTextAnnotation("MAX=" + (int) room.getMaximum(), count,
            room.getMaximum() * 1.01);
    plot.addAnnotation(maxLabel);
    XYPointerAnnotation maxPointer = new XYPointerAnnotation("", maxPointerKey, room.getMaximum(),
            Math.PI * 1.1);
    plot.addAnnotation(maxPointer);
    XYItemRenderer renderer = plot.getRenderer();
    renderer.setSeriesPaint(0, lineColor);
    if (preview) {
        chart.setTitle("");
        plot.clearAnnotations();
    }
    return chart;
}

From source file:umontreal.ssj.charts.XYChart.java

/**
 * Draws a vertical line on the chart at @f$x@f$-coordinate `x`. `name`
 * is written near the line at @f$y@f$ position `yfrac` (a fraction of
 * the @f$y@f$-size of the chart, 0 is the bottom, 1 is the top); if
 * `right` is `true`, `name` is written on the right of the line, else
 * on the left./*from w w w  .ja v a2s.  c o  m*/
 *  @param x            @f$x@f$-coordinate of the line
 *  @param name         description of the line
 *  @param yfrac        @f$y@f$-position of name
 *  @param right        @f$x@f$-position of name
 */
public void drawVerticalLine(double x, String name, double yfrac, boolean right) {
    double ybottom = YAxis.getAxis().getLowerBound();
    final Object o = this;
    if (this instanceof HistogramChart)
        ybottom = 0;
    double ytop = YAxis.getAxis().getUpperBound();
    XYLineAnnotation line = new XYLineAnnotation(x, ybottom, x, ytop);
    XYTextAnnotation text = new XYTextAnnotation(name, x, ytop * yfrac);
    if (!right)
        text.setTextAnchor(TextAnchor.HALF_ASCENT_RIGHT);
    else
        text.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
    XYPlot plot = getJFreeChart().getXYPlot();
    plot.addAnnotation(line);
    plot.addAnnotation(text);
}