score:1
You have to generate the legend labels yourself by defining a legend.labels.generateLabels
function together with a legend.onClick
function that takes care of hiding and showing individual pie slices. This could look as follows:
legend: {
labels: {
generateLabels: () => {
let labels = [];
config.data.datasets.forEach((ds, iDs) => labels = labels.concat(ds.labels.map((l, iLabel) => ({
datasetIndex: iDs,
labelIndex: iLabel,
text: l,
fillStyle: ds.backgroundColor[iLabel],
hidden: employeesGraph ? employeesGraph.getDatasetMeta(iDs).data[iLabel].hidden : false,
strokeStyle: '#fff'
}))));
return labels;
}
},
onClick: (event, legendItem) => {
const metaData = employeesGraph.getDatasetMeta(legendItem.datasetIndex).data;
metaData[legendItem.labelIndex].hidden = !metaData[legendItem.labelIndex].hidden;
employeesGraph.update();
}
},
Please have a look at your amended code below:
var platform_labels = ["Tablet", "Ordenador"];
var platform_dataset = [14, 5];
var os_labels = ["Android", "Windows", "GNU\/Linux"];
var os_dataset = [14, 4, 1];
var devices_labels = ["Tablet", "Ordenador", "Android", "Windows", "GNU\/Linux"];
var chartColors = {
red: 'rgb(255, 99, 132)',
orange: 'rgb(255, 159, 64)',
yellow: 'rgb(255, 205, 86)',
green: 'rgb(75, 192, 192)',
blue: 'rgb(54, 162, 235)',
purple: 'rgb(153, 102, 255)',
grey: 'rgb(201, 203, 207)'
};
var config = {
type: 'doughnut',
data: {
datasets: [{
data: platform_dataset,
backgroundColor: [
chartColors.red,
chartColors.orange,
],
label: 'Platform',
labels: platform_labels
}, {
data: os_dataset,
backgroundColor: [
chartColors.purple,
chartColors.green,
chartColors.blue
],
label: 'OS',
labels: os_labels
}],
labels: devices_labels
},
options: {
legend: {
labels: {
generateLabels: () => {
let labels = [];
config.data.datasets.forEach((ds, iDs) => labels = labels.concat(ds.labels.map((l, iLabel) => ({
datasetIndex: iDs,
labelIndex: iLabel,
text: l,
fillStyle: ds.backgroundColor[iLabel],
hidden: employeesGraph ? employeesGraph.getDatasetMeta(iDs).data[iLabel].hidden : false,
strokeStyle: '#fff'
}))));
return labels;
}
},
onClick: (event, legendItem) => {
const metaData = employeesGraph.getDatasetMeta(legendItem.datasetIndex).data;
metaData[legendItem.labelIndex].hidden = !metaData[legendItem.labelIndex].hidden;
employeesGraph.update();
}
},
tooltips: {
callbacks: {
label: function(tooltipItem, data) {
var dataset = data.datasets[tooltipItem.datasetIndex];
var index = tooltipItem.index;
return dataset.labels[index] + ": " + dataset.data[index];
}
}
}
}
};
var ctx = document.getElementById('deviceChart').getContext('2d');
var employeesGraph = new Chart(ctx, config);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.js"></script>
<canvas id="deviceChart" height="120"></canvas>
score:1
The second set of labels are striked through because chartjs see's them as hidden, i.e. they have no values so are not rendered and the labels are only getting drawn from the first dataset.
There is a few ways around this, you could either disable the legend and then create a custom legend but I'm not sure if this will grab all of the labels:
var chart = new Chart(ctx, {
type: 'line',
data: data,
options: {
legend: {
display: false
},
legendCallback: function(chart) {
var text = [];
text.push('<ul>');
for (var i=0; i<devices_labels.length; i++) {
text.push('<li>');
text.push('<span style="background-color:' +
chart.data.datasets[i].borderColor + '">' + devices_labels[i] +
'</span>');
text.push('</li>');
}
text.push('</ul>');
return text.join("");
}
}
});
Or you could extend chartjs to change the behaviour of labels for hidden data:- change legend item style when dataset is hidden
Or the simplest way is to just add in some dummy data, to your first dataset:-
var platform_labels = ["Tablet","Ordenador"];
var platform_dataset = [14, 5, 0, 0, 0];
var os_labels = ["Android","Windows","GNU\/Linux"];
var os_dataset = [14,4,1];
var devices_labels = ["Tablet","Ordenador","Android","Windows","GNU\/Linux"];
var chartColors = {
red: 'rgb(255, 99, 132)',
orange: 'rgb(255, 159, 64)',
yellow: 'rgb(255, 205, 86)',
green: 'rgb(75, 192, 192)',
blue: 'rgb(54, 162, 235)',
purple: 'rgb(153, 102, 255)',
grey: 'rgb(201, 203, 207)'
};
var config = {
type: 'doughnut',
data: {
datasets: [{
data: platform_dataset,
backgroundColor: [
chartColors.red,
chartColors.orange,
chartColors.purple,
chartColors.green,
chartColors.blue
],
label: 'Platform',
labels: platform_labels
}, {
data: os_dataset,
backgroundColor: [
chartColors.purple,
chartColors.green,
chartColors.blue
],
label: 'OS',
labels: os_labels
}],
labels: devices_labels
}
};
var ctx = document.getElementById('deviceChart').getContext('2d');
var employeesGraph = new Chart(ctx, config);
Source: stackoverflow.com
Related Query
- How to show second set of labels to Chart.js doughtnut chart?
- Chart.js how to show cursor pointer for labels & legends in line chart
- How to add a second set of labels to a Chart.js doughnut chart?
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- How to show scale labels and set min/max for yAxes?
- Chart.js: How to get x-axis labels to show on top of bars in bar chart
- How to show the chartjs bar chart data values labels as text?
- how can i show labels and value in both on bar chart
- Char.js - How to show labels by default in pie chart
- Chart.js how to show line chart without displaying the labels on xaxis and yaxis
- How remove duplicates xAxis labels and show all values on chart
- How to show bar chart labels clearly using ChartJS?
- ng2-charts / chart.js - How to set doughnut/pie chart color on specific labels programatically?
- how to show X and Y labels on points in Chart js 2 Line Chart
- How to show labels above pie chart in chart.js
- How to set data values as labels in Chart.js with a Radar Chart
- Chart.js - How to set a line chart dataset as disabled on load
- chart js 2 how to set bar width
- Chart.js Show labels on Pie chart
- chart.js: Show labels outside pie chart
- How set color family to pie chart in chart.js
- chartjs : how to set custom scale in bar chart
- How to set labels align left in Horizontal Bar using chart.js?
- How to display Line Chart dataset point labels with Chart.js?
- How to add second Y-axis for Bar and Line chart in Chart.js?
- chart js tooltip how to control the data that show
- How to show percentage (%) in chart js
- How to display data labels outside in pie chart with lines in ionic
- How to align Chart.JS line chart labels to the center
- How to access labels array using chart plugin (Chart.pluginService.register) in Chartjs 2.x?
More Query from same tag
- generate basic pie chart using chartjs
- Chart.js v2 overwrite draw function
- How to make data appear on the rightmost edge of line graph Chart.js
- ChartJS - ignore labels
- Why Chart.js's tooltip appears in wrong position when mouse hover?
- ChartsJS get value of Data/Dataset Label for Tooltip
- Drawing chart.js with external JSON
- y=0 gridline (or axis?) not showing in chart.js
- How to load data values from cookie into Chart.js
- How do I loop under Chart js in JavaScript for multiple axis?
- Chart.JS Chart top left corner is blocked by some visual nodejs
- Line is rendering only vertical line instead of sloped line
- using library without 'require' when using CDN
- Error Message when using ChartJS with React
- Chart.js making this chart more readable / scrollable
- How can I add a euro sign (€) to all tooltips in my chart js line chart
- Vue-chartjs not rendering chart until page resize
- chartjs: How to plot multi-line chart with irregular intervals
- Updating ng-charts barchart datasets in angular 2
- How to change width of the chart using Chart.js
- Which scenarios might make Chart.js's canvas element resize?
- Datas put one on each other on chart.js
- How can I style ChartJS to always be at the bottom of a div?
- Ionic 3 Angular Chart.js update data
- Dropdown component not having effect in Chromium browser
- ChartJS and require: Chart is undefined
- Chartjs dynamic chart return both x & y axis undefined
- How to detect when Tooltip closes in chart.js?
- ChartJS on NodeJS: error Chart is not defined
- how does this javascript recolor the doghnut?