当我从dataSource调用URL时显示我的数据:但是当我使用$ http.get时则不显示。我使用来自我的控制器的JsonResult将这些数据传递到dx图表。这是包含我的AngularJS文件和MVC视图的代码:
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>
答案 0 :(得分:2)
以下是我解决这个问题的方法。
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'
}
}
})
答案 1 :(得分:1)
$http.get("http://localhost:53640/Home/PostChart")
将返回一个promise对象,当服务器控制器操作返回数据时,该promise将得到解决。您应该在解析承诺后首先获取数据,然后通过传递数据来渲染图表。
<强>代码强>
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'
}
}
};
}]);
答案 2 :(得分:1)
您需要使用双向绑定才能使图表正确更新。在dx-chart属性中添加:
<div dx-chart="{
...
bindingOptions: {
dataSource: 'dataSource'
}
}"></div>
其中dataSource是您的范围($ scope.dataSource)的属性,您将在成功执行$ http.get后更新。
查看文档here