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.

Doughnut: write something in the center

See original GitHub issue

Hi,

I’m not very familiar with the canvas , I’d like to write something in the center of the doughnut and I’ve tried to use the ctx.fillText() without success, once I call the chartjs API it erases my text, and if I try to fillText after the call it output nothing.

I’d like to achieve something similar to this: carousel_image_analyse

Am I doing something wrong with my try or is just no way to do it using chartjs?

Thanks

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
corentingicommented, Jan 19, 2018

You can easily achieve this now with plugins:

var chart = new Chart(ctx, {
  plugins: [{
    id: 'my-plugin',
    afterDraw: function (chart, option) {
      chart.ctx.fillStyle = 'black'
      chart.ctx.textBaseline = 'middle'
      chart.ctx.textAlign = 'center'
      chart.ctx.font = '17px Arial'
      chart.ctx.fillText('50%', chart.canvas.width / 2, chart.canvas.height / 2)
    }
  }]
});

You can have more info on this there: http://www.chartjs.org/docs/latest/developers/plugins.html#using-plugins

2reactions
dunckrcommented, Sep 20, 2014

This worked for me by revising this

    Chart.types.Doughnut.extend({
        name: "DoughnutAlt",
        draw: function() {
            Chart.types.Doughnut.prototype.draw.apply(this, arguments);
            this.chart.ctx.fillStyle = 'black';
            this.chart.ctx.textBaseline = 'middle';
            this.chart.ctx.fillText(this.segments[0].value + "%", this.chart.width / 2 - 20, this.chart.width / 2, 200);
        }
    });
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Add Text and Value at the Center of a Doughnut Chart ...
What type of charts can you make with Chart.JS? With chart js you can make line chart, bar chart, pie chart, doughnut chart,...
Read more >
How to Add Multiple Text Labels In Center of Doughnut Chart ...
We cover the code in chart js but also what truly happens and why something happens when we write a line of code....
Read more >
How to add text inside the doughnut chart using Chart.js?
Here is a small script you can use to dynamically place any amount of text in the middle, and it will automatically resize...
Read more >
How can I add text in the centre of a Donut Chart - Edureka
To get the label center in donut chart, Select the 2nd pie chart and go to label, Click on automatic next to alignment....
Read more >
Centering Text in Donut - Highcharts official support forum
I was wondering if there was a proper way to center two lines of text inside a donut. I have tried a combination...
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