Tim
Tim

Reputation: 952

AngularJS $http.get not displaying data

My data displays when I call URL from dataSource: but not when I use $http.get. I am passing this data into a dx-chart using a JsonResult from my controller. Here is code which includes my AngularJS file and MVC View:

AngularJS

var app = angular.module('customCharts', ['dx']);

$http.get("http://localhost:53640/Home/PostChart").success(function (data) {
    $scope.dataSource = data;
    $scope.renderChart();
})

$scope.renderChart = function () {
    $scope.productSettings = {
        dataSource: $scope.dataSource,
        title: 'Displays Product Costs for items in our Database',
        series: {
            argumentField: "Name",
            valueField: "Cost",
            type: "bar",
            color: '#008B8B'
        },
        commonAxisSettings: {
            visible: true,
            color: 'black',
            width: 2
        },
        argumentAxis: {
            title: 'Items in Product Store Database'
        },
        valueAxis: {
            title: 'Dollor Amount',
            valueFormat: 'currency'
        }
    }
};

HTML

<div ng-app="customCharts">
    <div ng-controller="ChartController">
        <div dx-chart="productSettings"></div>
    </div>
</div>

Upvotes: 0

Views: 1592

Answers (3)

Tim
Tim

Reputation: 952

Here is how I solved this issue.

var app = angular.module('customCharts', ['dx']);

app.controller("ChartController", function ($scope, $http, $q) {
    $scope.productSettings = {
        dataSource: new DevExpress.data.DataSource({
            load: function () {
                var def = $.Deferred();
                $http({
                    method: 'GET',
                    url: 'http://localhost:80/Home/PostChart'
                }).success(function (data) {
                    def.resolve(data);
                });
                return def.promise();
            }
        }),
        series: {
            title: 'Displays Product Costs for items in our Database',
            argumentType: String,
            argumentField: "Name",
            valueField: "Cost",
            type: "bar",
            color: '#008B8B'
        },
        commonAxisSettings: {
            visible: true,
            color: 'black',
            width: 2
        },
        argumentAxis: {
            title: 'Items in Product Store Database'
        },
        valueAxis: {
            title: 'Dollor Amount',
            valueFormat: 'currency'
        }
    }
})

Upvotes: 2

Tiago Martins
Tiago Martins

Reputation: 727

You need to use two-way binding in order for the chart to update properly. Add this in your dx-chart attribute:

    <div dx-chart="{
       ...
       bindingOptions: {
         dataSource: 'dataSource'
       }
    }"></div>

where dataSource is an attribute of your scope ($scope.dataSource) that you will update after you perform the $http.get successfully.

Check the documentation here

Upvotes: 1

Pankaj Parkar
Pankaj Parkar

Reputation: 136134

$http.get("http://localhost:53640/Home/PostChart") will return a promise object, that promise will get resolved when your server controller action returns a data. You should get data first after resolving promise and then render your chart by passing a data.

Code

var app = angular.module('customCharts', ['dx']);

app.controller('ChartController', ['$scope', '$http', function($scope, $http) {
    $http.get("http://localhost:53640/Home/PostChart").success(function(data) {
        $scope.dataSource = data;
        $scope.renderChart();
    })

    $scope.renderChart = function() {
        $scope.productSettings = {
            dataSource: $scope.dataSource,
            title: 'Displays Product Costs for items in our Database',
            series: {
                argumentField: "Name",
                valueField: "Cost",
                type: "bar",
                color: '#008B8B'
            },
            commonAxisSettings: {
                visible: true,
                color: 'black',
                width: 2
            },
            argumentAxis: {
                title: 'Items in Product Store Database'
            },
            valueAxis: {
                title: 'Dollor Amount',
                valueFormat: 'currency'
            }
        }
    };
}]);

Upvotes: 1

Related Questions