import React from 'react';
import {Text, View} from 'react-native';
import Rule from '../lineSvg';
import {styles} from '../../LineChart/styles';
import {
getHorizSectionVals,
yAxisSides,
HorizSectionsType,
horizSectionPropTypes,
chartTypes,
} from 'gifted-charts-core';
export const renderHorizSections = (props: horizSectionPropTypes) => {
const {
chartType,
width,
noOfSectionsBelowXAxis,
totalWidth,
endSpacing,
yAxisSide,
horizontalRulesStyle,
noOfSections,
sectionColors,
stepHeight,
negativeStepHeight,
yAxisLabelWidth,
yAxisLabelContainerStyle,
yAxisThickness,
yAxisColor,
xAxisThickness,
xAxisColor,
xAxisLength,
xAxisType,
dashWidth,
dashGap,
backgroundColor,
hideRules,
rulesLength,
rulesType,
rulesThickness,
rulesColor,
rulesConfigArray,
spacing,
showYAxisIndices,
yAxisIndicesHeight,
yAxisIndicesWidth,
yAxisIndicesColor,
hideOrigin,
hideYAxisText,
yAxisTextNumberOfLines,
yAxisTextStyle,
rotateYAxisTexts,
rtl,
containerHeight,
maxValue,
yAxisOffset,
horizontal,
yAxisAtTop,
secondaryYAxis,
onlyReferenceLines,
onlyLabels,
renderReferenceLines,
secondaryXAxis,
customBackground,
allowFontScaling,
} = props;
const {
secondaryYAxisConfig,
horizSections,
yAxisExtraHeightAtTop,
secondaryHorizSections,
showReferenceLine1,
referenceLine1Config,
referenceLine1Position,
showReferenceLine2,
referenceLine2Config,
referenceLine2Position,
showReferenceLine3,
referenceLine3Config,
referenceLine3Position,
horizSectionsBelow,
secondaryHorizSectionsBelow,
getLabelTexts,
getLabelTextsForSecondaryYAxis,
} = getHorizSectionVals(props);
const secondaryYAxisExtraHeightAtBottom = 10;
const negativeSectionsCountDiffPrimaryVsSecondary =
secondaryHorizSectionsBelow.length - horizSectionsBelow.length;
const isLineChart = chartType === chartTypes.LINE;
const isLineBiColor = chartType === chartTypes.LINE_BI_COLOR;
const chartWiseTotalWidth =
chartType === chartTypes.BUBBLE ? totalWidth : totalWidth - spacing;
const renderAxesAndRules = (index: number) => {
const invertedIndex = horizSections.length - index - 1;
const rulesConfigArrayLocal = rulesConfigArray[invertedIndex - 1];
return (
{index === noOfSections ? (
) : hideRules ? null : (
)}
{showYAxisIndices && index !== noOfSections ? (
) : null}
);
};
const renderExtraHeightOfYAxisAtTop = () => (
);
const renderSecondaryYaxisLabels = (
horizSections: HorizSectionsType,
isBelow: boolean,
) => {
return horizSections.map((sectionItems, index) => {
let label = getLabelTextsForSecondaryYAxis(sectionItems.value, index);
if (secondaryYAxisConfig.hideOrigin && index === 0) {
label = '';
}
return (
{secondaryYAxisConfig.showYAxisIndices && (index !== 0 || isBelow) ? (
) : null}
{label}
);
});
};
const referenceLines = () => {
return (
<>
{showReferenceLine1 ? (
{referenceLine1Config.labelText ? (
{referenceLine1Config.labelText}
) : null}
) : null}
{showReferenceLine2 ? (
{referenceLine2Config.labelText ? (
{referenceLine2Config.labelText}
) : null}
) : null}
{showReferenceLine3 ? (
{referenceLine3Config.labelText ? (
{referenceLine3Config.labelText}
) : null}
) : null}
>
);
};
const horizSectionsBelowRenderer = (index: number, showBorder: boolean) => {
// negative sections height will correspond to negative Y-axis config in case there are extra negative horiz sections corresponding to the secondary Y axis
const localNegativeStepHeight = !showBorder
? (secondaryYAxisConfig.negativeStepHeight ?? negativeStepHeight)
: negativeStepHeight;
return (
{hideRules ? null : (
)}
);
};
const leftShiftForRIghtYaxis =
(width ? width + (isLineChart ? 50 : 20) : totalWidth) +
yAxisLabelWidth / 2 +
endSpacing -
(isLineChart ? 70 : 40);
return (
<>
{onlyReferenceLines ? (
{referenceLines()}
) : (
{onlyLabels ? (
!hideYAxisText &&
horizSections.map((sectionItems, index) => {
let label = getLabelTexts(sectionItems.value, index);
if (hideOrigin && index === horizSections.length - 1) {
label = '';
}
return (
{label}
);
})
) : (
<>
{customBackground ? (
{customBackground.component
? customBackground.component()
: null}
) : null}
{yAxisExtraHeightAtTop ? renderExtraHeightOfYAxisAtTop() : null}
{horizSections.map((sectionItems, index) => {
return (
{renderAxesAndRules(index)}
);
})}
{
/***********************************************************************************************/
/************************** Render the y axis labels separately **********************/
/***********************************************************************************************/
!hideYAxisText &&
horizSections.map((sectionItems, index) => {
let label = getLabelTexts(sectionItems.value, index);
if (hideOrigin && index === horizSections.length - 1) {
label = '';
}
return (
{label}
);
})
/***********************************************************************************************/
/***********************************************************************************************/
}
{horizSectionsBelow.map((_, index) =>
horizSectionsBelowRenderer(index, true),
)}
{
/***********************************************************************************************/
/* If more -ve sections in Secondary Y-axis, then we need to render the Rules for them */
/***********************************************************************************************/
secondaryYAxis &&
negativeSectionsCountDiffPrimaryVsSecondary > 0
? [
...Array(
negativeSectionsCountDiffPrimaryVsSecondary,
).keys(),
].map((_, index) =>
horizSectionsBelowRenderer(index, false),
)
: null
}
{
/***********************************************************************************************/
/************************* Render the y axis labels below origin *********************/
/***********************************************************************************************/
!hideYAxisText &&
horizSectionsBelow.map((_, index) => {
const invertedIndex =
horizSectionsBelow.length - 1 - index;
let label = getLabelTexts(
horizSectionsBelow[invertedIndex].value,
index,
);
return (
{label}
);
})
/***********************************************************************************************/
/***********************************************************************************************/
}
{/***********************************************************************************************/
/************************* Render the reference lines separately *********************/
/***********************************************************************************************/}
{renderReferenceLines ? referenceLines() : null}
>
)}
{
/***********************************************************************************************/
/************************* Render the secondary Y Axis *********************/
/***********************************************************************************************/
!onlyLabels && secondaryYAxis ? (
{!secondaryYAxisConfig.hideYAxisText
? renderSecondaryYaxisLabels(secondaryHorizSections, false)
: null}
) : null
}
{
/***********************************************************************************************/
/************************* Render the secondary Y Axis below origin *********************/
/***********************************************************************************************/
!onlyLabels &&
secondaryYAxis &&
secondaryYAxisConfig.noOfSectionsBelowXAxis ? (
{!secondaryYAxisConfig.hideYAxisText
? renderSecondaryYaxisLabels(
secondaryHorizSectionsBelow,
true,
)
: null}
) : null
}
)}
>
);
};