Noripsni
Noripsni

Reputation: 425

Drawing on JFreeChart after it has been created

I am doing a school project and I've been stuck with this problem for a week. I can't figure out how to annotate jfreechart after it has been created.

Right now the program looks like this -> My program

User should be able to draw lines or add strings on the chart with a mouse click. Does someone know how to do this?

How I have extended ChartPanel:

import java.awt.Graphics;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;

public class ExChartPanel extends ChartPanel {
private static final long serialVersionUID = 1L;
String txt="Can add Strings before creating chart";

public ExChartPanel(JFreeChart chart){
    super(chart);
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawString(txt, 100, 100);
}

public void setTxt(String txt) {
    this.txt = txt;
    repaint();
}}

Now when I run I can draw strings before the chart is made... But if on a main frame button is pressed to call setTxt("some txt"), it does not update text on panel. Same goes with lines and other shapes.

Upvotes: 0

Views: 1016

Answers (1)

sperumal
sperumal

Reputation: 1519

You need to extend JFree ChartPanel to add this functionality.

Your extended class should capture user actions, create annotations and add it to underlying JFreeChart using XYPlot.addAnnotation.

As described in ChartPanel API documentation, the chart will be redrawn automatically whenever any change notification is received.

Also, refer to ChartPanel source code to understand the API better and build your class on top of those APIs.

Upvotes: 2

Related Questions