希望在 react-vis 中开始一些图形,并且想知道将数据从 json 推送到状态的最佳方法是什么,以便我可以从图形中访问状态数据。下面是我的测试json:
[
{
"day": "1",
"flights":"58",
"passengers": "6076"
},
{
"day": "2",
"flights": "78",
"passengers": "7089"
},
{
"day": "3",
"flights": "101",
"passengers": "9760"
}
]
我本质上想将“day”和“flights”推送到数据数组中,以模仿 react-vis 提供的示例。下面是我的代码:
应用程序.js
//import logo from './logo.svg';
//import './App.css';
import React, { Component } from 'react';
import my_data from './data/csvjson.json';
import {XYPlot, XAxis, YAxis, HorizontalGridLines, LineSeries,
VerticalGridLines, LineMarkSeries} from 'react-vis';
import '../node_modules/react-vis/dist/style.css';
class App extends Component {
constructor(props) {
super(props);
this.state = {
data:[]
}
}
//react vis documentation points to data being an object like so:
//sample data
//data:[
// {x: 0, y: 8},
// {x: 1, y: 5},
// {x: 2, y: 4}
// ]
render() {
return (
<div>
<XYPlot width={400} height={300}><XAxis/><YAxis/>
<HorizontalGridLines />
<VerticalGridLines />
<LineMarkSeries data={this.state.data} />
</XYPlot>
</div>
);
}
}
export default App;