ChartJs自定义工具提示位置 [英] ChartJs custom tooltip position
问题描述
有.我使用ChartJS并自定义工具提示,但是位置第一个和最后一个工具提示存在问题.看:
我想为了解决此问题,我需要使用https://www.chartjs.org/docs/latest/configuration/tooltip.html#position-modes 但是,我不知道公式应该是什么.
CodePen示例- https://codepen.io/anon/pen/JzRooy >
< html>< head>< title>带有自定义工具提示的折线图</title>< script src ="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.bundle.js"></script>< script>window.chartColors = {红色:'rgb(255,99,132)',橙色:'rgb(255,159,64)',黄色:"rgb(255,205,86)",绿色:'rgb(75,192,192)',蓝色:'rgb(54,162,235)',紫色:'rgb(153,102,255)',灰色:"rgb(231,233,237)"};window.randomScalingFactor = function(){return(Math.random()> 0.5?1.0:-1.0)* Math.round(Math.random()* 100);}</script>< style>帆布{-moz-user-select:无;-webkit-user-select:无;-ms-user-select:无;}#chartjs-tooltip {不透明度:1;位置:绝对;背景:rgba(0,0,0,.7);白颜色;border-radius:3px;-webkit-transition:所有.1缓解;过渡:所有.1缓解;指针事件:无;-webkit-transform:translate(-50%,0);转换:translate(-50%,0);}.chartjs-tooltip-key {显示:inline-block;宽度:10px;高度:10px;右边距:10px;}</style></head><身体>< canvas id ="chart"/>< script>Chart.defaults.global.pointHitDetectionRadius = 1;var customTooltips = function(tooltip){var tooltipEl = document.getElementById('chartjs-tooltip');如果(!tooltipEl){tooltipEl = document.createElement('div');tooltipEl.id ='chartjs-tooltip';tooltipEl.innerHTML =< div class ='wrapper'></div>"document.body.appendChild(tooltipEl);}//如果没有工具提示则隐藏如果(tooltip.opacity === 0){tooltipEl.style.opacity = 0;返回;}//设置插入符位置tooltipEl.classList.remove('above','below','no-transform');如果(tooltip.yAlign){tooltipEl.classList.add(tooltip.yAlign);} 别的 {tooltipEl.classList.add('no-transform');}函数getBody(bodyItem){返回bodyItem.lines;}//设定文字如果(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;">'+标题+'</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实心#B4B6C1";tooltipEl.style.backgroundColor ="#FFFFFF";tooltipEl.style.color =#4C4F59";tooltipEl.style.fontFamily ='"open sans","helvetica neue","arial","sans-serif"';};var lineChartData = {标签:[一月",二月",三月",四月",五月",六月",七月"],数据集:[{标签:我的第一个数据集",borderColor:window.chartColors.red,pointBackgroundColor:window.chartColors.red,填写:假,数据: [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]},{标签:我的第二个数据集",borderColor:window.chartColors.blue,pointBackgroundColor:window.chartColors.blue,填写:假,数据: [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]}]};window.onload = function(){var chartEl = document.getElementById("chart");window.myLine = new Chart(chartEl,{类型:行",数据:lineChartData,选项: {标题:{显示:true,文字:"Chart.js折线图-自定义工具提示"},工具提示:{已启用:false,模式:最近",位置:平均",相交:假,自定义:customTooltips}}});};</script></body></html>
可以通过向Chart.Tooltip.positioners映射中添加功能来定义新模式(我创建的小提琴示例
希望对您有帮助.
there. I use ChartJS and customise tooltip, but have issue with position first and last tooltip's. Look:
I suppose that in order to fix the problem, I need to use the https://www.chartjs.org/docs/latest/configuration/tooltip.html#position-modes but, I cannot understand what the formula should be.
CodePen example - 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>
New modes can be defined by adding functions to the Chart.Tooltip.positioners map (DOC). This function returns the x and y position for the tooltip.
You can add a custom one to adjust the x at an offset. One way to do this would be to be:
//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 example that I created
I hope it helps.
这篇关于ChartJs自定义工具提示位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!