React Native 使用 react-native-chart-kit 实现图表

react-native-chart-kit

使用文档

GitHub - indiespirit/react-native-chart-kit: 📊React Native Chart Kit: 折线图, 贝塞尔折线图, 进度环, 条形图, 饼图, 贡献图(热图)

安装

yarn add react-native-chart-kit

使用

import React, {useEffect, useRef, useState} from 'react';
import {Text, View, StyleSheet} from 'react-native';
import {LineChart} from 'react-native-chart-kit';
import {Dimensions} from 'react-native';
const screenWidth = Dimensions.get('window').width * 0.84; // 获取图表可以显示的宽度
export const Demo = () => {
  const [chartData, setChartData] = useState({});
  const chartConfig = {
    backgroundGradientFrom: '#ffffff',
    backgroundGradientFromOpacity: 0,
    backgroundGradientTo: '#ffffff',
    backgroundGradientToOpacity: 0,
    color: (opacity = 0.5) => `rgba(51, 51, 51, ${opacity})`,
    strokeWidth: 2,
    barPercentage: 0.3,
  };
  const data: any = {
    labels: ['x1', 'x2', 'x3'],
    datasets: [
      {
        data: ['y1', 'y2', 'y3'],
        color: (opacity = 1) => `rgba(92, 186, 253, ${opacity})`, // optional
        strokeWidth: 2, // 折线宽度
      },
    ],
    legend: ['显示数值'], // 折线名
  };
  setChartData(data);

  return (
    <LineChart
      data={chartData}
      width={screenWidth}
      height={220}
      chartConfig={chartConfig}
      withVerticalLines={false} // 不显示垂直虚线
      withOuterLines={false} // 不显示外部虚线
      verticalLabelRotation={-60} // x轴的标签旋转-60°
    />
  );
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值