native react 折线图_【详解】纯 React Native 代码自定义折线图组件(译)

本文为 Marno 翻译,转载必须保留出处!

公众号【 Marno 】,关注后回复 RN 加入交流群

React Native 优秀开源项目大全:http://www.marno.cn

一、前言

在移动应用中制作折线图表是一件具有挑战性的事。本文将会教你如何只用 Component 和 StyleSheet 在 React Native 中制作一个折线图。

我们参考的是 《 Let’s drawing charts in React-Native without any library 》(需翻Q), 他介绍了如何在不引入三方库的情况下,在 React Native 中绘制柱状图和条形图。虽然在 react-native-chart这个库中已经有折线图了, 然而,今天我们要来定制我们自己的。

二、开始动手

首先,我们必须先绘制背景,为了显示水平轴,第一步要先绘制一些数字和直线。代码如下:

import React from 'react';

import { View, StyleSheet, Text } from 'react-native';

export default function LevelSeparator({ label, height }) {

return (

{label.toFixed(0)}

);

}

LevelSeparator.propTypes = {

label: React.PropTypes.number.isRequired,

height: React.PropTypes.number.isRequired

};

export const styles = StyleSheet.create({

container: {

flexDirection: 'row',

alignItems: 'center',

justifyContent: 'center'

},

label: {

textAlign: 'right',

width: 20

},

separatorRow: {

width: 250,

height: 1,

borderWidth: 0.5,

borderColor: 'rgba(0,0,0,0.3)',

marginHorizontal: 5

}

});

我们添加了一个 height 属性,因为我们会在下一步用到它。

然后使用上面封装好的直线组件,得到下图 1。代码如下:

export default class lineChartExample extends Component {

render() {

return (

);

}

}

const styles = StyleSheet.create({

container: {

flex: 1,

justifyContent: 'center',

alignItems: 'center',

backgroundColor: '#F5FCFF',

height: 100

}

});

图 1 ▲

三、绘制背景

重复使用 ,完成折线图背景水平轴的绘制,为了以后方便调用,我们将这个过程封装起来。

import React from 'react';

import { View, StyleSheet } from 'react-native';

import LevelSeparator from './LevelSeparator';

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值