简体   繁体   English

在汇总数据时将json网址传递给Google图表吗? -Javascript

[英]Passing json url to Google Charts while aggregating data? - Javascript

I have a json url that I want to use for my google charts - specifically I'm making a table if "aggregated results," ie volume of a specific month. 我有一个要用于我的Google图表的json网址-特别是如果“汇总结果”(即特定月份的数量),我正在制作一张表格。

I'm grabbing my json using $.getJson, but I'm having trouble figuring out how I can do calculations on the json other than looping through. 我正在使用$ .getJson来获取我的json,但是我很难弄清楚除循环之外如何在json上进行计算。 Can I make a secondary function in another file to declutter if a for loop is all I can do? 如果可以做一个for循环,是否可以在另一个文件中创建一个辅助函数来整理?

Thanks 谢谢

once you have created the DataTable 一旦创建了DataTable
there are Data Manipulation Methods you can use to group and aggregate the data 有一些数据处理方法可用于groupaggregate数据

you can use the standard agg functions... 您可以使用标准的agg函数...

google.visualization.data.avg
google.visualization.data.count
google.visualization.data.max
google.visualization.data.min
google.visualization.data.sum

or write your own, the function will receive an array of values from the column provided 或编写自己的函数,该函数将从提供的column接收值数组

column definition: 列定义:

{'column': 3, 'aggregation': doubleSum, 'type': 'number', 'label': 'double minutes'}

agg function: agg功能:

function doubleSum(values) {
  var groupValue = 0;
  values.forEach(function (v) {
    groupValue += (v * 2);
  });
  return groupValue;
}

see following working snippet... 请参阅以下工作片段...

 google.charts.load('current', { callback: function () { var data = new google.visualization.DataTable({ cols: [ {id: 'dat_ym', label: 'Start Date', type: 'date'}, {id: 'user-id', label: 'User-Id', type: 'string'}, {id: 'customer-id', label: 'Customer-Id', type: 'string'}, {id: 's_minutes', label: 'minutes', type: 'number'} ], rows: [ {c:[{v: new Date('2016, 01, 01')}, {v: '44836'}, {v: '67205'}, {v: 1122} ]}, {c:[{v: new Date('2016, 01, 01')}, {v: '86495'}, {v: '67205'}, {v: 332} ]}, {c:[{v: new Date('2016, 01, 01')}, {v: '44836'}, {v: '228626'}, {v: 90} ]}, {c:[{v: new Date('2016, 01, 01')}, {v: '86495'}, {v: '228626'}, {v: 334} ]}, {c:[{v: new Date('2016, 02, 01')}, {v: '44836'}, {v: '67205'}, {v: 554} ]}, {c:[{v: new Date('2016, 02, 01')}, {v: '86495'}, {v: '67205'}, {v: 819} ]}, {c:[{v: new Date('2016, 02, 01')}, {v: '44836'}, {v: '228626'}, {v: 420} ]}, {c:[{v: new Date('2016, 02, 01')}, {v: '86495'}, {v: '228626'}, {v: 544} ]}, ] }); // group data by date, customer var grouped_data = google.visualization.data.group( data, [0, 2], [ {'column': 3, 'aggregation': google.visualization.data.sum, 'type': 'number'}, {'column': 3, 'aggregation': doubleSum, 'type': 'number', 'label': 'double minutes'} ] ); // use custom aggregation function doubleSum(values) { var groupValue = 0; values.forEach(function (v) { groupValue += (v * 2); }); return groupValue; } var chart = new google.visualization.Table(document.getElementById('chart_div')); chart.draw(grouped_data); }, packages:['table'] }); 
 <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