有没有办法在 chart.js V3.7.0 工具提示上应用 css 而无需自定义或外部?
Is there a way to apply css on chart.js V3.7.0 tooltip without it being custom or external?
我有这个工具提示:
而且我想给它一些余地,以便更多地使用 top
。示例:
这个是工具提示代码:
tooltip: {
caretPadding: 3,
caretSize: 0,
displayColors: false,
backgroundColor: 'rgba(45,132,180,0.8)',
bodyFontColor: 'rgb(255,255,255)',
callbacks: {
title: () => {
return
},
label: (ttItem) => ( `${ttItem.parsed.y} ppm` ),
afterBody: (ttItems) => (ttItems[0].label)
}
},
问题:
如何给工具提示 css 使其更接近顶部?
您不能使用 CSS,因为它不是 HTML 样式,因为它是在 canvas 上绘制的。
您可以为 chart.js.
的工具提示提供自定义定位器
在下面的示例中,您可以看到如何操作。增加我在顶部声明的 yOffset
变量会将工具提示移到点上方,增加我在顶部声明的 xOffset
变量会使工具提示更向右移动:
const yOffset = 10;
const xOffset = 00;
Chart.Tooltip.positioners.custom = function(items) {
const pos = Chart.Tooltip.positioners.average(items);
// Happens when nothing is found
if (pos === false) {
return false;
}
const chart = this.chart;
return {
x: pos.x + xOffset,
y: pos.y - yOffset,
yAlign: 'bottom',
};
}
const options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1
}
]
},
options: {
plugins: {
tooltip: {
caretPadding: 3,
caretSize: 0,
displayColors: false,
backgroundColor: 'rgba(45,132,180,0.8)',
bodyFontColor: 'rgb(255,255,255)',
callbacks: {
title: () => {
return
},
label: (ttItem) => (`${ttItem.parsed.y} ppm`),
afterBody: (ttItems) => (ttItems[0].label)
},
position: 'custom'
}
}
}
}
const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.0/chart.js"></script>
</body>
我有这个工具提示:
而且我想给它一些余地,以便更多地使用 top
。示例:
这个是工具提示代码:
tooltip: {
caretPadding: 3,
caretSize: 0,
displayColors: false,
backgroundColor: 'rgba(45,132,180,0.8)',
bodyFontColor: 'rgb(255,255,255)',
callbacks: {
title: () => {
return
},
label: (ttItem) => ( `${ttItem.parsed.y} ppm` ),
afterBody: (ttItems) => (ttItems[0].label)
}
},
问题:
如何给工具提示 css 使其更接近顶部?
您不能使用 CSS,因为它不是 HTML 样式,因为它是在 canvas 上绘制的。
您可以为 chart.js.
的工具提示提供自定义定位器
在下面的示例中,您可以看到如何操作。增加我在顶部声明的 yOffset
变量会将工具提示移到点上方,增加我在顶部声明的 xOffset
变量会使工具提示更向右移动:
const yOffset = 10;
const xOffset = 00;
Chart.Tooltip.positioners.custom = function(items) {
const pos = Chart.Tooltip.positioners.average(items);
// Happens when nothing is found
if (pos === false) {
return false;
}
const chart = this.chart;
return {
x: pos.x + xOffset,
y: pos.y - yOffset,
yAlign: 'bottom',
};
}
const options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1
}
]
},
options: {
plugins: {
tooltip: {
caretPadding: 3,
caretSize: 0,
displayColors: false,
backgroundColor: 'rgba(45,132,180,0.8)',
bodyFontColor: 'rgb(255,255,255)',
callbacks: {
title: () => {
return
},
label: (ttItem) => (`${ttItem.parsed.y} ppm`),
afterBody: (ttItems) => (ttItems[0].label)
},
position: 'custom'
}
}
}
}
const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.0/chart.js"></script>
</body>