【问题标题】:Building custom chart on React Native在 React Native 上构建自定义图表
【发布时间】:2020-12-23 23:27:30
【问题描述】:

手头的任务:

到目前为止我学到了很多:

  1. 饼图来自 [https://github.com/JesperLekland/react-native-svg-charts-examples][2]

这里我将整个饼图分成 50 个 1 值的单位,以获得 Split-bar 效果。我可以根据上面显示的图像传递颜色。 但是我怎样才能添加内线(红色和绿色)和里面的数据呢? 任何帮助将不胜感激!

【问题讨论】:

  • 下一次,请尝试发布您尝试过的代码!

标签: reactjs react-native svg react-native-svg react-native-svg-charts


【解决方案1】:

所以我会采取的方法是使外环成为PieChart(就像您所做的那样),但是使内圈成为ProgressCirclehttps://github.com/JesperLekland/react-native-svg-charts#progresscircle),因为这个组件自然看起来像内圈在这张图中。您可以将其backgroundColor 属性更改为红色,将其progressColor 属性更改为绿色。

import {PieChart, ProgressCircle} from 'react-native-svg-charts';
import Svg, {Text as SvgText, ForeignObject} from 'react-native-svg';
import Icon from 'react-native-vector-icons/FontAwesome5';

// ...

class CustomPieChart extends React.PureComponent {
  render() {
    const data = Array.apply(null, Array(50)).map(Number.prototype.valueOf, 1);

    // Change to whatever your fill function looks like...
    const getFill = (index) => {
      if (index > 30) return 'purple';
      if (index > 20) return 'blue';
      if (index > 10) return 'green';
      return 'red';
    };

    const pieData = data.map((value, index) => ({
      value,
      svg: {
        fill: getFill(index),
      },
      key: `pie-${index}`,
    }));

    return (
      <PieChart innerRadius="90%" style={styles.pieChart} data={pieData}>
        <ProgressCircle
          style={styles.progressCircle}
          progress={0.7}
          backgroundColor="red"
          progressColor="green">
          <ForeignObject x={-100} y={-100}>
            <View style={styles.progressCircleContentContainer}>
              <Text style={{...styles.text, color: 'green', marginBottom: 5}}>
                Active
              </Text>
              <View
                style={{
                  ...styles.progressCircleContentView,
                  width: 110,
                }}>
                <Icon name="heartbeat" size={30} color="red" />
                <Text style={styles.text}>72 BPM</Text>
              </View>
              <View style={styles.progressCircleContentView}>
                <Icon name="shoe-prints" size={30} color="red" />
                <Text style={styles.text}>4,565</Text>
                <Icon name="bolt" size={30} color="red" />
                <Text style={styles.text}>45 min</Text>
              </View>
              <View style={styles.progressCircleContentView}>
                <Icon name="fire-alt" size={30} color="red" />
                <Text style={styles.text}>1,856</Text>
                <Icon name="glass-whiskey" size={30} color="red" />
                <Text style={styles.text}>Active</Text>
              </View>
              <View style={{...styles.progressCircleContentView, width: 150}}>
                <Icon name="moon" size={30} color="red" />
                <Text style={styles.text}>6 hr 10 min</Text>
              </View>
            </View>
          </ForeignObject>
        </ProgressCircle>
      </PieChart>
    );
  }
}

const styles = StyleSheet.create({
  pieChart: {
    height: 300,
  },
  progressCircle: {
    height: 250,
    marginTop: 25,
  },
  progressCircleContentContainer: {
    alignItems: 'center',
    width: 200,
    height: 200,
    transform: [],
  },
  progressCircleContentView: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    width: 200,
    marginBottom: 5,
  },
  text: {
    fontSize: 20,
  },
});

这个例子没有做什么

  • 在圆圈上添加背景阴影
  • 在外圈外侧添加图标

自定义形状是使用 react-native-svg 制作的,react-native-svg-charts 内部使用的库。您可以在此处阅读其文档:https://github.com/react-native-community/react-native-svg


外观:

【讨论】:

  • 非常感谢!我现在就试试这个,让你知道!
  • 由于某种原因,进度条和图标没有加载。但是没有错误 - snack.expo.io/HnAtix!VG 请看一下?这是我的项目^
  • 我已经为 Ionicons 使用了这个导入语句:import Icon from 'react-native-vector-icons/Ionicons'。但最重要的是您需要删除我在示例中留下的评论:// Add further ForeignObject for other text and icons...
  • 另外,如果你在零食中运行它,请务必选择 android 或 ios,它不会在 web 上正确显示。如果您看到问号,那是因为反应原生矢量图标需要一些您在零食中无法做到的配置(据我所知)。因此,您可能希望在本地对其进行配置并在设备上对其进行测试。有关说明,您可以参考:github.com/oblador/react-native-vector-icons#installation.
  • 这是我调整后的零食叉snack.expo.io/@bvdl/chart-draft
猜你喜欢
  • 2016-05-10
  • 1970-01-01
  • 2021-05-12
  • 1970-01-01
  • 1970-01-01
  • 2018-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多