Search code examples
google-visualizationhidelegendseries

Google Charts hide only some series in legend


Is it possible to hide only some series in the legend.

I have a combo chart containing:

  • amount of A
  • amount of B
  • amount of C

all displayed as one stacked column.

and also:

  • amount/second of A
  • amount/second of B
  • amount/second of C

all displayed as separate lines within the same chart.

Now "amount of A" has the same color as "amount/second of A". This color should be displayed in the legend only once and labelled "A".

Is it possible to do that with Google Charts? If so, how?


Solution

  • You can remove specific entries from the legend by setting the chart's series.<series index>.visibleInLegend options to false for the series you want to hide from the legend. As an example, if the amount/second series are the 4th, 5th, and 6th data series in your data set, you could create a series option like this to hide them:

    series: {
        3: {
            visibleInLegend: false
        },
        4: {
            visibleInLegend: false
        },
        5: {
            visibleInLegend: false
        }
    }
    

    The series index is counting data series only, not DataTable columns, so ignore your domain column and any special role columns you are using when determining the series index.