A couple people have asked me recently about altering their doughnut charts. The easiest way to do this is with the chartjs-plugin-datalabels plugin, which is automatically included in all QuickChart renders.
See this post for more information on customizing doughnut and pie charts.
I’m trying to replicate this in Python. I can’t add
formatter: (value) => {
return value + '%';
}
this in a dict for obvious reasons. I know I can pass the config as a string, but that means some messy code to have non-fixed data. Is there a better way to do this?
If you’re not using the library, you’ll ultimately have to send the chart config as a string.
One workaround that I’ve seen some people use to keep things cleaner: you could set formatter to some placeholder value like __FORMATTER_FUNCTION__, serialize your dict to JSON, and then do a string replacement on "__FORMATTER_FUNCTION__".
The easiest way to do this is to upgrade to Chart.js v3. You can do this by setting version=3 in your request. This will use a newer version of chartjs-plugin-annotations that is capable of drawing on pie charts.