Search code examples
hideflotlegend

hide legend for some series in flot


Hi I have a flot chart with series like this:

  • foo
  • bar
  • foo/second
  • bar/second

The relative values (per second) are displayed as lines, whereas the absolute values are displayed as columns. All within the same chart. Absolute and relative of the same entity have the same color. I want the legend to be like this:

  • [red] foo
  • [blue] bar

I achieved this by setting the label of the relative series to null. However, I use the label when hovering over a point in the series. Now the null value is futile.

So is there a way to explicitly hide a series in the legend, but still set its label?

Thanks.


Solution

  • Instead of setting the label of the data series itself to null, use a labelFormatter function for your legend (see here):

    labelFormatter: function(label, series) {
        // series is the series object for the label
        // label is the original label
    
        if (showInLegend) {
            return label;
        }
        else {
            return null;
        }
    }