question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update doc to show 'label inside (/middle of) doughnut' chart recipe

See original GitHub issue

Example code can be found at https://codepen.io/anon/pen/xqMQQB?editors=1010

and

https://stackoverflow.com/a/43176415/1410291

It is better, if this info is reflected in README as it is super helpful. Also, thanks for this amazing react wrapper over chartjs-2

Including code sample, in case one of those source links gets broken in future

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;
    var width = chart.chart.width,
        height = chart.chart.height,
        ctx = chart.chart.ctx;

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

    var sum = 0;
    for (var i = 0; i < chart.config.data.datasets[0].data.length; i++) {
      sum += chart.config.data.datasets[0].data[i];
    }

    var text = sum,
        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'
		]
	}]
};

class DonutWithText extends React.Component {

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

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:11
  • Comments:14

github_iconTop GitHub Comments

3reactions
jerairrestcommented, Oct 4, 2017

@xPhantomNL I had it implemented on the gh pages but ran into an issue where a Chart applys plugins to all the charts in the app. (Totally a different bug)

To fix this example change,

import {Doughnut} from 'react-chartjs-2';

to

import {Doughnut, Chart} from 'react-chartjs-2';

2reactions
walkingalchemycommented, Apr 15, 2018

@jerairrest Any chance you’ve made progress on plugins applying to all charts bug? I ran into it this morning and it seems to shut out the entire possibility of using plugins in an app that has more than one chart.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Update Text In Center of Doughnut Chart in Chart JS
How to Update Text In Center of Doughnut Chart in Chart JSIn this video we will explore how to update text in center...
Read more >
How to Add Multiple Text Labels In Center of Doughnut Chart ...
How to Add Multiple Text Labels In Center of Doughnut Chart in Chart JSIn this video we will explore how to add multiple...
Read more >
Present your data in a doughnut chart - Microsoft Support
Click a data label once to select the data labels for an entire data series, or select them from a list of chart...
Read more >
Labeling a pie and a donut — Matplotlib 3.6.2 documentation
We will create a pie and a donut chart through the pie method and show how to label them with a legend as...
Read more >
Pie Chart | Data Labels | JET Developer Cookbook - Oracle
Setting the label position to 'auto' will place the labels inside the slices if they fit or outside if they don't. Valid label...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found