Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react-chartjs-2 donut chart

import React from 'react';
import ReactDOM from 'react-dom';
import {Doughnut} from 'react-chartjs-2';

// some of this code is a variation on https://jsfiddle.net/cmyker/u6rr5moq/
var originalDoughnutDraw = Chart.controllers.doughnut.prototype.draw;
Chart.helpers.extend(Chart.controllers.doughnut.prototype, {
  draw: function() {
    originalDoughnutDraw.apply(this, arguments);
    
    var chart = this.chart.chart;
    var ctx = chart.ctx;
    var width = chart.width;
    var height = chart.height;

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

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

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

const data = {
	labels: [
		'Red',
		'Green',
		'Yellow'
	],
	datasets: [{
		data: [300, 50, 100],
		backgroundColor: [
		'#FF6384',
		'#36A2EB',
		'#FFCE56'
		],
		hoverBackgroundColor: [
		'#FF6384',
		'#36A2EB',
		'#FFCE56'
		]
	}],
  text: '23%'
};

class DonutWithText extends React.Component {

  render() {
    return (
      <div>
        <h2>React Doughnut with Text Example</h2>
        <Doughnut data={data} />
      </div>
    );
  }
};

ReactDOM.render(
  <DonutWithText />,
  document.getElementById('root')
);
Comment

react-chartjs-2 donut chart

const data = {
  labels: [...],
  datasets: [...],
  text: '23%'
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: package json replace to dev dependencies 
Javascript :: get raw sql query from codeigniter query builder 
Javascript :: send email using javascript and mailtrap 
Javascript :: delete all properties from an javascript object second solution 
Javascript :: js datatables sort hidden columns 
Javascript :: discord.js add role to user 
Javascript :: autosize a textarea using Prototype 
Javascript :: codeigniter get raw query 
Javascript :: angular dynamic script loading 
Javascript :: aurelia shadow dom 
Javascript :: divide string in lines react native max width 
Javascript :: diagonal difference javascript 
Javascript :: classe jquery 
Javascript :: dataForm js 
Javascript :: how to define an object in javascript 
Javascript :: Nodemon e criando o Servidor com express 
Javascript :: cancellable function 
Javascript :: VueJs System Modifier keys like exact ctrl alt shift meta 
Javascript :: nuxt two props 
Javascript :: count same product with price in angular 
Javascript :: convert componentWillUnmount into useEffect 
Javascript :: × react TypeError: Object(...) is not a function 
Javascript :: how to create hexadecimal encoded files in javascript 
Javascript :: google sheets simulate edit event 
Javascript :: disable button without losing value 
Javascript :: react-map-multidimensional-array 
Javascript :: How to assign set a function as Variable 
Javascript :: javascript to reload django partial 
Javascript :: sequlize where clause involving associated relationship 
Javascript :: linearSearch 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =