Include XYDrawableAnnotation into plot area.

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Joshi
Posts: 8
Joined: Tue Sep 26, 2017 12:46 pm
antibot: No, of course not.

Include XYDrawableAnnotation into plot area.

Post by Joshi » Thu Dec 14, 2017 7:32 am

I am trying to plot a dynamic plot using XYDrawableAnnotation,the problem is the plot is not visiable in plot area.the output is showing this Image but i want this Image.here is my code

Code: Select all

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYDrawableAnnotation;
import org.jfree.chart.annotations.XYShapeAnnotation;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.labels.XYItemLabelGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.*;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class MainPlots {

    ChartPanel chartPanel;

    public MainPlots()
    {
        XYSeriesCollection dataset = new XYSeriesCollection();
        XYSeries series1 = new XYSeries("Series1", false);
        XYSeries series2 = new XYSeries("Series2", false);

        series1.add(5,5);
        series1.add(10,10);

        dataset.addSeries(series1);
        JFreeChart chart = ChartFactory.createXYLineChart("Title", "X", "Y", dataset, PlotOrientation.VERTICAL, false, true, false);
        chartPanel = new ChartPanel(chart);
        XYPlot plot = (XYPlot) chart.getPlot();

        NumberAxis xAxis = new NumberAxis("Flow [m3/hr]");
        NumberAxis yAxis = new NumberAxis("Head [m]");

        plot.setDomainAxis(xAxis);
        plot.setRangeAxis(yAxis);

        XYLineAndShapeRenderer  renderer = new XYLineAndShapeRenderer();

        XYDrawableAnnotation annotation = new XYDrawableAnnotation(12, 12, 10, 10, new Drawable() {
            @Override
            public void draw(Graphics2D graphics2D, Rectangle2D rectangle2D) {
                Graphics2D g = graphics2D;
                g.setPaint(Color.RED);
                g.setStroke(new BasicStroke(4));
                g.drawRect(3,3,15,5);
            }
        });

        plot.addAnnotation(annotation);
        plot.setRenderer(renderer);
        plot.setDomainPannable(true);
        plot.setRangePannable(true);

        chartPanel.setMouseWheelEnabled(true);

        JFrame frame = new JFrame();
        frame.add(chartPanel);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        MainPlots task = new MainPlots();
    }
}
I know i should make plot area include drawableannotation,but i dont know how to make it.Can anyone help me on how to fix this ?

Locked