I have a line chart with many series. These series are grouped into one or more super-series. Each super-series may have many "breaks" in the line in order to accurately depict when a monitor process is not actively collecting data. Each data break is actually starting a new series.
I have been able to successfully overcome several technical issues with this such as the chart assigning a new color to each new series, chart line symbol color not matching the series color, etc. All is working quite well right now, except that each time I add a new series to the chart, it adds an item to the legend.
Is there a way to remove items from the legend, or do I have to hide the default legend and add my own custom legend pane?
Don't show the legend:
You can then create your own custom pane to make your own legend and render it how you wish.
After several failed attempts at implementing various suggestions, I found that the best way to allow a user to show/hide a data series in a JavaFx Chart (or sub-classes thereof) is to extend the chart class you want to use and override its updateLegend() method.
It's quite simple actually. Here's an example using a basic HBox as the legend containing check boxes as the legend items. In this example I have decided to make my LineChart with fixed axes types (CategoryAxis and NumberAxis). You might choose to leave your sub-class with generics for axes.
I'll leave it as an exercise for the reader to make the legend more readable, for example, borders around each checkbox and binding the color of the series to the something showing that color in the checkbox for the series.
One other thing, you might want to check the getLegendSide() method to decide which kind of layout container to use for the legend, i.e. HBox for TOP and BOTTOM but VBOX for LEFT and RIGHT. Your choice.
You can find a node based on it's type (and optionally style name) using this method:
Calling it with the chart in question to retrieve the Legend node:
Which you can then iterate through the children and remove the ones you don't want to be displayed:
JavaFX also has a
lookup
function which "Finds this Node, or the first sub-node, based on the given CSS selector." and acts similar to thefindNode
function from this answer.