chartist icon indicating copy to clipboard operation
chartist copied to clipboard

Label Text Centered in Donut Chart

Open redrobotx opened this issue 11 years ago • 8 comments

Hey--

I've been fiddling around with the various options and interpolation, but I cannot seem to get the label text to stay centered in the middle of the donut chart. I can get a position that passes for centered, but the position of the text changes and the donut chart values change.

How do I place a label in the absolute center of a donut chart?

screen shot 2015-04-04 at 21 48 43

redrobotx avatar Apr 04 '15 12:04 redrobotx

Hi there. You need to use a combination of Styling and the Chartist Draw events:

http://jsbin.com/buhoza/1/edit?css,js,output

gionkunz avatar Apr 05 '15 10:04 gionkunz

Closing this for now, please comment if you need anything else.

gionkunz avatar Apr 26 '15 15:04 gionkunz

The above shown jsbin does not work :( I'm trying to work this solution out but it seems to be related to new version of Chartist. Could someone help me out here? ( @gionkunz )

morettimatheus avatar Jun 23 '15 19:06 morettimatheus

I found out playing around labelOffset, labelDirection and labelPosition might do the trick. But still no luck. Anyone care to help please?

janakad avatar Mar 22 '16 06:03 janakad

Hi, I've updated the bin http://jsbin.com/buhoza/2/edit?css,js,output

gionkunz avatar Mar 22 '16 07:03 gionkunz

Thank you for the bin! It's perfect if you don't want to show a label for each of the slices. Is there a possibility to add a new label like that while keeping the labels corresponding to each slice?

thevictor13 avatar Mar 23 '16 14:03 thevictor13

Hey it's may be too late but it can help the other person . I was inspired by @gionkunz 's jsbin for that donut who displays the percentage of each value when we mouse over a slice. Hope i can help someone :) https://jsfiddle.net/k9mw7x9q/2/

(sorry for my English, it's not my native language)

launay12u avatar May 20 '16 10:05 launay12u

None of the above examples worked properly for me, the label would not vertically align. I found the answer here: https://stackoverflow.com/a/35394857/6036546

.ct-label {
  dominant-baseline: central; /* This is the key difference from the other examples */
  text-anchor: middle;
  font-size: 80px;
}
new Chartist.Pie('#chart', {
  series: [80]
}, {
  total: 100,
  donut: true,
  donutWidth: 20
}).on('draw', (context) => {
  if (context.type === 'label') {
    context.element.attr({
      dx: context.element.root().width() / 2,
      dy: context.element.root().height() / 2
    });
  }
});

besworks avatar Aug 13 '21 21:08 besworks