echarts [Feature] Adaptive width/height of legend container

tjrkku2a  于 5个月前  发布在  Echarts
关注(0)|答案(1)|浏览(142)

What problem does this feature solve?

Based on this issue: #7887

We need our chart to adapt the height/width of the legends both vertical and horizontal type so that items doesn't overflow onto the graph area. Currently they only way is to hardcode a pixel/percentage value but it's not responsive.

f87krz0w

f87krz0w1#

There is a way in which you can make it dynamic.
The idea is to retrieve the maximum length (in characters) and give that spacing to the grid (by multiplying it by X, depending on the font size).

MaxLength:

let maxLength = 0;
const series = option.series.map((key) => {
if (key.name.length > maxLength) {
maxLength = key.name.length;
}
....

GRid like this:

grid: {
containLabel: true,
right: showLegend ? maxLength * 9 : 40,
bottom: 80,
},

and thats it :)

相关问题