javascript Highcharts 动态改变标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12277568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Highcharts Dynamically Changing Labels?
提问by kamaci
There is a label at this graphic: http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/combo/label is "Total fruit consumption"
此图形有一个标签:http: //jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/combo/标签是“总计水果消费”
After chart created, how can I change that sentence dynamically?
创建图表后,如何动态更改该句子?
PS:I tries to set labels and redraw graphic but didn't work: http://jsfiddle.net/UXDqL/
PS:我尝试设置标签并重绘图形但没有用:http: //jsfiddle.net/UXDqL/
Any ideas?
有任何想法吗?
回答by Sebastian Bochan
Insted of using labels, you can use renderer text () and then use variable to keep object. If you need update dynamically this text, only what you need is use attr() function and update.
Insted使用标签,您可以使用渲染器文本()然后使用变量来保持对象。如果您需要动态更新此文本,只需使用 attr() 函数并更新。
var title = chart.renderer.text('Total fruits consumption', 100, 90)
.css({
fontSize: '12px'
})
.add();
$('#btn').click(function(){
title.attr({
text:'newText'
});
});
回答by Mark
If you want to change it after the plot is drawn, just act on the element itself. Using jquery selectors it's as easy as:
如果要在绘制绘图后更改它,只需对元素本身进行操作。使用 jquery 选择器就像这样简单:
// find me the tspan containing the specified text and change it to...
$("tspan:contains('Total fruit consumption')").text("Something New")
回答by Mayank Jaiswal
Highchart does not provide any method to dynamically update label header as of Highcharts 3.0. Although it does provide a method to update series dynamically: http://api.highcharts.com/highstock#Series.update()
自 Highcharts 3.0 起,Highchart 不提供任何动态更新标签标题的方法。虽然它确实提供了一种动态更新系列的方法:http: //api.highcharts.com/highstock#Series.update()
To update the label header, we need to follow a brute force approach here i.e. to render the chart again after updating chart options. The following code should do the trick :
要更新标签标题,我们需要在这里使用蛮力方法,即在更新图表选项后再次渲染图表。以下代码应该可以解决问题:
var mychart = $('#container').highcharts();
mychart.options.labels.items[0].html= "Changed label header";
mychart = new Highcharts.Chart(mychart.options);
mychart.render();
JsFiddle: http://jsfiddle.net/msjaiswal/ZD4N5/2/
JsFiddle:http: //jsfiddle.net/msjaiswal/ZD4N5/2/
A similar thread : here
一个类似的线程:here