如何以角度显示ng-repeat

时间:2018-10-22 04:44:06

标签: angularjs json charts

我有来自API的JSON列表数据,

$scope.link = globalVar.folderAPI+"/web/data.php";
$http.get($scope.link).then(function(response){
    $scope.listDataitem = response.data.respone;
})

我想显示图表的数据周期 我已经写过这个波纹管,但是它仍然无法显示,如何使数据显示在图表上,如何将数据解析到图表脚本部分

<script>
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: [<div ng-repeat=" x in listDataitem "> {{ x.period }} </div>],
        datasets: [{
                data: [<div ng-repeat="y in listDataitem"> {{ y.sales}} </div>],
                backgroundColor: [
                    'rgba(255, 99, 132, 0.2)',
                    'rgba(54, 162, 235, 0.2)',
                    'rgba(255, 206, 86, 0.2)',
                    'rgba(75, 192, 192, 0.2)',
                    'rgba(153,102,255,0.2)',
                    'rgba(255, 159, 64, 0.2)'
                ],
                borderColor: [
                    'rgba(255,99,132,1)',
                    'rgba(54, 162, 235, 1)',
                    'rgba(255, 206, 86, 1)',
                    'rgba(75, 192, 192, 1)',
                    'rgba(153, 102, 255, 1)',
                    'rgba(255, 159, 64, 1)'
                ],
                borderWidth: 1
            }]
    },
    options: {
        scales: {
            yAxes: [{
                    ticks: {
                        beginAtZero: true
                    }
                }]
        }
    }
});

1 个答案:

答案 0 :(得分:0)

您可以在每次调用API时尝试重绘图表。

$scope.link = globalVar.folderAPI+"/web/data.php";
$http.get($scope.link).then(function(response) {

    $scope.listDataitem = response.data.respone;

    let myLabels = [];
    let myDataset = [];

    for(let i = 0; i< $scope.listDitaitem); i++) {
        myLabels.push($scope.listDitaitem[i].period);
        myDataset.push($scope.listDitaitem[i].sales);
    }

    var ctx = document.getElementById("myChart");
    var myChart = new Chart(ctx, {
        type: 'bar',
        data: {
            labels: myLabels,
            datasets: [{
                data: myDataset,
                backgroundColor: [
                    'rgba(255, 99, 132, 0.2)',
                    'rgba(54, 162, 235, 0.2)',
                    'rgba(255, 206, 86, 0.2)',
                    'rgba(75, 192, 192, 0.2)',
                    'rgba(153,102,255,0.2)',
                    'rgba(255, 159, 64, 0.2)'
                ],
                borderColor: [
                    'rgba(255,99,132,1)',
                    'rgba(54, 162, 235, 1)',
                    'rgba(255, 206, 86, 1)',
                    'rgba(75, 192, 192, 1)',
                    'rgba(153, 102, 255, 1)',
                    'rgba(255, 159, 64, 1)'
                ],
                borderWidth: 1
            }]
        },
        options: {
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero: true
                    }
                }]
            }
        }
    }); 
})