gpt4 book ai didi

javascript - 如何加载动态数据到react-native-chart-kit?

转载 作者:行者123 更新时间:2023-11-29 15:07:29 25 4
gpt4 key购买 nike

实际上我会将数据动态加载到 react-native-chart-kit但我找不到任何关于我该怎么做的文档。

我有一个 API 可以提供每个月有一些收入的数据,响应如下所示

[{"IMPORTO":722.51},{"IMPORTO":911},{"IMPORTO":1188.53},{"IMPORTO":12},{"IMPORTO":390.65},{"IMPORTO":54.98}]

我还有一种方法可以获取该数据,但我不知道如何将其用作图表的数据集。

这是我的 Home.JS 的代码,我在其中构建图表,GetData 是 API 数据的获取函数:

  import React from 'react';
import {Text,View,Dimensions} from 'react-native';
import {LineChart} from "react-native-chart-kit";

const data = {
labels: ["Gen", "Feb", "Mar", "Apr", "Mag", "Giu", "Lug", "Ago", "Set", "Ott", "Nov", "Dic"],
datasets: [
{
data: []
}
]
}
const chartConfig = {
backgroundColor: "#e26a00",
backgroundGradientFrom: "#fb8c00",
backgroundGradientTo: "#ffa726",
decimalPlaces: 2, // optional, defaults to 2dp
color: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
labelColor: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
style: {
borderRadius: 16
},
propsForDots: {
r: "6",
strokeWidth: "2",
stroke: "#ffa726"
}
}

export default class Home extends React.Component {
constructor(props){
super(props);
this.state = {
isLoading: true,
dataSource: []
};

}

componentDidMount() {
this.GetData();
}

GetData = () => {
return fetch('http://192.168.100.158:3000/data')
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
dataSource: responseJson
});
})
.catch((error) =>{
console.error(error);
});
}

render() {
data.datasets[0].data = this.state.dataSource.map(value => value. IMPORTO);

return (
<View>
<LineChart
data={data}
width={Dimensions.get("window").width} // from react-native
height={220}
yAxisLabel={"$"}
chartConfig={chartConfig}
bezier
style={{
marginVertical: 8,
borderRadius: 16
}}
/>
</View>
);
}
}

我尝试设置 data = { this.state.dataSource.IMPORTO } 但它不起作用。

最佳答案

在您的渲染方法中,将数据更改为来自状态。

render() {

data.datasets[0].data = this.state.dataSource.map(value => value.IMPORTO);

return (
<View>
<LineChart
data={data}
width={Dimensions.get("window").width} // from react-native
height={220}
yAxisLabel={"$"}
chartConfig={chartConfig}
bezier
style={{
marginVertical: 8,
borderRadius: 16
}}
/>
</View>
);
}

现在,当异步调用“GetData()”完成时,它将更新状态,组件将使用新数据重新呈现。

您还可以在状态本身中设置初始值:

constructor(props) {
super(props);
this.state = {
isLoading: true,
data: {
labels: ["Gen", "Feb", "Mar", "Apr", "Mag", "Giu", "Lug", "Ago", "Set", "Ott", "Nov", "Dic"],
datasets: [
{
data: []
}
]
}
}
}

GetData = () => {
const self = this;

return fetch('http://192.168.100.158:3000/data')
.then((response) => response.json())
.then((responseJson) => {

// clone the data from the state
const dataClone = {...self.state.data}

const values = responseJson.map(value => value.IMPORTO);

dataClone.datasets[0].data = values;

self.setState({
isLoading: false,
data: dataClone,
});
})
.catch((error) =>{
console.error(error);
});
}

render() {

// since we're now referencing this.state.data, its value
// will be updated directly when we update the state

return (
<View>
<LineChart
data={this.state.data}
width={Dimensions.get("window").width} // from react-native
height={220}
yAxisLabel={"$"}
chartConfig={chartConfig}
bezier
style={{
marginVertical: 8,
borderRadius: 16
}}
/>
</View>
);
}

关于javascript - 如何加载动态数据到react-native-chart-kit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58505709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com