javascript – 如何在ng-repeat angularjs中显示highcharts
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 如何在ng-repeat angularjs中显示highcharts,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4217字,纯文字阅读大概需要7分钟。
内容图文
我正试图在ng-repeat中显示图表
这是html:
<tr ng-repeat="perspective in perspectives">
<td>
<highcharts-pie class="hc-pie" items="value"></highcharts-pie>
</td>
<td>{{perspective.perfomance}}</td>
<td class="right-align">{{perspective.current}}</td>
<td class="right-align">{{perspective.previous}}</td>
<td class="right-align isPluse-{{perspective.plus}}">{{perspective.variance}}</td>
</tr>
来自控制器的数组:
$scope.perspectives=[
{
perfomance:'a',
current:'1',
previous:'2',
variance:'-1',
plus:false,
graphData:[
{value: 32.4},
{value: 13.2},
{value: 84.5},
{value: 19.7},
{value: 22.6},
{value: 65.5},
{value: 77.4},
{value: 90.4},
{value: 17.6},
{value: 59.1},
{value: 76.8},
{value: 21.1}
]
}
];
这是指令:
.directive('hcPie', function () {
return {
restrict: 'C',
replace: true,
scope: {
items: '='
},
controller: function ($scope, $element, $attrs) {
},
template: '<div id="container" style="margin: 0 auto">not working</div>',
link: function (scope, element, attrs) {
var chart = new Highcharts.Chart({
chart: {
renderTo: element[0],
type: 'column',
backgroundColor: null
},
title: {
text: null
},
subtitle: {
text: null
},
xAxis: {
categories: [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
],
labels: {
enabled: false
},
gridLineWidth: 0,
minorGridLineWidth: 0,
lineColor: 'transparent',
tickLength: 0
},
yAxis: {
gridLineWidth: 0,
minorGridLineWidth: 0,
lineColor: 'transparent',
min: 0,
title: {
text: null
},
labels: {
enabled: false
}
},
tooltip: {
enabled: false
},
plotOptions: {
column: {
pointPadding: 0,
borderWidth: 0,
states: {
hover: {
color: '#FFFFFF'
}
}
}
},
legend: {
enabled: false
},
series: [{
name: 'Value',
color: '#EC5B00',
data: scope.items
}]
});
scope.$watch("items", function (newValue) {
chart.series[0].setData(newValue, true);
console.log(scope.items);
}, true);
}
}
});
我也试过了
<highcharts-pie class="hc-pie" items="perspectives.graphData.value"></highcharts-pie>
控制台显示我需要的值,
但我不能用HTML显示它
我的错误是什么?
解决方法:
您需要在使用之前预处理数据,可以这样做:
$scope.processed = $scope.perspectives[0].graphData.map(function(elem,i){
return [i,elem.value];
});
highcharts想要一个特定的系列格式来查看DOCS
var myApp = angular.module("myApp", []);
myApp.controller("myCtrl", function($scope) {
$scope.perspectives = [{
perfomance: 'a',
current: '1',
previous: '2',
variance: '-1',
plus: false,
graphData: [{
value: 32.4
}, {
value: 13.2
}, {
value: 84.5
}, {
value: 19.7
}, {
value: 22.6
}, {
value: 65.5
}, {
value: 77.4
}, {
value: 90.4
}, {
value: 17.6
}, {
value: 59.1
}, {
value: 76.8
}, {
value: 21.1
}]
}];
$scope.processed = $scope.perspectives[0].graphData.map(function(elem,i){
return [i,elem.value];
});
})
.directive('hcPie', function() {
return {
restrict: 'C',
replace: true,
scope: {
items: '='
},
controller: function($scope, $element, $attrs) {},
template: '<div id="container" style="margin: 0 auto">not working</div>',
link: function(scope, element, attrs) {
console.log(scope, element);
var chart = new Highcharts.Chart({
chart: {
renderTo: element[0],
type: 'column',
backgroundColor: null
},
title: {
text: null
},
subtitle: {
text: null
},
xAxis: {
categories: [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
],
labels: {
enabled: false
},
gridLineWidth: 0,
minorGridLineWidth: 0,
lineColor: 'transparent',
tickLength: 0
},
yAxis: {
gridLineWidth: 0,
minorGridLineWidth: 0,
lineColor: 'transparent',
min: 0,
title: {
text: null
},
labels: {
enabled: false
}
},
tooltip: {
enabled: false
},
plotOptions: {
column: {
pointPadding: 0,
borderWidth: 0,
states: {
hover: {
color: '#FFFFFF'
}
}
}
},
legend: {
enabled: false
},
series: [{
name: 'Value',
color: '#EC5B00',
data: scope.items
}]
});
scope.$watch("items", function(newValue) {
chart.series[0].setData(newValue, true);
console.log(scope.items);
}, true);
}
}
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
<table>
<tbody>
<tr ng-repeat="perspective in perspectives">
<td>
<highcharts-pie class="hc-pie" items="processed"></highcharts-pie>
</td>
<td>{{perspective.perfomance}}</td>
<td class="right-align">{{perspective.current}}</td>
<td class="right-align">{{perspective.previous}}</td>
<td class="right-align isPluse-{{perspective.plus}}">{{perspective.variance}}</td>
</tr>
</tbody>
</table>
{{processed}}
</body>
内容总结
以上是互联网集市为您收集整理的javascript – 如何在ng-repeat angularjs中显示highcharts全部内容,希望文章能够帮你解决javascript – 如何在ng-repeat angularjs中显示highcharts所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。