javascript - How do I populate a Google Chart (API) with data from HTTP calls? -
i have chart i'm trying populate pulls data our abas erp system via http calls , populates said chart data. idea show monthly revenue each month in previous 3 years (for instance: jan 2014, jan 2015, jan 2016, feb 2014-2016, etc, etc)
although @ (it's been couple weeks since worked on project) can have array of objects?
function loadarray() { var = 0; var beginning2014months = ["20140101", "20140201", "20140301", "20140401", "20140501", "20140601", "20140701", "20140801", "20140901", "20141001", "20141101", "20141201"]; var closing2014months = ["20140131", "20140228", "20140331", "20140430", "20140531", "20140630", "20140731", "20140831", "20140930", "20141031", "20141130", "20141231"]; var beginning2015months = ["20150101", "20150201", "20150301", "20150401", "20150501", "20150601", "20150701", "20150801", "20150901", "20151001", "20151101", "20151201"]; var closing2015months = ["20150131", "20150228", "20150331", "20150430", "20150531", "20150630", "20150731", "20150831", "20150930", "20151031", "20151130", "20151231"]; var beginning2016months = ["20160101", "20160201", "20160301", "20160401", "20160501", "20160601", "20160701", "20160801", "20160901", "20161001", "20161101", "20161201"]; var closing2016months = ["20160131", "20160228", "20160331", "20160430", "20160531", "20160630", "20160731", "20160831", "20160930", "20161031", "20161130", "20161231"]; (i = 0; < 12; i++) { runowsls("invoice", beginning2014months[i], closing2014months[i], "no", function (callbackresp) { $scope.invoice2014header[i] = callbackresp; }); runowsls("invoice", beginning2015months[i], closing2015months[i], "no", function (callbackresp) { $scope.invoice2015header[i] = callbackresp; }); runowsls("invoice", beginning2016months[i], closing2016months[i], "no", function (callbackresp) { $scope.invoice2016header[i] = callbackresp; }); }; }; google.charts.load('current', { packages: ['corechart', 'bar'] }); google.charts.setonloadcallback(drawmaterial); function drawmaterial() { var data = new google.visualization.datatable(); data.addcolumn('string', 'month'); data.addcolumn('number', 'revenue 2014'); data.addcolumn('number', 'revenue 2015'); data.addcolumn('number', 'revenue 2016'); data.addrows([ [{ v: [8, 0, 0], f: 'january' }, $scope.invoice2014header[0].ynofreight, $scope.invoice2015header[0].ynofreight, $scope.invoice2016header[0].ynofreight], [{ v: [9, 0, 0], f: 'febuary' }, $scope.invoice2014header[1].ynofreight, $scope.invoice2015header[1].ynofreight, $scope.invoice2016header[1].ynofreight], [{ v: [10, 0, 0], f: 'march' }, 3, 1, 1], [{ v: [11, 0, 0], f: 'april' }, 4, 2.25, 1], [{ v: [12, 0, 0], f: 'may' }, 5, 2.25, 1], [{ v: [13, 0, 0], f: 'june' }, 6, 3, 1], [{ v: [14, 0, 0], f: 'july' }, 7, 4, 1], [{ v: [15, 0, 0], f: 'august' }, 8, 5.25, 1], [{ v: [16, 0, 0], f: 'september' }, 9, 7.5, 1], [{ v: [17, 0, 0], f: 'october' }, 10, 10, 1], [{ v: [18, 0, 0], f: 'november' }, 11, 11, 1], [{ v: [19, 0, 0], f: 'december' }, 12, 12, 1], ]); var options = { title: 'monthly revenue', haxis: { title: 'month', //format: 'h:mm a', viewwindow: { min: [0, 30, 0], max: [600, 30, 0] } }, vaxis: { title: 'revenue per year' } }; var material = new google.charts.bar(document.getelementbyid('barchart_div')); material.draw(data, options); } $scope.runall = function () { $scope.$watch("unitytoken", function () { if (!$scope.unitytoken) { console.log("auto-login"); login(); } else { loadarray(); } }) };
you loading chart before retrieve data. should load chart after retrieve need populate chart.
one way use javascript promises.
var promisearray = []; (i = 0; < 12; i++) { promisearray.push( new promise(function(resolve,reject){ runowsls("invoice", beginning2014months[i], closing2014months[i], "no", function (callbackresp) { $scope.invoice2014header[i] = callbackresp; resolve(); }); }) ); promisearray.push( new promise(function(resolve,reject){ runowsls("invoice", beginning2015months[i], closing2015months[i], "no", function (callbackresp) { $scope.invoice2015header[i] = callbackresp; resolve(); }); }) ); promisearray.push( new promise(function(resolve,reject){ runowsls("invoice", beginning2016months[i], closing2016months[i], "no", function (callbackresp) { $scope.invoice2016header[i] = callbackresp; resolve(); }); }) ); } // end of loop - there 12*3 = 36 ajax calls. promise.all(promisearray).then(function(){ google.charts.setonloadcallback(drawmaterial); });
Comments
Post a Comment