【发布时间】:2015-07-14 07:25:59
【问题描述】:
尝试从 Web 服务中获取一些数据并将其显示在图表中。我认为图表 js 将是这样做的好方法。 (实际上使用 tc-angular-chartjs)。我用来测试的$http.get( ) 调用是:
$http.get('http://myjson.com/1chr1').success(function(data2) {
data2.forEach(function(r) {
$scope.labels.push(r.name);
$scope.scores.push(r.score);
});
});
这是整个 js 文件,仅用于圆环图:
'use strict';
angular
.module( 'app.doughnut', [] )
.controller( 'DoughnutCtrl', function ( $scope ) {
$scope.labels = [];
$scope.scores = [];
$scope.data = [
{
value: 700,
color:'#F7464A',
highlight: '#FF5A5E',
label: 'Red'
},
{
value: 50,
color: '#46BFBD',
highlight: '#5AD3D1',
label: 'Green'
},
{
value: 100,
color: '#FDB45C',
highlight: '#FFC870',
label: 'Yellow'
}
];
$scope.options = {
// Sets the chart to be responsive
responsive: true,
//Boolean - Whether we should show a stroke on each segment
segmentShowStroke : true,
//String - The colour of each segment stroke
segmentStrokeColor : '#fff',
//Number - The width of each segment stroke
segmentStrokeWidth : 2,
//Number - The percentage of the chart that we cut out of the middle
percentageInnerCutout : 50, // This is 0 for Pie charts
//Number - Amount of animation steps
animationSteps : 100,
//String - Animation easing effect
animationEasing : 'easeOutBounce',
//Boolean - Whether we animate the rotation of the Doughnut
animateRotate : true,
//Boolean - Whether we animate scaling the Doughnut from the centre
animateScale : false,
//String - A legend template
legendTemplate : '<ul class="tc-chart-js-legend"><% for (var i=0; i<segments.length; i++){%><li><span style="background-color:<%=segments[i].fillColor%>"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>'
};
});
我遇到的问题是,这些示例中的大多数使用相同的格式为图表提供数据(具有相同标签的静态数据,如值/标签/颜色/突出显示)。
根据我的需要,颜色或突出显示并不重要,但我需要从 wesbervice 提取的数据,其中图表所需的值称为 name,图表的标签称为 score。
所以我想我可以调用 $http.get( ) 并将标签和分数放入 2 个不同的数组中,然后 js 中的数据部分看起来像:
$scope.data = {
labels : ["option 1","option 2","option 3"],
values : [ 10, 20, 30 ],
datasets: [ value : values, color : #F7484A, highlight : #FF5A5E, label : labels]
};
我看到为 Chart.js 条形图做了类似的事情,但不是为圆环图做的,我似乎无法让它工作。也许不可能?
还有其他选择吗?我的意思是,我不能是唯一需要将动态数据显示到漂亮的响应式图表中的人,但所有示例都使用静态数据。
编辑答案我接受了 dubhov 的建议。还发现我的网络服务链接搞砸了,所以我没有得到任何数据:p。这是新的js供将来参考:
'use strict';
angular
.module('app.doughnut', [])
.controller('DoughnutCtrl', function($scope, $http) {
$http.get('https://api.myjson.com/bins/1chr1').success(function(data2) {
$scope.data = [];
data2.forEach(function(r) {
$scope.data.push({
'value': r.score,
'color': '#F7464A',
'highlight': '#FF5A5E',
'label': r.name
});
});
});
$scope.options = {
// Sets the chart to be responsive
responsive: true,
//Boolean - Whether we should show a stroke on each segment
segmentShowStroke: true,
//String - The colour of each segment stroke
segmentStrokeColor: '#fff',
//Number - The width of each segment stroke
segmentStrokeWidth: 2,
//Number - The percentage of the chart that we cut out of the middle
percentageInnerCutout: 50, // This is 0 for Pie charts
//Number - Amount of animation steps
animationSteps: 100,
//String - Animation easing effect
animationEasing: 'easeOutBounce',
//Boolean - Whether we animate the rotation of the Doughnut
animateRotate: true,
//Boolean - Whether we animate scaling the Doughnut from the centre
animateScale: false,
//String - A legend template
legendTemplate: '<ul class="tc-chart-js-legend"><% for (var i=0; i<segments.length; i++){%><li><span style="background-color:<%=segments[i].fillColor%>"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>'
};
});
【问题讨论】:
-
Chart.js 希望数据的格式与静态示例一样。你不能用你需要的数据向数据数组中添加一个对象吗?像这样:
$scope.data.push({'value':r.score, 'label':r.name, 'color':'#RANDOMCOLOR', 'highlight':'#SLIGHTLYSHADEDRANDOMCOLOR'}) -
这里还有一些东西可以让您开始使用随机颜色位:stackoverflow.com/a/25709983/769971 我想当您将颜色属性排除在外时,API 不喜欢它,但我自己还没有尝试过。那,或者如果您知道可以返回的数据集是有限的,您可以从静态颜色列表中进行选择。
-
嘿,我喜欢这个答案。我真的没有这么想。但我也没有运气,因为我的网络服务链接搞砸了:p 谢谢。如果你想让这些 cmets 成为一个问题,我想投票!
-
谢谢!抱歉花了这么长时间;在家生病忘记检查了...
标签: javascript jquery json angularjs charts