From 33818b9783b642a736f4e8269046bc9035a59edb Mon Sep 17 00:00:00 2001 From: Abhinandan-Kushwaha Date: Mon, 25 Apr 2022 00:36:47 +0530 Subject: [PATCH] Added onPress as a prop to Bar Chart and passed item and index as parameters --- docs/BarChart/BarChartProps.md | 1 + package.json | 2 +- src/BarChart/RenderBars.tsx | 9 ++++++++- src/BarChart/index.tsx | 2 ++ src/PieChart/index.tsx | 2 +- 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/docs/BarChart/BarChartProps.md b/docs/BarChart/BarChartProps.md index 4ff9ddf..341a5bd 100644 --- a/docs/BarChart/BarChartProps.md +++ b/docs/BarChart/BarChartProps.md @@ -9,6 +9,7 @@ | data | Array of items | An item object represents a bar in the bar chart. It is described in the next table. | \_ | | width | number | Width of the Bar chart | width of the parent | | height | number | Height of the Bar chart (excluding the bottom label) | 200 | +| onPress | Function | Callback function called on press of a Bar (takes item and index as parameter) | null | | maxValue | number | Maximum value shown in the Y axis | 200 | | minValue | number | Minimum negative value shown in the Y axis (to be used only if the data set has negative values too) | \_ | | noOfSections | number | Number of sections in the Y axis | 10 | diff --git a/package.json b/package.json index a678036..f6c1d2b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-gifted-charts", - "version": "1.2.6", + "version": "1.2.7", "description": "The most complete library for Bar, Line, Area, Pie, Donut and Stacked Bar charts in React Native. Allows 2D, 3D, gradient, animations and live data updates.", "main": "src/index.tsx", "files": [ diff --git a/src/BarChart/RenderBars.tsx b/src/BarChart/RenderBars.tsx index 52309b7..cf379c5 100644 --- a/src/BarChart/RenderBars.tsx +++ b/src/BarChart/RenderBars.tsx @@ -63,6 +63,7 @@ type Props = { barBackgroundPattern?: Function; patternId?: String; barMarginBottom?: number; + onPress?: Function; }; type itemType = { value?: number; @@ -319,7 +320,13 @@ const RenderBars = (props: Props) => { { + item.onPress + ? item.onPress() + : props.onPress + ? props.onPress(item, index) + : null; + }} style={[ { // overflow: 'visible', diff --git a/src/BarChart/index.tsx b/src/BarChart/index.tsx index 73b5b64..e22c1ae 100644 --- a/src/BarChart/index.tsx +++ b/src/BarChart/index.tsx @@ -130,6 +130,7 @@ type PropTypes = { barBackgroundPattern?: Function; patternId?: String; barMarginBottom?: number; + onPress?: Function; }; type lineConfigType = { initialSpacing?: number; @@ -1458,6 +1459,7 @@ export const BarChart = (props: PropTypes) => { labelTextStyle={ item.labelTextStyle || props.xAxisLabelTextStyle } + onPress={props.onPress} /> ))} diff --git a/src/PieChart/index.tsx b/src/PieChart/index.tsx index 3d6e4b7..99391be 100644 --- a/src/PieChart/index.tsx +++ b/src/PieChart/index.tsx @@ -230,7 +230,7 @@ export const PieChart = (props: propTypes) => { })} {data.map((item, index) => { - console.log('index', index); + // console.log('index', index); let nextItem; if (index === pData.length - 1) nextItem = pData[0]; else nextItem = pData[index + 1];