Tooltips for Pie Chart as Annotation?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Post Reply
converge
Posts: 30
Joined: Wed Jun 16, 2004 3:00 pm

Tooltips for Pie Chart as Annotation?

Post by converge » Mon Jan 08, 2018 3:20 pm

In my program, my main chart is a CombinedDomainXYPlot which displays different sets of data. I also have a small pie chart which I'm adding to this x-y chart as an XYDrawableAnnotation.

Everything works fine. However, I'm having trouble figuring out how/if I can implement tooltips for the individual slices of the pie chart annotation. The tooltips work fine if I add the pie chart to a separate JPanel, but not if I add the pie chart to the x-y chart as an annotation.

Any thoughts?

paradoxoff
Posts: 1632
Joined: Sat Feb 17, 2007 1:51 pm

Re: Tooltips for Pie Chart as Annotation?

Post by paradoxoff » Mon Jan 08, 2018 7:14 pm

During rendering, an XYDrawableAnnotation simply adds an instance of XYAnnotationEntity to the list of entities stored in the ChartRenderingInfo which is the parent of PlotRenderingInfo instance that is used ass parameter for the addEntity-method. The XYAnnotationEntity just contains a shape, a rendererIndex, and two text for the "tool tip" and for the "url". An XYAnnotationEntity does not contain a reference to the annotation by which it was created. Even if it did, i. e. even if the instance of the XYDrawableAnnotation could be retrieved from the annotation entity, it is not possible to get the Drawable (in your case, the JFreeChart with the PiePlot) from the XYDrawableAnnotation.
I suggest to write your own implementation of XYAnnotation and XYAnnotationEntity.
The code for your new XYAnnotation (lets call it "XYChartAnnotation") will be quite similar to that of XYDrawableAnnotation, but it will only accept instances of JFreeChart instead of any "Drawable". You would also have to create a class that extends XYAnnotationEntity that holds a reference to the XYChartAnnotation by which it was created.
Similar to an XYDrawableAnnotation, the XYChartAnnotation.draw method will simply draw the JFreeChart. The XYChartAnnotation should also have an instance of ChartRenderingInfo that can be used supplied as parameter to the draw-method of the JFreeChart.
If this is all done, you finally need an instance of ChartMouseListener. Inside its chartMouseXYZ method, you can do the following:
- the ChartMouseEvent gives you a reference to a ChartEntity and to a MouseEvent
- Check whether the ChartEntity is an instance of your new XYAnnotationEntity class. If not, return
- Get the XYChartAnnotation from the XYAnnotationEntity, and the ChartRenderingInfo from XYChartAnnotation. The ChartRenderingInfo should contains a reference to an EntityCollection.
- Check whether the EntityCollection contains an entity an the coordinates of the MouseEvent. You will need to convert the mouse event coordinates (which are from the coordinate space of the ChartPanel) to the coordinate space of the JFreeChart of the annotation.
- Check the type of the entity, and if it is of the desired type (in your case, it should be an instance of PieSectionEntity), do with it whatever you want.

converge
Posts: 30
Joined: Wed Jun 16, 2004 3:00 pm

Re: Tooltips for Pie Chart as Annotation?

Post by converge » Tue Jan 09, 2018 6:27 pm

Thank you as that was extremely helpful! I had already created my own AbstractXYAnnotation class as I needed the pie chart to to be displayed in a relative position (similar to XYTitleAnnotation) near the corner, as opposed to a fixed location in data space. This is because I want the pie chart to always be visible, even if the user zooms in.

So far I have the code working up to the chart mouse event. The rest looks to be pretty straight forward.

Post Reply