繁体   English   中英

Chart js总是在甜甜圈图上显示标签

[英]Chart js always show labels on a doughnut chart

所以我有一个甜甜圈图,我试图保持标签始终处于打开状态,在我的研究中我发现了这一点,但它似乎不起作用,这是我的代码

  function showPieChart(){
    var config = {
      type: 'doughnut',
      data: {
        datasets: [{
          data: [50,25,15,10],
          backgroundColor: ['#585ba7','#999acb','#8ac0e9','#363e96'],
          label: 'Dataset 1'
        }],
        labels: ['Token Sale','Foundation','Early Contributors','Team & Advisors']
      },
      options: {
        tooltipTemplate: "<%= value %>",
        showTooltips: true,
        onAnimationComplete: function() {
          this.showTooltip(this.datasets[0].points, true);
        },
        tooltipEvents: [],
        cutoutPercentage: 90,
        layout: {
          padding: {
            left: 0,
            right: 0,
            top: 0,
            bottom: 0
          }
        },
        responsive: true,
        legend: {
          display: false,
        },
        title: {
          display: false,
        },
        animation: {
          animateRotate: true,
          duration: 1000,
          animateScale: true,
          animationSteps: 15
        }
      }
    };
    var ctx = $("#pie-chart").get(0).getContext("2d");
    Chart.defaults.global.maintainAspectRatio = false;
    window.myDoughnut = new Chart(ctx, config);
  }

我已经以与我发现的答案相同的方式添加了toolTipTemplate,showToolTips,onAnimationComplete和toolTipEvents,但它似乎不起作用,chartjs文档中没有任何内容。 因此,我正在寻找这种方法不起作用的原因,以及如何使其能够以非hacky的方式工作。

假设您使用的是最新版本的chartjs(在本回答时为2.7.1),则可以使用此github问题中的插件正常工作

这是一个工作插件的小提琴: https : //jsfiddle.net/Lngyxg3r/

这是小提琴的代码:

HTML:

<canvas id="pie-chart"></canvas>

JS:

        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;
                }
            }
        });

 function showPieChart(){
    var config = {
      type: 'doughnut',
      data: {
        datasets: [{
          data: [50,25,15,10],
          backgroundColor: ['#585ba7','#999acb','#8ac0e9','#363e96'],
          label: 'Dataset 1'
        }],
        labels: ['Token Sale','Foundation','Early Contributors','Team & Advisors']
      },
      options: {
        tooltipTemplate: "<%= value %>",
        showTooltips: true,
                showAllTooltips: true,
        onAnimationComplete: function() {
          this.showTooltip(this.datasets[0].points, true);
        },
        tooltipEvents: [],
        cutoutPercentage: 90,
        layout: {
          padding: {
            left: 0,
            right: 0,
            top: 0,
            bottom: 0
          }
        },
        responsive: true,
        legend: {
          display: false,
        },
        title: {
          display: false,
        },
        animation: {
          animateRotate: true,
          duration: 1000,
          animateScale: true,
          animationSteps: 15
        }
      }
    };
    var ctx = $("#pie-chart").get(0).getContext("2d");
    Chart.defaults.global.maintainAspectRatio = false;
    window.myDoughnut = new Chart(ctx, config);
  }
  showPieChart();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM