TooltipGenerator-generateTooltip() with chart title argument

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Rex Feral
Posts: 11
Joined: Tue Feb 23, 2016 8:48 am
antibot: No, of course not.

TooltipGenerator-generateTooltip() with chart title argument

Post by Rex Feral » Thu Apr 28, 2016 4:41 pm

I have a custom Eclipse View that I fill with a lot charts representing signals. Now I want to make my own Tooltip generator for some Barcharts as to show some indications collected from a data layer on mouse hover.

The problem is the generateToolTip() method only accepts dataset:XYDataset, series:int and item:int as arguments and my algorithm can't figure out over which chart I am hovering over (in order to connect with the data provider) just by dataset alone (since multiple charts can have the exact same dataset) so I would need to at least send the chart title String (which is guaranteed by my algorithm to be unique) to the generateToolTip() in order for my implementation to figure out which message to build for the tooltip.

Now the question is so: How can I most easily implement a Custom Tooltip Generator that would work exactly the same like the Jfreechart with the sole difference that generateToolTip() now transmits the chart title string as well?

I was thinking something between this:
1. Make a custom class extending StandardXYToolTipGenerator and hack/duplicate and hack about every class that uses the generateToolTip to use the new one.
2. Make a custom class extending StandardXYToolTipGenerator, exactly as it is but with one static String field, and add per chartComposite a listener to constantly update the custom tooltip class' string field with the chart title over which i'm hovering my mouse over.

The first one would be extremely problematic as it would involve me to hack into multiple classes and if there's a lot of dependencies between them then it gets even worse.

The second one I tried but for some reason MouseTrackListeners don't work on my ChartComposites as they don't detect when my mouse enters their zone or not. Probably has some issues due to the fact that the ChartComposites are in another Composites which itself is in a ScrolledComposite.

So any ideas on how I might sort this stuff out in a simpler way?

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

Re: TooltipGenerator-generateTooltip() with chart title argu

Post by paradoxoff » Sat Apr 30, 2016 7:35 pm

I agree that method 1 looks very messy.
I do not understand why you want to modify your new StandardXYToolTipGenerator class from what sounds like a MouseListener or a ChartMouseListener.
What I would do instead:
Extend StandardXYToolTipGenerator and give your new class a reference to a Title or better yet to a JFreeChart. In your new class, override the protected method createItemArray(XYDataset dataset, int series, int item). This method is implemented in AbstractXYItemLabelGenerator and called by both StandardXYToolTipGenerator and StandardXYItemLabelGenerator to convert the informations from the dataset and the indices to either a tool tip or an itme label.
In your overriden createItemArray-method, extend the array returned by super.createItemArray by one or more further Objects which can represent anything that you can extract from the provided JFreeChart object. In your case, you could simply add one further Object containing the return value of yourJFreeChartReference.getTitle().getText().
When you construct your JFreeChart, all you need is to create an instance of your new StandardXYToolTipGenerator class, give that a reference of the JFreeChart, and add this instance as a tool tip generator to your renderer.
In order to show the chart titel, you will also have to select a suitable MessageFormat.

Rex Feral
Posts: 11
Joined: Tue Feb 23, 2016 8:48 am
antibot: No, of course not.

Re: TooltipGenerator-generateTooltip() with chart title argu

Post by Rex Feral » Tue May 03, 2016 1:08 pm

Brilliant!

Yes, indeed, the problem I had was that having lots of charts the tooltip generator, which was the same for all, couldn't tell from another if they had the same dataset but different data values in my data provider with which it needed to connect.
By adding the tooltip generator to the charts' subplots after the chart was created and setting a Jfreechart field in the tooltip generator it solved the problem of identifying the chart for which to generate tooltips.

It sounds simple now, but I hadn't thought of it this way and it escaped me before.

Thanks a lot, I owe you!

Locked