Added noOfVerticalLines and verticalLinesSpacing to Bar nad Line charts

This commit is contained in:
Abhinandan-Kushwaha 2022-05-26 03:08:35 +05:30
parent a10d7fa3fa
commit 63da725699
7 changed files with 104 additions and 25 deletions

View File

@ -147,6 +147,8 @@ The properties of this line chart can be controlled using the `lineConfig` prop
| verticalLinesColor | ColorValue | Color of the vertical lines | lightgray | | verticalLinesColor | ColorValue | Color of the vertical lines | lightgray |
| verticallinesThickness | number | Thickness of the vertical lines | 1 | | verticallinesThickness | number | Thickness of the vertical lines | 1 |
| verticalLinesZIndex | number | Z index of the vertical lines | -1 | | verticalLinesZIndex | number | Z index of the vertical lines | -1 |
| noOfVerticalLines | number | Number of vertical lines displayed | data.length |
| verticalLinesSpacing | number | Distance between consecutive vertical lines | spacing |
| showXAxisIndices | Boolean | To show the pointers on the X axis | false | | showXAxisIndices | Boolean | To show the pointers on the X axis | false |
| xAxisIndicesHeight | number | Height of the pointers on the X axis | 2 | | xAxisIndicesHeight | number | Height of the pointers on the X axis | 2 |
| xAxisIndicesWidth | number | Width of the pointers on the X axis | 4 | | xAxisIndicesWidth | number | Width of the pointers on the X axis | 4 |

View File

@ -135,6 +135,8 @@ When you are using the `dataPointLabelComponent`, make sure to provide the `data
| verticalLinesColor | ColorValue | Color of the vertical lines | lightgray | | verticalLinesColor | ColorValue | Color of the vertical lines | lightgray |
| verticallinesThickness | number | Thickness of the vertical lines | 1 | | verticallinesThickness | number | Thickness of the vertical lines | 1 |
| verticalLinesZIndex | number | Z index of the vertical lines | -1 | | verticalLinesZIndex | number | Z index of the vertical lines | -1 |
| noOfVerticalLines | number | Number of vertical lines displayed | data.length |
| verticalLinesSpacing | number | Distance between consecutive vertical lines | spacing |
| showXAxisIndices | Boolean | To show the pointers on the X axis | false | | showXAxisIndices | Boolean | To show the pointers on the X axis | false |
| xAxisIndicesHeight | number | Height of the pointers on the X axis | 2 | | xAxisIndicesHeight | number | Height of the pointers on the X axis | 2 |
| xAxisIndicesWidth | number | Width of the pointers on the X axis | 4 | | xAxisIndicesWidth | number | Width of the pointers on the X axis | 4 |

View File

@ -1,6 +1,6 @@
{ {
"name": "react-native-gifted-charts", "name": "react-native-gifted-charts",
"version": "1.2.24", "version": "1.2.25",
"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.", "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", "main": "src/index.tsx",
"files": [ "files": [

View File

@ -49,10 +49,6 @@ type Props = {
capThickness?: number; capThickness?: number;
capColor?: ColorValue; capColor?: ColorValue;
capRadius?: number; capRadius?: number;
showVerticalLines: Boolean;
verticalLinesThickness: number;
verticalLinesColor: ColorValue;
verticalLinesZIndex: number;
showXAxisIndices: Boolean; showXAxisIndices: Boolean;
xAxisIndicesHeight: number; xAxisIndicesHeight: number;
xAxisIndicesWidth: number; xAxisIndicesWidth: number;
@ -379,7 +375,7 @@ const RenderBars = (props: Props) => {
// { backgroundColor: item.frontColor || props.frontColor || 'black' }, // { backgroundColor: item.frontColor || props.frontColor || 'black' },
side !== 'right' && {zIndex: data.length - index}, side !== 'right' && {zIndex: data.length - index},
]}> ]}>
{props.showVerticalLines && ( {/* {props.showVerticalLines && (
<View <View
style={{ style={{
zIndex: props.verticalLinesZIndex, zIndex: props.verticalLinesZIndex,
@ -391,7 +387,7 @@ const RenderBars = (props: Props) => {
backgroundColor: props.verticalLinesColor, backgroundColor: props.verticalLinesColor,
}} }}
/> />
)} )} */}
{props.showXAxisIndices && ( {props.showXAxisIndices && (
<View <View
style={{ style={{

View File

@ -25,10 +25,6 @@ type Props = {
barWidth?: number; barWidth?: number;
rotateLabel?: Boolean; rotateLabel?: Boolean;
showVerticalLines: Boolean;
verticalLinesThickness: number;
verticalLinesColor: ColorValue;
verticalLinesZIndex: number;
showXAxisIndices: Boolean; showXAxisIndices: Boolean;
xAxisIndicesHeight: number; xAxisIndicesHeight: number;
xAxisIndicesWidth: number; xAxisIndicesWidth: number;
@ -256,7 +252,7 @@ const RenderStackBars = (props: Props) => {
marginRight: spacing, marginRight: spacing,
}, },
]}> ]}>
{props.showVerticalLines && ( {/* {props.showVerticalLines && (
<View <View
style={{ style={{
zIndex: props.verticalLinesZIndex, zIndex: props.verticalLinesZIndex,
@ -268,7 +264,7 @@ const RenderStackBars = (props: Props) => {
backgroundColor: props.verticalLinesColor, backgroundColor: props.verticalLinesColor,
}} }}
/> />
)} )} */}
{props.showXAxisIndices && ( {props.showXAxisIndices && (
<View <View
style={{ style={{

View File

@ -84,6 +84,8 @@ type PropTypes = {
verticalLinesThickness?: number; verticalLinesThickness?: number;
verticalLinesColor?: ColorValue; verticalLinesColor?: ColorValue;
verticalLinesZIndex?: number; verticalLinesZIndex?: number;
noOfVerticalLines?: number;
verticalLinesSpacing?: number;
showYAxisIndices?: Boolean; showYAxisIndices?: Boolean;
showXAxisIndices?: Boolean; showXAxisIndices?: Boolean;
@ -348,6 +350,13 @@ export const BarChart = (props: PropTypes) => {
props.verticalLinesThickness === 0 ? 0 : props.verticalLinesThickness || 1; props.verticalLinesThickness === 0 ? 0 : props.verticalLinesThickness || 1;
const verticalLinesColor = props.verticalLinesColor || 'lightgray'; const verticalLinesColor = props.verticalLinesColor || 'lightgray';
const verticalLinesZIndex = props.verticalLinesZIndex || -1; const verticalLinesZIndex = props.verticalLinesZIndex || -1;
let verticalLinesAr = [];
props.noOfVerticalLines
? (verticalLinesAr = [...Array(props.noOfVerticalLines).keys()])
: (verticalLinesAr = [
...Array(props.stackData ? props.stackData.length : data.length).keys(),
]);
const verticalLinesSpacing = props.verticalLinesSpacing || 0;
const showYAxisIndices = props.showYAxisIndices || false; const showYAxisIndices = props.showYAxisIndices || false;
const showXAxisIndices = props.showXAxisIndices || false; const showXAxisIndices = props.showXAxisIndices || false;
@ -1372,6 +1381,76 @@ export const BarChart = (props: PropTypes) => {
// data={props.stackData || data} // data={props.stackData || data}
keyExtractor={(item, index) => index.toString()}> keyExtractor={(item, index) => index.toString()}>
<Fragment> <Fragment>
{showVerticalLines &&
verticalLinesAr.map((item: itemType, index: number) => {
let totalSpacing = initialSpacing;
if (verticalLinesSpacing) {
totalSpacing = verticalLinesSpacing * (index + 1);
} else {
if (props.stackData) {
totalSpacing +=
(props.stackData[0].barWidth || props.barWidth || 30) / 2;
} else {
totalSpacing +=
(props.data[0].barWidth || props.barWidth || 30) / 2;
}
for (let i = 0; i < index; i++) {
let actualSpacing = spacing;
if (props.stackData) {
if (i >= props.stackData.length - 1) {
actualSpacing += (props.barWidth || 30) / 2;
} else {
if (
props.stackData[i].spacing ||
props.stackData[i].spacing === 0
) {
actualSpacing = props.stackData[i].spacing;
}
if (props.stackData[i + 1].barWidth) {
actualSpacing += props.stackData[i + 1].barWidth;
} else {
actualSpacing += props.barWidth || 30;
}
}
} else {
if (i >= props.data.length - 1) {
actualSpacing += (props.barWidth || 30) / 2;
} else {
if (
props.data[i].spacing ||
props.data[i].spacing === 0
) {
console.log('here for index ' + index + ' and i ' + i);
actualSpacing = props.data[i].spacing;
}
if (props.data[i + 1].barWidth) {
actualSpacing += props.data[i + 1].barWidth;
} else {
actualSpacing += props.barWidth || 30;
}
}
}
console.log('i = ' + i + ' actualSpacing ' + actualSpacing);
totalSpacing += actualSpacing;
}
}
return (
<View
key={index}
style={{
position: 'absolute',
zIndex: verticalLinesZIndex || -1,
marginBottom: xAxisThickness,
height: containerHeight + 15 - xAxisThickness,
width: verticalLinesThickness,
backgroundColor: verticalLinesColor,
bottom: 60,
left: totalSpacing,
}}
/>
);
})}
{showLine {showLine
? lineConfig.isAnimated ? lineConfig.isAnimated
? renderAnimatedLine() ? renderAnimatedLine()
@ -1394,10 +1473,6 @@ export const BarChart = (props: PropTypes) => {
opacity={opacity} opacity={opacity}
disablePress={item.disablePress || props.disablePress} disablePress={item.disablePress || props.disablePress}
rotateLabel={rotateLabel} rotateLabel={rotateLabel}
showVerticalLines={showVerticalLines}
verticalLinesThickness={verticalLinesThickness}
verticalLinesColor={verticalLinesColor}
verticalLinesZIndex={verticalLinesZIndex}
showXAxisIndices={showXAxisIndices} showXAxisIndices={showXAxisIndices}
xAxisIndicesHeight={xAxisIndicesHeight} xAxisIndicesHeight={xAxisIndicesHeight}
xAxisIndicesWidth={xAxisIndicesWidth} xAxisIndicesWidth={xAxisIndicesWidth}
@ -1459,10 +1534,6 @@ export const BarChart = (props: PropTypes) => {
capThickness={props.capThickness} capThickness={props.capThickness}
capColor={props.capColor} capColor={props.capColor}
capRadius={props.capRadius} capRadius={props.capRadius}
showVerticalLines={showVerticalLines}
verticalLinesThickness={verticalLinesThickness}
verticalLinesColor={verticalLinesColor}
verticalLinesZIndex={verticalLinesZIndex}
showXAxisIndices={showXAxisIndices} showXAxisIndices={showXAxisIndices}
xAxisIndicesHeight={xAxisIndicesHeight} xAxisIndicesHeight={xAxisIndicesHeight}
xAxisIndicesWidth={xAxisIndicesWidth} xAxisIndicesWidth={xAxisIndicesWidth}

View File

@ -110,6 +110,8 @@ type propTypes = {
verticalLinesThickness?: number; verticalLinesThickness?: number;
verticalLinesColor?: ColorValue; verticalLinesColor?: ColorValue;
verticalLinesZIndex?: number; verticalLinesZIndex?: number;
noOfVerticalLines?: number;
verticalLinesSpacing?: number;
hideAxesAndRules?: Boolean; hideAxesAndRules?: Boolean;
areaChart?: Boolean; areaChart?: Boolean;
@ -1377,6 +1379,12 @@ export const LineChart = (props: propTypes) => {
const hideRules = props.hideRules || false; const hideRules = props.hideRules || false;
const showVerticalLines = props.showVerticalLines || false; const showVerticalLines = props.showVerticalLines || false;
const verticalLinesUptoDataPoint = props.verticalLinesUptoDataPoint || false; const verticalLinesUptoDataPoint = props.verticalLinesUptoDataPoint || false;
let verticalLinesAr = [];
props.noOfVerticalLines
? (verticalLinesAr = [...Array(props.noOfVerticalLines).keys()])
: (verticalLinesAr = [...Array(data.length).keys()]);
const verticalLinesSpacing = props.verticalLinesSpacing || 0;
const showYAxisIndices = props.showYAxisIndices || false; const showYAxisIndices = props.showYAxisIndices || false;
const showXAxisIndices = props.showXAxisIndices || false; const showXAxisIndices = props.showXAxisIndices || false;
@ -3290,7 +3298,7 @@ export const LineChart = (props: propTypes) => {
props.width && {width: props.width + 10}, props.width && {width: props.width + 10},
]}> ]}>
{showVerticalLines && {showVerticalLines &&
data.map((item: itemType, index: number) => { verticalLinesAr.map((item: itemType, index: number) => {
return ( return (
<View <View
key={index} key={index}
@ -3299,13 +3307,17 @@ export const LineChart = (props: propTypes) => {
zIndex: verticalLinesZIndex || -1, zIndex: verticalLinesZIndex || -1,
marginBottom: xAxisThickness, marginBottom: xAxisThickness,
height: verticalLinesUptoDataPoint height: verticalLinesUptoDataPoint
? (item.value * containerHeight) / maxValue - xAxisThickness ? index < data.length
? (data[index].value * containerHeight) / maxValue -
xAxisThickness
: 0
: containerHeight + 15 - xAxisThickness, : containerHeight + 15 - xAxisThickness,
width: verticalLinesThickness, width: verticalLinesThickness,
backgroundColor: verticalLinesColor, backgroundColor: verticalLinesColor,
bottom: 60, bottom: 60,
left: left: verticalLinesSpacing
index * spacing + (initialSpacing - dataPointsWidth1 / 2), ? verticalLinesSpacing * (index + 1)
: index * spacing + (initialSpacing - dataPointsWidth1 / 2),
}} }}
/> />
); );