Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize the tooltip of a Chart.js 2.0 Doughnut Chart?

I'm trying to show some data using a doughnut chart from Chart.js2.

My current chart looks like this:
current chart

My desired output must show another attribute, which is the percentage, and looks like this:
desired chart with percentage

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'         ]       }]     }; 
like image 604
user7334203 Avatar asked Apr 25 '17 07:04

user7334203


People also ask

How to add a tooltip to a donut chart?

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.

How to set tooltip for entire chart in Salesforce?

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"

What are the options for chart tooltips in namespace?

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.

What is the tooltip object in customtooltips?

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.


2 Answers

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/

like image 197
Tot Zam Avatar answered Oct 03 '22 03:10

Tot Zam


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     }   } } 
like image 37
Merenzo Avatar answered Oct 03 '22 04:10

Merenzo