我的目標是創建自定義工具提示並始終顯示它們。稍後會有更多花哨的東西,但現在我正在努力實現這兩件事。Charts.js v2:始終在圖表上顯示自定義(html)工具提示
爲了創建自定義工具提示,我使用了charts.js docs的教程;這裏在stackoverflow我發現solution for always showing the tooltips。
所以現在我有兩個工具提示:標準的工具提示,出現在Draw後並保持可見狀態,以及自定義工具提示,這些提示工作到目前爲止,但只出現在懸停點/數據點。
jsFiddlehttps://jsfiddle.net/2c4261wj/1/以下圖片顯示問題。紅色容器顯示自定義工具提示,只出現在懸停點上。
而不是標準的提示,我想總是顯示自定義的afterDraw,但我不能設法做到這一點。
原料codebits在提示選項
自定義工具提示:
custom: function(tooltipModel) {
// Tooltip Element
var tooltipEl = document.getElementById('chartjs-tooltip');
// Create element on first render
if (!tooltipEl) {
tooltipEl = document.createElement('div');
tooltipEl.id = 'chartjs-tooltip';
tooltipEl.innerHTML = "<table></table>";
document.body.appendChild(tooltipEl);
}
// Hide if no tooltip
if (tooltipModel.opacity === 0) {
tooltipEl.style.opacity = 1;
return;
}
// Set caret Position
tooltipEl.classList.remove('above', 'below', 'no-transform');
if (tooltipModel.yAlign) {
tooltipEl.classList.add(tooltipModel.yAlign);
} else {
tooltipEl.classList.add('no-transform');
}
function getBody(bodyItem) {
return bodyItem.lines;
}
// Set Text
if (tooltipModel.body) {
var titleLines = tooltipModel.title || [];
var bodyLines = tooltipModel.body.map(getBody);
var innerHtml = '<div>';
titleLines.forEach(function(title) {
innerHtml += '<span>' + title + '</span>';
});
innerHtml += '</div>';
bodyLines.forEach(function(body, i) {
var colors = tooltipModel.labelColors[i];
var style = 'background:' + colors.backgroundColor;
style += '; border-color:' + colors.borderColor;
style += '; border-width: 2px';
var span = '<span class="chartjs-tooltip-key" style="' + style + '"></span>';
innerHtml += '<div class="inner">' + span + body + '</div>';
});
innerHtml += 'last';
var tableRoot = tooltipEl;
tableRoot.innerHTML = innerHtml;
}
// `this` will be the overall tooltip
var position = this._chart.canvas.getBoundingClientRect();
// Display, position, and set styles for font
tooltipEl.style.opacity = 1;
tooltipEl.style.left = position.left + tooltipModel.caretX - 100 + 'px';
tooltipEl.style.top = position.top + tooltipModel.caretY - 100 + 'px';
tooltipEl.style.fontFamily = tooltipModel._fontFamily;
tooltipEl.style.fontSize = tooltipModel.fontSize;
tooltipEl.style.fontStyle = tooltipModel._fontStyle;
tooltipEl.style.padding = tooltipModel.yPadding + 'px ' + tooltipModel.xPadding + 'px';
}
始終顯示工具提示:
Chart.pluginService.register({
beforeRender: function (chart) {
if (chart.config.options.showAllTooltips) {
// create an array of tooltips
// we can't use the chart tooltip because there is only one tooltip per chart
chart.pluginTooltips = [];
chart.config.data.datasets.forEach(function (dataset, i) {
chart.getDatasetMeta(i).data.forEach(function (sector, j) {
chart.pluginTooltips.push(new Chart.Tooltip({
_chart: chart.chart,
_chartInstance: chart,
_data: chart.data,
_options: chart.options.tooltips,
_active: [sector]
}, chart));
});
});
// turn off normal tooltips
chart.options.tooltips.enabled = false;
}
},
afterDraw: function (chart, easing) {
if (chart.config.options.showAllTooltips) {
// we don't want the permanent tooltips to animate, so don't do anything till the animation runs atleast once
if (!chart.allTooltipsOnce) {
if (easing !== 1)
return;
chart.allTooltipsOnce = true;
}
// turn on tooltips
chart.options.tooltips.enabled = true;
Chart.helpers.each(chart.pluginTooltips, function (tooltip) {
tooltip.initialize();
tooltip.update();
// we don't actually need this since we are not animating tooltips
tooltip.pivot();
tooltip.transition(easing).draw();
});
chart.options.tooltips.enabled = false;
}
}
});
我試圖用自定義提示右側的第一個插件內,但沒有結果,也許我在插件中引用它是錯誤的。 任何想法?
是的,我添加了鏈接。謝謝 – Tonsenson
好的,那你到底想要什麼?根據你的問題,你目前的圖表狀態對我來說似乎很好。 –
對不起,如果我沒有解釋確切的問題:現在我有2種工具提示:自定義HTML和「標準」。我設法在Draw之後使標準顯示器正確顯示,但是我需要對它們進行很多定製,所以我想使用已經創建的定製顯示器。而不是標準的工具提示,我想在Draw之後始終顯示自定義的工具提示,但我無法做到這一點。(會試着更新這個新增的問題) – Tonsenson