2017-06-15 41 views
1

我正在使用angular-chart.js在我的應用程序中繪製圖表。但是我面臨一個問題。圖表圖例顯示未定義而不是標籤。圖表顯示未定義的角度圖

JS代碼:

 $scope.labels_std = ['Total Students', 'Submitted fee', 'Has to submit']; 
     $scope.options = {legend: {display: true, position: 'bottom'}}; 
     $scope.colors = ['#4D5360', '#949FB1', '#97BBCD']; 
     $scope.data_std = [ 
        $scope.allCount.students, 
        $scope.allCount.fees, 
        $scope.allCount.students - $scope.allCount.fees 
       ]; 

標記爲

<canvas id="bar" class="chart chart-bar" chart-labels="labels_std" chart-data="data_std" chart-colors="colors" chart-options="options"></canvas> 

結果是這樣的:

enter image description here

+0

才能添加工作實例,其中問題。另外在現有代碼 –

+0

中的'$ scope.series'我添加了系列,但沒有工作 –

+0

請參閱http://jtblin.github.io/angular-chart.js/,但添加一些plunker,以便它更容易調試 –

回答

2

它顯示undefined,因爲您尚未爲數據集定義label屬性。

您需要在每個數據集的$scope.datasetOverride模型中設置label屬性。另外,在你的視圖標記中添加指令。

ᴡᴏʀᴋɪɴɢᴇxᴀᴍᴘʟᴇ

var app = angular.module('app', ['chart.js']); 
 

 
app.controller("BarCtrl", function($scope) { 
 
    $scope.labels = ['Total Students', 'Submitted fee', 'Has to submit']; 
 
    $scope.colors = ['#4D5360', '#949FB1', '#97BBCD']; 
 
    $scope.std = 65, $scope.fee = 59, $scope.nfee = 80; 
 
    $scope.data = [ 
 
     [$scope.std, $scope.fee, $scope.nfee], 
 
     [$scope.std, $scope.fee, $scope.nfee], 
 
     [$scope.std, $scope.fee, $scope.nfee] 
 
    ]; 
 
    $scope.options = { 
 
     legend: { 
 
     display: true 
 
     } 
 
    } 
 
    $scope.datasetOverride = [{ 
 
     label: 'My First Dataset' 
 
    }, { 
 
     label: 'My Second Dataset' 
 
    }, { 
 
     label: 'My Third Dataset' 
 
    }]; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script> 
 
<script src="https://cdn.jsdelivr.net/angular.chartjs/latest/angular-chart.min.js"></script> 
 
<div ng-app="app" ng-controller="BarCtrl"> 
 
    <canvas id="bar" class="chart chart-bar" chart-data="data" chart-colors="colors" chart-labels="labels" chart-series="series" chart-options="options" chart-dataset-override="datasetOverride"></canvas> 
 
</div>

+0

但是在餅圖中,它工作正常,即使我沒有添加「datasetOverride」 –

+0

它的原因餅圖不使用'label'屬性來顯示'legend's。它顯示標籤數組中每個*標籤*的圖例。 –

+0

我試過你的方式,但它並沒有解決我的問題..顯示undefined –