Is it possible to add Legend to the plot in JFreeChart?
Asked Answered
R

3

5

I'm trying to add a legend under the plot in scattered chart as shown below. Anyone knows if this is possible?

Before: alt text

After: alt text

Rotter answered 30/6, 2010 at 6:4 Comment(0)
E
5

Extending StandardXYItemLabelGenerator is often a useful approach, but the supplied constructors may suffice. For this generator, the MessageFormat ArgumentIndex values correspond to the series name, domain and range.

NumberFormat format = NumberFormat.getNumberInstance();
format.setMaximumFractionDigits(2); // etc.
XYItemLabelGenerator generator =
    new StandardXYItemLabelGenerator("{0} {1} {2}", format, format);
renderer.setBaseItemLabelGenerator(generator);
renderer.setBaseItemLabelsVisible(true);

In addition, you can control individual series labeling with

renderer.setSeriesItemLabelsVisible(true);
Echo answered 1/7, 2010 at 14:54 Comment(1)
See also this example for StandardCategoryToolTipGenerator.Echo
R
7

Here's the custom label generator that I created:

public class LegendXYItemLabelGenerator extends StandardXYItemLabelGenerator
        implements XYItemLabelGenerator, Cloneable, PublicCloneable,
        Serializable {
    private LegendItemCollection legendItems;

    public LegendXYItemLabelGenerator(LegendItemCollection legendItems) {
        super();
        this.legendItems = legendItems;
    }

    @Override
    public String generateLabel(XYDataset dataset, int series, int item) {
        LegendItem legendItem = legendItems.get(series);
        return legendItem.getLabel();
    }
}

and then I added this code in addition to @Guilaume's code:

renderer.setBaseItemLabelsVisible(true);
renderer.setBaseItemLabelGenerator(new LegendXYItemLabelGenerator(plot.getLegendItems()));

and here's the result:

alt text

Rotter answered 1/7, 2010 at 0:21 Comment(1)
+1 for the good example. I've suggested an alternative approach in another answer.Echo
E
5

Extending StandardXYItemLabelGenerator is often a useful approach, but the supplied constructors may suffice. For this generator, the MessageFormat ArgumentIndex values correspond to the series name, domain and range.

NumberFormat format = NumberFormat.getNumberInstance();
format.setMaximumFractionDigits(2); // etc.
XYItemLabelGenerator generator =
    new StandardXYItemLabelGenerator("{0} {1} {2}", format, format);
renderer.setBaseItemLabelGenerator(generator);
renderer.setBaseItemLabelsVisible(true);

In addition, you can control individual series labeling with

renderer.setSeriesItemLabelsVisible(true);
Echo answered 1/7, 2010 at 14:54 Comment(1)
See also this example for StandardCategoryToolTipGenerator.Echo
I
1

This is controlled on the renderer:

XYPlot plot = (XYPlot) chart.getPlot();
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setBaseItemLabelsVisible(true);
Insomniac answered 30/6, 2010 at 9:48 Comment(3)
Thanks for the answer. I tried this but it still did now show up. Is there anything else that I probably need to do before? Maybe assigning labels to each plot?Rotter
I think I got it now.. I've created my own item label generator called "LegendXYItemLabelGenerator(..)" and got it to work. In addition to "renderer.setBaseItemLabelsVisible(true);", I added "renderer.setBaseItemLabelGenerator(new LegendXYItemLabelGenerator(plot.getLegendItems()));"Rotter
Is this possible on an XYAreaRenderer? I am trying to get it working and I can for the XYLineAndShapeRenderer but the same code is not working for the XYAreaRenderer.'Bushhammer

© 2022 - 2024 — McMap. All rights reserved.