为了显示我的数据库中的一些数据,我想使用这个漂亮的图形库(Angular Chart)。
数据的格式让我有点困惑,我有一些来自我的数据库的不错的 JSON 数据,格式如下:
{ "data": [
{
"meting": "749",
"bericht": "408",
"plant": "01",
"timestamp": "2016-03-18T09:40:58.977051"
},
{
"meting": "775",
"bericht": "177",
"plant": "01",
"timestamp": "2016-03-18T07:35:59.007320"
}
]
};
但是对于这些图表,我需要将它放在这样的数组中:
$scope.labels = ["January", "February", "March", "April", "May", "June", "July"];
$scope.series = ['Series A', 'Series B'];
$scope.data = [
[65, 59, 80, 81, 56, 55, 40],
[28, 48, 40, 19, 86, 27, 90]
];
我相信这是一种糟糕的数据格式,但无论如何我都愿意使用这个库。
现在我如何从我的数据中获取这些疯狂的丑陋数组?
我当前的代码是这样的,但我确信有更好的方法可以做到这一点,但我无法$scope.data
正确,所以现在它是硬编码的。
// data test
$scope.zdata =
{ "data": [
{
"meting": "749",
"bericht": "408",
"plant": "01",
"timestamp": "2016-03-18T09:40:58.977051"
},
{
"meting": "775",
"bericht": "177",
"plant": "01",
"timestamp": "2016-03-18T07:35:59.007320"
}
]
};
$scope.labels = [];
$scope.series = [];
$scope.data = [];
var datalength = $scope.zdata.data.length;
for (var i = 0; i < datalength; i++) {
$scope.labels.push($scope.zdata.data[i].timestamp);
}
for (var i = 0; i < datalength; i++) {
$scope.series.push($scope.zdata.data[i].plant);
}
$scope.data = [
[65, 59, 80, 81, 56, 55, 40]
];