I am working to read a CSV File in AngularJS, then create a chart using the data from the CSV.
CSV file
Date,Energy
2001,0.851
2002,0.841
2003,1.000
2004,0.984
2005,1.006
2006,2.769
2007,2.791
I use this code to transform the CSV file into data objects
var Mymodule = angular.module('Mymodule', []);
Mymodule.factory('Items', ['$http', function($http){
var Url = "data/ex.csv";
var Items = $http.get(Url).then(function(response){
return csvParser(response.data);
});
return Items;
}]);
Here is chart I use (on jsfiddle)
Now I have problems getting the data from the function that transforms the CSV to the function that makes charts. Inspired this thread thread, I tried using injectors but it didn't work out.
Here is what I tried.
var $injector = angular.injector(["ng", "MyModule"]);
$injector.invoke(['Items', function(Items){
/* do stuff */
console.log(Items); // e.g. print the Items
}]);
I also tried using d3.csv with a global variable:
var rows;
d3.csv("path/to/file.csv", function(rows) {
doSomethingWithRows(rows);
});
function doSomethingWithRows(rows) {
// USE AngularJS Chart
}
My question: how can I load a CSV file and get the data to angularjs-chart to build my chart?