React-native-svg-charts: Stacked Bar Chart: Different color for each bar.

Created on 24 Jun 2019  路  5Comments  路  Source: JesperLekland/react-native-svg-charts

How can I obtain to make every stacked bar chart different color from each other.

Most helpful comment

put your data in an array and map it in your stacked chart together with your color scheme.
like this one:

const data_sample = [{
  color : ['blue','orange'],
  data : [{
    positive: 3840,
    negative: 1920,
  }],
  keys : ['positive','negative'],
  label : 'test'
},
{
  color : ['brown','green','red'],
  data : [{
    positive: 3840,
    negative: 1920,
    neutral : 1000
  }],
  keys : ['positive','negative','neutral'],
  label : 'test2'
}]

<View style={[{flexDirection:'column',alignText : 'left', justifyContent : 'flex-start', borderWidth:0,flex:1}]}>
  <View style={[{flexDirection:'row',alignText : 'left', justifyContent : 'flex-start', borderWidth:0}]}>
  {data_sample.map(value => {
    return(<StackedBarChart
      style={{ height: 100, width:'20%' ,marginLeft: '5%'}}
      keys={ value.keys}
      colors={value.color}
      data={ value.data}
      showGrid={ false }
      horizontal={false}/>
      )
  })
  } 
  </View>
</View>

this will produce this view :

Screen Shot 2019-06-26 at 2 37 23 PM

All 5 comments

This worked for me :

<StackedBarChart style={style} keys={ ['positive','negative']} colors={['green','red']} data={ data} showGrid={ false } horizontal={true}/>

@Unicity-Pimentel currently that is what I am doing. But I need something like the image below.
NmrFC

yup same application. try setting the horizontal={false}

with the same code i created this one.

Screen Shot 2019-06-26 at 11 42 18 AM

the upper part is when i set horizontal={false} and the other one is the one you needed

@Unicity-Pimentel how about for example, in the image I posted, the first bar's colors are blue and orange while for the second bar the colors are blue, white and red?

put your data in an array and map it in your stacked chart together with your color scheme.
like this one:

const data_sample = [{
  color : ['blue','orange'],
  data : [{
    positive: 3840,
    negative: 1920,
  }],
  keys : ['positive','negative'],
  label : 'test'
},
{
  color : ['brown','green','red'],
  data : [{
    positive: 3840,
    negative: 1920,
    neutral : 1000
  }],
  keys : ['positive','negative','neutral'],
  label : 'test2'
}]

<View style={[{flexDirection:'column',alignText : 'left', justifyContent : 'flex-start', borderWidth:0,flex:1}]}>
  <View style={[{flexDirection:'row',alignText : 'left', justifyContent : 'flex-start', borderWidth:0}]}>
  {data_sample.map(value => {
    return(<StackedBarChart
      style={{ height: 100, width:'20%' ,marginLeft: '5%'}}
      keys={ value.keys}
      colors={value.color}
      data={ value.data}
      showGrid={ false }
      horizontal={false}/>
      )
  })
  } 
  </View>
</View>

this will produce this view :

Screen Shot 2019-06-26 at 2 37 23 PM

Was this page helpful?
0 / 5 - 0 ratings