我正在使用 angular-chart.js 在我的应用程序中绘制图表。但我面临一个问题。图表图例显示的是 undefined 而不是标签。
我的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>
但是结果是这样的:
最佳答案
它显示 undefined
因为您还没有为数据集定义 label
属性。
您需要在 $scope.datasetOverride
模型中为每个数据集设置 label
属性。此外,在您的 View 标记中为其添加指令。
ᴡᴏʀᴋɪɴɢ ᴇ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>
关于javascript - 在 Angular 图中显示未定义的图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44564173/