added the prop customDataPoint to render a custom component as the data points

This commit is contained in:
Abhinandan Kushwaha 2021-12-02 18:55:03 +05:30
parent ad2a132666
commit 62835a4010
4 changed files with 82 additions and 52 deletions

30
App.js
View File

@ -51,7 +51,7 @@ const App = () => {
height={300} height={300}
maxValue={360} maxValue={360}
showLine showLine
initialSpacing={20} initialSpacing={30}
// showVerticalLines // showVerticalLines
lineConfig={{ lineConfig={{
// isAnimated: true, // isAnimated: true,
@ -135,12 +135,26 @@ const App = () => {
data={data} data={data}
// data2={lineData1} // data2={lineData1}
areaChart areaChart
initialSpacing={10} initialSpacing={20}
customDataPoint={() => {
return (
<View
style={{
height: 10,
width: 10,
backgroundColor: 'red',
borderWidth: 2,
borderColor: 'blue',
borderRadius: 5,
}}
/>
);
}}
onPress={(item, index) => { onPress={(item, index) => {
console.log('index-->', index); console.log('index-->', index);
setData(data => { setData(data => {
item.dataPointColor = 'green'; item.dataPointColor = 'green';
item.dataPointRadius = 6; // item.dataPointRadius = 5;
item.dataPointText = item.value; item.dataPointText = item.value;
// item.dataPointShape = 'rectangular'; // item.dataPointShape = 'rectangular';
// item.dataPointWidth = 10; // item.dataPointWidth = 10;
@ -160,9 +174,9 @@ const App = () => {
textFontSize={18} textFontSize={18}
textColor={'green'} textColor={'green'}
stripWidth={1} stripWidth={1}
stripHeight={200}
// stripHeight={200} // stripHeight={200}
stripOpacity={0.4} // stripHeight={200}
stripOpacity={1}
curved curved
isAnimated isAnimated
animationDuration={2000} animationDuration={2000}
@ -172,9 +186,9 @@ const App = () => {
color={'rgb(78, 0, 142)'} color={'rgb(78, 0, 142)'}
yAxisColor={'rgb(78, 0, 142)'} yAxisColor={'rgb(78, 0, 142)'}
xAxisColor={'rgb(78, 0, 142)'} xAxisColor={'rgb(78, 0, 142)'}
dataPointsColor={'rgb(78, 0, 142)'} // dataPointsColor={'yellow'}
dataPointsWidth={8} dataPointsWidth={22}
dataPointsHeight={8} dataPointsHeight={22}
xAxisThickness={3} xAxisThickness={3}
yAxisThickness={3} yAxisThickness={3}
dataPointsRadius={4} dataPointsRadius={4}

View File

@ -174,6 +174,7 @@ type referenceConfigType = {
| textFontSize | number | Font size of the dataPointText | \_ | | textFontSize | number | Font size of the dataPointText | \_ |
| textShiftX | number | To shift the dataPointText text horizontally | 0 | | textShiftX | number | To shift the dataPointText text horizontally | 0 |
| textShiftY | number | To shift the dataPointText text vertically | 0 | | textShiftY | number | To shift the dataPointText text vertically | 0 |
| customDataPoint | Function | A callback function to render a custom component as the data points | \_ |
--- ---

View File

@ -1,6 +1,6 @@
{ {
"name": "react-native-gifted-charts", "name": "react-native-gifted-charts",
"version": "0.2.0", "version": "0.2.1",
"description": "The most complete library for Bar, Line, Area, Pie and Donut charts in React Native. Allows 2D, 3D, gradient, animations and live data updates.", "description": "The most complete library for Bar, Line, Area, Pie and Donut charts in React Native. Allows 2D, 3D, gradient, animations and live data updates.",
"main": "src/index.tsx", "main": "src/index.tsx",
"files": [ "files": [

View File

@ -138,6 +138,7 @@ type propTypes = {
dataPointsRadius3?: number; dataPointsRadius3?: number;
dataPointsColor3?: string; dataPointsColor3?: string;
dataPointsShape3?: string; dataPointsShape3?: string;
customDataPoint?: Function;
startFillColor?: string; startFillColor?: string;
endFillColor?: string; endFillColor?: string;
@ -1182,24 +1183,18 @@ export const LineChart = (props: propTypes) => {
)} )}
{index === selectedIndex && showStripOnPress ? ( {index === selectedIndex && showStripOnPress ? (
<Rect <Rect
x={ x={initialSpacing + (spacing * index - dataPointsWidth / 2)}
initialSpacing / 2 +
(spacing * index - stripWidth) +
stripWidth / 2 +
1
}
y={ y={
stripHeight stripHeight
? containerHeight - stripHeight + 8 ? containerHeight - stripHeight + 8
: containerHeight - : containerHeight -
dataPointsHeight / 2 + dataPointsHeight / 2 +
15 - 20 -
(item.value * containerHeight) / maxValue (item.value * containerHeight) / maxValue
} }
width={stripWidth} width={stripWidth}
height={ height={
stripHeight || stripHeight || containerHeight - dataPointsHeight / 2 + 20
containerHeight - dataPointsHeight / 2 + 10 - 0
} }
opacity={stripOpacity} opacity={stripOpacity}
fill={stripColor} fill={stripColor}
@ -1207,26 +1202,44 @@ export const LineChart = (props: propTypes) => {
) : null} ) : null}
</> </>
) : null} ) : null}
{props.customDataPoint ? (
<View
style={{
position: 'absolute',
height: dataPointsHeight,
width: dataPointsWidth,
// backgroundColor: 'orange',
top:
containerHeight - (item.value * containerHeight) / maxValue,
left: initialSpacing - dataPointsWidth + spacing * index,
justifyContent: 'center',
alignItems: 'center',
}}>
{props.customDataPoint()}
</View>
) : null}
{dataPointsShape === 'rectangular' ? ( {dataPointsShape === 'rectangular' ? (
<Fragment key={index}> <Fragment key={index}>
<Rect {props.customDataPoint ? null : (
x={initialSpacing - dataPointsWidth + spacing * index} <Rect
y={ x={initialSpacing - dataPointsWidth + spacing * index}
containerHeight - y={
dataPointsHeight / 2 + containerHeight -
10 - dataPointsHeight / 2 +
(item.value * containerHeight) / maxValue 10 -
} (item.value * containerHeight) / maxValue
width={dataPointsWidth} }
height={dataPointsHeight} width={dataPointsWidth}
fill={ height={dataPointsHeight}
showDataPointOnPress fill={
? index === selectedIndex showDataPointOnPress
? dataPointsColor ? index === selectedIndex
: 'none' ? dataPointsColor
: dataPointsColor : 'none'
} : dataPointsColor
/> }
/>
)}
{text ? ( {text ? (
!showTextOnPress || index === selectedIndex ? ( !showTextOnPress || index === selectedIndex ? (
<CanvasText <CanvasText
@ -1255,22 +1268,24 @@ export const LineChart = (props: propTypes) => {
</Fragment> </Fragment>
) : ( ) : (
<Fragment key={index}> <Fragment key={index}>
<Circle {props.customDataPoint ? null : (
cx={initialSpacing - dataPointsWidth / 2 + spacing * index} <Circle
cy={ cx={initialSpacing - dataPointsWidth / 2 + spacing * index}
containerHeight + cy={
10 - containerHeight +
(item.value * containerHeight) / maxValue 10 -
} (item.value * containerHeight) / maxValue
r={dataPointsRadius} }
fill={ r={dataPointsRadius}
showDataPointOnPress fill={
? index === selectedIndex showDataPointOnPress
? dataPointsColor ? index === selectedIndex
: 'none' ? dataPointsColor
: dataPointsColor : 'none'
} : dataPointsColor
/> }
/>
)}
{text ? ( {text ? (
!showTextOnPress || index === selectedIndex ? ( !showTextOnPress || index === selectedIndex ? (
<CanvasText <CanvasText