score:5
Unfortunately there is no option for automatically wrap long legend labels.
You'll have to generate custom HTML legend using legendCallback
together with some CSS
. The following code expects that dataset.label
to be either a string
or an array
of strings in case of a multi-line label (For some types of charts (i.e. pie chart), individual legend labels represent a value from a unique dataset
. In such cases, the code looks slightly different as shown in this answer).
legendCallback: chart => {
let html = '<ul>';
chart.data.datasets.forEach((ds, i) => {
html += '<li>' +
'<span style="width: 36px; height: 14px; background-color:' + ds.backgroundColor + '; border:' + ds.borderWidth + 'px solid ' + ds.borderColor + '" onclick="onLegendClicked(event, \'' + i + '\')"> </span>' +
'<span id="legend-label-' + i + '" onclick="onLegendClicked(event, \'' + i + '\')">' +
(Array.isArray(ds.label) ? ds.label.join('<br/>') : ds.label) + '</span>' +
'</li>';
});
return html + '</ul>';
},
To make this behave the same as standard Chart.js charts, the function
onLegendClicked
is invoked when a mouse click occurs on a legend label. This function toggles the hidden state of individual datasets and changes label text style between normal and strike-through.
function onLegendClicked(e, i) {
const hidden = !chart.data.datasets[i].hidden;
chart.data.datasets[i].hidden = hidden;
const legendLabelSpan = document.getElementById("legend-label-" + i);
legendLabelSpan.style.textDecoration = hidden ? 'line-through' : '';
chart.update();
};
Please take a look at the executable code below and see how it works in action:
function onLegendClicked(e, i) {
const hidden = !chart.data.datasets[i].hidden;
chart.data.datasets[i].hidden = hidden;
const legendLabelSpan = document.getElementById("legend-label-" + i);
legendLabelSpan.style.textDecoration = hidden ? 'line-through' : '';
chart.update();
};
const chart = new Chart(document.getElementById("chart"), {
type: "bar",
data: {
labels: ['A', 'B', 'C'],
datasets: [{
label: ['Legend label', 'on two lines'],
data: [5, 8, 4],
fill: false,
backgroundColor: "rgba(255, 99, 132, 0.2)",
borderColor: "rgb(255, 99, 132)",
borderWidth: 1
},
{
label: ['Legend label', 'spread over', 'three lines'],
data: [3, 5, 4],
fill: false,
backgroundColor: "rgba(255, 159, 64, 0.2)",
borderColor: "rgb(255, 159, 64)",
borderWidth: 1
},
{
label: "Short legend label",
data: [6, 5, 7],
fill: false,
backgroundColor: "rgba(255, 205, 86, 0.2)",
borderColor: "rgb(255, 205, 86)",
borderWidth: 1
}
]
},
options: {
legend: {
display: false
},
legendCallback: chart => {
let html = '<ul>';
chart.data.datasets.forEach((ds, i) => {
html += '<li>' +
'<span style="width: 36px; height: 14px; background-color:' + ds.backgroundColor + '; border:' + ds.borderWidth + 'px solid ' + ds.borderColor + '" onclick="onLegendClicked(event, \'' + i + '\')"> </span>' +
'<span id="legend-label-' + i + '" onclick="onLegendClicked(event, \'' + i + '\')">' +
(Array.isArray(ds.label) ? ds.label.join('<br/>') : ds.label) + '</span>' +
'</li>';
});
return html + '</ul>';
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
document.getElementById("legend").innerHTML = chart.generateLegend();
#legend>ul {
display: flex;
justify-content: center;
}
#legend li {
cursor: pointer;
margin: 0px 10px;
display: flex;
}
#legend li span {
padding-left: 8px;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<div>
<canvas id="chart" height="60"></canvas>
<div id="legend"></div>
</div>
Source: stackoverflow.com
Related Query
- How to wrap legend text in Chartjs?
- ChartJS - How to add Text between Pie Chart and Legend
- How to add label for ChartJs Legend
- How to change the color of legend in chartjs and be able to add one more legend?
- How to remove rectangle box next to the legend text in Chart.js
- How to create custom legend in ChartJS
- How can I force my ChartJS canvas legend to stay in a single column?
- How to add ChartJS code in Html2Pdf to view image
- How can I automatically wrap tooltip text content to multiple lines?
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- How do I change axis, title, legend formatting on chartjs template
- How can I hide legend text if it's zero?
- how to increase space between legend and chart in chartjs (ng2charts ) using angular
- How to format the left legend on chartjs
- ChartJS Pie Chart How default just show 2 legend datas
- Chartjs remove text striking on legend
- How can I move chartJs legend left side and change the width and Hight of the chart?
- How to trigger ChartJS legend onClick with out interrupting its normal working
- Custom Legend ChartJS not showing the text decoration: line through
- How to make custom text appear underneath each bar in chartjs bar chart?
- How to make a interactive legend with chartjs
- chartjs V3 align legend text
- How to change legend icon on hover Chartjs
- How to show text likt this in Chartjs Dougnet chart
- How to dynamically update Chartjs legend label colors?
- How can I add functionality to Chartjs Doughnut chart custom legend
- How to remove legend at the bottom of chartjs doughnuts
- How to run Chart.js samples using source code
- How to add dataset legend in chartjs tooltip
- How to add text inside the doughnut chart using Chart.js?
More Query from same tag
- How can I keep the vertical lines under the horizontal ruler line is chartjs?
- How to improve PDF rasterization quality in PhantomJS?
- How to make dynamic chart with the next view? What libraries allows to customise exactly this like in my design?
- How to get value of defaultdic in JS (Django framework)?
- How to set labels align left in Horizontal Bar using chart.js?
- ChartJS - Override Attributes (AngularJS)
- chart.js: Show labels outside pie chart
- Load data from a JSON object into an array
- Make labels of ChartJS radar in a few rows
- Remove excess lines on y axis using chartjs
- generating a Chart.js chart with python data
- Separating list of data in Ajax
- how to use chart.js in webpacker rails 6
- Chart works uncorrectly chart.js
- How to sort color segments by value within the 100% stacked bars rather than by value across all the stacked bars in Highcharts or ChartJs
- How do I obtain a Chart instance for Chart.js
- Puppeteer with Chart.js
- Bug in canvas class in chart js
- Vuechart js not rendering on a component
- Reference Javascript variable in JSON
- charts are not being show with wicked_pdf
- Change List<Object> into List<List<int>> using LINQ
- How to change label color of ng2 chart in angular
- How to customize tooltip on mouse hover of a polar area chart of Angular Charts
- Single Stacked bar with three sections in Angular
- Chart js: bar width
- Chart.js max legend height
- I can't import ChartsModule
- canvas fill text vanishes when hovering over chartjs pie chart
- The real time chart sometimes does not display when I switch the target