如何使用自己的按钮隐藏数据集?
How to hide a dataset with your own button?
图表 v3
我在一个图表上有多个数据集,我找不到合适的变量来隐藏特定的一组。
图表 v2
我以前用过
mychart.config.data.datasets[0]._meta[0].hidden = true;
mychart.config.data.datasets[0]._meta[0].hidden = null;
您可以将 toggleDataVisibility
用于饼图和圆环图,将 setDatasetVisibility
用于所有其他图表类型
实例:
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1
}
]
},
options: {
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
const chart = new Chart(ctx, options);
document.getElementById("myBtn").addEventListener("click", () => {
const {
type
} = chart.config;
if (type === 'pie' || type === 'doughnut') {
// Pie and doughnut charts only have a single dataset and visibility is per item
chart.toggleDataVisibility(0);
} else {
chart.setDatasetVisibility(0, !chart.isDatasetVisible(0));
}
chart.update();
});
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<button id="myBtn">
Hide dataset
</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.3.2/chart.js"></script>
</body>
图表 v3 我在一个图表上有多个数据集,我找不到合适的变量来隐藏特定的一组。
图表 v2 我以前用过
mychart.config.data.datasets[0]._meta[0].hidden = true;
mychart.config.data.datasets[0]._meta[0].hidden = null;
您可以将 toggleDataVisibility
用于饼图和圆环图,将 setDatasetVisibility
用于所有其他图表类型
实例:
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1
}
]
},
options: {
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
const chart = new Chart(ctx, options);
document.getElementById("myBtn").addEventListener("click", () => {
const {
type
} = chart.config;
if (type === 'pie' || type === 'doughnut') {
// Pie and doughnut charts only have a single dataset and visibility is per item
chart.toggleDataVisibility(0);
} else {
chart.setDatasetVisibility(0, !chart.isDatasetVisible(0));
}
chart.update();
});
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<button id="myBtn">
Hide dataset
</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.3.2/chart.js"></script>
</body>