那里。我使用ChartJS并自定义工具提示,但是有位置第一个和最后一个工具提示的问题。看:
我想,为了解决问题,我需要使用https://www.chartjs.org/docs/latest/configuration/tooltip.html#position-modes但是,我无法理解公式应该是什么。
CodePen示例 - https://codepen.io/anon/pen/JzRooy
<html>
<head>
<title>Line Chart with Custom Tooltips</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.bundle.js"></script>
<script>
window.chartColors = {
red: 'rgb(255, 99, 132)',
orange: 'rgb(255, 159, 64)',
yellow: 'rgb(255, 205, 86)',
green: 'rgb(75, 192, 192)',
blue: 'rgb(54, 162, 235)',
purple: 'rgb(153, 102, 255)',
grey: 'rgb(231,233,237)'
};
window.randomScalingFactor = function() {
return (Math.random() > 0.5 ? 1.0 : -1.0) * Math.round(Math.random() * 100);
}
</script>
<style>
canvas{
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
#chartjs-tooltip {
opacity: 1;
position: absolute;
background: rgba(0, 0, 0, .7);
color: white;
border-radius: 3px;
-webkit-transition: all .1s ease;
transition: all .1s ease;
pointer-events: none;
-webkit-transform: translate(-50%, 0);
transform: translate(-50%, 0);
}
.chartjs-tooltip-key {
display: inline-block;
width: 10px;
height: 10px;
margin-right: 10px;
}
</style>
</head>
<body>
<canvas id="chart"/>
<script>
Chart.defaults.global.pointHitDetectionRadius = 1;
var customTooltips = function(tooltip) {
var tooltipEl = document.getElementById('chartjs-tooltip');
if (!tooltipEl) {
tooltipEl = document.createElement('div');
tooltipEl.id = 'chartjs-tooltip';
tooltipEl.innerHTML = "<div class='wrapper'></div>"
document.body.appendChild(tooltipEl);
}
// Hide if no tooltip
if (tooltip.opacity === 0) {
tooltipEl.style.opacity = 0;
return;
}
// Set caret Position
tooltipEl.classList.remove('above', 'below', 'no-transform');
if (tooltip.yAlign) {
tooltipEl.classList.add(tooltip.yAlign);
} else {
tooltipEl.classList.add('no-transform');
}
function getBody(bodyItem) {
return bodyItem.lines;
}
// Set Text
if (tooltip.body) {
var titleLines = tooltip.title || [];
var bodyLines = tooltip.body.map(getBody);
var innerHtml = '';
titleLines.forEach(function(title) {
innerHtml += '<span style="margin-bottom: 10px;display: inline-block;">' + title + '</span>';
});
innerHtml += '<div style="display: flex;flex-direction: row;">';
bodyLines.forEach(function(body, i) {
var parts = body[0].split(':');
innerHtml += '<div style="display: flex;flex-direction: column;margin-right: 10px;font-size: 12px;">';
innerHtml += '<span>' + parts[0].trim() + '</span>';
innerHtml += '<b>' + parts[1].trim() + '</b>';
innerHtml += '</div>';
});
innerHtml += '</div>';
var root = tooltipEl.querySelector('.wrapper');
root.innerHTML = innerHtml;
}
var canvas = this._chart.canvas;
tooltipEl.style.opacity = 1;
tooltipEl.style.left = canvas.offsetLeft + tooltip.caretX + 'px';
tooltipEl.style.top = canvas.offsetTop + tooltip.caretY + 'px';
tooltipEl.style.fontFamily = tooltip._fontFamily;
tooltipEl.style.fontSize = tooltip.fontSize;
tooltipEl.style.fontStyle = tooltip._fontStyle;
tooltipEl.style.padding = "10px";
tooltipEl.style.border = "1px solid #B4B6C1";
tooltipEl.style.backgroundColor = "#FFFFFF";
tooltipEl.style.color = "#4C4F59";
tooltipEl.style.fontFamily = '"open sans", "helvetica neue", "arial", "sans-serif"';
};
var lineChartData = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: "My First dataset",
borderColor: window.chartColors.red,
pointBackgroundColor: window.chartColors.red,
fill: false,
data: [
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor()
]
}, {
label: "My Second dataset",
borderColor: window.chartColors.blue,
pointBackgroundColor: window.chartColors.blue,
fill: false,
data: [
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor()
]
}]
};
window.onload = function() {
var chartEl = document.getElementById("chart");
window.myLine = new Chart(chartEl, {
type: 'line',
data: lineChartData,
options: {
title:{
display:true,
text:'Chart.js Line Chart - Custom Tooltips'
},
tooltips: {
enabled: false,
mode: 'nearest',
position: 'average',
intersect: false,
custom: customTooltips
}
}
});
};
</script>
</body>
</html>
可以通过向Chart.Tooltip.positioners映射(DOC)添加函数来定义新模式。此函数返回工具提示的x和y位置。
您可以添加自定义的一个来调整偏移处的x。一种方法是:
//register custome positioner
Chart.Tooltip.positioners.custom = function(elements, position) {
if (!elements.length) {
return false;
}
var offset = 0;
//adjust the offset left or right depending on the event position
if (elements[0]._chart.width / 2 > position.x) {
offset = 20;
} else {
offset = -20;
}
return {
x: position.x + offset,
y: position.y
}
}
Fiddle我创建的例子
我希望它有所帮助。