使用角度js图表在每个扇区上显示极坐标图表上的标签

时间:2018-02-22 08:56:42

标签: javascript angularjs chart.js

我正在使用angular-chart.js library中的极坐标图表。但我的极地图表需要如下所示:

enter image description here

我如何才能达到这个要求?有人以前做过吗?

目前我只是使用来自源代码的代码:

angular.module("app", ["chart.js"]).controller("PolarAreaCtrl", function ($scope) {
  $scope.labels = ["Download Sales", "In-Store Sales", "Mail-Order Sales", "Tele Sales", "Corporate Sales"];
  $scope.data = [300, 500, 100, 40, 120];
});

和我的观点:

<canvas id="polar-area" class="chart chart-polar-area"
  chart-data="data" chart-labels="labels" chart-options="options">
</canvas> 

2 个答案:

答案 0 :(得分:3)

使用此扩展程序还有另一种选择:

  

Chart.PieceLabel.js

这是一个小提琴:https://jsfiddle.net/beaver71/39293jes/

或SO中的摘录:

angular.module("app", ["chart.js"]).controller("ChartCtrl", function($scope) {

    $scope.labels = ["January", "February", "March", "April", "May", "June", "July"];
    
    $scope.data = [65, 59, 80, 81, 56, 55, 40];
    var sum = $scope.data.reduce(function add(a, b) {
        return a + b;
    }, 0);
    
    $scope.options = {
      pieceLabel: {
        render: function (args) {
          return args.label + " " + Math.round(args.value*100/sum,2)+"%";
        },
        fontColor: '#000',
        position: 'outside',
        segment: true
      }
    };
  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.js"></script>
<script src="https://cdn.jsdelivr.net/angular.chartjs/latest/angular-chart.js"></script>
<script src="https://rawgit.com/beaver71/Chart.PieceLabel.js/master/build/Chart.PieceLabel.min.js"></script>

<div ng-app="app" ng-controller="ChartCtrl">
  <canvas id="pie" class="chart chart-polar-area"
         chart-data="data" chart-labels="labels" chart-options="options">
  </canvas> 
</div>

答案 1 :(得分:2)

请检查我的JSfiddle Polar Chart以获取答案,

我使用了名为

的额外扩展程序
  

chartjs-plugin-datalabels

让我知道是否需要更多帮助