Chart.js
Chart.js copied to clipboard
Legend Overflow Handling
I have been using Chart.js 2 quite a lot recently and there is one feature I have seen suggested but has been overlooked. Adding overflow handling for the legend would help me out immensely. Currently I have some datasets that could have anywhere from 10 - 50 keys in the legend. I have seen solutions like those found here but would like to avoid having to separate my legend from the chart. Adding the ability to set the dimensions of the legend within the canvas object as well as what do when our labels go beyond those dimensions would be very beneficial to my projects.
legend:{
labels:{
fontStyle: "bold",
},
display: true,
position: 'right',
width: 100, //suggested
height: 200, //suggested
overflowX: hide, //suggested
overflowY: scroll, //suggested
},
You can make a custom html legend: https://www.chartjs.org/docs/latest/configuration/legend.html#html-legends
Although we can create a custom legend, having the possibility of using the provided one and modify its height, width and overflow behaviour would be certainly easier for the user.
I would like to see this feature implemented.
The legend is drawn in the canvas (unless you add a custom html legend), they would have to add custom scrolling behaivior for this, which will never be as good as the browser's build in scrolling behaivior. This simply doesn't seem feasible?
If you want scrolling behavior, using the browser's build in functionality seems like the way to go, which means using a html legend.
You can update and move the legend position, change the font size, or even deactivate the legend dynamically by using onResize function (https://www.chartjs.org/docs/latest/configuration/responsive.html), this function passed two arguments which are Context and size.
So, we can use onResize function to mutating the option configuration (the documentation for this : (https://www.chartjs.org/docs/latest/developers/updates.html) . In that documentation example shows that you can use this function to mutating the option configuration :
function updateConfigByMutating(chart) {
chart.options.plugins.title.text = 'new title';
chart.update();
}
From here, we can utilize the query inside function updateConfigByMutating(chart) place it inside onResize method and set whatever screen size condition.
Hi guys. If you want to customize the legend visually, please use an HTML legend. Due to Chart.js using canvas, we can't implement every requested feature cause this will bloat the code.