gpt4 book ai didi

javascript - 如何将 Angular 图表与 json 数据一起使用?

转载 作者:行者123 更新时间:2023-11-30 00:16:42 25 4
gpt4 key购买 nike

我正在尝试使用 angular-charts 可视化一些数据但我不知道我做错了什么。我是 angularjs 的初学者。

数据应该这样传递:

  $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]
];

我正在使用 $http 从 API 获取它,所以我有这样的东西:

app.controller("LineCtrl", function($scope, $http) {
$http({
method: 'GET',
url: '/meteo_dane'
}).then(function successCallback(response) {
$scope.dane = response.data;
$scope.labels = [$scope.dane.updated_at];
$scope.series = ['Series A'];
$scope.data = [
[$scope.dane.temp],
];
console.log(this.dane);
}, function errorCallback(response) {
//
});

但它现在可以工作了——图表是空的。我将要使用的数据采用以下格式:

[
{
"_id": "567aa394d452128c6d595f0e",
"windspeedmph": 0,
"winddir": 135,
"humidity": 30.5,
"temp": 24,
"updated_at": "2015-12-23T13:37:24.881Z",
"__v": 0
},
{
"_id": "567aa39ed452128c6d595f0f",
"windspeedmph": 0,
"winddir": 135,
"humidity": 30.4,
"temp": 24.1,
"updated_at": "2015-12-23T13:37:34.836Z",
"__v": 0
},
]

我要及时显示温度。稍后我想展示我拥有的其余数据。

应该怎么写?

最佳答案

我认为你的 $scope.data 赋值格式错误,它应该是多维数组。

您应该创建 $scope.temp 数组,每个 dane 都有 temp

$scope.temp = [] 
angular.forEach($scope.dane, function(value){
$scope.temp.push(value.temp)
});
$scope.data = $scope.temp //removed [] because its already an array.

Example Plunkr

关于javascript - 如何将 Angular 图表与 json 数据一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34443735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com