我在使用每个函数时都遇到了麻烦,我想使用 ajax 函数在具有相同名称的不同 div 中加载 highcharts,以便它重新加载。
我正在阅读一篇旧文章,其中图表是这样加载的:
$('.portlet_content_18').each(function(){
var chart = new Highcharts.Chart({
chart: {
renderTo: this,
height: 400
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
}));
这对我有用,但当我更改为 ajax 调用时却不起作用。
我的代码使用ajax调用,仅在第一个名为“graphcontainer”的div中加载是下一个:
function graph() {
$(function () {
$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?', function (data) {
// Create the chart
window.chart = new Highcharts.StockChart({
chart: {
renderTo: 'graphcontainer'
},
rangeSelector: {
selected: 1
},
title: {
text: 'AAPL Stock Price'
},
series: [{
name: 'AAPL',
data: data,
tooltip: {
valueDecimals: 2
}
}]
});
});
});
}
我尝试添加每个函数,以便它加载到其他 div 中,但没有结果。 希望你能帮助我。
最佳答案
根据 HighChart API和 HighStock API :
renderTo: String|Object
The HTML element where the chart will be rendered.
If it is a string, the element by that id is used.
The HTML element can also be passed by direct reference. Defaults to null.
首先,Highchart 需要传递 div 元素的 "id"
。因此,我怀疑传递类名如何与您提到的第一个示例一起使用。
您能否分享一下您从哪里获得之前的 HighChart
示例?
第二,您是否有一个包含 id="graphcontainer"
并包含 Highstock
的 div
<script src="http://code.highcharts.com/stock/highstock.js"></script>
关于javascript - Highcharts 显示在多个同名的 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14124233/