react-nativereact-native-svg-charts

How to apply gradient color on React Native StackedAreaChart?


I'm using this library to make a StackedAreaChart. It's currently working but I need to change the colors to gradients. The examples provide in this package apply gradients only on AreaChart.

Here is my code

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}
/>

Can you please provide me with some help in this ?

EDIT

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>

enter image description here


Solution

  • 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>
    
    

    and in your colors just fill the areas with gradients like

    
    let gradients = map(data, d=> {
      let fill = `url(#gradient${d.id})`;
    return fill;
    })
    
    //this will generate an array of gradient defs
    

    pass the gradients array instead of colors in your stackedareachart

    PS. I have tested this type of implementation in other charts not in area chart