Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions patches/victory-native/details.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# `victory-native` patches

### [victory-native+41.20.2+001+horizontal-bars.patch](victory-native+41.20.2+001+horizontal-bars.patch)

- Reason:

```
This patch adds support to horizontal bars when rendered in <BarGroup />
```

- Upstream PR/issue: Not yet. This is urgent patch with deadline.
- E/App issue: https://github.com/Expensify/Expensify/issues/636601
- PR introducing patch: https://github.com/Expensify/App/pull/91659
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
diff --git a/node_modules/victory-native/dist/cartesian/components/BarGroup.d.ts b/node_modules/victory-native/dist/cartesian/components/BarGroup.d.ts
index b486f76..45788f0 100644
--- a/node_modules/victory-native/dist/cartesian/components/BarGroup.d.ts
+++ b/node_modules/victory-native/dist/cartesian/components/BarGroup.d.ts
@@ -16,6 +16,7 @@ type BarGroupProps = {
groupWidth: number;
gapWidth: number;
}) => void;
+ isHorizontal?: boolean;
};
export declare function BarGroup({ betweenGroupPadding, withinGroupPadding, chartBounds, roundedCorners, children, onBarSizeChange, barWidth: customBarWidth, barCount, }: BarGroupProps): React.DetailedReactHTMLElement<React.InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>[] | null;
export declare namespace BarGroup {
diff --git a/node_modules/victory-native/dist/cartesian/hooks/useBarGroupPaths.d.ts b/node_modules/victory-native/dist/cartesian/hooks/useBarGroupPaths.d.ts
index 63b69bc..75641b1 100644
--- a/node_modules/victory-native/dist/cartesian/hooks/useBarGroupPaths.d.ts
+++ b/node_modules/victory-native/dist/cartesian/hooks/useBarGroupPaths.d.ts
@@ -1,6 +1,6 @@
import type { ChartBounds, PointsArray } from "../../types";
import { type RoundedCorners } from "../../utils/createRoundedRectPath";
-export declare const useBarGroupPaths: (points: PointsArray[], chartBounds: ChartBounds, betweenGroupPadding?: number, withinGroupPadding?: number, roundedCorners?: RoundedCorners, customBarWidth?: number, barCount?: number) => {
+export declare const useBarGroupPaths: (points: PointsArray[], chartBounds: ChartBounds, betweenGroupPadding?: number, withinGroupPadding?: number, roundedCorners?: RoundedCorners, customBarWidth?: number, barCount?: number, isHorizontal?: boolean) => {
barWidth: number;
groupWidth: number;
gapWidth: number;
diff --git a/node_modules/victory-native/src/cartesian/components/BarGroup.tsx b/node_modules/victory-native/src/cartesian/components/BarGroup.tsx
index f46fad7..ad75c04 100644
--- a/node_modules/victory-native/src/cartesian/components/BarGroup.tsx
+++ b/node_modules/victory-native/src/cartesian/components/BarGroup.tsx
@@ -20,6 +20,7 @@ type BarGroupProps = {
groupWidth: number;
gapWidth: number;
}) => void;
+ isHorizontal?: boolean;
};

export function BarGroup({
@@ -31,6 +32,7 @@ export function BarGroup({
onBarSizeChange,
barWidth: customBarWidth,
barCount,
+ isHorizontal,
}: BarGroupProps) {
// Collect the bar props
const bars = [] as BarGroupBarProps[];
@@ -49,6 +51,7 @@ export function BarGroup({
roundedCorners,
customBarWidth,
barCount,
+ isHorizontal,
);

// Handle bar size change
diff --git a/node_modules/victory-native/src/cartesian/hooks/useBarGroupPaths.ts b/node_modules/victory-native/src/cartesian/hooks/useBarGroupPaths.ts
index 4bfb6d4..9805bfe 100644
--- a/node_modules/victory-native/src/cartesian/hooks/useBarGroupPaths.ts
+++ b/node_modules/victory-native/src/cartesian/hooks/useBarGroupPaths.ts
@@ -15,17 +15,19 @@ export const useBarGroupPaths = (
roundedCorners?: RoundedCorners,
customBarWidth?: number,
barCount?: number,
+ isHorizontal?: boolean,
) => {
const numGroups = points[0]?.length || 0;

- const { yScale } = useCartesianChartContext();
+ const { xScale, yScale } = useCartesianChartContext();

const groupWidth = React.useMemo(() => {
+ const boundSize = isHorizontal ? chartBounds.top - chartBounds.bottom : chartBounds.right - chartBounds.left;
Comment thread
s77rt marked this conversation as resolved.
Resolved
return (
- ((1 - betweenGroupPadding) * (chartBounds.right - chartBounds.left)) /
+ ((1 - betweenGroupPadding) * boundSize) /
Math.max(1, numGroups)
);
- }, [betweenGroupPadding, chartBounds.left, chartBounds.right, numGroups]);
+ }, [betweenGroupPadding, chartBounds, numGroups, isHorizontal]);

const barWidth = React.useMemo(() => {
if (customBarWidth) return customBarWidth;
@@ -46,19 +48,24 @@ export const useBarGroupPaths = (
const offset = -groupWidth / 2 + i * (barWidth + gapWidth);
pointSet.forEach(({ x, y, yValue }) => {
if (typeof y !== "number") return;
- const barHeight = yScale(0) - y;
+ const barHeight = isHorizontal ? xScale(0) - x : yScale(0) - y;
Comment thread
s77rt marked this conversation as resolved.
Resolved
if (roundedCorners) {
const nonUniformRoundedRect = createRoundedRectPath(
- x + offset,
- y,
- barWidth,
- barHeight,
- roundedCorners,
+ isHorizontal ? x : x + offset,
+ isHorizontal ? y + offset : y,
+ isHorizontal ? barHeight : barWidth,
+ isHorizontal ? barWidth : barHeight,
+ isHorizontal ? {topLeft: roundedCorners.bottomLeft, topRight: roundedCorners.topLeft, bottomLeft: roundedCorners.bottomRight, bottomRight: roundedCorners.topRight} : roundedCorners,
Number(yValue),
);
p.addRRect(nonUniformRoundedRect);
} else {
- p.addRect(Skia.XYWHRect(x + offset, y, barWidth, barHeight));
+ p.addRect(Skia.XYWHRect(
+ isHorizontal ? x : x + offset,
+ isHorizontal ? y + offset : y,
+ isHorizontal ? barHeight : barWidth,
+ isHorizontal ? barWidth : barHeight,
+ ));
}
});
return p;
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,37 @@ import {useVictoryChartRenderArgs} from '@components/HTMLEngineProvider/HTMLRend
import getYKey from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/getYKey';
import parseAttribute from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/parseAttribute';
import parseCornerRadius from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/parseCornerRadius';
import parseOffset from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/parseOffset';
import parseStyles from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/parseStyles';

type VictoryCharBarGroupProps = {tnode: TNode};
type VictoryChartBarGroupProps = {
tnode: TNode;
isHorizontal?: boolean;
};

const BAR_BETWEEN_GROUP_PADDING = 2 / 3;
const BAR_WITHIN_GROUP_PADDING = BAR_INNER_PADDING;

function VictoryCharBarGroup({tnode}: VictoryCharBarGroupProps) {
function VictoryChartBarGroup({tnode, isHorizontal}: VictoryChartBarGroupProps) {
const {points, chartBounds} = useVictoryChartRenderArgs();
const barChildren = tnode.children.filter((child) => child.tagName === 'victorybar');
const firstBarChild = barChildren.at(0);

if (!firstBarChild) {
return null;
}

const roundedCorners = parseCornerRadius(firstBarChild?.attributes?.cornerradius ?? '');
const barWidth = firstBarChild.attributes.barwidth !== undefined ? Number(parseAttribute(firstBarChild.attributes.barwidth)) : undefined;
const betweenGroupPadding = barWidth
? parseOffset(tnode.attributes.offset, chartBounds, barChildren.length, barWidth, points[getYKey(firstBarChild)].length, isHorizontal ?? false)
: undefined;

return (
<BarGroup
chartBounds={chartBounds}
betweenGroupPadding={BAR_BETWEEN_GROUP_PADDING}
withinGroupPadding={BAR_WITHIN_GROUP_PADDING}
roundedCorners={parseCornerRadius(barChildren.at(0)?.attributes?.cornerradius ?? '')}
barWidth={parseAttribute(barChildren.at(0)?.attributes?.barwidth ?? '')}
betweenGroupPadding={betweenGroupPadding}
withinGroupPadding={BAR_INNER_PADDING}
roundedCorners={roundedCorners}
barWidth={barWidth}
isHorizontal={isHorizontal}
>
{barChildren.map((child) => {
const yKey = getYKey(child);
Expand All @@ -40,6 +54,6 @@ function VictoryCharBarGroup({tnode}: VictoryCharBarGroupProps) {
);
}

VictoryCharBarGroup.displayName = 'VictoryCharBarGroup';
VictoryChartBarGroup.displayName = 'VictoryChartBarGroup';

export default VictoryCharBarGroup;
export default VictoryChartBarGroup;
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import {CartesianChart} from 'victory-native';
import {useVictoryChartContext} from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/context/VictoryChartContext';
import {VictoryChartRenderArgsProvider} from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/context/VictoryChartRenderArgsContext';
import getYKey from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/getYKey';
import parseAttribute from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/parseAttribute';
import parseDomainPadding from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/parseDomainPadding';
import VictoryChartLabels from './VictoryChartLabels';
import VictoryChartLegend from './VictoryChartLegend';
import VictoryChartSeries from './VictoryChartSeries';
Expand All @@ -14,7 +12,7 @@ import VictoryChartSeries from './VictoryChartSeries';
* Labels and legend overlays are handled internally via `renderOutside`.
*/
function VictoryChartCartesian() {
const {data, xKey, yKeys, xAxis, yAxis, tnode, labelItems, legendItems} = useVictoryChartContext();
const {tnode, data, xKey, yKeys, xAxis, yAxis, domain, domainPadding, padding, isHorizontal, labelItems, legendItems} = useVictoryChartContext();

return (
<CartesianChart
Expand All @@ -23,9 +21,9 @@ function VictoryChartCartesian() {
yKeys={yKeys}
xAxis={xAxis}
yAxis={yAxis}
domain={parseAttribute(tnode.attributes.domain)}
domainPadding={parseDomainPadding(tnode.attributes.domainpadding)}
padding={parseAttribute(tnode.attributes.padding)}
domain={domain}
domainPadding={domainPadding}
padding={padding}
renderOutside={(renderArgs) => (
<VictoryChartRenderArgsProvider value={renderArgs}>
<VictoryChartLabels labelItems={labelItems} />
Expand All @@ -39,6 +37,7 @@ function VictoryChartCartesian() {
<VictoryChartSeries
key={`${child.tagName ?? 'node'}-${getYKey(child)}`}
tnode={child}
isHorizontal={isHorizontal}
/>
))}
</VictoryChartRenderArgsProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import VictoryChartBar from './VictoryChartBar';
import VictoryChartBarGroup from './VictoryChartBarGroup';
import VictoryChartLine from './VictoryChartLine';

type VictoryChartSeriesProps = {tnode: TNode};
type VictoryChartSeriesProps = {
tnode: TNode;
isHorizontal?: boolean;
};

type SeriesComponent = (props: VictoryChartSeriesProps) => React.ReactElement | null;

Expand All @@ -19,7 +22,7 @@ const SERIES_RENDERERS: Partial<Record<string, SeriesComponent>> = {
victoryline: VictoryChartLine,
};

function VictoryChartSeries({tnode}: VictoryChartSeriesProps) {
function VictoryChartSeries({tnode, isHorizontal}: VictoryChartSeriesProps) {
const SeriesRenderer = SERIES_RENDERERS[tnode.tagName ?? ''];

useEffect(() => {
Expand All @@ -33,7 +36,12 @@ function VictoryChartSeries({tnode}: VictoryChartSeriesProps) {
return null;
}

return <SeriesRenderer tnode={tnode} />;
return (
<SeriesRenderer
tnode={tnode}
isHorizontal={isHorizontal}
/>
);
}

VictoryChartSeries.displayName = 'VictoryChartSeries';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ type VictoryChartContextValue = {
yKeys: ProcessNodeResult['yKeys'];
xAxis: ProcessNodeResult['xAxis'];
yAxis: ProcessNodeResult['yAxis'];
domain: ProcessNodeResult['domain'];
domainPadding: ProcessNodeResult['domainPadding'];
padding: ProcessNodeResult['padding'];
isHorizontal: ProcessNodeResult['isHorizontal'];
categories: ProcessNodeResult['categories'];
labelItems: ProcessNodeResult['labelItems'];
legendItems: ProcessNodeResult['legendItems'];
chartContentStyles: ReturnType<typeof parseStyles>['nodeStyles'];
Expand All @@ -28,7 +33,7 @@ const VictoryChartContext = createContext<VictoryChartContextValue | null>(null)
*/
function VictoryChartProvider({tnode, children}: {tnode: TNode; children: React.ReactNode}) {
const {regular: regularTypeface} = useChartDefaultTypeface();
const {data, xKey, yKeys, xAxis, yAxis, labelItems, legendItems} = processVictoryChartTree(tnode, regularTypeface);
const {data, xKey, yKeys, xAxis, yAxis, domain, domainPadding, padding, isHorizontal, categories, labelItems, legendItems} = processVictoryChartTree(tnode, regularTypeface, null);
const {nodeStyles: chartContentStyles, parentNodeStyles: chartContainerStyles} = parseStyles(tnode);

const hasCartesianData = Object.keys(data).length > 0;
Expand All @@ -55,6 +60,11 @@ function VictoryChartProvider({tnode, children}: {tnode: TNode; children: React.
yKeys,
xAxis,
yAxis,
domain,
domainPadding,
padding,
isHorizontal,
categories,
labelItems,
legendItems,
chartContentStyles,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {NodeParser} from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/types';
import parseVictoryAxisNode from './victoryAxisParser';
import parseVictoryChartNode from './victoryChartParser';
import parseVictoryLabelNode from './victoryLabelParser';
import parseVictoryLegendNode from './victoryLegendParser';
import parseVictorySeriesNode from './victorySeriesParser';
Expand All @@ -9,6 +10,7 @@ import parseVictorySeriesNode from './victorySeriesParser';
* To support a new VictoryChart tag, add a new entry here and create the parser file.
*/
const PARSER_REGISTRY: Partial<Record<string, NodeParser>> = {
victorychart: parseVictoryChartNode,
victorybar: parseVictorySeriesNode,
victoryline: parseVictorySeriesNode,
victoryaxis: parseVictoryAxisNode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ import PARSER_REGISTRY from './parserRegistry';
* Recursively walk the HTML tnode tree, dispatching each node to its registered parser
* and merging the results into a single chart config.
*/
function processVictoryChartTree(tnode: TNode, typeface: SkTypeface | null): ProcessNodeResult {
function processVictoryChartTree(tnode: TNode, typeface: SkTypeface | null, rootProcessedResult: ProcessNodeResult | null): ProcessNodeResult {
const data: ProcessNodeResult['data'] = {};
const yKeys: ProcessNodeResult['yKeys'] = [];
let xAxis: ProcessNodeResult['xAxis'];
let yAxis: ProcessNodeResult['yAxis'];
let domain: ProcessNodeResult['domain'];
let domainPadding: ProcessNodeResult['domainPadding'];
let padding: ProcessNodeResult['padding'];
let isHorizontal: ProcessNodeResult['isHorizontal'];
let categories: ProcessNodeResult['categories'];
const labelItems: ProcessNodeResult['labelItems'] = [];
const legendItems: ProcessNodeResult['legendItems'] = [];

const parser = PARSER_REGISTRY[tnode.tagName ?? ''];
if (parser) {
const result = parser(tnode, typeface);
const result = parser(tnode, typeface, rootProcessedResult);
if (result.data) {
lodashMerge(data, result.data);
}
Expand All @@ -32,6 +37,21 @@ function processVictoryChartTree(tnode: TNode, typeface: SkTypeface | null): Pro
if (result.yAxis?.length) {
yAxis = [...(yAxis ?? []), ...result.yAxis];
}
if (result.domain) {
domain = result.domain;
}
if (result.domainPadding) {
domainPadding = result.domainPadding;
}
if (result.padding) {
padding = result.padding;
}
if (result.isHorizontal) {
isHorizontal = result.isHorizontal;
}
if (result.categories) {
categories = result.categories;
}
if (result.labelItems) {
labelItems.push(...result.labelItems);
}
Expand All @@ -40,8 +60,11 @@ function processVictoryChartTree(tnode: TNode, typeface: SkTypeface | null): Pro
}
}

// If we have `rootProcessedResult` then forward it as is, otherwise we must be the root so pass the data that we just built
const rootProcessedNodeResult = rootProcessedResult ?? {data, xKey: X_KEY, yKeys, xAxis, yAxis, domain, domainPadding, padding, isHorizontal, categories, labelItems, legendItems};

for (const child of tnode.children) {
const childResult = processVictoryChartTree(child, typeface);
const childResult = processVictoryChartTree(child, typeface, rootProcessedNodeResult);
lodashMerge(data, childResult.data);
yKeys.push(...childResult.yKeys);
if (childResult.xAxis) {
Expand All @@ -50,11 +73,26 @@ function processVictoryChartTree(tnode: TNode, typeface: SkTypeface | null): Pro
if (childResult.yAxis?.length) {
yAxis = [...(yAxis ?? []), ...childResult.yAxis];
}
if (childResult.domain) {
Comment thread
s77rt marked this conversation as resolved.
Resolved
domain = childResult.domain;
}
if (childResult.domainPadding) {
domainPadding = childResult.domainPadding;
}
if (childResult.padding) {
padding = childResult.padding;
}
if (childResult.isHorizontal) {
isHorizontal = childResult.isHorizontal;
}
if (childResult.categories) {
categories = childResult.categories;
}
labelItems.push(...childResult.labelItems);
legendItems.push(...childResult.legendItems);
}

return {data, xKey: X_KEY, yKeys, xAxis, yAxis, labelItems, legendItems};
return {data, xKey: X_KEY, yKeys, xAxis, yAxis, domain, domainPadding, padding, isHorizontal, categories, labelItems, legendItems};
}

export default processVictoryChartTree;
Loading
Loading