[英]In react.js with chart.js, I can't access to my data in a componant
I have a simple chart with chart.js in React.js app.我在 React.js 应用程序中有一个带有 chart.js 的简单图表。 I would like to acces at my data in my component.
我想访问我的组件中的数据。 For create a simple button to add data in my chart.
创建一个简单的按钮来在我的图表中添加数据。 But he send me an error.
但是他给我发了一个错误。 Could you please help me and explain me to acces to my data in an other componant.
您能否帮助我并解释我在其他组件中访问我的数据。 Thx you so much.
谢谢你。
My componant BarChart.js我的组件 BarChart.js
import React from "react"
import { Bar } from 'react-chartjs-2'
const BarChart = () => {
return (
<div>
<Bar
data={{
labels: ['Paris', 'Boston', 'Berlin', 'Dublin', 'Barcelone', 'Lisbonne'],
datasets: [{
label: 'Populations en millions',
data: [2.2, 0.7, 3.8, 1.4, 1.6, 0.6],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
}}
height={500}
width={500}
options={{
maintainAspectRatio: false,
scales: {
yAxes: [
{
ticks: {
beginAtZero: true,
},
},
],
},
}}
/>
</div>
)
}
export default BarChart
My componant ButtonChart.js我的组件 ButtonChart.js
import React from "react"
import BarChart from "./BarChart";
const ButtonChart = () => {
return (
<div className="inputBox">
<input type="number" className="inputAdd" placeholder="Valeur" id="inputData"/>
<input type="text" className="inputAdd" placeholder="Année" id="label"/>
<button onClick={updateChart()} id="insertData">Ajouter une donnée</button>
</div>
)
}
function updateChart() {
const inputData = document.getElementById('inputData');
BarChart.data.datasets[0].data.push(inputData.value);
}
export default ButtonChart
Accessing BarChart.data
seems wrong as, BarChart
is basically a functional React component.访问
BarChart.data
似乎是错误的,因为BarChart
基本上是一个功能性 React 组件。
What you want is probably this:你想要的可能是这样的:
data
array.data
数组。data
in a BarChart
BarChart
中显示该data
button
to the user such that when user clicks that button, a new row is added into that data
array.button
,以便当用户单击该按钮时,会在该data
数组中添加一个新行。data
array changes, you want the BarChart
to refresh and display the updated data
array.data
数组发生变化时,您希望BarChart
刷新并显示更新后的data
数组。 If this is the case, then I would recommend checking the Lifting State Up in the React docs.如果是这种情况,那么我建议您查看 React 文档中的Lifting State Up 。 It is pretty much the same use case here.
这里的用例几乎相同。 You have a single data source shared across 2 components.
您有一个跨 2 个组件共享的数据源。
Also, check the usage of React hooks for checking state usage: https://reactjs.org/docs/hooks-state.html另外,检查 React 钩子的使用以检查 state 的使用: https://reactjs.org/docs/hooks-state.html
Here is a pseudocode you can try:这是您可以尝试的伪代码:
ParentComponent = () => {
// 1. Create your data state here using React Hooks
const [data, setData] = useState([someInitialData]);
return (
<div>
// 2. BarChart now relies on the data in the state
// Whenever the data updates, the chart refreshes
<BarChart data={data}/>
// 3. Pass setData to the component and trigger it with the
// button's onClick handler
<ButtonChart updateData={setData}/>
</div>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.