canvas fill text vanishes when hovering over chartjs pie chart

前端 未结 3 1641
心在旅途
心在旅途 2021-01-15 05:21

I am using a doughnut chart from chartjs, and in the center of this canvas I am filling two lines of text. These show up fine after the initial animation, but when I hover o

3条回答
  •  -上瘾入骨i
    2021-01-15 05:54

    Actually you can do this with a simple extension of the chart type you are using and moving your draw code to inside the draw override


    Preview


    Code

    Chart.types.Doughnut.extend({
        name: "DoughnutAlt",
        draw: function () {
            Chart.types.Doughnut.prototype.draw.apply(this, arguments);
    
            this.chart.ctx.textBaseline = "middle";
            this.chart.ctx.fillStyle = 'black'
            this.chart.ctx.font = "50px Roboto";
            this.chart.ctx.textAlign = "center";
            this.chart.ctx.fillText(distributionChartData[3] + " %", 135, 120);
            this.chart.ctx.font = "20px Roboto";
            this.chart.ctx.fillText((distributionChartData[0] + distributionChartData[1] + distributionChartData[2]) + " Responses", 135, 160);
        }
    });
    
    var pieChart = document.getElementById("pieChart").getContext("2d");
    new Chart(pieChart).DoughnutAlt(pieData, {
        percentageInnerCutout: 80, animationEasing: "easeOutQuart"
    });
    

    Fiddle - http://jsfiddle.net/p979zyLj/

提交回复
热议问题