【发布时间】:2015-11-19 01:09:37
【问题描述】:
我试图在 ChartJS 中始终显示多数据集折线图的工具提示
现有解决方案仅适用于单个数据集(例如Chart.js - Doughnut show tooltips always?),如下所示:
var options =
{
tooltipTemplate: "<%= value %>",
onAnimationComplete: function()
{
this.showTooltip(this.segments, true);
//Show tooltips in bar chart (issue: multiple datasets doesnt work http://jsfiddle.net/5gyfykka/14/)
//this.showTooltip(this.datasets[0].bars, true);
//Show tooltips in line chart (issue: multiple datasets doesnt work http://jsfiddle.net/5gyfykka/14/)
//this.showTooltip(this.datasets[0].points, true);
},
tooltipEvents: [],
showTooltips: true
}
var context = $('#chart').get(0).getContext('2d');
var chart = new Chart(context).Pie(data, options);
用于工作单数据集解决方案的现有 JS Fiddle:http://jsfiddle.net/5gyfykka/14/
我已经设法让 How to display Line Chart dataset point labels with Chart.js? 使用不同的 onAnimationComplete 函数工作
onAnimationComplete: function () {
var ctx = document.getElementById("LineWithLine").getContext("2d");
ctx.font = '.8rem "Gotham Book",sans-serif';
ctx.fontWeight = 'bold';
ctx.fillStyle = '#000';
ctx.textAlign="right";
var self = this;
$(self.datasets).each(function(idx,dataset){
$(dataset.points).each(function(pdx,pointinfo){
if ( pointinfo.value !== null ) {
ctx.fillText(pointinfo.value,pointinfo.x,pointinfo.y - 18);
}
});
});
},
虽然这可行,但它不如 Chart JS 提供的多工具提示那么好。
【问题讨论】:
标签: javascript charts chart.js