【问题标题】:How to point a Chart.js plugin to different doughnut charts?如何将 Chart.js 插件指向不同的圆环图?
【发布时间】:2017-07-09 23:01:35
【问题描述】:

我发现了一个 Chart.js 插件,它在 Chart.js 圆环图的中间写入文本,如下所示。

我在页面上有两个甜甜圈。一种称为“myChart”,一种称为“secondChart”。如果我将下面的代码更改为指向第二个图表,如果只指向第二个图表。

但如果我将它指向第一个图表,则会在两个图表中呈现。有没有办法将此插件更改为可重用功能以在页面上的多个图表上使用?

  Chart.pluginService.register({
  beforeDraw: function(chart) {
    var width = chart.chart.width,
        height = chart.chart.height,
        ctx = chart.chart.ctx;

        ctx.restore();
        var fontSize = (height / 120).toFixed(2);
        ctx.font = fontSize + "em sans-serif";
        ctx.textBaseline = "middle";

     var text = "100%",
        textX = Math.round((width - ctx.measureText(text).width) / 2),
        textY = height / 2;

        ctx.fillText(text, textX, textY);
        ctx.save();
      }
    }); 

【问题讨论】:

    标签: javascript chart.js


    【解决方案1】:

    试试下面的代码。测试和工作。希望对你有帮助:)

       var doughnutCenterText = {
        beforeDraw: function (chart) {
        var width = chart.chart.width,
        height = chart.chart.height,
        ctx = chart.chart.ctx;
    
        ctx.restore();
        var fontSize = (height / 120).toFixed(2);
        ctx.font = fontSize + "em sans-serif";
        ctx.textBaseline = "middle";
    
        var text = "100%",
        textX = Math.round((width - ctx.measureText(text).width) / 2),
        textY = height / 2;
    
        ctx.fillText(text, textX, textY);
        ctx.save();
            }
        };
    
    //Chart1
        this.doughnutChart1 = new Chart(this.doughnutCanvas1.nativeElement, {
            type: 'doughnut',
            data: {
                datasets: [{
                    data: [65, 35],
                    backgroundColor: [
                        'rgba(229, 57, 53, 1)',
                        'rgba(229, 57, 53, 0.2)',
                    ],
                    hoverBackgroundColor: [
                        'rgba(229, 57, 53, 1)',
                        'rgba(229, 57, 53, 0.2)',
                    ],
                    hoverBorderColor: [
                        '#fff',
                        '#fff'
                    ]
                }]
            },
            plugins: [doughnutCenterText]    //Note here
        });
    
    //Chart 2
        this.doughnutChart2 = new Chart(this.doughnutCanvas2.nativeElement, {
            type: 'doughnut',
            data: {
                datasets: [{
                    data: [55, 45],
                    backgroundColor: [
                        'rgba(0, 137, 123, 1)',
                        'rgba(0, 137, 123, 0.2)',
                    ],
                    hoverBackgroundColor: [
                        'rgba(0, 137, 123, 1)',
                        'rgba(0, 137, 123, 0.2)',
                    ],
                    hoverBorderColor: [
                        '#fff',
                        '#fff'
                    ]
                }]
            },
            plugins: [doughnutCenterText]    //Note here
        });
    

    【讨论】:

      猜你喜欢
      • 2021-02-23
      • 1970-01-01
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-18
      相关资源
      最近更新 更多