program_bumble_bee
program_bumble_bee

Reputation: 305

Passing dynamic data Victory-native charts

I am using victory-native chart to render a pie chart. I am confused on how to pass the data fetched from Rest API to the {data} being passed to the pie chart for it's 'y' values. Here is my code:

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

This is the consoled form of my data that I want to pass into as 'Y' values to the chart: Rest API Data from console

The static data is rendering chart perfectly, but stuck on dynamic on how to loop the values. Please help to figure out.

Upvotes: 1

Views: 3049

Answers (1)

Tim
Tim

Reputation: 10719

You need to transform your data to match your example data:

Try this:

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

and then:

<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}`}
/>

Upvotes: 3

Related Questions