Search code examples
javajfreechart

Accessing Legend Item text


I am creating a Bar Chart:

final JFreeChart result = ChartFactory.createBarChart(TITLE, // chart title
    X_TITLE, // domain axis label
    Y_TITLE, // range axis label
    dataset, // data
    PlotOrientation.HORIZONTAL, // the plot orientation
    true, // legend
    true, // tooltips
    false // urls
    );

I then added a ChartMouseListener as described here.

When I click on a legend item, "LegendItemEntity: seriesKey=null, dataset=null" is displayed.

How do I access the legend text for the corresponding item?


Solution

  • Starting from org.jfree.chart.demo.BarChartDemo1 and adding the same ChartMouseListener to the source yields results like this:

    LegendItemEntity: seriesKey=First, dataset=org.jfree.data.category.DefaultCategoryDataset@d9510071
    LegendItemEntity: seriesKey=Second, dataset=org.jfree.data.category.DefaultCategoryDataset@d9510071
    LegendItemEntity: seriesKey=Third, dataset=org.jfree.data.category.DefaultCategoryDataset@d9510071
    

    If you're geting a different result, an sscce may help.