reactjs - 如何在 Chart js 2 折线图中的点上显示 X 和 Y 标签
问题描述
当我悬停一个点时,我在 React js 中使用 chartjs 2 制作了 X 和 Y 轴的折线图,我想在框中同时显示 x 和 y 轴,但我只有 X 轴 这是代码。我用过散点图。我希望折线图点悬停与散点图相同。请检查并让我知道解决方案
<Line
options={{
title: {
display: true,
},
legend: {
display: false,
},
responsive: true,
tooltips: {
mode: 'index',
intersect: false,
},
hover: {
mode: 'index',
intersect: false,
},
elements: {
line: {
fill: false,
borderWidth: 5,
},
},
scales: {
yAxes: [
{
ticks: {
fontColor: 'black',
},
labels: [
{ x: 80, y: 90 },
{ x: 81, y: 29 },
{ x: 56, y: 36 },
{ x: 55, y: 25 },
{ x: 40, y: 18 },
].map((z) => {
return z.y;
}),
scaleLabel: {
display: true,
fontSize: 15,
fontColor: 'black',
fontStyle: 'bold',
labelString: 'True Positive Rate',
},
},
],
xAxes: [
{
ticks: {
fontColor: 'black',
},
labels: [
{ x: 80, y: 90 },
{ x: 81, y: 29 },
{ x: 56, y: 36 },
{ x: 55, y: 25 },
{ x: 40, y: 18 },
].map((z) => {
return 'X-axis' + z.x;
}),
scaleLabel: {
display: true,
fontSize: 15,
fontColor: 'black',
fontStyle: 'bold',
labelString: 'False Positive Rate',
},
},
],
},
}}
height={150}
data={{
datasets: [
{
fill: false,
borderColor: '#EC932F',
backgroundColor: '#212F3D',
pointBorderColor: '#B2BABB',
pointBackgroundColor: '#D4AC0D',
pointHoverBackgroundColor: '#D4AC0D',
pointHoverBorderColor: 'black',
lineTension: 0.1,
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBorderWidth: 2,
pointRadius: 3,
pointHitRadius: 10,
data: [
{ x: 80, y: 90 },
{ x: 81, y: 29 },
{ x: 56, y: 36 },
{ x: 55, y: 25 },
{ x: 40, y: 18 },
],
},
],
}}
/>
解决方案
试试这个,
<Line
options={{
title: {
display: true
},
legend: {
display: false
},
responsive: true,
tooltips: {
mode: "index",
intersect: false,
callbacks: { //Added callbacks for label
title: () => {
return "";
},
label: (tooltipItems, data) => {
return "(" + tooltipItems.xLabel + "," + tooltipItems.yLabel + ")";
}
}
},
hover: {
mode: "index",
intersect: false
},
elements: {
line: {
fill: false,
borderWidth: 5
}
},
scales: {
yAxes: [
{
ticks: {
fontColor: "black"
},
labels: [
{ x: 80, y: 90 },
{ x: 81, y: 29 },
{ x: 56, y: 36 },
{ x: 55, y: 25 },
{ x: 40, y: 18 }
].map((z) => {
return z.y;
}),
scaleLabel: {
display: true,
fontSize: 15,
fontColor: "black",
fontStyle: "bold",
labelString: "True Positive Rate"
}
}
],
xAxes: [
{
ticks: {
fontColor: "black"
},
labels: [
{ x: 80, y: 90 },
{ x: 81, y: 29 },
{ x: 56, y: 36 },
{ x: 55, y: 25 },
{ x: 40, y: 18 }
].map((z) => {
return z.x; // Changed this line
}),
scaleLabel: {
display: true,
fontSize: 15,
fontColor: "black",
fontStyle: "bold",
labelString: "False Positive Rate"
}
}
]
}
}}
height={150}
data={{
datasets: [
{
fill: false,
borderColor: "#EC932F",
backgroundColor: "#212F3D",
pointBorderColor: "#B2BABB",
pointBackgroundColor: "#D4AC0D",
pointHoverBackgroundColor: "#D4AC0D",
pointHoverBorderColor: "black",
lineTension: 0.1,
borderCapStyle: "butt",
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: "miter",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBorderWidth: 2,
pointRadius: 3,
pointHitRadius: 10,
data: [
{ x: 80, y: 90 },
{ x: 81, y: 29 },
{ x: 56, y: 36 },
{ x: 55, y: 25 },
{ x: 40, y: 18 }
]
}
]
}}
/>
推荐阅读
- python - 如何建立抛硬币三次的贝叶斯模拟模型
- hive - 用于 hive-hbase 集成的 Hive 列的复合键
- php - 通过 api 添加带有 child_attachments 的 Facebook 帖子时出现“无法使用 URL”错误
- firefox - vuetify 设置后在 Firefox 上显示空白页
- javascript - 字符串转换为整数
- mysql - 从mysql中的unix格式转换后比较两个日期
- spring-boot - 无法解析“javax.persistence.EntityManager”的 bean
- sparql - GraphDB Free 8.8 Sparql INSERT 返回 400。不支持 INSERT?
- node.js - 如何使用 azure bot 框架向 Skype 组发送通知消息?
- sas - 折叠大型数据集,同时有条件地保留一些缺失值