how can i use inline plugin inner title for chart js?
Asked Answered
H

1

0

i am creating a JavaScript web page with two doughnut charts. to create the charts i'm using one function and calling it twice with different data for each chart. however when i do that the text in the middle of the doughnut chart is being changed for both charts not only for current one. how can i write the text individually for each? this is my code

function chart(dummynames, dummyValues, dummyColors, percentage, id) {
  //dummy names, dummy values and dummy colors are arrays

  new Chart(document.getElementById(id), {
    type: 'doughnut',
    data: {
      labels: dummynames,
      datasets: [{
        label: "tessers",
        backgroundColor: dummyColors,
        data: dummyValues,
      }]
    },
    options: {
      title: {
        display: true,
        align: 'center',
        horizontalAlign: "center",
        verticalAlign: "bottom",
        dockInsidePlotArea: true
      },
      legend: {
        display: false
      },
      cutoutPercentage: 70,
    },
  });

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

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

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

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

enter image description here

Honora answered 28/10, 2020 at 6:33 Comment(0)
R
0

You should place Chart.pluginService.register outside of the chart function, thus making sure that it is invoked once only.

Please take a look at your amended code and see how it works.

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

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

    var text = chart.data.datasets[0].percentage,
      textX = Math.round((width - ctx.measureText(text).width) / 2),
      textY = height / 2;

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

function chart(dummynames, dummyValues, dummyColors, percentage, id) {
  new Chart(document.getElementById(id), {
    type: 'doughnut',
    data: {
      labels: dummynames,
      datasets: [{
        label: "tessers",
        backgroundColor: dummyColors,
        data: dummyValues,
        percentage: percentage
      }]
    },
    options: {
      title: {
        display: true,
        align: 'center',
        horizontalAlign: "center",
        verticalAlign: "bottom",
        dockInsidePlotArea: true
      },
      legend: {
        display: false
      },
      cutoutPercentage: 70,
    },
  });
}

chart(['A', 'B'], [5, 6], ['red', 'blue'], '26.846%', 'myChart1');
chart(['X', 'Y'], [7, 5], ['green', 'purple'], '19.451%', 'myChart2');
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
<div style="display: flex">
  <div>
    <canvas id="myChart1"></canvas>
  </div>
  <div>
    <canvas id="myChart2"></canvas>
  </div>
</div>
Runoff answered 7/11, 2020 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.