1

I am using https://github.com/bouil/angular-google-chart for Data Visualization.

I need to get data from JSON file.

Below is the snippet which was responsible for data display on html page:

$scope.chartObject.data = {"cols": [
        {id: "t", label: "Topping", type: "string"},
        {id: "s", label: "Slices", type: "number"}
    ], "rows": [
        {c: [
            {v: "Mushrooms"},
            {v: 3},
        ]},
        {c: $scope.onions},
        {c: [
            {v: "Olives"},
            {v: 31}
        ]},
        {c: [
            {v: "Zucchini"},
            {v: 1},
        ]},
        {c: [
            {v: "Pepperoni"},
            {v: 2},
        ]}
    ]};

I changed it to:

    $scope.chartObject.data = function(){
    $http.get('data.json').always(function(response) {
    return response.data;
    });
    }

And my console throws following error :

Object {id: "google-visualization-errors-0", message: "Table has no columns.", detailedMessage: "", options: Object}

My data.json file looks like:

{
  "cols": [
        {"id":"","label":"Topping","pattern":"","type":"string"},
        {"id":"","label":"Slices","pattern":"","type":"number"}
      ],
  "rows": [
        {"c":[{"v":"Mushrooms","f":null},{"v":3,"f":null}]},
        {"c":[{"v":"Onions","f":null},{"v":1,"f":null}]},
        {"c":[{"v":"Olives","f":null},{"v":1,"f":null}]},
        {"c":[{"v":"Zucchini","f":null},{"v":1,"f":null}]},
        {"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null}]}
      ]
}

NOTE: FilePath is not a problem here.

4 Answers 4

2

There is something wrong about the way that you set the $http request response to your $scope object. Change it to:

$scope.chartObject.data = {} 

$http.get('data.json').then(function(response) {
    $scope.chartObject.data = response.data;
});

This is because you cannot directly set a Promise (the object that $http.get returns) to a object in your scope, because it is async.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked fine, I needed to pass " $http" as parameter in my controller. Thanks
1

Change you code to this

$scope.chartObject = {};

     $scope.chartObject.data = {} 

$http.get('data.json').then(function(response) {
    $scope.chartObject.data = response.data;
});

1 Comment

pass $http in your controller parameter
1

Use this. Also you will need add $http dependency on the controller.

$http.get(url).success(function(data){
return data;
}).error(function(data){
})

Comments

1

Better you use factory Then consume it from anywhere.

Service

app.factory('dataService', function($http){
    var getData = function(){
         return $http.get('data.json');
    }
    return {
        getData: getData
    }               
});

Before using it don't forget to add dependency of service inside your controller.

Controller Code

$scope.chartObject = {}; //do this if chartObject is not initialized
dataService.getData().then(function(response) {
    $scope.chartObject.data = response.data;
});

Hope better code will work for you.Thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.