-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to change the color of the bar based on click event #512
Comments
Hi @prasannakulal16 👋 Thanks for requesting this feature. I will try to add it in the next release. For now, you can make use of the onPress={(item, index) => {
const newData = [...data];
newData.forEach(dataItem => {
dataItem.frontColor = 'blue'; // coloring the rest of the bars with the initial color - blue
});
newData[index].frontColor = 'lightgreen'; // coloring the selected bar with the a new color - lightgreen
setData(newData);
}} Here's your code, with modifications to achieve this- const dataArray = [
{value: 50, xValue: '4AM'},
{value: 80, xValue: '8AM'},
{value: 90, xValue: '12AM'},
{value: 70, xValue: '4PM'},
{value: 70, xValue: '8PM'},
{value: 70, xValue: '9AM'},
];
const [data, setData] = useState(dataArray); // set the data in state, so that on clicking a bar, we can update it and re-render
const topLabelComponent = item => {
return (
<View
style={{
// width: GetDeviceSize(70),
// height: GetDeviceSize(60),
paddingHorizontal: 10,
paddingVertical: 8,
borderRadius: 8,
backgroundColor: 'black',
marginBottom: 4,
marginLeft: -20,
}}>
<Text
style={{
color: 'white',
textAlign: 'center',
}}>
{item.xValue}
</Text>
<View
style={{
flexDirection: 'row',
alignItems: 'center',
}}>
<View>
{/* <TimerIcon
width={GetDeviceSize(12)}
height={GetDeviceSize(12)}
/> */}
</View>
<Text
style={{
color: 'yellow',
// textAlign: 'center',
paddingLeft: 3,
}}>
{item.value} mins
</Text>
</View>
</View>
);
};
return (
<BarChart
data={data}
height={100}
// width={Dimensions.get('window').width}
barBorderTopLeftRadius={4}
barBorderTopRightRadius={4}
yAxisTextStyle={{color: 'red'}}
showYAxisIndices={false}
hideYAxisText
xAxisLabelTextStyle={{
color: 'gray',
fontSize: 10,
}}
xAxisLabelTexts={['4AM', '8PM', '12AM', '4PM', '8PM', '9AM']}
renderTooltip={topLabelComponent}
barStyle={{color: 'red'}}
barWidth={26}
yAxisColor={'transparent'}
xAxisColor={'gray'}
barBorderColor={'green'}
frontColor={'blue'}
yAxisExtraHeight={60}
// rulesType={'solid'}
hideRules
onPress={(item, index) => {
const newData = [...data];
newData.forEach(dataItem => {
dataItem.frontColor = 'blue'; // coloring the rest of the bars with the initial color - blue
});
newData[index].frontColor = 'lightgreen'; // coloring the selected bar with the a new color - lightgreen
setData(newData);
}}
/>
); The output is- highlightBar.mov |
Hi @prasannakulal16 |
Hey Its worked as expected. Thanks for the quick response👍 |
Hey @Abhinandan-Kushwaha
I want to change the color of the bar when i click on it.
Something like this -
The text was updated successfully, but these errors were encountered: