1

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

Ina
  • 4,400
  • 6
  • 30
  • 44

3 Answers3

7

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

Reverend Gonzo
  • 39,701
  • 6
  • 59
  • 77
  • See also related example [here](http://stackoverflow.com/a/8056418/230513) and [here](http://stackoverflow.com/a/6620017/230513). – trashgod Jul 06 '12 at 01:18
1

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
Community
  • 1
  • 1
GETah
  • 20,922
  • 7
  • 61
  • 103
  • How do you access the chart's paintComponent method since it is created by a factory and thus cannot be extended? – Hovercraft Full Of Eels Jul 05 '12 at 18:59
  • @HovercraftFullOfEels True, the chart should be add to a JPanel instead – GETah Jul 05 '12 at 19:03
  • Annotations are designed for this, but a second series is a useful alternative; overriding `paintComponent()` is a bad idea, as it can;t easily access the renderer's geometry. – trashgod Jul 06 '12 at 01:21
0

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.

ch.Joshi elijah
  • 103
  • 2
  • 11