I need help regarding a requirement in JFreeChart. I need to get the y coordinate in java2D for the corresponding category value in a CategoryPlot
. Say for example I give a value = 12000
for category A, it will give me the corresponding y coordinate in the plot.
Asked
Active
Viewed 261 times
1

Eric Leibenguth
- 4,167
- 3
- 24
- 51

Rajesh Saha
- 21
- 3
-
Try `valueToJava2D()` for your chosen `ValueAxis`. – trashgod Aug 14 '15 at 07:49
1 Answers
2
The following should do it:
CategoryPlot plot = chart.getCategoryPlot();
ValueAxis axis = plot.getRangeAxis();
Rectangle2D area = chartPanel.getChartRenderingInfo().getPlotInfo().getPlotArea();
double yCoordinate = axis.valueToJava2D(12000, area, plot.getRangeAxisEdge());
But note that it can only work after the rendering of the chart is finished. So if this is called during chart creation it won't work. You can delay it with an invokeLater()
though.

Eric Leibenguth
- 4,167
- 3
- 24
- 51
-
2Eric is correct, but also consider a `ChartMouseListener`, examined [here](http://stackoverflow.com/q/20081801/230513). – trashgod Aug 14 '15 at 18:20
-
This wont work for me.I need to get the y co ordinate to place an arrow in the plot.Is there any way to find it out from the dataset?? – Rajesh Saha Aug 17 '15 at 04:45
-
1What do you mean it won't work? You mean you want the coordinate *only* from the dataset (meaning the chart hasn't been plotted yet)? The only way to do that would be to pre-calculate the coordinates by calculating the size of the charts and the different margins... (ie. everything that is done during rendering) – Eric Leibenguth Aug 17 '15 at 07:25
-
@RajeshSaha: "to place an arrow in the plot," maybe use a `CategoryPointerAnnotation` with `value = 12000`. – Catalina Island Aug 17 '15 at 14:07
-
@EricLeibenguth what you are saying is exactly my requirement but can you provide me some sample codes to do it?It would be pretty nice of you. – Rajesh Saha Aug 19 '15 at 03:45
-
@CatalinaIsland I have to plot an image inside the plot.so that wont suit my requirement. – Rajesh Saha Aug 19 '15 at 03:49
-
What you are asking is quite difficult. The only way to achieve it is to look deep in the source code of JFreeChart to understand how rendering is done (and there is a lot going on). However, I still do not understand why you cannot wait for the chart to render before you add the annotation (It will be transparent to the user). – Eric Leibenguth Aug 19 '15 at 06:48
-
Actually, if you are trying to implement the solution proposed [here](http://stackoverflow.com/questions/31912323/jfree-chart-draw-image-on-a-category-plot), you don't have to worry about rendering, because the `draw()` method provides you with `dataArea` which you can use directly instead of `Rectangle2D area = chartPanel.getChartRenderingInfo().getPlotInfo().getPlotArea();` – Eric Leibenguth Aug 19 '15 at 09:02
-
@RajeshSaha: You can override the annotation's `draw()` method to draw an image. – Catalina Island Aug 19 '15 at 10:15