英文:
How to get element on click in Chart Js?
问题
我在我的React应用中添加了Chart Js来显示柱状图的数据。代码能够正常工作,我可以正确显示数据,唯一的问题是,如果其中一个数据值太高,柱状图将只显示它。
如您在这张图片中所看到的,如果一月份的数据是100,000,而其他月份的数据小于10,000,我们只能看到一月份的数据。当我点击一月份的数据时,我得到的是索引0,我需要得到其他月份所有元素的索引,例如当我点击二月份的空白处时,我应该得到索引1等。
我的代码如下:
import React, { useRef } from "react";
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
BarElement,
Title,
Tooltip,
Legend
} from "chart.js";
import { Bar, getElementAtEvent } from "react-chartjs-2";
ChartJS.register(
CategoryScale,
LinearScale,
BarElement,
Title,
Tooltip,
Legend
);
export const options = {
responsive: true,
plugins: {
legend: {
position: "top"
},
title: {
display: true,
text: "Chart.js柱状图"
}
}
};
const labels = ["一月", "二月", "三月", "四月", "五月", "六月", "七月"];
export const data = {
labels,
datasets: [
{
label: "数据集1",
data: [100000, 500, 40],
backgroundColor: "rgba(255, 99, 132, 0.5)"
}
]
};
export default function App() {
const barRef = useRef();
const handleGetData = (event) => {
console.log(getElementAtEvent(barRef.current, event)[0]?.index);
};
return (
<Bar ref={barRef} options={options} data={data} onClick={handleGetData} />
);
}
在这种情况下,如何获取二月份的索引?如果我点击白色空白处,getElementAtEvent(barRef.current, event)[0]?.index
将返回undefined。
英文:
I'm adding Chart Js in my react app to display data as bar chart. The code works and I can display data correctly, the only issue I'm facing is that if one of the data values is too high, bar chart will only display it
As you can see on this image, if data for January is 100,000 and for other months it's less than 10,000, we can only see data for January. When I click on element for data (January) I get index 0, I need to get index for all elements for other months, for example when I click on white space for February I should get index 1 etc.
Code Sandbox - link
My code:
import React, { useRef } from "react";
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
BarElement,
Title,
Tooltip,
Legend
} from "chart.js";
import { Bar, getElementAtEvent } from "react-chartjs-2";
ChartJS.register(
CategoryScale,
LinearScale,
BarElement,
Title,
Tooltip,
Legend
);
export const options = {
responsive: true,
plugins: {
legend: {
position: "top"
},
title: {
display: true,
text: "Chart.js Bar Chart"
}
}
};
const labels = ["January", "February", "March", "April", "May", "June", "July"];
export const data = {
labels,
datasets: [
{
label: "Dataset 1",
data: [100000, 500, 40],
backgroundColor: "rgba(255, 99, 132, 0.5)"
}
]
};
export default function App() {
const barRef = useRef();
const handleGetData = (event) => {
console.log(getElementAtEvent(barRef.current, event)[0]?.index);
};
return (
<Bar ref={barRef} options={options} data={data} onClick={handleGetData} />
);
}
How can I get the index for February in this case? If I click on white space, getElementAtEvent(barRef.current, event)[0]?.index
returns undefined.
答案1
得分: 1
条形元素的索引对应于其数值 x
值,因此要获得与单击事件对应的索引,只需将事件的 clientX
从像素转换为 "真实" 值。
如果我们获得了 Chart
对象,如下所示:
const chart = barRef.current;
我们可以使用以下代码获取单击事件的 x 值:
const xClick = chart.scales.x.getValueForPixel(event.nativeEvent.offsetX);
这将在 x 轴上从 0 到您为 x 轴定义的分类数之间进行变化。
要获取 BarElement
实例(并查看该索引处是否存在柱形图),您可以使用以下代码:
const barElement = chart.getDatasetMeta(0).data[xClick];
如果没有柱形图,则该值可能为未定义。这里 是对您的 CodeSandbox 进行了这些更改的分支。
英文:
The index of the bar element corresponds to its numeric x
value,
so to get the index corresponding to a click event you just have to convert the event's clientX
from pixel to "real" values.
If we get hold of the Chart
object, as
const chart = barRef.current;
we can get the x value for a click event using:
const xClick = chart.scales.x.getValueForPixel(event.nativeEvent.offsetX);
this goes from 0 to how many categories you defined for the x axis.
To get the BarElement
instance (and to see that there is a bar at that index) you can use
const barElement = chart.getDatasetMeta(0).data[xClick];
which might be undefined if there's no bar there. Here's a fork of your codesandbox with these changes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论