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

前端 未结 2 1977
眼角桃花
眼角桃花 2021-01-07 00:33

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

相关标签:
2条回答
  • 2021-01-07 01:28

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

    Add the following options to your scope

    $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/


    0 讨论(0)
  • 2021-01-07 01:37

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

    1.Make sure you have showAllTooltips set to true

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

    2.Register your custom plugin on 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;
        }
      }
    });
    
    0 讨论(0)
提交回复
热议问题