我正在学习reactjs,正在尝试绘制条形图。因此,我试图绘制随时间变化且看起来像这样的人口种族图。Population race bar chart
用于绘制图形,我正在使用chart-race-react。但似乎我在代码中做错了。
我的代码:
import React, { Component } from 'react';
import BarChart from 'chart-race-react';
import './App.css';
const API_URL = 'https://codejudge-question-artifacts.s3.ap-south-1.amazonaws.com/poplution-countries-yearwise.json';
const randomColor = () => {
return `rgb(${255 * Math.random()}, ${255 * Math.random()}, ${255})`;
}
class App extends Component {
constructor(props) {
super(props);
this.state = {
countries: []
}
}
componentDidMount() {
fetch(API_URL)
.then(v => v.json())
.then(array => {
const dataForVisualisation = {};
array.forEach(element => {
const countryName = element['Country Name'];
if (!dataForVisualisation[countryName]) {
dataForVisualisation[countryName] = [];
}
dataForVisualisation[countryName].push(element.Value);
});
this.setState({countries: dataForVisualisation});
})
}
render() {
return (
<div style={{width: "500px"}}>
<BarChart
start={true}
data = {this.state.countries}
len = {this.state.countries[Object.keys(this.state.countries)[0]].length}
keys = {Object.keys(this.state.countries)}
timeout={400}
delay={100}
colors = {Object.keys(this.state.countries).reduce((res, item) => ({
...res,
...{ [item]: randomColor() }
}), {})}
labels = {Object.keys(this.state.countries).reduce((res, item, idx) => {
return{
...res,
...{[item]: (
<div style={{textAlign:"center",}}>
<div>{item}</div>
</div>
)}
}}, {})}
timeline = {Array(20).fill(0).map((itm, idx) => idx + 1)}
timelineStyle={{
textAlign: "center",
fontSize: "50px",
color: "rgb(148, 148, 148)",
marginBottom: "100px"
}}
textBoxStyle={{
textAlign: "right",
color: "rgb(133, 131, 131)",
fontSize: "30px",
}}
barStyle={{
height: "60px",
marginTop: "10px",
borderRadius: "10px",
}}
width={[15, 75, 10]}
maxItems = {this.state.countries.length}
/>
</div>
);
}
}
export default App;
我遇到TypeError: this.state.countries[Object.keys(...)[0]] is undefined
错误,因为此处的数据未定义。我应该如何更改代码以使输出如gif
所示?是否有其他图书馆可以这样做?
您应该等到国家填补之前再尝试渲染任何东西。例如,仅当countrys数组具有至少一个元素时,才可以渲染BarChart组件:
[...]
{this.state.countries.length > 0 && <BarChart
start={true}
data = {this.state.countries}
len = {this.state.countries[Object.keys(this.state.countries)[0]].length}
keys = {Object.keys(this.state.countries)}
timeout={400}
delay={100}
colors = {Object.keys(this.state.countries).reduce((res, item) => ({
...res,
...{ [item]: randomColor() }
}), {})}
labels = {Object.keys(this.state.countries).reduce((res, item, idx) => {
return{
...res,
...{[item]: (
<div style={{textAlign:"center",}}>
<div>{item}</div>
</div>
)}
}}, {})}
timeline = {Array(20).fill(0).map((itm, idx) => idx + 1)}
timelineStyle={{
textAlign: "center",
fontSize: "50px",
color: "rgb(148, 148, 148)",
marginBottom: "100px"
}}
textBoxStyle={{
textAlign: "right",
color: "rgb(133, 131, 131)",
fontSize: "30px",
}}
barStyle={{
height: "60px",
marginTop: "10px",
borderRadius: "10px",
}}
width={[15, 75, 10]}
maxItems = {this.state.countries.length}
/>}
{this.state.countries.length === 0 && <span>Loading...</span>}
[...]