简体   繁体   English

在Highcharts中,如何在调整图例项目宽度后重绘图表?

[英]In Highcharts, how to redraw chart after legend item width resized?

The requirements are: 要求是:
1. "ellipsis" visual effect "..." for overflowed legend items' text. 1.“省略号”视觉效果“ ...”,用于溢出的图例项的文本。
2. After resized, the legend item width should adjust with the new container size. 2.调整大小后,图例项的宽度应随新的容器大小调整。 Means if the legend item had text "blahblahblahblah", and after resizing, the chart container get smaller and have no enough space to display the full text, it should display "blahblah...". 意味着如果图例项具有文本“ blahblahblahblah”,并且在调整大小后,图表容器会变小并且没有足够的空间来显示全文,则应该显示“ blahblah ...”。

I've figured out the 1st requirement by giving the legend itemWidth a fixed width in pixel, and set the textOverFlow to "ellipsis": 我通过给图例itemWidth提供一个固定的像素宽度来解决第一个要求,并将textOverFlow设置为“省略号”:

chartOptions.legend.itemStyle.width = $container.width() + "px";
chartOptions.legend.itemStyle.textOverflow = "ellipsis";

But for the 2nd requirement, I changed the chart.legend.itemStyle.width to the current container width, and call the chart.redraw() , and it doesn't really redraw the chart. 但是对于第二个要求,我将chart.legend.itemStyle.width更改为当前容器的宽度,并调用了chart.redraw() ,它并没有真正重绘图表。

`chart.legend.itemStyle.width = $container.width() + "px";
`chart.redraw()`

Is there any way I can update the chart with new legend.itemStyle.width ? 有什么办法可以使用新的legend.itemStyle.width更新图表?

Jsfiddle: https://jsfiddle.net/scottszb1987/z5qv1t80/ Jsfiddle: https ://jsfiddle.net/scottszb1987/z5qv1t80/

Cheers 干杯

In your case, I'd suggest adding a window .resize() event and then producing a new version of the chart with the updated options. 对于您的情况,我建议添加一个window .resize()事件,然后使用更新的选项生成图表的新版本。 That way, your legend size will pull in the new width of the container element. 这样,您的图例大小将拉入container元素的新宽度。

// whenever the window is resized, destroy the chart and redraw it using updated options
$(window).resize(function() {
    chart.destroy();
    chart = new Highcharts.Chart(getChartOptions($("#container")));
});

Here's an updated version of your fiddle with this change: https://jsfiddle.net/brightmatrix/z5qv1t80/5/ 这是您的小提琴的更新版本,其中包含以下更改: https : //jsfiddle.net/brightmatrix/z5qv1t80/5/

I hope this is helpful for you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM