Chart.js V2: Add prefix or suffix to tooltip label

前端 未结 6 1606
情书的邮戳
情书的邮戳 2020-12-04 16:03

In Chart.js V1.0, I would add tooltipTemplate: \"<%if (label){%><%=label %>: <%}%><%= \'€\' + value %>\" to add a euro symbol as prefix

6条回答
  •  天涯浪人
    2020-12-04 16:15

    In the V2.0 the tooltipTemplate option is deprecated. Instead you can use callbacks to modify the displayed tooltips. There is a sample for the usage of callbacks here and you can find the possible callbacks in the documentation under Chart.defaults.global.tooltips

    In your case I would do the following:

    window.myLine = new Chart(chart, {
        type: 'line',
        data: lineChartData,
        options: {
                tooltips: {
                    enabled: true,
                    mode: 'single',
                    callbacks: {
                        label: function(tooltipItems, data) { 
                            return tooltipItems.yLabel + ' €';
                        }
                    }
                },
         }       
      });
    

    Don't forget to set the HTML meta tag:

    
    

提交回复
热议问题