【问题标题】:How to apply gradient color on React Native StackedAreaChart?如何在 React Native StackedAreaChart 上应用渐变颜色?
【发布时间】:2020-06-15 15:18:08
【问题描述】:

我正在使用this 库来创建StackedAreaChart。它目前正在工作,但我需要将颜色更改为渐变。此包中提供的示例仅在 AreaChart 上应用渐变。

这是我的代码

const hardCodedData = [
        {
            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 = ['#8800cc', '#aa00ff', '#cc66ff', '#eeccff']
    const keys = ['apples', 'bananas', 'cherries', 'dates']
    const svgs = [
        { onPress: () => alert('apples') },
        { onPress: () => alert('bananas') },
        { onPress: () => alert('cherries') },
        { onPress: () => alert('dates') },
    ]
<StackedAreaChart
   style={{ height: 200, paddingVertical: 16 }}
   data={hardCodedData}
   keys={keys}
   colors={colors}
   curve={shape.curveNatural}
   showGrid={false}
   svgs={svgs}
/>

您能在这方面为我提供一些帮助吗?

编辑

const Gradient = () =>
        map(data, (d, i) => {
            let key = 'gradient' + d.id;
            let color = colors[d.id];

            return (
                <Defs key={key}>
                    <LinearGradient id={key} x1={'0'} y={'0%'} x2={'0'} y2={'100%'}>
                        <Stop offset={'0%'} stopColor={color} />
                        <Stop
                            offset={'100%'}
                            stopColor={'red'}
                        />
                    </LinearGradient>
                </Defs>
            );
        });

    let fill1 = `url(#gradient${d.id})`;
    let fill2 = `url(#gradient${d.id})`;
    let fill3 = `url(#gradient${d.id})`;
    let fill4 = `url(#gradient${d.id})`;

    let gradients = [fill1, fill2, fill3, fill4]

 <StackedAreaChart
     style={{ height: 200, paddingVertical: 16 }}
     data={hardCodedData}
     keys={keys}
     colors={gradients}
     curve={shape.curveNatural}
     showGrid={false}
     svgs={svgs}
   >
     <Gradient />
   </StackedAreaChart>

【问题讨论】:

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


    【解决方案1】:
    const Gradient = () =>
    map(data, (d, i) => {
      let key = 'gradient' + d.id;
      let color = Yourcolors[d.id];
    
      return (
        <Defs key={key}>
          <LinearGradient id={key} x1={'0'} y={'0%'} x2={'0'} y2={'100%'}>
            <Stop offset={'0%'} stopColor={color} />
            <Stop
              offset={'100%'}
              stopColor={color2}
            />
          </LinearGradient>
        </Defs>
      );
    });
    ...
    
    <StackedAreaChart
       style={{ height: 200, paddingVertical: 16 }}
       data={hardCodedData}
       keys={keys}
       colors={gradients}
       curve={shape.curveNatural}
       showGrid={false}
       svgs={svgs}
    >
    <Gradient/>
    </StackedAreaChart>
    
    

    在你的颜色中,只需用像

    这样的渐变填充区域
    
    let gradients = map(data, d=> {
      let fill = `url(#gradient${d.id})`;
    return fill;
    })
    
    //this will generate an array of gradient defs
    

    在您的堆叠区域中传递渐变数组而不是颜色

    PS。我已经在其他图表中测试过这种类型的实现,而不是在面积图中

    【讨论】:

    • 感谢您的回答,但我正在努力让它运行,首先我有一个错误 can't find variable map ,其次是 let fill = url(#gradient${d.id}) ;,我应该在哪里声明?因为d.id 是未定义的。
    • 这个仅供参考,您可以从lodash导入地图,需要根据您的数据自己生成渐变数组。
    • 只需创建一个新数组,您将在其中使用数据上的地图创建颜色数组
    • 我会试试这个并回复你。
    • 我将用修改后的代码更新我的问题伙伴,我在填充1、填充2、...can't find variable d
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2019-02-01
    • 2022-01-24
    • 1970-01-01
    • 2020-06-19
    • 2022-01-07
    • 2018-08-24
    相关资源
    最近更新 更多