JavaScript Chart.js - Custom data formatting to display on tooltip

前端 未结 11 1940
面向向阳花
面向向阳花 2020-12-07 20:13

I have looked at various documentation and similar questions on here, but cannot seem to find the particular solution. Apologies if I have missed anything obvious or have re

相关标签:
11条回答
  • 2020-12-07 20:43

    This works perfectly fine with me. It takes label and format the value.

    options: {
            tooltips: {
                callbacks: {
                    label: function(tooltipItem, data) {
    
                        let label = data.labels[tooltipItem.index];
                        let value = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
                        return ' ' + label + ': ' + value + ' %';
    
                    }
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-07 20:44

    For chart.js 2.0+, this has changed (no more tooltipTemplate/multiTooltipTemplate). For those that just want to access the current, unformatted value and start tweaking it, the default tooltip is the same as:

    options: {
        tooltips: {
            callbacks: {
                label: function(tooltipItem, data) {
                    return tooltipItem.yLabel;
                }
            }
        }
    }
    

    I.e., you can return modifications to tooltipItem.yLabel, which holds the y-axis value. In my case, I wanted to add a dollar sign, rounding, and thousands commas for a financial chart, so I used:

    options: {
        tooltips: {
            callbacks: {
                label: function(tooltipItem, data) {
                    return "$" + Number(tooltipItem.yLabel).toFixed(0).replace(/./g, function(c, i, a) {
                        return i > 0 && c !== "." && (a.length - i) % 3 === 0 ? "," + c : c;
                    });
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-07 20:45

    You want to specify a custom tooltip template in your chart options, like this :

     // String - Template string for single tooltips
     tooltipTemplate: "<%if (label){%><%=label %>: <%}%><%= value + ' %' %>",
     // String - Template string for multiple tooltips
     multiTooltipTemplate: "<%= value + ' %' %>",
    

    This way you can add a '%' sign after your values if that's what you want.

    Here's a jsfiddle to illustrate this.

    Note that tooltipTemplate applies if you only have one dataset, multiTooltipTemplate applies if you have several datasets.

    This options are mentioned in the global chart configuration section of the documentation. Do have a look, it's worth checking for all the other options that can be customized in there.

    Note that Your datasets should only contain numeric values. (No % signs or other stuff there).

    0 讨论(0)
  • 2020-12-07 20:48
    tooltips: {
              callbacks: {
                label: (tooltipItem, data) => {
                  // data for manipulation
                  return data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
                },
              },
            },
    
    0 讨论(0)
  • 2020-12-07 20:50
    tooltips: {
                enabled: true,
                      mode: 'single',
                      callbacks: {
                        label: function(tooltipItems, data) { 
                          return data.datasets[tooltipItems.datasetIndex].label+": "+tooltipItems.yLabel;
                        }
                      }
                    }
    
    0 讨论(0)
  • 2020-12-07 20:52

    You can give tooltipTemplate a function, and format the tooltip as you wish:

    tooltipTemplate: function(v) {return someFunction(v.value);}
    multiTooltipTemplate: function(v) {return someOtherFunction(v.value);}
    

    Those given 'v' arguments contain lots of information besides the 'value' property. You can put a 'debugger' inside that function and inspect those yourself.

    0 讨论(0)
提交回复
热议问题