Javascript 在Highcharts.js中将数组用作数据值
我正在使用highcharts.js库为我的网站生成损益表 我有一个数组,其值是从服务器的ajax响应中获得的 这就是代码:Javascript 在Highcharts.js中将数组用作数据值,javascript,jquery,html,ajax,highcharts,Javascript,Jquery,Html,Ajax,Highcharts,我正在使用highcharts.js库为我的网站生成损益表 我有一个数组,其值是从服务器的ajax响应中获得的 这就是代码: 我认为数组利润[]未被识别,或者这不是一种有效的方法?谢谢 因为ajax调用是异步的,所以显示图表时利润数组仍然为空,定义为[]。您必须将图表创建代码移动到ajax成功函数,如: $(document).ready(function(e) { $.ajax({ url : "/php/get-inflow.php", dataType:
我认为数组利润[]未被识别,或者这不是一种有效的方法?谢谢 因为ajax调用是异步的,所以显示图表时利润数组仍然为空,定义为[]。您必须将图表创建代码移动到ajax成功函数,如:
$(document).ready(function(e) {
$.ajax({
url : "/php/get-inflow.php",
dataType: "json",
type: "POST",
success: function(data){
for(var i =0; i<data.length; i++){
if(data[i] == null){
profit[i] = 0;
}else{
profit[i] = data[i];
}
}
$('#profitloss').highcharts({
//some other highcharts code
series: [{
name: 'Inflow',
data: profit
}, {
name: 'Outflow',
data: [2, 2, 3, 2, 1,10,10,10,10,10,10,10]
}]
});
}
});
});
并在成功函数中的for循环之后调用该函数:
success: function(data){
for(var i =0; i<data.length; i++){
if(data[i] == null){
profit[i] = 0;
}else{
profit[i] = data[i];
}
}
drawChart();
}
function drawChart() {
$('#profitloss').highcharts({
series: [{
name: 'Inflow',
data: profit
}, {
name: 'Outflow',
data: [2, 2, 3, 2, 1,10,10,10,10,10,10,10]
}]
});
}
success: function(data){
for(var i =0; i<data.length; i++){
if(data[i] == null){
profit[i] = 0;
}else{
profit[i] = data[i];
}
}
drawChart();
}