javascript 将 HighCharts 渲染到类而不是 id?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10996778/
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
Rendering HighCharts to class instead of id?
提问by oshirowanen
I have the following which works fine:
我有以下工作正常:
$(document).ready(function() {
get_data_for_chart();
function get_data_for_chart() {
$.ajax({
url: 'get_data.aspx?rand=' + Math.random(),
type: 'GET',
dataType: 'json',
error: function(xhr, status, error) {
console.log(status);
console.log(xhr.responseText);
},
success: function(results) {
var chart1;
chart1 = new Highcharts.Chart( {
chart: {
renderTo: 'portlet_content_18',
defaultSeriesType: 'column'
}
});
}
});
}
});
Where the HTML looks something like this:
HTML 看起来像这样:
<div id="portlet_content_18">
The user can dynamically select which portlet
s/he wants on screen. S/He can also select to have the same portlet
on the screen more than once for comparison reasons.
用户可以动态选择portlet
他/她想要的屏幕。portlet
出于比较原因,他/她也可以选择在屏幕上多次显示相同的内容。
So if the HTML ends up becoming:
所以如果 HTML 最终变成:
<div id="portlet_content_18">
<div id="portlet_content_18">
Only the first div
gets populated with the chart, and the second one remains blank. How can I get around this issue?
只有第一个div
填充了图表,第二个保持空白。我怎样才能解决这个问题?
采纳答案by Moin Zaman
Yes you can. See their example here: http://jsfiddle.net/gh/get/jquery/1.7.1/highslide-software/highcharts.com/tree/master/samples/highcharts/chart/renderto-jquery/
是的你可以。在此处查看他们的示例:http: //jsfiddle.net/gh/get/jquery/1.7.1/highslide-software/highcharts.com/tree/master/samples/highcharts/chart/renderto-jquery/
basically you assign an jQuery element to a variable:
基本上你将一个 jQuery 元素分配给一个变量:
renderTo: $('.myclass')[0]
renderTo: $('.myclass')[0]
回答by Seza
As Ido already said, you can't have multiple ids, but you can have multiple classes.
正如 Ido 已经说过的,你不能有多个 id,但你可以有多个类。
I had to do the following:
我必须执行以下操作:
var $containers = $('.container'),
chartConfig = {
chart: {
renderTo: null,
defaultSeriesType: 'column'
}
};
$containers.each(function(i, e){
chartConfig.chart.renderTo = e;
new Highcharts.Chart(chartConfig);
});
Also, you don't really have to assign the Chart object to a variable - at least I didn't want to.
此外,您实际上不必将 Chart 对象分配给变量 - 至少我不想这样做。
Hope it helps somebody.
希望它可以帮助某人。