【问题标题】:ChartJS - Show percentage on hover (AngularJS)ChartJS - 显示悬停百分比(AngularJS)
【发布时间】:2021-07-27 03:13:46
【问题描述】:

我在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'];

我尝试在他们的文档站点中到处查看,但没有看到任何内容。

对我有什么建议吗?

小提琴

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

【问题讨论】:

    标签: javascript angularjs chart.js


    【解决方案1】:

    您可以在选项的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);
        }
      }
    

    更新的小提琴示例:https://jsfiddle.net/Leelenaleee/9ghu8efs/5/

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 这不是真的,使用 afterLabel 回调您将编辑条目之后的内容,因此您将在工具提示中拥有双重数据
      猜你喜欢
      • 2021-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多