Chartjs 工具提示换行符

2022-01-22 00:00:00 javascript chart.js

是否可以在 chartjs 工具提示中获得换行符?

Is it possible to get line breaks in chartjs tooltips?

tooltipTemplate: "<%if (label){%><%=label%>: <%}%><%= value %>"

我想用新的一行替换:".

I want to replace ": " with a new line.

我试过 &#013;u000D <br/> 无济于事.

I've tried with &#013;, u000D, and <br /> to no avail.

更新:现在我已将接受的答案更改为 chart.js 是第 2 版.

Update: I have changed my accepted answer now that chart.js is on version 2.

推荐答案

如果您使用的是 2.0.0-beta2,您可以使用 tooltip 回调并在此处返回字符串数组.

If you are using 2.0.0-beta2, you can use tooltip callback and return array of strings there.

tooltips: {
    mode: 'single',
    callbacks: {
        afterBody: function(data) {
            var multistringText = ['first string'];
            // do some stuff
            multistringText.push('another string');

            return multistringText;
        }
    }
}

相关文章