Chart.js: is it possible to toggle legend items programatically?

Created on 15 Aug 2016  路  6Comments  路  Source: chartjs/Chart.js

Hi,

When i click on the legend items they are toggled in the chart. strikethrough items are not rendered.

image

How can i do the same thing by using chart instance, instead of clicking on legend items?

some code like;

chart.hideLabel('RGI');
chart.showLabel('RGI');

Most helpful comment

For me it worked to use this:

//Hide
chart.getDatasetMeta(1).hidden=true;
chart.update();

//Show
chart.getDatasetMeta(1).hidden=null;
chart.update();

Where 1 is the index of RGB in the dataset.

All 6 comments

Following :-)

For me it worked to use this:

//Hide
chart.getDatasetMeta(1).hidden=true;
chart.update();

//Show
chart.getDatasetMeta(1).hidden=null;
chart.update();

Where 1 is the index of RGB in the dataset.

@mennodekker has the correct answer

@mennodekker is right. Just one point: instead of null, better to use false.

The reason is that if the dataset is hidden in the first place, programmatically setting hidden to null won't actually show the dataset.

//Hide
chart.getDatasetMeta(1).hidden=true;
chart.update();

//Show
chart.getDatasetMeta(1).hidden=false;
chart.update();

I'm actually facing a similar issue. I"ve make a custom legend component and I'm able to show/hide using the above codes. However, this only works for bar chart. It raise the following exception when using it with donut chart:

TypeError: Cannot read property '_meta' of undefined at Chart.getDatasetMeta (core.controller.js:656)

Have anyone faced a similar issue please?

Is there anyway to hide or show specific lines, and not the whole chart? @mennodekker 's way of doing this toggles the whole chart.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gouthamrv picture gouthamrv  路  3Comments

Woogles picture Woogles  路  3Comments

joebirkin picture joebirkin  路  3Comments

lizbanach picture lizbanach  路  3Comments

JewelsJLF picture JewelsJLF  路  3Comments