Sometimes chart legend can get out of control size-wise, especially if you have long labels. This tutorial will show how you can get it back in check by capping the length of its labels.
Long labels will bloat up legend size. This is especially prominent where chart size is not very big:
Even if you put the legend to the side, which will limit whole width of the legend, the long labels look kinda messy:
The solution is quite simple: set maximum width for the labels and make them truncate.
Those can be achieved using maxWidth and truncate respectively. They are set on legend.labels.template:
chart . legend . labels . template . maxWidth = 150 ;chart . legend . labels . template . truncate = true ;
chart . legend . labels . template . maxWidth = 150 ; chart . legend . labels . template . truncate = true ;
{ // ... "legend": { "labels": { "maxWidth": 150, "truncate": true } }}
Now, size of the legend items become somewhat manageable:
There’s a catch, though. We don’t see the whole text.
Let’s add a tooltip that would show full category name on hover. It’s easy, since we can add a tooltip to pretty much anything, as well as make it reference to data using curly bracket placeholders.
We’re going to set that on the whole of the legend item container:
chart . legend . itemContainers . template . tooltipText = " {category} " ;
chart . legend . itemContainers . template . tooltipText = " {category} " ;
{ // ... "legend": { // .. "itemContainers": { "tooltipText": "{category}" } }}
Go ahead, try it out:
Result JS HTML CSS
Open in: New tab CodePen (opens in a new tab) jsFiddle (opens in a new tab) Run * ---------------------------------------
* This demo was created using amCharts 4.
* For more information visit:
* https://www.amcharts.com/
* Documentation is available at:
* https://www.amcharts.com/docs/v4/
* ---------------------------------------
var chart = am4core . create ( " chartdiv " , am4charts . PieChart );
category: " No Country for Old Men " ,
category: " All the Pretty Horses " ,
category: " Blood Meridian or the Evening Redness in the West " ,
category: " The Orchard Keeper " ,
// Add and configure Series
var pieSeries = chart . series . push ( new am4charts . PieSeries ());
pieSeries . dataFields . value = " value " ;
pieSeries . dataFields . category = " category " ;
pieSeries . ticks . template . disabled = true ;
pieSeries . labels . template . disabled = true ;
chart . legend = new am4charts . Legend ();
chart . legend . position = " right " ;
chart . legend . labels . template . maxWidth = 120 ;
chart . legend . labels . template . truncate = true ;
chart . legend . itemContainers . template . tooltipText = " {category} " ;
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
tooltips on legend items tooltips-on-legend-items
There’s an alternative: wrapping labels instead of truncating them.
It works similarly to what we had above, but instead of setting truncate we set wrap.
chart . legend . labels . template . truncate = false ;chart . legend . labels . template . wrap = true ;
chart . legend . labels . template . truncate = false ; chart . legend . labels . template . wrap = true ;
{ // ... "legend": { "labels": { // ... "truncate": false, "wrap": true } }}
Result JS HTML CSS
Open in: New tab CodePen (opens in a new tab) jsFiddle (opens in a new tab) Run * ---------------------------------------
* This demo was created using amCharts 4.
* For more information visit:
* https://www.amcharts.com/
* Documentation is available at:
* https://www.amcharts.com/docs/v4/
* ---------------------------------------
am4core . options . commercialLicense = true ;
var chart = am4core . create ( " chartdiv " , am4charts . PieChart );
category: " No Country for Old Men " ,
category: " All the Pretty Horses " ,
category: " Blood Meridian or the Evening Redness in the West " ,
category: " The Orchard Keeper " ,
// Add and configure Series
var pieSeries = chart . series . push ( new am4charts . PieSeries ());
pieSeries . dataFields . value = " value " ;
pieSeries . dataFields . category = " category " ;
pieSeries . ticks . template . disabled = true ;
pieSeries . labels . template . disabled = true ;
chart . legend = new am4charts . Legend ();
chart . legend . position = " right " ;
chart . legend . width = 250 ;
chart . legend . labels . template . maxWidth = 150 ;
chart . legend . labels . template . truncate = false ;
chart . legend . labels . template . wrap = true ;
chart . legend . itemContainers . template . tooltipText = " {category} " ;
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
tooltips on legend items tooltips-on-legend-items-2