我正在做一个Java项目,我们使用GsonBuilder用ChartJ创建图表。我的JSON看起来如下:
{
"data": {
"datasets": [
{
"data": [
{
"x": "05/12/2022 07:00:00",
"y": "1",
"value": "value A"
},
{
"x": "05/13/2022 14:08:00",
"y": "1",
"value": "value B"
}
],
"label": "Bacteria",
"borderColor": "#a1c450",
"yAxisID": "Values",
}
]
},
"options": {
"plugins": {
"tooltip": {
"mode": "single",
"callbacks": {
"label": "function(tooltipItem, data) {return tooltipItem.value;}"
},
"enabled": true
}
},
"scales": {
"yAxes": [
{
"scaleLabel": {
"labelString": "Values",
"display": false
},
"display": false,
"id": "Values",
"position": "right"
}
],
"xAxes": [
{
"display": true,
"type": "time"
}
]
}
},
"type": "scatter"
}工具提示不是显示“值A/B”,而是显示相应数据项的"x,y“。
我尝试过返回data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index].value;,但没有成功。
发布于 2022-05-18 10:30:03
这是因为您使用的是V2 of Chart.js,而工具提示使用的是V3语法。这不管用。V2中工具提示的选项放置在不同的位置:
const options = {
type: 'scatter',
data: {
datasets: [{
label: '# of Votes',
data: [{
x: 0.2505597250394178,
y: 12
},
{
x: 0.1629651642029184,
y: 19
},
{
x: 0.6825862858121792,
y: 3
},
{
x: 0.39821027356939087,
y: 5
},
{
x: 0.1088502186196103,
y: 2
},
{
x: 0.7836566822098099,
y: 3
}
],
backgroundColor: 'orange'
}]
},
options: {
tooltips: {
callbacks: {
label: (ttItem) => (ttItem.value)
}
},
scales: {
yAxes: [{
ticks: {
reverse: false
}
}]
}
}
}
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/2.9.4/Chart.js"></script>
</body>
https://stackoverflow.com/questions/72281650
复制相似问题