score:54
You need to edit the generateLabels
property in your options :
options: {
legend: {
labels: {
generateLabels: function(chart) {
// Here
}
}
}
}
Since it is quite a mess to create on your own a great template. I suggest using the same function as in the source code and then edit what is needed.
Here are a small jsFiddle, where you can see how it works (edited lines - from 38 - are commented), and its result :
score:0
I wanted to let the user select from 100+ data sets, but rather than adding/removing them from my Chart I decided to set the showLine: false
on any dataset that I want hidden. Unfortunately the default legend would show all 100+. So in my solution I generate the legend manually, filtering out any dataset that has showLine: false
.
Your settings will have this:
legend: {
labels: {
generateLabels: (a) => {
return a.data.labels
}
}
And you'll generate your own labels with a helper function:
function updateAllLabels() {
const myNewLabels = [];
myChart.data.datasets.forEach((element) => {
if (element.showLine) {
myNewLabels.push(generateLabel(element));
}
});
myChart.data.labels = myNewLabels;
}
And you'll generate the label with another function:
function generateLabel(data) {
return {
fillStyle: data.borderColor,
lineWidth: 1,
strokeStyle: data.borderColor,
text: data.countyName, // I attach countryName to my datasets for convenience
}
}
Now just don't forget to call the function whenever updating your chart:
updateAllLabels();
myChart.update();
Happy graphing!
score:1
Following on from tektiv's answer, I've modified it for ES6 which my linter requires;
options: {
legend: {
labels: {
generateLabels: (chart) => {
const { data } = chart;
if (data.labels.length && data.datasets.length) {
return data.labels.map((label, i) => {
const meta = chart.getDatasetMeta(0);
const ds = data.datasets[0];
const arc = meta.data[i];
const custom = (arc && arc.custom) || {};
const { getValueAtIndexOrDefault } = Chart.helpers;
const arcOpts = chart.options.elements.arc;
const fill = custom.backgroundColor ? custom.backgroundColor : getValueAtIndexOrDefault(ds.backgroundColor, i, arcOpts.backgroundColor);
const stroke = custom.borderColor ? custom.borderColor : getValueAtIndexOrDefault(ds.borderColor, i, arcOpts.borderColor);
const bw = custom.borderWidth ? custom.borderWidth : getValueAtIndexOrDefault(ds.borderWidth, i, arcOpts.borderWidth);
const value = chart.config.data.datasets[arc._datasetIndex].data[arc._index];
return {
text: `${label}: ${value}`,
fillStyle: fill,
strokeStyle: stroke,
lineWidth: bw,
hidden: Number.isNaN(ds.data[i]) || meta.data[i].hidden,
index: i,
};
});
}
return [];
},
},
},
},
score:5
Maybe this is a hacky solution, but for me seems simpler.
The filter
parameter
ChartJS legend options have a filter
parameter. This is a function that is called for each legend item, and that returns true/false whether you want to show this item in the legend or not.
filter
has 2 arguments:
legendItem
: The legend item to show/omit. Its properties are described heredata
: The data object passed to the chart.
The hack
Since JS passes objects by reference, and filter
is called for each legend item, then you can mutate the legendItem
object to show the text that you want.
legend : {
labels: {
filter: (legendItem, data) => {
// First, retrieve the data corresponding to that label
const label = legendItem.text
const labelIndex = _.findIndex(data.labels, (labelName) => labelName === label) // I'm using lodash here
const qtd = data.datasets[0].data[labelIndex]
// Second, mutate the legendItem to include the new text
legendItem.text = `${legendItem.text} : ${qtd}`
// Third, the filter method expects a bool, so return true to show the modified legendItem in the legend
return true
}
}
}
Source: stackoverflow.com
Related Query
- Pie Chart Legend - Chart.js
- Chart,js Pie Chart can the gap between a pie chart and the legend be adjusted
- Chart.js: Pie chart legend "onclick" gets overwritten by "options.onclick" if both are present
- chartjs Adding percentages to Pie Chart Legend
- how to add percentage value to legend field in pie chart using chart.js
- how to add legend in pie chart
- Chart.js - Pie chart calculate sum of visible values after legend click
- How can I re-distribute values within a charts.js pie chart when a legend item is turned off/on?
- Legend color not working with randomly generated background colors in chartjs pie chart
- ChartJS Pie Chart How default just show 2 legend datas
- chart.js legend not working for pie chart
- Pie chart legend styling using Chart js
- ChartJS - How to add Text between Pie Chart and Legend
- Chart js nested pie label colors in legend
- How to increase the size of the donut or pie chart and keep the legend next to it in chart JS?
- React-chart does not render the legend for PIE chart
- Donut Chart : Trigger legend or pie click event while selecting outside filter state change
- Can we remove bar chart line on click on pie chart legend label?
- Legend option destroys pie chart in Chart.js
- Customize Pie Chart Legend in chart.js
- How to add a coloured legend box to a pie chart with Chart.js v1?
- Chart.js Show labels on Pie chart
- Chart.js - Increase spacing between legend and chart
- Chartjs random colors for each part of pie chart with data dynamically from database
- chart.js: Show labels outside pie chart
- How set color family to pie chart in chart.js
- chartjs datalabels change font and color of text displaying inside pie chart
- Chartjs Bar Chart Legend
- chart js how to fill legend box with colour
- Chart.js v2.6: Add arrows to pie chart output values
More Query from same tag
- Chart.js render problem when routing Angular
- Is there a way to store a chart as an image directly to server using Chart.js?
- How to assign different background colors to chart.js pie chart data set?
- Chart.js - get base 64 encoded string as variable
- Chart.JS Data Element To Be settled as Euro Currency
- How display straight corner in chartjs
- Chart.js: Don't stretch axes beyond chart
- i18n with chart.js (javascript)
- AmCharts: Clustered bar chart with sub-categories
- ChartJS: add a clickable text in title
- chart.js legend not working for pie chart
- How to display data labels outside in pie chart with lines in ionic
- How to change font size, padding of chart title only, in chart.js 3.x
- How can I customize p:polarAreaChart tooltips?
- Data-labeling ChartJS, Removing Duplicates?
- Chart.js piechart, only have border on the outside of arc
- Chart JS : Getting issue with draw bar chart
- Printing Chart using ngPrint and tc-chartjs
- Chart.js | Trouble refreshing line chart with "setInterval"
- ng2-charts does not work When data is between 2 and 5
- How can I create a vertical scrolling in Chart.js for line chart?
- Chart.JS Alternate background color for xAxis
- Angular-chart copied from example not showing up
- Chart.JS multiple plugins do not operate
- Chart.js - show tooltip when hovering on legend
- Getting dynamic data for chart.js from Django model
- Chart.js dynamic dataset and tooltip on stacked bar
- why Graph is not showing until i minimize window. (using chart.js )
- Add data dynamically to chart
- how to set the color of all points in a line graph to the same in chart.js?