3

How can I generate a tooltip on chart mouse over in JFreeChart? I tried this:

chartPanel.setToolTipText("this is the string");

but this does not work. Am I supposed to do something else before? Something like this:

chartPanel.createToolTip().

I am calling these methods in the chartMouseMoved event.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
jpo
  • 3,959
  • 20
  • 59
  • 102

1 Answers1

7

Most ChartFactory methods include a boolean tooltips parameter. Just look in the source for your factory of choice to see how to instantiate a default tooltip generator suitable for the designated renderer. You shouldn't need to handle the events yourself.

Addendum: As you are using createXYLineChart, an instance of StandardXYToolTipGenerator is supplied by default. The DEFAULT_TOOL_TIP_FORMAT is {0}: ({1}, {2}); the MessageFormat symbols represent the dataset, series and item, respectively. You can use these symbols in your own generator, as shown in this XYItemLabelGenerator, or you can override generateToolTip() to return anything at all.

Addendum: Here's an example that shows just the dataset:

XYPlot plot = chart.getXYPlot();
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setLegendItemToolTipGenerator(
    new StandardXYSeriesLabelGenerator("Legend {0}"));
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks,. I am using createXYLineChart and set the tooltip property to true. But how do I defined where and what to show in each of the tooltips. I I tried doing this in the chartmouseclick and chartMouseMoved but this did not work. I tried did using the methods described above. – jpo Jul 21 '11 at 15:54
  • Thanks. But my tooltip string is actually not related to any series or dataset, but on the position at which the mouse is and some calculated value. Let say I want to display the mouse coordinates in a tool tip... – jpo Jul 21 '11 at 19:09
  • Just store the `MouseEvent` from your listener in your `StandardXYToolTipGenerator` subclass. – trashgod Jul 21 '11 at 19:20
  • I used the XYItemLabelGenerator, but the value is displayed on each series point rather than on the mouse location :( Also, can I have it display on top of any annotation that may be on my chart? – jpo Jul 21 '11 at 22:57
  • Instead of implementing `XYItemLabelGenerator `, you should extend `StandardXYToolTipGenerator` and add it via the renderer's `setBaseToolTipGenerator()`. – trashgod Jul 22 '11 at 01:20
  • Example will be very much appreciated – jpo Jul 25 '11 at 18:33