我想在饼图中显示label的值. (vue-chartjs/pieceLabel) [英] I want to show the value of label inside the pie graph. (vue-chartjs / pieceLabel)

查看:417
本文介绍了我想在饼图中显示label的值. (vue-chartjs/pieceLabel)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名学习vue的学生. 我使用Vue-chartjs绘制图形,并希望在饼图上显示该值. 但是我不知道该怎么办.

请帮助我...

当前情况(图像):在此处输入图像描述

我的愿望(图片):在此处输入图片描述

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指南表示,

在mount()中,使用this.addPlugin像下面的演示一样添加您的插件:

 Vue.config.productionTip = false
//below plugin is copied from https://stackoverflow.com/a/37989832/5665870
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> 

I am a student studying vue. I used Vue-chartjs to draw a graph, and I'd like to display the value on a pie graph. But I don't know what to do.

Please help me...

the current situation (image) : enter image description here

My wish (image) : 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();
      }
    }
  });

解决方案

As The dicusstion on tooltip of chart.js at Stackoverflow, uses plugin is one solution.

then as Vue chart.js guide said,

in mounted(), uses this.addPlugin to add your plugin like below demo:

Vue.config.productionTip = false
//below plugin is copied from https://stackoverflow.com/a/37989832/5665870
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>

这篇关于我想在饼图中显示label的值. (vue-chartjs/pieceLabel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆