Ina
Ina

Reputation: 4470

Place a circle on top of an XYLineChart in JFreeChart

I've drawn a standard normal distribution using JFreeChart:

NormalDistributionFunction2D normalDistributionFunction2D = new NormalDistributionFunction2D(0.5, 0.15);
XYDataset dataset = DatasetUtilities.sampleFunction2D(normalDistributionFunction2D, 0.0, 1.0, 1000, "Normal");
JFreeChart chart = ChartFactory.createXYLineChart("MyTitle --, "", "", xySeriesCollection, PlotOrientation.VERTICAL, false, false, false);

Standard normal distribution

On top of this, I would like to place a circle at a given point. I have no problems with calculating the [x,y] co-ordinates of the circle, but I am unsure as to how I can add it to the chart. Any help appreciated. An MS Paint knockup of what I want to achieve is below.

enter image description here

Upvotes: 1

Views: 3855

Answers (3)

ch.Joshi elijah
ch.Joshi elijah

Reputation: 113

One way of doing is add a point with x and y coordinates say (0,0) to a series. Now, set renderer.setSeriesShape(series,new Ellipse2D.Double(-3, -3, 6, 6)); now you can update the series and make point moveable aswell.

Upvotes: 0

GETah
GETah

Reputation: 21439

If you have the point's coordinates, you can:

  1. Add your chart to a JPanel and draw the circle on its paintComponent(a bit difficult as you have to compensate for the chart borders
  2. Draw a second series on the same chart that contains only one point. See this post for defining custom shapes to render the data point - you can define a circle to be rendered as your data point

Upvotes: 1

Reverend Gonzo
Reverend Gonzo

Reputation: 40851

You absolutely do not need to override the paint method or add a layer on top. JFreeChart already has support for this behavior.

The correct way is to add annotations to the chart, specifically:

chart.getPlot().addAnnotation(new XYShapeAnnotation(new Ellipse2D.Double(x - radius, y - radius, radius + radius, radius + radius))

where x and y is the center of the circle. Note, the coordinates are in your plot space, not the graphical space. JFreeChart will automatically transform them when rendering.

Take a look at: http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/annotations/XYShapeAnnotation.htm http://www.java2s.com/Code/Java/Chart/JFreeChartPlotOrientationDemo2.htm

Upvotes: 7

Related Questions