[英]Not being able to display value from json data into chart js
I am using react js.我正在使用反应 js。 I want to make a chart using
chartjs
from the data I fetched from the api.我想使用从
chartjs
获取的数据中的 chartjs 制作图表。 Here, I just want to plot the chart between the below two arrays.在这里,我只想plot下面两个arrays之间的图表。
symbolsdateforchart: []
closingdateforchart: []
But, I am not sure how to use the value of these (please have a look at my code) since it has not been assigned yet, and the value get assigned in componentDidMount()
and I making my chart inside this.state
.但是,我不确定如何使用这些值(请查看我的代码),因为它尚未分配,并且在
componentDidMount()
中分配了值,我在this.state
中制作了我的图表。 Is there a better way to make the chart using chartjs
between the above two array I mentioned.有没有更好的方法在我提到的上述两个数组之间使用
chartjs
制作图表。 Or can I make this work somehow?或者我可以以某种方式完成这项工作吗? Any help is appreciated.
任何帮助表示赞赏。
My code:我的代码:
import React from "react";
import { Line } from "react-chartjs-2";
import Chart from "./Chart";
export default class Symbols extends React.Component {
constructor(props) {
super(props);
this.state = {
items: [],
isLoaded: false,
symbolsdateforchart: [],
closingdateforchart: [],
search: "",
symbol: this.props.location.symbol,
//here I am making my chart
chartData: {
labels: ["how to add the data of symbolsdateforchart here"], //and here I know the data is not yet assigned to symbolsdateforchart: [], so not sure how to make it work
datasets: [
{
label: "Closing price",
data: ["how to add the data of closingdateforchart here"], //and here I know the data is not yet assigned to closingdateforchart: [], so not sure how to make it work
fill: false,
backgroundColor: [
"rgba(255,99,132,0.6)",
"rgba(54,162,235,0.6)",
"rgba(255,206,86,0.6)",
"rgba(75,192,192,0.6)",
"rgba(153,102,255,0.6)",
"rgba(255,159,64,0.6)",
"rgba(255,99,132,0.6)",
],
},
],
},
};
}
componentDidMount() {
let symbolsdate = [];
let closingprice = [];
fetch(`http://131.181.190.87:3001/history?symbol=${this.state.symbol}`)
.then((res) => res.json())
.then((json) => {
console.log(json);
for (const dataobj of json) {
let tempsymbolsDate = dataobj.timestamp.split("T")[0];
let tempclosingPrice = dataobj.close;
let tempArray = tempsymbolsDate.split("-");
tempsymbolsDate =
tempArray[2] + "/" + tempArray[1] + "/" + tempArray[0];
symbolsdate.push(tempsymbolsDate);
closingprice.push(tempclosingPrice);
}
this.setState({
isLoaded: true,
items: json,
//here the value get assigned for them
symbolsdateforchart: symbolsdate,
closingdateforchart: closingprice,
});
});
}
render() {
return (
//here I just print the table using the api I fetched in omponentDidMount()
);
}
}
In componentDidMount()
you can update state variable chartData
as在
componentDidMount()
中,您可以将 state 变量chartData
更新为
this.setState({
isLoaded: true,
items: json,
chartData: {
labels: symbolsdate,
datasets: [{...this.state.chartData.datasets, data: closingprice}]
}
});
Better is to separate dynamic part from static.最好将动态部分与 static 分开。 Store static data like
backgroundColor
outside of state & provide statically in HTML of render()
while, use state variables for dynamic data.在 state 之外存储 static 数据(如
backgroundColor
)并在render()
的 HTML 中静态提供,同时将 Z9ED39E2EA93142EF578B5766 变量用于动态数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.