Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-native-svg-charts animate does not work

I would like to know how to animate chart on react native iOS when I'm using react-native-svg-charts or if someone can help me to find another library chart with visualization data. I tried to use the animate prop of the StackedAreaChart but it has no results!

Here is my code :

export default class LinksScreen extends React.Component {
  static navigationOptions = {
    title: 'react chart',
  };

  render() {

      const data = [
          {
              month: new Date(2015, 0, 1),
              apples: 3840,
              bananas: 1920,
              cherries: 960,
              dates: 400,
          },
          {
              month: new Date(2015, 1, 1),
              apples: 1600,
              bananas: 1440,
              cherries: 960,
              dates: 400,
          },
          {
              month: new Date(2015, 2, 1),
              apples: 640,
              bananas: 960,
              cherries: 3640,
              dates: 400,
          },
          {
              month: new Date(2015, 3, 1),
              apples: 3320,
              bananas: 480,
              cherries: 640,
              dates: 400,
          },
      ]

      const colors = [ 'green', '#aa00ff', 'red', 'yellow' ]
      const keys   = [ 'apples', 'bananas', 'cherries', 'dates' ]
      const svgs = [
                  { onPress: () => console.log('apples') },
                  { onPress: () => console.log('bananas') },
                  { onPress: () => console.log('cherries') },
                  { onPress: () => console.log('dates') },
              ]

      return (
          <StackedAreaChart
              style={ { height: 200, paddingVertical: 16 } }
              data={ data }
              keys={ keys }
              colors={ colors }
              curve={ shape.curveNatural }
              showGrid={ false }
              svgs={ svgs }
              animate={true}
              animationDuration={300}
          />
      )
  }
}

Any idea?

like image 318
Hardinah Rajaonanirina Avatar asked Nov 22 '18 06:11

Hardinah Rajaonanirina


1 Answers

What do you mean by animate chart? The animate={true} will have effect if you change the data!

Let's see that with an example:

import React, { Component } from "react";
import { View, Text, TouchableOpacity } from "react-native";
import { LineChart } from "react-native-svg-charts";

class TestingCharts extends Component {
  constructor(props) {
    super(props);

    this.state = {
      data: [
        {
          a: 3840
        },
        {
          b: 1920
        },
        {
          c: 960
        },
        {
          d: 400
        }
      ]
    };
  }

  render() {
    newData = [
      {
        a: 2000
      },
      {
        b: 4902
      },
      {
        c: 325
      },
      {
        d: 7812
      }
    ];

    return (
      <View
        style={{
          flex: 1,
          flexDirection: "column",
          justifyContent: "center",
          alignItems: "center"
        }}
      >
        <View
          style={{
            flex: 1,
            justifyContent: "center",
            alignItems: "center",
            alignSelf: "stretch"
          }}
        >
          <LineChart
            style={{
              flex: 1,
              alignSelf: "stretch",
              borderWidth: 1,
              borderColor: "rgba(255,255,255,0.5)",
              margin: 10
            }}
            data={this.state.data}
            svg={{
              strokeWidth: 2,
              stroke: Colors.WHITE
            }}
            animate
          />
        </View>

        <View
          style={{
            flex: 1,
            justifyContent: "center",
            alignItems: "center",
            alignSelf: "stretch"
          }}
        >
          <TouchableOpacity
            style={{
              flex: 1,
              justifyContent: "center",
              alignItems: "center",
              alignSelf: "stretch"
            }}
            onPress={() => this.setState({ data: newData })}
          >
            <Text>Change Data!</Text>
          </TouchableOpacity>
        </View>
      </View>
    );
  }
}

export default TestingCharts;

UPDATE

As you mentioned in the comments, you want that chart start with an straight line then it animate to new data:

import React, { Component } from "react";
import { View, Text, TouchableOpacity } from "react-native";
import { LineChart } from "react-native-svg-charts";

class TestingCharts extends Component {
  constructor(props) {
    super(props);

    this.state = {
      data: [
        {
          a: 0
        },
        {
          b: 0
        },
        {
          c: 0
        },
        {
          d: 0
        }
      ]
    };

    this.changeData();
  }

  changeData() {
    newData = [
      {
        a: 2000
      },
      {
        b: 4902
      },
      {
        c: 325
      },
      {
        d: 7812
      }
    ];

    setTimeout(() => {
      this.setState({ data: newData });
    }, 1000);
  }

  render() {
    return (
      <View
        style={{
          flex: 1,
          flexDirection: "column",
          justifyContent: "center",
          alignItems: "center"
        }}
      >
        <View
          style={{
            flex: 1,
            justifyContent: "center",
            alignItems: "center",
            alignSelf: "stretch"
          }}
        >
          <LineChart
            style={{
              flex: 1,
              alignSelf: "stretch",
              borderWidth: 1,
              borderColor: "rgba(255,255,255,0.5)",
              margin: 10
            }}
            data={this.state.data}
            svg={{
              strokeWidth: 2,
              stroke: Colors.WHITE
            }}
            animate
          />
        </View>

        <View
          style={{
            flex: 1,
            justifyContent: "center",
            alignItems: "center",
            alignSelf: "stretch"
          }}
        >
          <View
            style={{
              flex: 1,
              justifyContent: "center",
              alignItems: "center",
              alignSelf: "stretch"
            }}
          >
            <Text>My Beautiful Chart :D</Text>
          </View>
        </View>
      </View>
    );
  }
}

export default TestingCharts;

Consider that you can change the duration of timeout from 1000 to any number in milliseconds!

like image 169
MohamadKh75 Avatar answered Nov 03 '22 08:11

MohamadKh75