Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Highcharts.js中将数组用作数据值_Javascript_Jquery_Html_Ajax_Highcharts - Fatal编程技术网

Javascript 在Highcharts.js中将数组用作数据值

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:

我正在使用highcharts.js库为我的网站生成损益表 我有一个数组,其值是从服务器的ajax响应中获得的

这就是代码:


我认为数组利润[]未被识别,或者这不是一种有效的方法?谢谢

因为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();
    }