简体   繁体   English

react-native-svg-charts 动画不起作用

[英]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.我想知道当我使用react-native-svg-charts或者是否有人可以帮助我找到另一个带有可视化数据的库图表时,如何在 React Native iOS 上制作图表动画。 I tried to use the animate prop of the StackedAreaChart but it has no results!我尝试使用StackedAreaChartanimate道具,但没有结果!

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?任何的想法?

What do you mean by animate chart ?动画图表是什么意思? The animate={true} will have effect if you change the data!如果您更改数据, animate={true}将生效!

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!考虑到您可以将超时持续时间从1000更改为任何以毫秒为单位的数字!

Animation did not work with multiple lines动画不适用于多行

 lineChartData: [
  {
    data: [50, 10, 40, 95, -4, -24, 85, 50, -20, -80],
    svg: { stroke: Colors.lightBlue, strokeWidth: 1, },
  },
  {
    data: [-87, 66, -69, 92, -40, 47, -89, -44, 18],
    svg: { stroke: Colors.graphLineForPurchases, strokeWidth: 1, },
  },
]
                <LineChart
                  style={{ height: hp(25) }}
                  data={lineChartData}
                  svg={{ stroke: Colors.lightBlue, strokeWidth: 1, }}
                  contentInset={{ bottom: 20 }}
                  animate={true}
                  animationDuration={500}
                >                     
                </LineChart>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM