图表JS-记住页面刷新后隐藏的标签状态



在Chart.js中,可以使用"hidden"参数以编程方式从图形中隐藏标签。当用户单击标签并将其隐藏时,我希望将此状态更改存储在cookie/缓存中,因此当用户返回页面时,标签已经隐藏。这可能吗?我该怎么做?

我使用的是Chart.js v2.9.3

我想你已经知道如何处理cookie了,下面是覆盖默认函数的代码,该函数为你提供了一个索引,你可以将其存储在cookie中以供进一步使用。

onClick(e, legendItem) {
// Get the index
const index = legendItem.datasetIndex;
//index alert
alert("dataset index " + index + " was clicked")
// this table object
const ci = this.chart;
// show/hide dataset
var meta = ci.getDatasetMeta(index);
meta.hidden = meta.hidden === null? !ci.data.datasets[index].hidden : null;
// We hid a dataset ... rerender the chart
ci.update();
},

将此代码复制粘贴到选项图例中:{此处}

最后,这里是一个jsFiddle实例。https://jsfiddle.net/pt0s95r7/1/

最新更新