Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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 使用海图';getJSON调用中的addChart函数(在多个本地文件上循环)_Javascript_Json_Asynchronous_Highcharts_Getjson - Fatal编程技术网

Javascript 使用海图';getJSON调用中的addChart函数(在多个本地文件上循环)

Javascript 使用海图';getJSON调用中的addChart函数(在多个本地文件上循环),javascript,json,asynchronous,highcharts,getjson,Javascript,Json,Asynchronous,Highcharts,Getjson,我真的被困在这里,试图了解highcharts与getjson和javascript循环的结合(这是前一个问题的后续内容-) 本质上,我试图创建一个包含多个类别和系列的highcharts条形图。我有许多本地json文件,每个文件都包含不同信息的计数。我想循环遍历每个类别文件,读取计数并将结果添加到特定系列中(然后绘制)。我在循环文件的循环中使用了$.getJSON(我想现在通过将其包装到函数中(请参见下面的代码),可以实现这一点)。但是,我也尝试使用highchart的“chart.addSe

我真的被困在这里,试图了解highcharts与getjson和javascript循环的结合(这是前一个问题的后续内容-)

本质上,我试图创建一个包含多个类别和系列的highcharts条形图。我有许多本地json文件,每个文件都包含不同信息的计数。我想循环遍历每个类别文件,读取计数并将结果添加到特定系列中(然后绘制)。我在循环文件的循环中使用了$.getJSON(我想现在通过将其包装到函数中(请参见下面的代码),可以实现这一点)。但是,我也尝试使用highchart的“chart.addSeries”将每个新系列添加到图表中(即代码中的serieCount)。我目前的编码方式意味着我需要从getJSON调用返回SerieCount——我知道这是不正确的,应该在getJSON调用内部进行进一步处理。这正是我遇到的问题所在——我不知道如何在getJSON调用中添加新的系列,作为多个系列的不断增加的一部分

我希望上述内容有意义,如果没有,代码本身可能会有所帮助

(只需补充一点,大部分代码似乎都能按预期工作——这是我无法理解的最后一部分)

谢谢你的建议

$(document).ready(function() {

  var options = {
  // standard highcharts options set here....
         series: []
  };

  //the series to loop through              
  var fileTypes = new Array('SeriesTypeA', 
                    'SeriesTypeB');

  //the categories to loop through                                      
  var mfiles = new Array('/assets/Cat1.json',
                 '/assets/Cat2.json',
                 '/assets/Cat3.json',
                 '/assets/Cat4.json',
                 '/assets/Cat5.json',
                 '/assets/Cat6.json',
                 '/assets/Cat7.json');

  //plot the initial empty chart              
  var chart = new Highcharts.Chart(options);


  //Loop over the different files and do a count for each
  for (var i=0; i<fileTypes.length; i++) {  
      var seriescount = [];
      for (var j=0; j < mfiles.length; j++) {

          (function(i, j, seriescount){   //force the local i and j
              $.getJSON(mfiles[j], function(data) {
                  // get the count of files
                  var count = 0;
                  var index = 0;
                  var entry;
                  for (index = 0; index < data.length; ++index) {
                      entry = data[index];
                      if (entry.file_processing_status == fileTypes[i]) {
                          count ++;
                      }
                  };
                  seriescount.push(count);
                  return seriescount; //This is not correct
              }); 

          })(i, j, seriescount);
      }

      //add the results to make a new series               
      chart.addSeries({                        
          name: fileTypes[i],
          data: seriescount   //as expected I don't get seriescount at this stage
      }, false);            
  };

  //and now redraw the final chart          
  chart.redraw();

});
$(文档).ready(函数(){
变量选项={
//此处设置的标准highcharts选项。。。。
系列:[]
};
//要循环的序列
var fileTypes=新数组('SeriesTypeA',
“序列类型B”);
//要循环的类别
var mfiles=new数组('/assets/Cat1.json',
“/assets/Cat2.json”,
“/assets/Cat3.json”,
“/assets/Cat4.json”,
“/assets/Cat5.json”,
“/assets/Cat6.json”,
“/assets/Cat7.json”);
//绘制初始空图表
var图表=新的Highcharts.图表(选项);
//循环不同的文件,并对每个文件进行计数

对于(var i=0;i什么是令人不安的,这是这一行:
$.getJSON(mfiles[j],function(data){
)-这意味着您正在为每个系列(
for
with
i
变量)加载相同的文件。对我来说,这很奇怪

现在,关于解决方案,每个系列都有固定数量的类别,对吗?它位于
mfiles.length
变量中。您可以增加一些计数器,并将其添加到getJSON回调中:


谢谢Pawel,这正是我所需要的。随着计数器的添加,它现在正以我想要的方式工作。我将根据您的第一条评论清理代码。
if(categoryCounter == mfiles.length){ 
  chart.addSeries(series);
}