最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - customizing tooltip on chart.js bar chart - Stack Overflow

programmeradmin3浏览0评论

I would like to customize the tooltip on a chart.js bar chart. This is my code:

$(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
});
});

I have tried adding tooltipTemplate: "<%if (label){%><%=label%> <%}%>(<%= value %> example)", to barOptions but it has no effect.

I would like to customize the tooltip on a chart.js bar chart. This is my code:

$(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
});
});

I have tried adding tooltipTemplate: "<%if (label){%><%=label%> <%}%>(<%= value %> example)", to barOptions but it has no effect.

Share Improve this question asked Oct 30, 2016 at 20:48 WessiWessi 1,8125 gold badges39 silver badges73 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

Chart.js moved from Templates to Object interfaces in v2+, so for instance If you just want to modify the tooltip text...

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

Result:

Codepen: Chart.js Custom Tooltip

For more plex tooltip customizations see their samples on github: tooltips

发布评论

评论列表(0)

  1. 暂无评论