Repository navigation
Chart.js : legend label with several curves #12156
Replies: 2 comments
|
The Replace your flat font props with a generateLabels: function(chart) {
const datasets = chart.data.datasets;
return datasets.map((dataset, i) => {
return {
text: dataset.label,
fillStyle: dataset.borderColor,
strokeStyle: dataset.borderColor,
fontColor: (i === 0) ? "red" : "green",
font: {
family: (i === 0) ? "Arial" : "sans-serif",
style: (i === 0) ? "italic" : "normal",
weight: (i === 0) ? "bold" : "normal",
size: (i === 0) ? 20 : 12
},
datasetIndex: i
};
});
}The reason See the LegendItem interface in the docs for the full list of supported properties. |
|
For Chart.js 4.6.1, the font setting belongs to An HTML legend lets you set normal CSS separately for each label, for example: firstButton.style.font = 'italic 700 20px Arial, sans-serif';
secondButton.style.font = 'normal 400 12px Georgia, serif';Disable the canvas legend with chart.setDatasetVisibility(index, !chart.isDatasetVisible(index));
chart.update();Here is a complete two-line example using those APIs. Its native legend buttons support pointer, Space and Enter, update |
Uh oh!
There was an error while loading. Please reload this page.
Hello everyone !
I want to create a graph containing two 'line' type curves.
My difficulty is that I want to be able to use a different font for the legend label of the first curve than for the second curve.
The result is that I get both curves correctly, but the font is the same for both labels. The code only works for the text color, but it doesn't change the size, style, or weight.
What is wrong in this code ?
Thanks in advance for your help.
Here is the code I'm using :
All reactions