在chart.js条形图上自定义工具提示

Wes*_*ssi 1 javascript charts chart.js

我想在chart.js条形图上自定义工具提示.这是我的代码:

$(function () {
  var barData = no_houses_person

var barOptions = {
    scaleBeginAtZero: true,
    scaleShowGridLines: true,
    scaleGridLineColor: "rgba(0,0,0,.05)",
    legend: {
        display: true,
        position: 'top'
    },
    scaleGridLineWidth: 1,
    barShowStroke: true,
    barStrokeWidth: 1,
    barValueSpacing: 5,
    barDatasetSpacing: 1,
    responsive: true,
};
   var ctx = document.getElementById("barChart").getContext("2d");
   var myNewChart = new Chart(ctx, {
    type: 'bar',
    data: barData,
    options: barOptions
});
});
Run Code Online (Sandbox Code Playgroud)

我已经尝试添加tooltipTemplate: "<%if (label){%><%=label%> <%}%>(<%= value %> example)",到barOptions但它没有任何效果.

Ken*_*eno 8

Chart.js 在v2 +中从Templates移动到Object接口,例如,如果你只想修改工具提示文本...

tooltips: {
    callbacks: {
        label: function(tooltipItem) {
            return "$" + Number(tooltipItem.yLabel) + " and so worth it !";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

Codepen:Chart.js自定义工具提示

有关更复杂的工具提示自定义,请参阅github上的示例:工具提示