UNPKG

victory-native

Version:

A charting library for React Native with a focus on performance and customization.

113 lines (112 loc) 3.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getHorizontalStackedBarOptionsContext = exports.getVerticalStackedBarOptionsContext = exports.getStackedBarSegments = void 0; const getStackedBarSegments = (points) => { const stacksByCategory = new Map(); const segments = []; points.forEach((pointsArray, seriesIndex) => { pointsArray.forEach((point, datumIndex) => { if (!hasRenderableEndpoint(point)) return; const categoryKey = point.xValue; const stack = getCategoryStack(stacksByCategory, categoryKey); const value = getFiniteValue(point.yValue); if (value === 0) { segments.push({ point, categoryKey, value, startValue: 0, endValue: 0, seriesIndex, datumIndex, isPositive: false, isStart: false, isEnd: false, }); return; } const isPositive = value > 0; const signSegments = isPositive ? stack.positiveSegments : stack.negativeSegments; const startValue = isPositive ? stack.positiveTotal : stack.negativeTotal; const endValue = startValue + value; const segment = { point, categoryKey, value, startValue, endValue, seriesIndex, datumIndex, isPositive, isStart: signSegments.length === 0, isEnd: false, }; signSegments.push(segment); segments.push(segment); if (isPositive) { stack.positiveTotal = endValue; } else { stack.negativeTotal = endValue; } }); }); stacksByCategory.forEach(({ positiveSegments, negativeSegments }) => { const positiveEnd = positiveSegments.at(-1); const negativeEnd = negativeSegments.at(-1); if (positiveEnd) positiveEnd.isEnd = true; if (negativeEnd) negativeEnd.isEnd = true; }); return segments; }; exports.getStackedBarSegments = getStackedBarSegments; const getVerticalStackedBarOptionsContext = (segment) => { const { seriesIndex, datumIndex, isPositive, isStart, isEnd } = segment; return { columnIndex: seriesIndex, rowIndex: datumIndex, seriesIndex, datumIndex, isStart, isEnd, isBottom: isPositive ? isStart : isEnd, isTop: isPositive ? isEnd : isStart, }; }; exports.getVerticalStackedBarOptionsContext = getVerticalStackedBarOptionsContext; const getHorizontalStackedBarOptionsContext = (segment) => { const { seriesIndex, datumIndex, isPositive, isStart, isEnd } = segment; return { columnIndex: seriesIndex, rowIndex: datumIndex, seriesIndex, datumIndex, isStart, isEnd, isLeft: isPositive ? isStart : isEnd, isRight: isPositive ? isEnd : isStart, }; }; exports.getHorizontalStackedBarOptionsContext = getHorizontalStackedBarOptionsContext; const getCategoryStack = (stacksByCategory, categoryKey) => { const stack = stacksByCategory.get(categoryKey); if (stack) return stack; const nextStack = { positiveTotal: 0, negativeTotal: 0, positiveSegments: [], negativeSegments: [], }; stacksByCategory.set(categoryKey, nextStack); return nextStack; }; const hasRenderableEndpoint = (point) => typeof point.y === "number" && Number.isFinite(point.x) && Number.isFinite(point.y); const getFiniteValue = (value) => typeof value === "number" && Number.isFinite(value) ? value : 0;