ChartType.AREA has a cool feature that adjust the data displayed in the chart when an individual legend item is clicked.
However, the legend toggle feature fails after adding a LegendItemClickListener .
Simply adding the following causes the failure.
chart.addLegendItemClickListener(new LegendItemClickListener() { @Override
public void onClick(LegendItemClickEvent event) {
}
});
Questions:
Is addLegendItemClickListener adding a listener or setting the listener?
Is this a bug?
I worked around the behavior by get the ListSeries object and simply series.setVisible(!series.isVisible());
The issue is marked currently as an enhancement, but is almost a bug IMO… At least method should be named as setLegendItemClickListener if the current behaviour is not fixed.
After running a test, multiple listeners can be added and receive messages using addLegendItemClickListener().
The trick is reproducing the default behavior, which requires maintaining series state, and re-establishing the visibility with the Listener. Example below.
chart.addLegendItemClickListener(new LegendItemClickListener() {
@Override
public void onClick(com.vaadin.addon.charts.LegendItemClickEvent event) {
// Note, series data is maintained else where,
// the getSeriers() in LegendItemClickEvent does not really contain the series data.
// I think the bad verbiage throughout the Vaadin charts has lead to some confusion.
// Example getLabels() does not return labels.
ListSeries data = series.get(event.getSeriesItemIndex());
data.setVisible(!data.isVisible());
... do something useful...
}
}
I had the same problem and solved it just the same way you did.
But sadly, in a pie chart you cannot do this, since there is no method in the API to toggle the visibility of a single DataSeriesItem. That’s why I marked my ticket
http://dev.vaadin.com/ticket/16540 as enhancement. Feel free to comment on it.