简体   繁体   English

如何从JSON文件读取数据并将其传递给Google图表行(javascript)

[英]how to read data from an JSON file and pass it to google chart rows (javascript)

have the following function JSONChart() it reads json data from var "allText" and should be able to parse the data and use it as row data for google charts. 具有以下功能JSONChart(),它从var“ allText”读取json数据,并且应该能够解析该数据并将其用作Google图表的行数据。

Commenting out the adding row part displays the column data correctly with empty graph. 注释掉添加的行部分可以正确显示带有空白图形的列数据。

Need a way to parse the given sample data from a file and display it as row data in the google chart. 需要一种从文件中解析给定样本数据并将其显示为google图表中的行数据的方法。

function JSONChart() {
google.charts.load('current', {'packages':['corechart']});
 var data = new google.visualization.DataTable();
   data.addColumn('string', 'Time stamp');
   data.addColumn('number', 'CPU');
   data.addColumn('number', 'MEMORY');
   data.addColumn({type:'string', role:'annotation'});
   data.addColumn({type:'string', role:'annotationText'});
   var data1  = JSON.parse(allText);
   var dataTableData = google.visualization.arrayToDataTable(data1);
   data.addRows (dataTableData);

var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
// Set chart options
   var options = {'title' : 'CPU & Memory',
      hAxis: {
         title: 'Time'
      },
      vAxis: {
         title: 'Percentage'
      },
      'width':1400,
      'height':600,
      curveType: 'function'
   };

chart.draw(data, options);
}

window.onload = function() {
  google.charts.setOnLoadCallback(JSONChart());
};

Sample JSON passed into variable "allText" 样本JSON传递到变量“ allText”中

{"2017/11/03 01:06:51":{"SCREEN":" ABC ","MEMORY":" 32.0142% ","CPU":" 9.1% "},"2017/11/03 02:22:20":{"SCREEN":" XYZ ","MEMORY":" 31.101% ","CPU":" 10.3% "}

a few things... 一些东西...

1) arrayToDataTable expects a simple array, not a json object 1) arrayToDataTable需要一个简单的数组,而不是json对象
it also returns an entire data table, which has already been created --> data 它还返回一个完整的数据表,该表已经创建-> data
instead, convert each json object to an array, 而是将每个json对象转换为数组,
then use addRows to add the data to the existing data table --> data 然后使用addRows将数据添加到现有数据表-> data

something like... 就像是...

for (var date in data1) {
  if (data1.hasOwnProperty(date)) {
    chartData.push([
      date,
      parseFloat(data1[date].MEMORY.replace('%', '').trim()),
      parseFloat(data1[date].CPU.replace('%', '').trim()),
      data1[date].SCREEN,
      ''  // not sure what value you want to use here
    ]);
  }
}
data.addRows(chartData);

2) google.charts.load -- this statement waits for the window / document to load, before calling the callback 2) google.charts.load此语句在调用回调之前等待窗口/文档加载
no need for --> window.onload = function() {... 不需要-> window.onload = function() {...

google.charts.load actually returns a promise, google.charts.load实际上会返回一个承诺,
so you can do something like... 所以你可以做...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  // draw chart code here...  

3) when passing a callback function to setOnLoadCallback , 3)将回调函数传递给setOnLoadCallback
a reference to the function should be passed --> JSONChart 对函数的引用应传递-> JSONChart
not the result of a function --> JSONChart() (remove parens) 不是函数的结果-> JSONChart() (删除括号)

4) recommend similar setup as following working snippet... 4)建议与以下工作片段类似的设置...

 google.charts.load('current', { packages: ['corechart'] }).then(function () { var data = new google.visualization.DataTable(); data.addColumn('string', 'Time stamp'); data.addColumn('number', 'CPU'); data.addColumn('number', 'MEMORY'); data.addColumn({type:'string', role:'annotation'}); data.addColumn({type:'string', role:'annotationText'}); var chartData = []; var data1 = {"2017/11/03 01:06:51":{"SCREEN":" ABC ","MEMORY":" 32.0142% ","CPU":" 9.1% "},"2017/11/03 02:22:20":{"SCREEN":" XYZ ","MEMORY":" 31.101% ","CPU":" 10.3% "}}; for (var date in data1) { if (data1.hasOwnProperty(date)) { chartData.push([ date, parseFloat(data1[date].MEMORY.replace('%', '').trim()), parseFloat(data1[date].CPU.replace('%', '').trim()), data1[date].SCREEN, '' // not sure what value you want to use here ]); } } data.addRows(chartData); var chart = new google.visualization.LineChart(document.getElementById('chart_div')); var options = {'title' : 'CPU & Memory', hAxis: { title: 'Time' }, vAxis: { title: 'Percentage' }, height: 600, curveType: 'function' }; chart.draw(data, options); }); 
 <script src="https://www.gstatic.com/charts/loader.js"></script> <div id="chart_div"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM