ChartJS - 悬停时显示百分比 (AngularJS)

ChartJS - Show percentage on hover (AngularJS)

我在angularjs中有简单的chartjs,悬停时会显示(%)。

HTML

<div ng-app="chartDemo" ng-controller="MainController as mainCtrl">
      <canvas id="doughnut" class="chart chart-doughnut"
        chart-data="mainCtrl.data" chart-labels="mainCtrl.labels" chart-options="mainCtrl.options">
      </canvas>
</div>

JS

angular.module('chartDemo', ['chart.js'])
    .config(['ChartJsProvider', function (ChartJsProvider) {
    // Configure all charts
    ChartJsProvider.setOptions({
      //animation: false,
      //responsive: false
    });
  }])
    .controller('MainController', MainController);

function MainController($scope, $timeout) {
  var vm = this;
  
  vm.labels = ["Download Sales", "In-Store Sales", "Mail-Order Sales"];
  vm.data = [300, 500, 100];
  vm.options = {
    legend: {
      display: true,
      position: 'bottom'
    },
    cutoutPercentage: 60,
    tooltipEvents: [],
    tooltipCaretSize: 0,
    showTooltips: true,
    onAnimationComplete: function() {
      self.showTooltip(self.segments, true);
    }
  }
  
}

MainController.$inject = ['$scope', '$timeout'];

我试过在他们的文档站点上到处查看,但看不到任何东西。

我有什么建议可以让它正常工作吗?

Fiddle

https://jsfiddle.net/bheng/3pw5oLyh/

您可以使用 tooltip callback 将数据修改为实际百分比和工具提示值。特别是 label.

您可以在选项的 tooltips 部分使用 label 回调。

  vm.options = {
    legend: {
      display: true,
      position: 'bottom'
    },
    tooltips: {
        callbacks: {
        label: (ttItem,data) => (`${data.labels[ttItem.index]}: ${data.datasets[ttItem.datasetIndex].data[ttItem.index]}%`)
      }
    },
    cutoutPercentage: 60,
    tooltipEvents: [],
    tooltipCaretSize: 0,
    showTooltips: true,
    onAnimationComplete: function() {
      self.showTooltip(self.segments, true);
    }
  }

已更新 fiddle 示例:https://jsfiddle.net/Leelenaleee/9ghu8efs/5/