gpt4 book ai didi

javascript - 图表.js v3 : Tooltip callback doesn't identify clicked dataset of stacked bar chart

转载 作者:行者123 更新时间:2023-12-05 04:47:21 25 4
gpt4 key购买 nike

在 Chart.js v2 中,ChartTooltipItem[] 的 datasetIndex 属性标识单击了堆叠条形图的哪个部分。这允许为堆叠条形图的每个部分自定义工具提示内容。

在 v3 中,TooltipItem[] 提供数据集但不识别单击了哪个数据集。每个 TooltipItem 都有一个 datasetIndex 字段,但它只是匹配 TooltipItem[] 中的索引,而不是识别被点击的段。

有没有人在 V3 工具提示回调中找到一个字段来识别点击了堆叠条形图的哪个部分?或者这个功能在 v3 重写中丢失了吗?

最佳答案

它工作正常,唯一不同的是它似乎在 v2 中默认为 point 模式,而现在它使用 index 模式,如果你把它改回 point 它按预期工作:

var options = {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: 'red'
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
backgroundColor: 'blue'
}
]
},
options: {
plugins: {
tooltip: {
mode: 'point',
callbacks: {
beforeBody: (ttItems, x) => {
ttItems.forEach((ttItem) => {
console.log('BeforeBody: ', ttItem.datasetIndex, ttItem.dataIndex)
})
},
afterBody: (ttItems, x) => {
ttItems.forEach((ttItem) => {
console.log('AfterBody: ', ttItem.datasetIndex, ttItem.dataIndex)
})
}
}
}
},
scales: {
y: {
stacked: true
},
x: {
stacked: true
}
}
}
}

var 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.4.1/chart.js"></script>
</body>

关于javascript - 图表.js v3 : Tooltip callback doesn't identify clicked dataset of stacked bar chart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68514265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com