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

伊戈尔(Igor Mytyuk)

实际上,我会动态地将数据加载到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 DATA的提取函数:

  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>
    );
  }
}

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

M极

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

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>
    );
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

react-native-chart-kit问题检查render方法

如何在react-native-chart-kit中使用装饰器?

如何在 react-native-chart-kit 中更改 LineChart 的标签

条形图中带有react-native-chart-kit的实心条

華為 Map Kit React Native 不渲染地圖

如何在将数据加载到 UseEffect 中的数组后执行排序 - React Native

如何向此React Native Chart添加渐变色?

React-Native Firebase ML Kit 视觉图像标签不适用于 iOS

为什么 React Native 中的 HMS Ads Kit 在构建过程中会显示 package list missing 错误?

(React Native)将本地HTML文件加载到WebView中

(React Native)如何将App签名密钥上载到Google Play控制台?

Chart.js-如何将折线图数据集设置为在加载时禁用

如何使用React Native从api加载数据

将Sprite Kit放入UITabBarController

如何操作Sprite Kit的SKNode?

如何在按钮上动态加载 API 数据 单击 React Native

使用react-native-chart-android后应用程序崩溃

为什么 React Native 中的 HMS Location Kit 给出 java.lang.NullPointerException:Attempt to invoke virtual method 'android.location.Location.toString()'?

如何将个人PNG数据集放入Sci-Kit Learn进行图像识别?

如何使用 React Native 将数据数组加载为数据集格式?

将json文件加载到chart.js

如何将数据加载到recyclerView

如何将数据加载到HDInsight?

如何将数据加载到ListFragment?

在React Native中将数据源加载到listView的正确方法是什么

使用UI Kit添加动态边距

FontAwesome Kit 加载所有版本

Sprite Kit提升Sprite加载性能

将MobX存储保存并加载到React Native AsyncStorage的最佳模式?