Chart JS snippet - Dipak Raval(dkrvl2011)

This is chart js snippet

by Dipak Raval

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
<canvas id="riskFactorChart"></canvas>

CSS

canvas{
  width:200px;
  height:auto;
  margin:0 auto;
}

JavaScript

var options = {
  cutoutPercentage: 75,
  rotation: Math.PI,
  animation: {
    animateScale: true,
    onComplete: function() {
      var width = this.chart.width,
        height = this.chart.height;

      var fontSize = (height / 114).toFixed(2);
      this.chart.ctx.font = fontSize + "em Verdana";
      this.chart.ctx.textBaseline = "middle";

      var text = "82%",
        textX = Math.round((width - this.chart.ctx.measureText(text).width) / 2),
        textY = height / 2;
        console.log(textY);
        console.log(textX);
      this.chart.ctx.fillText(doughnutData.datasets[0].data[0] + "%", textX, textY);
    }
  },

  legend: {
    display: true,
  },
  tooltips: {
    enabled: true,
  },

};

var doughnutData = {
  labels: [
    "Red",
    "Blue",
    "Yellow"
  ],
  datasets: [{
    data: [300, 50, 100],
    backgroundColor: [
      "#FF6384",
      "#36A2EB",
      "#FFCE56"
    ],
    hoverBackgroundColor: [
      "#84FF63",
      "#EBA236",
      "#CE56FF"
    ]
  }]
};

$('#riskFactorChartLoading').hide("fast");
var ctx = $("#riskFactorChart").get(0).getContext("2d");

var riskFactorChart = new Chart(ctx, {
  type: 'doughnut',
  data: doughnutData,
  options: options
});