传递动态数据 Victory-native charts

我正在使用胜利原生图表来呈现饼图。我对如何将从 Rest API 获取的数据传递给传递给饼图的 {data} 感到困惑,因为它是 'y' 值。这是我的代码:


import React, {Component} from "react";

import { StyleSheet, View } from "react-native";

import axios from 'axios'

import { VictoryPie, VictoryGroup, VictoryTheme } from "victory-native";

import Svg from 'react-native-svg';


export default class Chart extends Component {


  state= {

    id: '********************',

    data: []

}

componentDidMount(){


    var headers = {

        'Content-Type': 'application/x-www-form-urlencoded',

        'Access-Control-Allow-Headers': 'x-access-token',

        'x-access-token': this.state.id

    }


    axios.post('http://bi.servassure.net/api/SalesOverviewOEMLevel2', {

        oem:'all'

    }, 

    {headers: headers}).then((res) => {


        let TotalSales = res.data.data[0].TotalSales;


        this.setState({

          data: TotalSales

        })

        console.log(this.state.data);

    })

    .catch(err => {

        console.log(err)

    });


}


  render() {

    const myColorScale = ["#1b4f72", "#21618c", "#2e86c1","#3498db", "#76d7c4", "#d1f2eb"];

    const data = [

      { x: 1, y: 6, i: 0 },

      { x: 2, y: 2, i: 1 },

      { x: 3, y: 3, i: 2 },

      { x: 4, y: 5, i: 3 },

    ]

    return (

      <View style={styles.container}>

        <Svg

            height={200}

            width={200}

            viewBox={'0 0 300 300'}

            preserveAspectRatio='none'

        >

        <VictoryGroup width={300} theme={VictoryTheme.material}>


          <VictoryPie 

          style={{

              data: {

                fill: (d) => myColorScale[d.i]

              },

              labels: { fill: "white", fontSize: 10, fontWeight: "bold" }

            }} 

            radius={100}

            innerRadius={60}

            labelRadius={70}

            data={data}

            labels={(d) => `y: ${d.y}`}


          />


        </VictoryGroup>

        </Svg>

      </View>

    );

  }

}

这是我想作为“Y”值传递给图表的数据的控制台形式:

http://img1.mukewang.com/60e6ae700001236d02240129.jpg

静态数据完美地呈现图表,但在如何循环值方面停留在动态上。请帮忙弄清楚。


慕田峪4524236
浏览 172回答 1
1回答

PIPIONE

您需要转换数据以匹配示例数据:试试这个:const your_data = this.state.data.map((val, index) => {&nbsp; &nbsp;// you can also pass an i- value, but that's up to you&nbsp; &nbsp;return { x: index, y: val };&nbsp;});然后:<VictoryPie&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; style={{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fill: (d) => myColorScale[d.x] // d.x, because i didn't specify an "i-value"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; labels: { fill: "white", fontSize: 10, fontWeight: "bold" }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }}&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; radius={100}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; innerRadius={60}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; labelRadius={70}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data={your_data} // pass here your new data&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; labels={(d) => `y: ${d.y}`}/>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript