在 React Native 图表套件中自定义图表

问题描述 投票:0回答:1

我想编辑图表的样式如下:

Spected Result

但我已经走到了这一步:

Current Graph

我唯一缺少的就是使线条更清晰一点,并且看起来不透明,并从图表中删除填充,以便仅保留线条。

这是我的代码:

import React from 'react';
import { ScrollView, Dimensions, View, Text } from 'react-native';
import {
     LineChart,
     BarChart,
     PieChart,
     ProgressChart,
     ContributionGraph,
     StackedBarChart
   } from "react-native-chart-kit";

 import Styles from './Styles';

 export default function Charts(){
 const chartConfig = {
    backgroundGradientFromOpacity: 0,
    backgroundGradientToOpacity: 0,
    color: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
    strokeWidth: 3, 
    barPercentage: 0.5,
    useShadowColorFromDataset: false,
    propsForBackgroundLines: {
        strokeDasharray: "",
        strokeOpacity: 0.0
    },

  };


return (
    <View>
        <ScrollView>
            <View style={[Styles.chart.card]}>
                <LineChart data={{
                                labels: ["January", "February", "March", "April", "May", "June"],
                                datasets: [
                                            {
                                                data: [
                                                    Math.random() * 100,
                                                    Math.random() * 100,
                                                    Math.random() * 100,
                                                    Math.random() * 100,
                                                    Math.random() * 100,
                                                    Math.random() * 100
                                                ]
                                            }
                                        ]
                                }}
                                width={Dimensions.get("window").width}
                                height={300}
                                yAxisLabel={undefined}
                                yAxisInterval={undefined}
                                chartConfig={chartConfig}
                                withHorizontalLabels={false}
                                bezier
                                hidePointsAtIndex={[0, 1, 2, 3, 4, 5]}
                                style={{
                                }}/>
            </View>
        </ScrollView> 
    </View>
    
  );
}

我将 React NativeExpoReact-Native-Chart-Kit 库一起使用。

此外,如果有任何其他库可以更好地自定义图表,那就更好了。

感谢您的帮助。


javascript react-native react-native-chart-kit
1个回答
0
投票

我找到了解决方案,我像这样调整了chartConfig

const chartConfig = {
    backgroundGradientFromOpacity: 0,
    backgroundGradientToOpacity: 0,
    color: () => `rgba(255, 255, 255, 1)`, /* Avoid opacity */
    strokeWidth: 3, 
    barPercentage: 0.5,
    useShadowColorFromDataset: false,
    propsForBackgroundLines: {
        strokeDasharray: "",
        strokeOpacity: 0.0
    },

    /* Adjusts the fill of the chart */
    fillShadowGradient: 'transparent',
    fillShadowGradientOpacity: 0,
    fillShadowGradientFromOffset: 0,
    fillShadowGradientTo: 'transparent',
    fillShadowGradientToOpacity: 0,

  };

结果:

Final Result

© www.soinside.com 2019 - 2024. All rights reserved.