我正在尝试使用针对Chart.js的ng2-chart Angular2指令构建甜甜圈图。此chart.js具有工具提示定位的默认选项,我想使工具提示显示在图表外部。有没有办法使之成为可能?
public doughnutChartOptions = {
responsive: true,
tooltips:{
mode: 'nearest',
}
}
我希望类似工具提示的内容显示在图表外部,而不是显示在图表内部;顶部,底部和侧面。
答案 0 :(得分:0)
您可以像这样在custom
上使用tooltip
属性。
tooltips:{
custom:function(tooltipModel){
//Top-Left
tooltipModel.x=10;
tooltipModel.y=0;
}.bind(this)
实时示例链接为:https://stackblitz.com/edit/ng2-charts-doughnut-template-6wef9r
答案 1 :(得分:0)
您可以通过自定义工具提示来实现。 https://www.chartjs.org/docs/latest/configuration/tooltip.html#position-modes
下面是我遇到相同问题时实现的源代码。
// import the required classes and modules.
@ViewChild("piechart", { static: true }) private piechartRef;
// put the below code into the method based on your requirement
// pie chart start
this.piechart = new Chart(this.piechartRef.nativeElement, {
type: 'pie',
data: {
labels: ["Normal", "Human Impersonation", "Bot"],
datasets: [
{
fill: true,
backgroundColor: ['#53AF50', '#F8DBD8', 'rgb(255, 129, 0)'],
data: [73, 42, 19]
}
]
},
options: {
title: {
display: false,
},
legend: {
display: false,
},
tooltips: {
// Disable the on-canvas tooltip
enabled: false,
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>';
tooltipEl.style.backgroundColor = "#FFFFFF";
tooltipEl.style.borderColor = "#B2BABB";
tooltipEl.style.borderWidth = "thin";
tooltipEl.style.borderStyle = "solid";
document.body.appendChild(tooltipEl);
}
// Hide if no tooltip
if (tooltipModel.opacity === 0) {
tooltipEl.style.opacity = "0";
return;
} else {
tooltipEl.style.opacity = "1";
}
// 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 = '<thead>';
titleLines.forEach(function (title) {
innerHtml += '<tr><th>' + title + '</th></tr>';
});
innerHtml += '</thead><tbody>';
bodyLines.forEach(function (body, i) {
let name = body[0].split(":")[0];
let icon = null;
if(name == "Human Impersonation"){
icon = '<div style="margin-top:2px;margin-right:2px;float: left;height: 15px;width: 15px;border: 1px solid black;clear: both;background-color: #F8DBD8;"></div>';
name = icon + name;
}else if(name == "Bot"){
icon = '<div style="margin-top:2px;margin-right:2px;float: left;height: 15px;width: 15px;border: 1px solid black;clear: both;background-color: rgb(255, 129, 0);"></div>';
name = icon + name;
}else if(name == "Normal"){
icon = '<div style="margin-top:2px;margin-right:2px;float: left;height: 15px;width: 15px;border: 1px solid black;clear: both;background-color: #53AF50;"></div>';
name = icon + name;
}
let value = body[0].split(":")[1];
innerHtml += '<tr><td style="padding: 0px"><b>' + name + ':</b> ' + value + '</td></tr>';
});
innerHtml += '</tbody>';
var tableRoot = tooltipEl.querySelector('table');
tableRoot.innerHTML = innerHtml;
}
// `this` will be the overall tooltip
var position = this._chart.canvas.getBoundingClientRect();
// Display, position, and set styles for font
tooltipEl.style.position = 'absolute';
tooltipEl.style.left = position.left + window.pageXOffset + tooltipModel.caretX + 'px';
tooltipEl.style.top = position.top + window.pageYOffset + tooltipModel.caretY + 'px';
tooltipEl.style.fontFamily = tooltipModel._bodyFontFamily;
tooltipEl.style.fontSize = tooltipModel.bodyFontSize + 'px';
tooltipEl.style.fontStyle = tooltipModel._bodyFontStyle;
tooltipEl.style.padding = tooltipModel.yPadding + 'px ' + tooltipModel.xPadding + 'px';
tooltipEl.style.pointerEvents = 'none';
}
}
}
});
this.piechart.update();
// pie chart end
<canvas #piechart style="height: 60px !important">
{{piechart}}
</canvas>
输出