score:2
Accepted answer
The toggle function could be written as follows:
function toggleData(button, category) {
chart.toggleDataVisibility(category);
chart.update();
if (chart.getDataVisibility(category)) {
button.classList.remove('hidden');
} else {
button.classList.add('hidden');
}
}
Further information about the Chart.js API can be found here.
Please take a look at your amended code and see how it works:
const chart = new Chart('chart', {
type: 'doughnut',
data: {
labels: ['Cat1', 'Cat2', 'Cat3', 'Cat4'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5],
backgroundColor: [
'rgb(143,219,226)',
'rgb(255,206,148)',
'rgb(158,239,181)',
'rgb(251,172,172)'
],
borderColor: [
'rgb(143,219,226)',
'rgb(255,206,148)',
'rgb(158,239,181)',
'rgb(251,172,172)'
],
borderWidth: 1,
}]
},
options: {
responsive: false,
plugins: {
legend: {
display: false
}
}
}
});
function toggleData(button, category) {
chart.toggleDataVisibility(category);
chart.update();
if (chart.getDataVisibility(category)) {
button.classList.remove('hidden');
} else {
button.classList.add('hidden');
}
}
.hidden {
text-decoration: line-through;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.0/chart.js"></script>
<div style="display: flex; flex-direction: column; align-items: center;">
<div style="display: flex; gap: 10px; margin-bottom: 10px">
<button id="category0" onclick="toggleData(this, 0)">category0</button>
<button id="category1" onclick="toggleData(this, 1)">category1</button>
<button id="category2" onclick="toggleData(this, 2)">category2</button>
<button id="category3" onclick="toggleData(this, 3)">category3</button>
</div>
<canvas id="chart" width="200"></canvas>
</div>
Source: stackoverflow.com
Related Query
- ChartJS doughnut data toggle on custom button click
- Chartjs to show more set of data of click of a button
- Changing data when click button in chart.js
- Switch chart.js data with button click
- Custom data in label on ChartJS pie chart
- Custom data position on the doughnut chart in chart.js
- How to create a custom tooltip for chartJS graph with data in JSON array in JavaScript?
- ChartJS doughnut legend click
- Update the chart data from an array stored in a variable on button click
- Can I use a custom data format when using Vue + Chartjs
- ChartJS have xAxes labels match data source
- How to toggle between Custom tooltip and normal tooltip in chartjs and angular
- Updating Chartjs to 2.5 with custom code
- ChartJS custom doughnut chart not showing in version 2.6.0
- ChartJS custom legend doughnut separate legend from chart area
- I need to increase data in a bar chart by one with a button click
- Calling data from outside of Chartjs code
- Chartjs doughnut chart for conditional data
- Chart.js - Show new data on button click
- getting additional value fields from data source for dx.chartjs doughnut chart
- My ChartJS Line needs to click the color legend first before it plots the data
- Angular how to display data inside every part of doughnut chartjs
- How to change chart js data on button click
- Chartjs Custom Legend for Doughnut Chart Having Labelled Lines Sticking Out of Each Section
- How can I add functionality to Chartjs Doughnut chart custom legend
- Changing data when click button in chart.js/vue.js
- How to implement doughnut ChartJS with CodeIgniter 3 and display data from database
- Using data in HTML to display ChartJS Doughnut chart
- Group Bar Chart Dinamic Data Update By Button Fuction ChartJS
- JavaScript Chart.js - Custom data formatting to display on tooltip
More Query from same tag
- Use Ternary Option with Chart.js and AngularJs
- how to change thickness of two doughnut charts in chartjs
- How to add panning to chart in chartjs?
- Trying to refresh label when click on it. Cannot read property '_meta' of undefined"
- How can I build a vertical line without going through my dots on my line graph? (charts.js)
- Is there way to get counts of every day of week by created_at in laravel php
- Why I don't see the second plot?
- Connecting Two Datasets in Chart.js Into a Single Line
- How to make bars dashed in chart js?
- Pie Chart Label is not visible in ReactJS
- How do I place 2 <div>s side by side
- chart.js v2 - how to 'fill' the graph when using time scale
- Unique identifier in Chartjs Bar segments?
- Chart.js setting maximum bar size of bar chart
- Is it possible to draw min / max lines on Chart JS
- Displaying a chartjs chart within a bulma full height hero
- Responsive canvas extends beyond div
- Creating a grouped and stacked chart in Chart.js
- Unable to hide X-Axis in horizontal stacked chart.js
- Chartjs: How to offset ticks on radarchart?
- Vue.js using chart.js options not working
- Chart.js: Truncate Legend's Labels
- Chart in Ionic Framework doesn't show up
- Set custom colours for tooltip squares Chart.js
- Using Chart.js 2.0, display line chart values
- charts.js straight lines - i cant find a solution
- d3.csv parseFloat will not parse correctly
- zooming/blowup and panning option in chart.js on category scales
- Shorten number labels in Charts.js
- get JSON data from function to give value to Chart JS