2

I am generating a dynamic chart (XYLineChart) using jFreeChart and I have a field which is not included in the dataset. The field is generated dynamically. I want to include that in my tooltip. Is there any possibility I can do it ?

Here is the flow of the program:

Create Chart using an empty dataset.

Set chartPanel. (I guess here is the place where we define the TooltipGenerator).

Receive dynamic data from socket.

Add data to the dataset. ( Here is the only place where I have the data which I need to have in my tooltip text).

Refresh Chart.

chrisrhyno2003
  • 3,906
  • 8
  • 53
  • 102

2 Answers2

1

You don't have to care about data added dynamically to the dataset. Tooltips are created on the fly using the data from the dataset. The individual XYToolTipGenerator just needs to be assigned to the renderer instance.

As an example, start with the TimeSeriesChartDemo1 class from JFreeChart, and add an individual XYToolTipGenerator as shown below.

XYItemRenderer r = plot.getRenderer();
…    
// define your own tooltip generator   
StandardXYToolTipGenerator tooltipGenerator = new StandardXYToolTipGenerator()
{
    @Override
    public String generateToolTip(XYDataset dataset, int series, int item)
    {
        return "Series " + series + " Item: " + item + " Value: "
            + dataset.getXValue(series, item) + ";"
            + dataset.getYValue(series, item);
    }
};
// and assign it to the renderer
r.setBaseToolTipGenerator(tooltipGenerator);
Uli
  • 1,390
  • 1
  • 14
  • 28
  • Also consider leveraging the `MessageFormat` values shown [here](http://stackoverflow.com/a/3158958/230513). – trashgod Jul 08 '15 at 09:08
  • @trashgod : That's the problem I am facing. The name to be displayed is not included anywhere in the dataset. In the example you suggested, the company names are included in the legend. I do not have such a design in my project. – chrisrhyno2003 Jul 08 '15 at 23:29
  • @Uli: I am sorry, but that approach would not work. I want to pass a specific data to the tooltip (if that's possible). – chrisrhyno2003 Jul 08 '15 at 23:30
  • @AshwinVenkataraman: I've proposed an alternative [here](http://stackoverflow.com/a/31306690/230513) to complement Uli's suggestion. – trashgod Jul 09 '15 at 01:39
  • the link is broken :( – Line Dec 19 '18 at 09:56
  • Link is fixed now :-) – Uli Jan 09 '19 at 09:59
1

The name to be displayed is not included anywhere in the dataset.

As shown here for a custom XYItemLabelGenerator, you can extend a suitable dataset, e.g. AbstractXYDataset, to include the required information and access it from your implementation of XYToolTipGenerator.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045