2

I have an XYSeries to which I add values from a HashMap. I'd like the datapoints on the graph to have labels on them based on the key value in the HashMap.

So my question is, how do you create custom data point labels in JFreeChart?

Steve
  • 4,457
  • 12
  • 48
  • 89

1 Answers1

3

An XYItemLabelGenerator works well. If the standard one, discussed here, is not sufficient, you can always override generateLabel() to access your Map.

Addendum: In outline, your generator would look something like this:

private static class MyGenerator implements XYItemLabelGenerator {

    @Override
    public String generateLabel(XYDataset dataset, int series, int item) {
        return "Series " + series + " Item " + item;
    }
}

And you would install it in your renderer as shown in the example.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I've gone through the Javadoc and while that does seem to be the place to do it...I'm not really sure how to go about overriding generateLabel(XYDataset dataset, int series, int item)...wouldn't it need the same parameters for the XYItemLabelGenerator to use it? And thanks for the help, I really appreciate it! – Steve Oct 27 '11 at 18:51
  • Your custom generator will be called with the parameters already set for each item to be rendered. The example above just returns the `series` and `item`, but you can return any `String` you want. – trashgod Oct 27 '11 at 19:40