如何将数据集切换到Chart.js? [英] How to add a dataset toggle to Chart.js?
问题描述
我正在使用Chart.js来创建折线图。我希望有四个不同的数据集,默认情况下都是可见的,但可以通过单击按钮来打开和关闭。怎么能实现这一目标?我似乎无法在文档中找到答案。 .addData()
, .removeData()
和 .update()
似乎都用于向现有数据集添加或删除值,但不添加或删除整个数据集。我认为这将是相当常用的功能,但我无法在任何地方找到答案。
I'm using Chart.js to create a line chart. I would like to have four different datasets that will all be visibile by default, but can be toggled on and off by clicking a button. How can this be achieved? I can't seem to find an answer in the documentation. .addData()
, .removeData()
and .update()
all seem to be used for adding or removing values to existing datasets, but not adding or removing entire datasets. I would think this would be fairly commonly used feature but I can't find an answer anywhere.
推荐答案
经过深入研究后,似乎没有任何内置函数来切换整个数据集。我使用 .destroy()
函数删除整个现有图表,然后使用一些逻辑用必要的数据集重绘它。
After thoroughly researching this, there doesn't appear to be any built in function to toggle entire datasets. I used the .destroy()
function to remove the entire existing chart, and then some logic to redraw it with the necessary datasets.
编辑:如果对任何人都有帮助,这里有我的完整代码 - > http://jsfiddle.net/21xg27kr/4/
Here's a fiddle with my full code if it's helpful to anyone -> http://jsfiddle.net/21xg27kr/4/
这篇关于如何将数据集切换到Chart.js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!