Hide certain series in the legend

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Post Reply
Posts: 1
Joined: Fri Nov 05, 2004 8:29 am

Hide certain series in the legend

Post by andreak » Fri Nov 05, 2004 8:31 am

Hi. Is it possible to hide some series in the legend. Say I have 3 TimeSeries, and I only want two of them to appear in the legend, how do I do this? If it's not possible, is there a nice work-around?


Avatar Viper

Post by Avatar Viper » Fri Nov 05, 2004 9:25 am

One quick solution is to disable the showing of the legend in the graph and draw your own one. Another solution is to override the legend class, i do not know which one neither do i know how to implement. Maybe you can figure it out by looking @ the source code.

Posts: 6
Joined: Wed Nov 10, 2004 8:32 am

Post by xiaoqiao » Wed Nov 10, 2004 9:39 am

You may revise the source code of jfreechart.
Commonly u should revise the getLegendItems() method in plot.
The following is a simple sample for PiePlot:

[ public LegendItemCollection getLegendItems() {

LegendItemCollection result = new LegendItemCollection();

List keys = null;
if (this.dataset != null) {
keys = this.dataset.getKeys();
int section = 0;
Iterator iterator = keys.iterator();
while (iterator.hasNext()) {
Comparable key = (Comparable) iterator.next();
Number n = this.dataset.getValue(key);
if (n != null || !this.ignoreNullValues) {
String label = key.toString();
String description = label;
Shape shape = null;
Paint paint = getSectionPaint(section);
Paint outlinePaint = getSectionOutlinePaint(section);
Stroke stroke = getSectionOutlineStroke(section);

LegendItem item = new LegendItem(
label, description, shape, true, paint, stroke, outlinePaint, stroke

// result.add(item);
// if u want to disable the 2nd legendItem ,just to:

return result;

Posts: 4
Joined: Fri Nov 19, 2004 8:14 am
Location: Denmark

Post by flembo » Thu Dec 23, 2004 12:05 pm

If you are using CategoryPlot or XYPlot you can use the setFixedLegendItems(LegendItemCollection items) method

Posts: 2
Joined: Wed Jan 26, 2005 2:33 pm
Location: Nîmes, France

setFixedLegendItems() and CombinedDomainCategoryPlot

Post by logimine » Tue Feb 08, 2005 8:25 pm

It ' s easier to remove items in the LegendItemCollection :

LegendItemCollection legend = plot.getLegendItems();
Iterator iterator=legend.iterator();
LegendItem item=(LegendItem)iterator.next();
if(item.getLabel().equals("serie name"))

!!! if you use CombinedDomainCategoryPlot, you need to refer the sub plots.

Posts: 1
Joined: Tue Mar 19, 2019 2:37 pm
antibot: No, of course not.

Re: Hide certain series in the legend

Post by vipin.vjayan » Tue Mar 19, 2019 2:53 pm

I use below code to hide certain series based on my application criteria;

Code: Select all

plot.getRenderer().setSeriesVisibleInLegend(seriesIndex, Boolean.FALSE, true);
If above code doesn't work make sure series are set to visible before calling above method.

Post Reply