javascript Angular-Charts - 饼图,显示每个数据切片内的标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31719156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:15:32  来源:igfitidea点击:

Angular-Charts - Pie Chart,show labels inside each slice of data

javascriptangularjschart.jsangular-chart

提问by Panchaxari Hiremath

I am learning stage of angular-chart-js. I am trying to use angular-chart.js to plot a pie chart. But I am unable to find a way for showing labels (not tooltips) on the pie chart, which describe each slice of data.

我正在学习 angular-chart-js 的阶段。我正在尝试使用 angular-chart.js 来绘制饼图。但是我无法找到一种在饼图上显示标签(不是工具提示)的方法,这些标签描述了每个数据切片。

Here is how I did it:

这是我如何做到的:

angular.module('App').controller('Controller', ['$scope', '$http', '$location', '$window', '$routeParams',
    function($scope, $http, $location, $window) {
        var diskDataJson = {
            "data": [80, 12],
            "labels": [Used space, Free Space],
            "colours": ['#9AB6F0', '#C2D3F6']
        };
    
        $scope.pieDiskData = json;
    }
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<table border="0" width="100%">
  <tr>
    <td style="border-left: 1px solid #0099CC" width="25%">
      <center><span><label ng-bind-html="'load.static.dashboard.system.DUSAGE' | translate"/></span>
      </center>
      <canvas id="pie33" class="chart chart-pie chart-xs ng-isolate-scope" height="120" width="240" data="pieDiskData.data" labels="pieDiskData.labels" colours="pieDiskData.colours" legend="true"></canvas>
    </td>
  </tr>
</table>

回答by potatopeelings

Adapted from https://stackoverflow.com/a/25913101/360067for angular-chart

改编自https://stackoverflow.com/a/25913101/360067的角度图表

Add the following optionsto your scope

将以下内容添加options到您的范围

$scope.options = {
    tooltipEvents: [],
    showTooltips: true,
    tooltipCaretSize: 0,
    onAnimationComplete: function () {
        this.showTooltip(this.segments, true);
    },
};

And use that in your directive

并在您的指令中使用它

<canvas id="pie33" options="options"...


Fiddle (with relevant sections from your code) - http://jsfiddle.net/zuhp8k5f/154/

Fiddle(带有代码中的相关部分)- http://jsfiddle.net/zuhp8k5f/154/



enter image description here

在此处输入图片说明

回答by Tatenda Zifudzi

You can also use a custom plugin to achieve the same behaviour.

您还可以使用自定义插件来实现相同的行为。

1.Make sure you have showAllTooltips set to true

1.确保您已将 showAllTooltips 设置为 true

  options: any = {showAllTooltips: true,legend: {position: 'left'}};

2.Register your custom plugin on ngOnInit

2.在ngOnInit注册你的自定义插件

    Chart.pluginService.register({
  beforeRender: function (chart) {
    if (chart.config.options.showAllTooltips) {
      // create an array of tooltips
      // we can't use the chart tooltip because there is only one tooltip per chart
      chart.pluginTooltips = [];
      chart.config.data.datasets.forEach(function (dataset, i) {
        chart.getDatasetMeta(i).data.forEach(function (sector, j) {
          chart.pluginTooltips.push(new Chart.Tooltip({
            _chart: chart.chart,
            _chartInstance: chart,
            _data: chart.data,
            _options: chart.options.tooltips,
            _active: [sector]
          }, chart));
        });
      });

      // turn off normal tooltips
      chart.options.tooltips.enabled = false;
    }
  },
  afterDraw: function (chart, easing) {
    if (chart.config.options.showAllTooltips) {
      // we don't want the permanent tooltips to animate, so don't do anything till the animation runs atleast once
      if (!chart.allTooltipsOnce) {
        if (easing !== 1) {
          return;
        }
        chart.allTooltipsOnce = true;
      }

      // turn on tooltips
      chart.options.tooltips.enabled = true;
      Chart.helpers.each(chart.pluginTooltips, function (tooltip) {
        tooltip.initialize();
        tooltip.update();
        // we don't actually need this since we are not animating tooltips
        tooltip.pivot();
        tooltip.transition(easing).draw();
      });
      chart.options.tooltips.enabled = false;
    }
  }
});