[英]How to get column chart in angular using highcharts using dynamic data
I am implemented the high charts in angular js ,It is working fine with hard coded data but it is not working when data came from web service dynamically . 我在angular js中实现了高级图表,它可以很好地处理硬编码数据,但是当数据动态地来自Web服务时却无法正常工作。 In my controller : 在我的控制器中:
$scope.months = [];
$scope.retail = [];
$scope.wholesale = [];
$scope.fetchChart = function(){
$scope.chartConfig = {
title: {
text: ""
},
options: {
chart: {
type: 'column'
},
plotOptions: {
series: {
stacking: ''
}
},
legend: {
layout: 'vertical',
align: 'topleft',
verticalAlign: 'top',
borderWidth: 1
}
},
xAxis: {
categories: $scope.months
},
credits: {
enabled: true
},
series: [{
name: 'Retail',
data: $scope.retail
},
{
name: 'Wholesale',
data: $scope.wholesale
}
],
loading: false
}
};
$http.get(http://localhost:8080/abc/pqr/mno/getData).success(function(response) {
$scope.data = angular.fromJson(response);
$scope.comlete = false;
var count=0;
for(var i = 0; i < $scope.data.length; i++){
count++;
$scope.months.push($scope.data[i].month);
$scope.retail.push($scope.data[i].retail);
$scope.wholesale.push($scope.data[i].wholesale);
if(count == $scope.data.length){
$scope.fetchChart();
$scope.comlete = true;
}
}
$scope.toggleHighCharts = function () {
this.chartConfig.useHighStocks = !this.chartConfig.useHighStocks
}
$scope.$watch("comlete",function(){
alert(JSON.stringify($scope.months)+"---"+JSON.stringify($scope.retail)+"=-=--"+JSON.stringify($scope.wholesale));
},true);
$scope.reflow = function () {
$scope.$broadcast('highchartsng.reflow');
};
});
$scope.chartTypes = [
{"id": "line", "title": "Line"},
{"id": "spline", "title": "Smooth line"},
{"id": "area", "title": "Area"},
{"id": "areaspline", "title": "Smooth area"},
{"id": "column", "title": "Column"},
{"id": "bar", "title": "Bar"},
{"id": "pie", "title": "Pie"},
{"id": "scatter", "title": "Scatter"}
];
$scope.dashStyles = [
{"id": "Solid", "title": "Solid"},
{"id": "ShortDash", "title": "ShortDash"},
{"id": "ShortDot", "title": "ShortDot"},
{"id": "ShortDashDot", "title": "ShortDashDot"},
{"id": "ShortDashDotDot", "title": "ShortDashDotDot"},
{"id": "Dot", "title": "Dot"},
{"id": "Dash", "title": "Dash"},
{"id": "LongDash", "title": "LongDash"},
{"id": "DashDot", "title": "DashDot"},
{"id": "LongDashDot", "title": "LongDashDot"},
{"id": "LongDashDotDot", "title": "LongDashDotDot"}
];
$scope.chartSeries = [
{"name": "Retail", "data": $scope.retail, type: "column"},
{"name": "Wholesale", "data": $scope.wholesale, type: "column"}
];
In My Html : 在我的HTML中:
<div>
<highchart id="chart1" config="chartConfig"></highchart>
</div>
In Controller for the statements: 在Controller中的语句:
$scope.data = angular.fromJson(response);
I got the data as 我得到的数据是
[{"wholesale":"1","retail":"0","month":"Jan"},
{"wholesale":"2","retail":"0","month":"May"},
{"wholesale":"0","retail":"1","month":"Jun"},
{"wholesale":"0","retail":"2","month":"Jul"}]
In Controller for the statements: 在Controller中的语句:
$scope.$watch("comlete",function(){
alert(JSON.stringify($scope.months)+"---"+JSON.stringify($scope.retail)+"=-=--"+JSON.stringify($scope.wholesale));
},true);
I got data as: 我得到的数据为:
["Jan","May","Jun","Jul"]---["0","0","1","2"]=-=--["1","2","0","0"]
and in series: 并依次:
series: [{ name: 'Retail',
data: $scope.retail
},
{ name: 'Wholesale',
data: $scope.wholesale
}
],
when i replace data: $scope.retail
with data: [250,500,1500,1800]//$scope.retail
and data: $scope.wholesale
with data: [700,800,200,1300]//$scope.wholesale
it is working .How can i get the chart with dynamic data. 当我替换data: $scope.retail
与data: [250,500,1500,1800]//$scope.retail
和data: $scope.wholesale
与data: [700,800,200,1300]//$scope.wholesale
正常工作时。我如何获得带有动态数据的图表。
Answer to the Problem:- 问题的答案:
I solved the problem by myself. 我自己解决了这个问题。 I am answering the solution for my future reference and in case it helps somebody having the same requirement. 我正在回答该解决方案,以供将来参考,以防它帮助有相同要求的人。
$scope.$watch("comlete",function(){
alert(JSON.stringify($scope.months)+"---"+JSON.stringify($scope.retail)+"=-=--"+JSON.stringify($scope.wholesale));
},true);
In the above statements the $scope.retail
and $scope.wholesale
data as string of arrays , the statement 在以上语句中, $scope.retail
和$scope.wholesale
数据为数组字符串,语句
series: [{
name: 'Retail',
data: $scope.retail
},
{
name: 'Wholesale',
data: $scope.wholesale
}
],
series is not accepting the data as string ,By converting the string of numbers into numbers (converting string to number type) then it is accepting the series . series不接受数据作为字符串,通过将数字字符串转换为数字(将字符串转换为数字类型),然后接受该系列。 the answer statements are : 答案是:
if(angular.isNumber($scope.data[i].retail))
{
$scope.retail.push($scope.data[i].retail);
}else{
$scope.retail.push(+$scope.data[i].retail);
}
if(angular.isNumber($scope.data[i].wholesale))
{
$scope.wholesale.push($scope.data[i].wholesale);
}else{
$scope.wholesale.push(+$scope.data[i].wholesale);
}
instead of 代替
$scope.retail.push($scope.data[i].retail);
$scope.wholesale.push($scope.data[i].wholesale);
These two statement in for
loop. 这两个语句在for
循环中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.