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?
--
andreak[/i][/b]
Hide certain series in the legend
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:
if(section!=1)
result.add(item);
section++;
}
}
}
return result;
}
][/code]
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:
if(section!=1)
result.add(item);
section++;
}
}
}
return result;
}
][/code]
setFixedLegendItems() and CombinedDomainCategoryPlot
It ' s easier to remove items in the LegendItemCollection :
LegendItemCollection legend = plot.getLegendItems();
Iterator iterator=legend.iterator();
while(iterator.hasNext()){
LegendItem item=(LegendItem)iterator.next();
if(item.getLabel().equals("serie name"))
iterator.remove();
}
plot.setFixedLegendItems(legend);
!!! if you use CombinedDomainCategoryPlot, you need to refer the sub plots.
LegendItemCollection legend = plot.getLegendItems();
Iterator iterator=legend.iterator();
while(iterator.hasNext()){
LegendItem item=(LegendItem)iterator.next();
if(item.getLabel().equals("serie name"))
iterator.remove();
}
plot.setFixedLegendItems(legend);
!!! if you use CombinedDomainCategoryPlot, you need to refer the sub plots.
Bruno
-
- Posts: 1
- Joined: Tue Mar 19, 2019 2:37 pm
- antibot: No, of course not.
Re: Hide certain series in the legend
I use below code to hide certain series based on my application criteria;
If above code doesn't work make sure series are set to visible before calling above method.
Code: Select all
plot.getRenderer().setSeriesVisibleInLegend(seriesIndex, Boolean.FALSE, true);