为了账号安全,请及时绑定邮箱和手机立即绑定

传递动态数据 Victory-native charts

传递动态数据 Victory-native charts

慕田峪4524236 2021-06-28 09:51:44
我正在使用胜利原生图表来呈现饼图。我对如何将从 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”值传递给图表的数据的控制台形式:静态数据完美地呈现图表,但在如何循环值方面停留在动态上。请帮忙弄清楚。
查看完整描述

1 回答

?
PIPIONE

TA贡献1829条经验 获得超9个赞

您需要转换数据以匹配示例数据:


试试这个:


const your_data = this.state.data.map((val, index) => {

   // you can also pass an i- value, but that's up to you

   return { x: index, y: val }; 

});

然后:


<VictoryPie 

          style={{

              data: {

                fill: (d) => myColorScale[d.x] // d.x, because i didn't specify an "i-value"

              },

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

            }} 

            radius={100}

            innerRadius={60}

            labelRadius={70}

            data={your_data} // pass here your new data 

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

/>


查看完整回答
反对 回复 2021-07-08
  • 1 回答
  • 0 关注
  • 171 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信