I'm trying to show some data using a doughnut chart from Chart.js2.
My current chart looks like this:
My desired output must show another attribute, which is the percentage, and looks like this:
I've read the documentation, but I can't cope with this because it's very general and I'm new to JavaScript.
My code for the first chart is the following:
const renderCashCurrencyPie = (cashAnalysisBalances) => { if (cashAnalysisBalances) { const currenciesName = cashAnalysisBalances .map(curName => curName.currency); const availableCash = cashAnalysisBalances .map(avCash => avCash.availableCash); let currenciesCounter = 0; for (let i = 0; i < currenciesName.length; i += 1) { if (currenciesName[i] !== currenciesName[i + 1]) { currenciesCounter += 1; } } const currenciesData = { labels: currenciesName, datasets: [{ data: availableCash, backgroundColor: [ '#129CFF', '#0C6DB3', '#FF6384', '#00FFFF' ], hoverBackgroundColor: [ '#FF6384', '#36A2EB', '#FFCE56', '#00FFFF' ] }] };
You'll have to create a tooltip div, css AND javascript code for each of your doughnut charts. Then repeat the above for donut2, etc. making sure you create the tooltip divs and the css for each new id. The important thing is to add the custom tooltip code to the initialisation of the doughnut, NOT as a general extension as in the example.
toolTip for entire chart can be set by adding content at toolTip object. content is a string and contentFormatter is a custom function that returns HTML/String to be displayed inside the toolTip. Here is an example that uses a combination of string literals and keywords. eg. content: "Category {x} <br/>Value: {y} units"
Namespace: options.plugins.tooltip, the global options for the chart tooltips is defined in Chart.defaults.plugins.tooltip. The bubble, doughnut, pie, polar area, and scatter charts override the tooltip defaults.
The tooltip object is first parameter passed to your customTooltips function. In your chart defaults (or in an individual chart, if you want to really get custom: { // ... customTooltips: function(tooltip){ // tooltip is the tooltip object }, // ... } Sorry, something went wrong.
You can customize the tooltips using the chart options tooltip configuration section, as explained here: http://www.chartjs.org/docs/latest/configuration/tooltip.html#tooltip-configuration
As shown in the example code below, you can change things like color, sizing and styles. Check out the documentation linked above for a full list of configurable options.
If you want to add the percentage to the tooltip display, you can use tooltip callbacks. The documentation has a list of all the possible customizable callback fields.
In the below example, I set the "title" to show the label name, "label" to show the value, and added the percentage to "afterLabel".
var myChart = new Chart(ctx, { type: 'doughnut', data: data, options: { tooltips: { callbacks: { title: function(tooltipItem, data) { return data['labels'][tooltipItem[0]['index']]; }, label: function(tooltipItem, data) { return data['datasets'][0]['data'][tooltipItem['index']]; }, afterLabel: function(tooltipItem, data) { var dataset = data['datasets'][0]; var percent = Math.round((dataset['data'][tooltipItem['index']] / dataset["_meta"][0]['total']) * 100) return '(' + percent + '%)'; } }, backgroundColor: '#FFF', titleFontSize: 16, titleFontColor: '#0066ff', bodyFontColor: '#000', bodyFontSize: 14, displayColors: false } } });
Working JSFiddle: https://jsfiddle.net/m7s43hrs/
As per @Tot Zam's answer, but using arrow functions for brevity:
options: { tooltips: { callbacks: { title: (items, data) => data.datasets[items[0].datasetIndex].data[items[0].index].myProperty1, label: (item, data) => data.datasets[item.datasetIndex].data[item.index].myProperty2 } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With