tool tips on axis labels

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
crotty
Posts: 2
Joined: Tue Aug 17, 2010 3:33 pm
antibot: No, of course not.

tool tips on axis labels

Post by crotty » Wed Aug 18, 2010 4:35 pm

Hi guys, I'm trying to put tool tips on my axis labels. I've seen this post:

http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=27411

I see in JFreeChart::draw(), it rebuilds the EntitiesCollection, and calls this.plot.draw(...). In my case, I'm dealing with an XYPlot that has NumberAxis objects as its axes. In the post referred to above, it says I need to create an entity for my axes (I assume specifically in the area that the labels are drawn).

I'm all the way down to Axis::drawLabel(...) . Is this the function I should override (basically copying), and add my entities to the main EntitiesCollection by following this.plot.getChart(), and somehow getting access to the ChartRenderingInfo object? I guess my question is...where do I add these entities, and how do I get access to that ChartRenderingInfo object?

Any advice would be very appreciated. Thanks in advance.

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

Re: tool tips on axis labels

Post by paradoxoff » Wed Aug 18, 2010 9:18 pm

You can access the ChartRenderingInfo object from the PlotRenderingInfo object which is provided as plotState argument to the draw() method of the Axis class. See the source code of Axis.createAndAddEntity(double , AxisState, Rectangle2D, RectangleEdge, PlotRenderingInfo) to see how you can do this.
At present, it is not possible to create specific entities for the label of a ValueAxis since ValueAxis.drawLabel() has not PlotRenderingInfo parameter. To introduce that, you could of course make the small change to the source. I wonder why whether you need specific label entites?
If you can live with entities than cover the space of the entire axis (labels, tick labels, tick marks, axis line), then you will still have to change the source to introduce tool tips. At present, the AxisEntity does not contain a tool tip or an url.
The most flexible way to add tool tip support is probably this one:
- define an interface AxisToolTipGenerator with a generateToolTip(Axis axis) method.
- change the source of the Axis class and
- allow to set an AxisToolTipGenerator
- change the createAndAddEntity method to use the return value of an eventual axisToolTipGenerator.generateToolTip(Axis axis) as tool tip for the entity.
- finally define your own custom AxisToolTipGenerator implementation. You could for example return the class of the axis and its label.

crotty
Posts: 2
Joined: Tue Aug 17, 2010 3:33 pm
antibot: No, of course not.

Re: tool tips on axis labels

Post by crotty » Thu Aug 19, 2010 2:37 pm

Hey, thanks. I will take your advice, and try to improve my own solution. For the time being, I simply extended the Axis class (actually, the NumberAxis class, but whatever...), and overrode the draw and drawLabel method. The draw was overridden just to keep the PlotRenderingInfo in a class variable, and the drawLabel was basically copy-pasted, and at the bottom I added:

Code: Select all

// labelx and labely are dead center of the label, so we move our x and y left and up
double x = labelx - (labelBounds.getWidth() / 2.0);
double y = labely - (labelBounds.getHeight() / 2.0);
        
Rectangle2D.Double rec = new Rectangle2D.Double(x, y, labelBounds.getWidth(), labelBounds.getHeight());
        
// uncomment these to see the bounds of the tooltip
//g2.setColor(Color.BLUE);
//g2.draw(rec);

// plotRenderingInfo saved in the draw(...) method        
ChartRenderingInfo chartRenderingInfo = plotRenderingInfo.getOwner();
EntityCollection entityCollection = chartRenderingInfo.getEntityCollection();
ChartEntity axisLabelEntity = new ChartEntity(rec, "Axis fake TT text");
entityCollection.add(axisLabelEntity);
It's working for now, but i'm working further on making it more general (for my purposes). For the record, I'm using JFreeChart 1.0.9 (b/c of corporate realities...<sad trombone>)

Thanks again!

Locked