我想在饼图中显示标签的值。 (vue-chartjs / pieceLabel)

I want to show the value of label inside the pie graph. (vue-chartjs / pieceLabel)

我是学习vue的学生。 我用Vue-chartjs画了一个图,想把数值显示在饼图上。 但是我不知道怎么办。

请帮帮我...

现状(图片):enter image description here

我的愿望(图片):enter image description here

Vue.component('pie-chart', {
 extends : VueChartJs.Pie,
 props: ['data', 'options'],
 mounted(){
   this.renderPieChart();
 },
 computed: {
   attendanceData : function(){
     return this.data
   }
 },
 methods : {
   renderPieChart : function(){

     this.renderChart(
       {
         labels: ['a','b','c','d'],
         datasets: [{
             backgroundColor: ['#10a236', '#f9cd41', '#fe7272', '#5c7add'],
             data: [10,20,30,40]
           }]
       },
       {
         responsive: true,
         maintainAspectRatio: false,
         pieceLabel: {
           render: 'value',
           precision: 1,
         }
       }
     )

   }
 },
 watch : {
      attendanceData : function(){
        this.$data._chart.destroy();
        this.renderPieChart();
      }
    }
  });

作为,使用插件是一种解决方案。

然后正如Vue chart.js guide所说,

在 mounted() 中,使用 this.addPlugin 添加您的插件,如下所示:

Vue.config.productionTip = false
//below plugin is copied from 
let pluginConfig = {
    id: 'my-plugin',
  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;
    }
  }
}

Vue.component('pie-chart', {
 extends : VueChartJs.Pie,
 props: ['data', 'options'],
 mounted(){
  this.addPlugin(pluginConfig);
   this.renderPieChart();
 },
 computed: {
   attendanceData : function(){
     return this.data
   }
 },
 methods : {
   renderPieChart : function(){
     this.renderChart(
       {
         labels: ['a','b','c','d'],
         datasets: [{
             backgroundColor: ['#10a236', '#f9cd41', '#fe7272', '#5c7add'],
             data: [10,20,30,40]
           }]
       },
       {
         responsive: true,
         maintainAspectRatio: false,
         pieceLabel: {
           render: 'value',
           precision: 1
         },
         showAllTooltips: true
       }
     )

   }
 },
 watch : {
    attendanceData : function(){
      //this.$data._chart.destroy();
      //this.renderPieChart();
    }
  }
})
var vm = new Vue({
  el: '#app',
  data: {
    message: 'Hello World'
  }
})
<script src="https://unpkg.com/vue@2.5.16/dist/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script>
<script src="https://unpkg.com/vue-chartjs/dist/vue-chartjs.min.js"></script>
<div id="app">
  <pie-chart></pie-chart>
</div>