簡體   English   中英

如何將角度圖表與 json 數據一起使用?

[英]How to use angular-charts with json data?

我試圖用角度圖表可視化一些數據,但我不知道我做錯了什么。 我是 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.

示例 Plunkr

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM