i'm trying build a dashboard using google chart via REST. The chart displays okay but the issue is that for some of the charts i need to calculate the sum of the values for certain values in a field. Which means i have to do a GroupBy and then perform a Sum.
I have used the REST API build a chart but will be cool if there was some other way maybe SPservices. I'm not sure. but it seems REST doesnt have those features for Grouping and calculating SUM. Was wandering if anyone could help
<!--Load the AJAX API--><script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script><script type="text/javascript" src="https://code.jquery.com/jquery-1.7.1.min.js"></script><script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
google.charts.setOnLoadCallback(drawSecondChart);
function drawChart() {
var SalesData = yfCharts();
var options = {'title':'Time Lost in All Location',
'width':700,
'height':500};
var chartSales = new google.visualization.PieChart(document.getElementById('SalesChart'));
chartSales.draw(SalesData, options);
console.log(chartSales);
}
var yfCharts = function YourFirstCharts () {
var dataCharts = new google.visualization.DataTable();
dataCharts.addColumn('string', 'Regions');
dataCharts.addColumn('number', 'Time Lost');
var requestUri = _spPageContextInfo.webAbsoluteUrl +"/_api/Web/Lists/getByTitle('Time Lost Form')/items?$select=Location,Time_x0020_Lost";
// execute AJAX request
$.ajax({
url: requestUri,
type: "GET",
async:false,
headers: { "ACCEPT": "application/json;odata=verbose" },
success: function (data) {
console.log(data);
var count = 0;
var dataResults = data.d.results;
var Regions;
var timelost;
//console.log(dataResults);
for (i=0; i< data.d.results.length; i++) {
count = count + 1;
Regions = dataResults[i].Regions;
Timelost = dataResults[i].Time_x0020_Lost;
Location = dataResults[i].Location;
dataCharts.addRow(["'"+ Location +"'", Timelost ]);
}
console.log(count);
},
});
return dataCharts;
console.log(dataCharts);
}
}
</script><div id="SalesChart" style="width:700px;height: 500px;float:left ;margin:0 auto;display:block"></div>What i want is to be able to Group by Location and then sum the Time lost column before feeding the data to google charts.
Any ideas to achieve this would be most welcome. if possible with snippets
thanks