【发布时间】:2020-05-13 09:46:52
【问题描述】:
嘿,我正在尝试使用来自反应本机的数据在线 json 数据的胜利来制作图表。该组件的代码如下所示
export default class Dashboard extends Component {
static navigationOption = {
title: 'DashboardScreen'
}
state = {
getValue: '',
dataSource:[],
isLoading: true
}
componentDidMount() {
const url = 'myurl';
fetch(url, {
method: 'GET',
headers: new Headers({
'Content-Type' : 'application/json',
})
})
.then((response)=> response.json() )
.then((responseJson) => {
console.log(responseJson.Views)
this.setState({
dataSource: responseJson,
isLoading: false
})
})
.catch((Error) => {
console.log(Error)
})
}
render() {
return(
<View style = {styles.container}>
<VictoryChart minDomain={{ y: 0, x:0 }} width={350} theme={VictoryTheme.material}>
<VictoryBar
style={{
data: { stroke: "#c43a31" },
parent: { border: "1px solid #ccc"}
}}
data={[
{ x: 1, y: 1 },
{ x: 2, y: dataSource[0].views},
{ x: 3, y: dataSource[1].views},
{ x: 4, y: dataSource[2].views},
{ x: 5, y: dataSource[3].views}
]}>
</VictoryBar>
</VictoryChart>
</View>
)
}
}
我认为 fetch 已经使 json 信息成为 dataSource 变量中的一个数组,并且可以用作胜利中的数据,但我总是遇到诸如 referenceError: Can't find variable 'dataSource' 之类的错误
所以,如果有人帮助我编写这段代码并建议如何将 json 数据制作成图表,那就太好了
【问题讨论】:
标签: react-native fetch victory-charts