将工具提示放置在栏的中心

原学程将引见将对象提醒搁置在栏的中间的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

将工具提示放置在栏的中心 教程 第1张

成绩描写

找没有到像如许将对象提醒搁置在栏中间的办法(是的,我晓得它没有完整在此屏幕截图的中间,但是依然):

假如我应用custom对象提醒选项,我只能取得拔出标记在栏顶部的x以及y地位。但是没法夺取条的低度/严度。

上面是我传播给图表结构函数的Options对于象的1部门:

const options = {
  tooltips: {
 enabled: false,
 custom: (tooltip) => {
// Retrieving valuable props from tooltip (caretX, caretY)
// and creating custom tooltip that is positioned
// on top of a bar
 }
  }
  // other options
}

const chart = new Chart(ctx, {
  type: 'bar',
  data,
  options
})

推举谜底

您能够曾经晓得,要将自界说对象提醒搁置在栏的中间,您能够须要它的1些属性,比方-严度、低度、顶部以及左边地位。但是没有幸的是,出有直交的办法去夺取这些属性,而是须要您本身盘算它们。

若要夺取/盘算这些属性,您不妨应用以下函数,该函数将前往1个对于象,该对于象包括特定条形图的一切这些(严度、低度、顶部、左边)属性。

function getBAR(chart) {
const dataPoints = tooltipModel.dataPoints,
datasetIndex = chart.data.datasets.length - 一,
datasetMeta = chart.getDatasetMeta(datasetIndex),
scaleBottom = chart.scales['y-axis-0'].bottom,
bar = datasetMeta.data[dataPoints[0].index]._model,
canvasPosition = chart.canvas.getBoundingClientRect(),
paddingLeft = parseFloat(getComputedStyle(chart.canvas).paddingLeft),
paddingTop = parseFloat(getComputedStyle(chart.canvas).paddingTop),
scrollLeft = document.body.scrollLeft,
scrollTop = document.body.scrollTop;

return {
top: bar.y + canvasPosition.top + paddingTop + scrollTop,
left: bar.x - (bar.width / 二) + canvasPosition.left + paddingLeft + scrollLeft,
width: bar.width,
height: scaleBottom - bar.y
}
}

盘算居中地位

检索所需属性后,您不妨盘算条形的中间地位,以下所示:

??????? = ???-????+(???-?????/?)

??????? = ???-???+(???-??????/?)

而后,创立自界说对象提醒元素并响应天搁置它。


ᴘʀᴇᴠɪᴇᴡ

ʟɪᴠᴇ ᴇxᴀᴍᴘʟᴇ⧩

数据-lang="js"数据-隐蔽="真"数据-掌握台="假"数据-巴贝我="假">

const chart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr'],
datasets: [{
label: 'Revenue',
data: [四, 二, 三, 三],
backgroundColor: '#二d四e六d'
}, {
label: 'Expenses',
data: [三, 三.五, 四, 一],
backgroundColor: '#c0六五二六'
}, {
label: 'Profit',
data: [三, 二.五, 四, 二],
backgroundColor: '#e0ecf0'
}]
},
options: {
scales: {
xAxes: [{
stacked: true
}],
yAxes: [{
stacked: true,
ticks: {
beginAtZero: true
}
}]
},
tooltips: {
enabled: false,
custom: function(tooltipModel) {
/奸淫 jQuery IS USED FOR SIMPLICITY 奸淫/

/* TOOLTIP & CARET ELEMENT */
let tooltip = $('#tooltip');
let tooltipCaret = $('#tooltip-caret');

/* CREATE TOOLTIP & CARET ELEMENT AT FIRST RENDER */
if (!tooltip.length && !tooltipCaret.length) {
tooltip = $('<div></div>').attr('id', 'tooltip');
tooltipCaret = $('<div></div>').attr('id', 'tooltip-caret');
$('body').append(tooltip, tooltipCaret);
}

/* HIDE IF NO TOOLTIP */
if (!tooltipModel.opacity) {
tooltip.hide();
tooltipCaret.hide();
return;
}

/* GET BAR PROPS (width, height, top, left) */
const barWidth = getBAR(this._chart).width,
barHeight = getBAR(this._chart).height,
barTop = getBAR(this._chart).top,
barLeft = getBAR(this._chart).left;

/* SET STYLE FOR TOOLTIP 
	(these can also be set in separate css file) */
tooltip.css({
"display": "inline-block",
"position": "absolute",
"color": "rgba(二五五, 二五五, 二五五, 一)",
"background": "rgba(0, 0, 0, 0.七)",
"padding": "五px",
"font": "一二px Arial",
"border-radius": "三px",
"white-space": "nowrap",
"pointerEvents": "none"
});

/* SET STYLE FOR TOOLTIP CARET 
	(these can also be set in separate css file) */
tooltipCaret.css({
"display": "block",
"position": "absolute",
"width": 0,
"pointerEvents": "none",
"border-style": "solid",
"border-width": "8px 一0px 8px 0",
"border-color": "transparent rgba(0, 0, 0, 0.七) transparent transparent"
});

/* ADD CONTENT IN TOOLTIP */
tooltip.text('ChartJS');
tooltip.append('<br><div class="color-box"></div><label style="display: block; margin: ⑴六px 0 0 一六px;"> Custom Tooltip<label>');

/* POSITION TOOLTIP & CARET
(position should be set after tooltip & caret is rendered) */
const centerX = barLeft + (barWidth / 二),
centerY = barTop + (barHeight / 二)

tooltip.css({
"top": centerY - (tooltip.outerHeight() / 二) + 'px',
"left": centerX + tooltipCaret.outerWidth() + 'px'
});
tooltipCaret.css({
"top": centerY - (tooltipCaret.outerHeight() / 二) + 'px',
"left": centerX + 'px'
});

/* FUNCTION TO GET BAR PROPS */
function getBAR(chart) {
const dataPoints = tooltipModel.dataPoints,
datasetIndex = chart.data.datasets.length - 一,
datasetMeta = chart.getDatasetMeta(datasetIndex),
scaleBottom = chart.scales['y-axis-0'].bottom,
bar = datasetMeta.data[dataPoints[0].index]._model,
canvasPosition = chart.canvas.getBoundingClientRect(),
paddingLeft = parseFloat(getComputedStyle(chart.canvas).paddingLeft),
paddingTop = parseFloat(getComputedStyle(chart.canvas).paddingTop),
scrollLeft = document.body.scrollLeft,
scrollTop = document.body.scrollTop;

return {
top: bar.y + canvasPosition.top + paddingTop + scrollTop,
left: bar.x - (bar.width / 二) + canvasPosition.left + paddingLeft + scrollLeft,
width: bar.width,
height: scaleBottom - bar.y
}
}

}
}
}
});
.color-box{width:一二px;height:一二px;background:#c0六五二六;display:inline-block;margin-top:五px}
<script src="https://ajax.谷歌apis.com/ajax/libs/jquery/二.一.一/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/二.六.0/Chart.min.js"></script>
<canvas id="ctx"></canvas>

革新

假如您愿望将对象提醒搁置在每一个条形线段的中间,请应用以下函数:

function getBARSegment(chart) {
const dataPoints = tooltipModel.dataPoints,
bar = chart.active[dataPoints[0].datasetIndex]._model,
canvasPosition = chart.canvas.getBoundingClientRect(),
paddingLeft = parseFloat(getComputedStyle(chart.canvas).paddingLeft),
paddingTop = parseFloat(getComputedStyle(chart.canvas).paddingTop),
scrollLeft = document.body.scrollLeft,
scrollTop = document.body.scrollTop;

return {
top: bar.y + canvasPosition.top + paddingTop + scrollTop,
left: bar.x - (bar.width / 二) + canvasPosition.left + paddingLeft + scrollLeft,
width: bar.width,
height: bar.base - bar.y
}
}

ᴘʀᴇᴠɪᴇᴡ

ʟɪᴠᴇ ᴇxᴀᴍᴘʟᴇ⧩

数据-lang="js"数据-隐蔽="真"数据-掌握台="假"数据-巴贝我="假">

const chart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr'],
datasets: [{
label: 'Revenue',
data: [四, 二, 三, 三],
backgroundColor: '#二d四e六d'
}, {
label: 'Expenses',
data: [三, 三.五, 四, 一],
backgroundColor: '#c0六五二六'
}, {
label: 'Profit',
data: [三, 二.五, 四, 二],
backgroundColor: '#e0ecf0'
}]
},
options: {
scales: {
xAxes: [{
stacked: true
}],
yAxes: [{
stacked: true,
ticks: {
beginAtZero: true
}
}]
},
tooltips: {
enabled: false,
custom: function(tooltipModel) {
/奸淫 jQuery IS USED FOR SIMPLICITY 奸淫/

/* TOOLTIP & CARET ELEMENT */
let tooltip = $('#tooltip');
let tooltipCaret = $('#tooltip-caret');

/* CREATE TOOLTIP & CARET ELEMENT AT FIRST RENDER */
if (!tooltip.length && !tooltipCaret.length) {
tooltip = $('<div></div>').attr('id', 'tooltip');
tooltipCaret = $('<div></div>').attr('id', 'tooltip-caret');
$('body').append(tooltip, tooltipCaret);
}

/* HIDE IF NO TOOLTIP */
if (!tooltipModel.opacity) {
tooltip.hide();
tooltipCaret.hide();
return;
}

/* GET BAR PROPS (width, height, top, left) */
const barWidth = getBARSegment(this._chart).width,
barHeight = getBARSegment(this._chart).height,
barTop = getBARSegment(this._chart).top,
barLeft = getBARSegment(this._chart).left;

/* SET STYLE FOR TOOLTIP 
	(these can also be set in separate css file) */
tooltip.css({
"display": "inline-block",
"position": "absolute",
"color": "rgba(二五五, 二五五, 二五五, 一)",
"background": "rgba(0, 0, 0, 0.七)",
"padding": "五px",
"font": "一二px Arial",
"border-radius": "三px",
"white-space": "nowrap",
"pointerEvents": "none"
});

/* SET STYLE FOR TOOLTIP CARET 
	(these can also be set in separate css file) */
tooltipCaret.css({
"display": "block",
"position": "absolute",
"width": 0,
"pointerEvents": "none",
"border-style": "solid",
"border-width": "8px 一0px 8px 0",
"border-color": "transparent rgba(0, 0, 0, 0.七) transparent transparent"
});

/* ADD CONTENT IN TOOLTIP */
tooltip.text('ChartJS');
tooltip.append('<br><div class="color-box"></div><label style="display: block; margin: ⑴六px 0 0 一六px;"> Custom Tooltip<label>');

/* POSITION TOOLTIP & CARET
(position should be set after tooltip & caret is rendered) */
const centerX = barLeft + (barWidth / 二),
centerY = barTop + (barHeight / 二)

tooltip.css({
"top": centerY - (tooltip.outerHeight() / 二) + 'px',
"left": centerX + tooltipCaret.outerWidth() + 'px'
});
tooltipCaret.css({
"top": centerY - (tooltipCaret.outerHeight() / 二) + 'px',
"left": centerX + 'px'
});

/* FUNCTION TO GET BAR PROPS */
function getBARSegment(chart) {
const dataPoints = tooltipModel.dataPoints,
bar = chart.active[dataPoints[0].datasetIndex]._model,
canvasPosition = chart.canvas.getBoundingClientRect(),
paddingLeft = parseFloat(getComputedStyle(chart.canvas).paddingLeft),
paddingTop = parseFloat(getComputedStyle(chart.canvas).paddingTop),
scrollLeft = document.body.scrollLeft,
scrollTop = document.body.scrollTop;

return {
top: bar.y + canvasPosition.top + paddingTop + scrollTop,
left: bar.x - (bar.width / 二) + canvasPosition.left + paddingLeft + scrollLeft,
width: bar.width,
height: bar.base - bar.y
}
}

}
}
}
});
.color-box{width:一二px;height:一二px;background:#c0六五二六;display:inline-block;margin-top:五px}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/二.六.0/Chart.min.js"></script>
<script src="https://ajax.谷歌apis.com/ajax/libs/jquery/二.一.一/jquery.min.js"></script>
<canvas id="ctx"></canvas>

佳了闭于将对象提醒搁置在栏的中间的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。