score:6
You may see an implementation as a plugin here and below.
// A plugin that hides slices, given their indices, across all datasets.
var hideSlicesPlugin = {
afterInit: function(chartInstance) {
// If `hiddenSlices` has been set.
if (chartInstance.config.data.hiddenSlices !== undefined) {
// Iterate all datasets.
for (var i = 0; i < chartInstance.data.datasets.length; ++i) {
// Iterate all indices of slices to be hidden.
chartInstance.config.data.hiddenSlices.forEach(function(index) {
// Hide this slice for this dataset.
chartInstance.getDatasetMeta(i).data[index].hidden = true;
});
}
chartInstance.update();
}
}
};
Chart.pluginService.register(hideSlicesPlugin);
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'pie',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
data: [15, 1, 1, 1, 45, 1],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}, {
data: [5, 1, 25, 10, 5, 1],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}],
// Hide the second (index = 1) and the fourth (index = 3) slice.
hiddenSlices: [1, 3]
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>
The slices to be hidden are provided using the hiddenSlices
attribute, which should be an array of indices corresponding to existing slices. The slices are hidden across all datasets using the hideSlicesPlugin
, if hiddenSlices
has been set.
score:2
Update - 17/Nov/2019
Don't do this, at the time I wrote this I was new to ChartJS and Web Development as well and it seems that the solution above already outdated, so I tried to answer with things that works for me. Maybe it's already outdated too, I haven't used ChartJS much since then. I moved to Echarts.
Since @xnakos answer is already outdated, I just wanted to share the same thing can be done easier in the current ChartJS version (2.7.3). Simply set the values to undefined
, setting it to other values such as null
will leave the legends being seen (or not crossed).
var ctx = document.getElementById("myDoughnut").getContext("2d");
var myChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: ["Red", "Green", "Blue"],
datasets: [{
label: '# of Votes',
data: [12, 19, undefined],
// Set this value to undefined
backgroundColor: [
'#f87979',
'#79f879',
'#7979f8'
],
borderWidth: 5
}]
},
options: {
tooltips: {
mode: null
},
plugins: {
datalabels: {
borderWidth: 5,
borderColor: "white",
borderRadius: 8,
// color: 0,
font: {
weight: "bold"
},
backgroundColor: "lightgray"
}
}
}
});
<div id="app">
<div width="400">
<canvas id="myDoughnut"></canvas>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@0.4.0/dist/chartjs-plugin-datalabels.js"></script>
<!-- Made in 21 Nov 2018 -->
<!-- with ChartJS 2.7.3 -->
Here's a link to JSFiddle, for you to play around. http://jsfiddle.net/irfandyjip89/fokbgnvz/5/
However, must be noted, if you're using datasets on Pie/Doughnut, it's better to set it into null
. Because one undefined
value in any of the dataset will cross the legends.
But if you do set one undefined
values on any of the dataset, it will result in the legends being crossed (or strikethrough). EVEN WHEN THERE'S A VALUE ON THE NEXT DATASET.
This means that if you only use one dataset, and you don't want the data being shown in the chart and getting the legends crossed you want to set the values to undefined
. Whereas, null
values will get you same result without the legends being crossed.
However it's preference on how the Chart should appear, but personally I would want to use undefined
values if I would like to cross the legends (usually on one dataset chart)and hide the data, and will use null
values if I would like to just hide the data without getting the legends crossed (usually on multiple dataset chart).
If you want to check how it went when playing with multiple datasets, you can check this fiddle http://jsfiddle.net/irfandyjip89/fokbgnvz/19/
P.S. If you're wondering how did I put a label value on the center, it's using a plugin called chartjs-plugin-datalabels
score:2
None of the answers provided here have worked for Chart.js version 3.2.1 (what I am using). Here is what I came up with (and it works):
let ctx = document.getElementById('myChart').getContext('2d');
window.chart = new Chart(ctx, {
type: 'pie',
data: {
labels: ['Red', 'Orange', 'Yellow', 'Green', 'Blue'],
datasets: [{
data: [100, 100, 200, 300, 140],
backgroundColor: ['rgb(255, 99, 132)', 'rgb(255, 159, 64)', 'rgb(255, 205, 86)', 'rgb(75, 192, 192)', 'rgb(54, 162, 235)'],
hoverOffset: 6,
}]
},
options: {
layout: { padding: 10 },
responsive: true,
maintainAspectRatio: true,
plugins: {
title: {
display: true,
text: 'Colors - Sample Pie Chart'
}
}
}
});
toggleData('red');
window.chart.render();
function toggleData(txt) {
let ci = window.chart;
let legendItems = ci.legend.legendItems;
let index;
for (index = 0; index < legendItems.length; ++index) {
let legendItem = legendItems[index];
if (legendItem.text.toLowerCase() === txt.toLowerCase()) {
// let visible = chart.getDataVisibility(index);
ci.toggleDataVisibility(index);
ci.update();
return;
}
}
}
/*
Note: Do not style the canvas element. See:
https://github.com/chartjs/Chart.js/issues/9089
*/
#myChartContainer {
width:400px;
height:400px;
}
<div id="myChartContainer">
<canvas id="myChart"></canvas>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.2.1/dist/chart.min.js"
integrity="sha256-uVEHWRIr846/vAdLJeybWxjPNStREzOlqLMXjW/Saeo=" crossorigin="anonymous"></script>
References:
score:4
After creating the chart, you can retrieve its metadata through .getDatasetMeta(index)
, change the hidden
attribute of the desired slice and update the chart as shown in the following code snippet. When the user clicks on the label of the hidden slice, it will become visible again.
const chart = new Chart(document.getElementById('myChart'), {
type: 'pie',
data: {
labels: ['Red', 'Orange', 'Yellow', 'Green', 'Blue'],
datasets: [{
data: [100, 100, 200, 300, 140],
backgroundColor: ['rgb(255, 99, 132)', 'rgb(255, 159, 64)', 'rgb(255, 205, 86)', 'rgb(75, 192, 192)', 'rgb(54, 162, 235)']
}]
},
options: {
responsive: true,
maintainAspectRatio: true,
title: {
display: true,
text: 'Colors - Sample Pie Chart'
}
}
});
chart.getDatasetMeta(0).data[4].hidden = true;
chart.update();
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="myChart" height="150"> </canvas>
Source: stackoverflow.com
Related Query
- How to hide section in a Chart.js Pie Chart
- How set color family to pie chart in chart.js
- How do you hide the title of a chart tooltip?
- How to display data labels outside in pie chart with lines in ionic
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- How to create single value Doughnut or Pie chart using Chart.js?
- How do you set pie chart colors in angular-chart.js
- ChartJs - Pie Chart - how to remove labels that are on the pie chart
- How can I rotate a pie chart in charts.js?
- How to show percentage (%) using chartjs-plugin-labels ( Pie chart ) in angular 2/8
- ChartJS version 3 how to add percentage to pie chart tooltip
- How to hide value in Chart JS bar
- PrimeNg bar chart how do I hide the bar labels?
- Chart.Js how to hide datapoints with "0" value in Stacked Bar Chart
- how to add percentage value to legend field in pie chart using chart.js
- how to add legend in pie chart
- How to show slice value inside of slice in pie chart using chart.js
- Hide labels from pie chart in chartjs
- Chart JS - How to output JSON objects into PIE variant
- chart.js pie chart - how to update dataset with smooth transition
- Chart JS, ng2-Charts - How to make the label to the right of pie chart instead on top?
- How to style a pie chart in chart js? I want to change the border color, border width and give them shadow
- How to get labels on ChartJS Pie chart segments
- How can I re-distribute values within a charts.js pie chart when a legend item is turned off/on?
- How can I change the cursor on pie chart segment hover in ChartJS 3?
- How to make chartjs pie chart responsive
- How to group smaller Pie Chart slices together to Improve Readability in chartjs
- How to hide other section of chartjs on click of lengend
- How add the sizes of the slices in the pie chart (at the top) in Chart.js?
- how to display name on multi series pie chart in chartjs
More Query from same tag
- How to use chart.js in Angular2?
- Hide legends in Chartjs
- ChartJS set default axis
- Chart.js - draw horizontal line based on a certain y-value
- TS how can I use a class variable inside method?
- Bar charts not getting displayed correctly when bar is below zero (chart.js)
- chart.js API returns bad x and y points
- How to draw a line in line chart for single value in Charts.JS
- Chart not rendering with pug/jade and nodejs
- Map with pie chart and label
- Chart js - Customize chart
- Chart.js HTML custom legend issues with doughnut chart
- Chartjs Array.join
- Resizing chart before downloading as image
- Chart not rendering w/Chart.js on button click
- Chart js 2.x renders the canvas invisible
- Background color hides Y labels
- ChartJS: How to add empty values?
- Change color of line along labels Chart.js
- Rotate left legend chartjs
- Chart.js Ionic 2 Angular2: Background color of bar graph not changing
- SCRIPT438: Object doesn't support property or method 'values'
- Is there way to get counts of every day of week by created_at in laravel php
- Update ChartJS line chart from DynamoDB query
- Combining multiple legend elements in chart.js
- ChartJS large data range
- Graph is not displayed
- Chart.js time object labels not updating correctly
- How to use log scale with Chart Kick?
- How to change colour of specific xAxis label in charts js