Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import {VictoryChartRenderArgsProvider} from '@components/HTMLEngineProvider/HTM
import getChartDesignWidth from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/getChartDesignWidth';
import getChartLayoutModeProps from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/getChartLayoutModeProps';
import getHierarchyID from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/getHierarchyID';
import resolveChartThemeColor from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/resolveChartThemeColor';
import useCurrentTimezone from '@hooks/useCurrentTimezone';
import useTheme from '@hooks/useTheme';
import ThemeContext from '@styles/theme/context/ThemeContext';
import VictoryChartLabel from './VictoryChartLabel';
import VictoryChartLegend from './VictoryChartLegend';
import VictoryChartSeries from './VictoryChartSeries';
Expand All @@ -22,16 +25,30 @@ type VictoryChartCartesianProps = {
*/
function VictoryChartCartesian({explicitSize, headless}: VictoryChartCartesianProps) {
const {tnode, data, xKey, yKeys, xAxis, yAxis, domain, domainPadding, padding, isHorizontal, labelItems, legendItems, chartContentStyles} = useVictoryChartContext();
const theme = useTheme();
Comment thread
daledah marked this conversation as resolved.
Resolved
const timezone = useCurrentTimezone();
const designWidth = getChartDesignWidth(explicitSize, chartContentStyles.width);

const resolvedXAxis = xAxis
? {
...xAxis,
lineColor: typeof xAxis.lineColor === 'string' ? resolveChartThemeColor(xAxis.lineColor, theme) : xAxis.lineColor,
labelColor: typeof xAxis.labelColor === 'string' ? resolveChartThemeColor(xAxis.labelColor, theme) : xAxis.labelColor,
}
: xAxis;
const resolvedYAxis = yAxis?.map((axis) => ({
...axis,
lineColor: typeof axis.lineColor === 'string' ? resolveChartThemeColor(axis.lineColor, theme) : axis.lineColor,
labelColor: typeof axis.labelColor === 'string' ? resolveChartThemeColor(axis.labelColor, theme) : axis.labelColor,
}));

return (
<CartesianChart
data={Object.values(data)}
xKey={xKey}
yKeys={yKeys}
xAxis={xAxis}
yAxis={yAxis}
xAxis={resolvedXAxis}
yAxis={resolvedYAxis}
domain={domain}
domainPadding={domainPadding}
padding={padding}
Expand Down Expand Up @@ -60,8 +77,12 @@ function VictoryChartCartesian({explicitSize, headless}: VictoryChartCartesianPr
return overlayContent;
}

// Chart font context does not propagate across the Skia renderOutside boundary.
return <ChartFontsLoaderProvider>{overlayContent}</ChartFontsLoaderProvider>;
// React context does not propagate across the Skia renderOutside boundary.
return (
<ThemeContext.Provider value={theme}>
<ChartFontsLoaderProvider>{overlayContent}</ChartFontsLoaderProvider>
</ThemeContext.Provider>
);
}}
>
{(renderArgs) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React from 'react';
import type {ViewStyle} from 'react-native';
import {View} from 'react-native';
import {useVictoryChartContext} from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/context/VictoryChartContext';
import {resolveChartContainerBgColor} from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/resolveChartThemeColor';
import useTheme from '@hooks/useTheme';
import type {VictoryChartContainerLayout, VictoryChartContainerThemeStyles} from './types';

type VictoryChartContainerFixedProps = {
Expand All @@ -12,7 +14,9 @@ type VictoryChartContainerFixedProps = {

function VictoryChartContainerFixed({children, layout, themeStyles}: VictoryChartContainerFixedProps) {
const {chartContentStyles, chartContainerStyles} = useVictoryChartContext();
const {backgroundColor, borderRadius, ...layoutContainerStyles} = chartContainerStyles;
const theme = useTheme();
const {backgroundColor: rawBgColor, borderRadius, ...layoutContainerStyles} = chartContainerStyles;
const backgroundColor = resolveChartContainerBgColor(rawBgColor, theme);
const layoutKind = layout.kind;
const fixedWidth = layout.kind === 'fixed' ? layout.width : undefined;
const fixedHeight = layout.kind === 'fixed' ? layout.height : undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import React from 'react';
import {View} from 'react-native';
import {useVictoryChartContext} from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/context/VictoryChartContext';
import computeChartScale from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/computeChartScale';
import {resolveChartContainerBgColor} from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/resolveChartThemeColor';
import useSafeAreaInsets from '@hooks/useSafeAreaInsets';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';

Expand All @@ -12,6 +14,7 @@ const CHAT_MESSAGE_HORIZONTAL_PADDING = 92;

function VictoryChartContainer({children}: {children: React.ReactNode}) {
const styles = useThemeStyles();
const theme = useTheme();
const {chartContentStyles, chartContainerStyles} = useVictoryChartContext();
const {windowWidth} = useWindowDimensions();
const {left: safeAreaLeft, right: safeAreaRight} = useSafeAreaInsets();
Expand All @@ -23,7 +26,8 @@ function VictoryChartContainer({children}: {children: React.ReactNode}) {
const availableWidth = windowWidth - safeAreaLeft - safeAreaRight - CHAT_MESSAGE_HORIZONTAL_PADDING;
const scale = hasExplicitDimensions ? computeChartScale(designWidth, availableWidth) : 1;

const {backgroundColor, borderRadius, ...layoutContainerStyles} = chartContainerStyles;
const {backgroundColor: rawBgColor, borderRadius, ...layoutContainerStyles} = chartContainerStyles;
Comment thread
daledah marked this conversation as resolved.
Resolved
const backgroundColor = resolveChartContainerBgColor(rawBgColor, theme);

const contentStyle = hasExplicitDimensions
? [chartContentStyles, {backgroundColor, borderRadius, overflow: 'hidden' as const, transform: [{scale}], transformOrigin: 'top left' as const}]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import getChartSkiaTypeface from '@components/Charts/utils/getChartSkiaTypeface'
import type {LabelItem} from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/types';
import computeTextAnchorPosition from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/computeTextAnchorPosition';
import {getLocalizedVictoryChartLabelText} from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/localizeVictoryChartLabelText';
import resolveChartThemeColor from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/resolveChartThemeColor';
import useTheme from '@hooks/useTheme';
import type {SelectedTimezone} from '@src/types/onyx/PersonalDetails';

type VictoryChartLabelsProps = LabelItem & {
Expand All @@ -27,10 +29,11 @@ type ProcessedLine = {
*/
function VictoryChartLabel({x, y, text, color, fontSize, fontWeight, fontFamily, fontStyle, lineHeight, textAnchor = 'start', verticalAnchor = 'middle', timezone}: VictoryChartLabelsProps) {
const typefaces = useChartTypefaces();
const theme = useTheme();
const displayText = getLocalizedVictoryChartLabelText(text, timezone);
const processedLines = displayText.split('\n').reduce(
(acc, line, index) => {
const lineColor = color?.[index];
const lineColor = resolveChartThemeColor(color?.[index], theme);
const lineFontSize = fontSize?.[index];
const lineFontWeight = fontWeight?.[index];
const lineFontFamily = fontFamily?.[index];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import React, {Fragment} from 'react';
import {useChartTypefaces} from '@components/Charts/context/ChartFontsContext';
import getChartSkiaTypeface from '@components/Charts/utils/getChartSkiaTypeface';
import type {LegendItem} from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/types';
import resolveChartThemeColor from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/resolveChartThemeColor';
import useTheme from '@hooks/useTheme';

type VictoryChartLegendProps = LegendItem & {
chartWidth?: number;
Expand All @@ -27,6 +29,7 @@ type ProcessedEntry = {
*/
function VictoryChartLegend({x, y, entries, gutter, symbolSpacer, chartWidth}: VictoryChartLegendProps) {
const typefaces = useChartTypefaces();
const theme = useTheme();
const processedEntries = entries.reduce(
(acc, {text, color, fontSize, fontWeight, fontFamily, fontStyle, symbolColor, symbolSize}) => {
const typeface = getChartSkiaTypeface(typefaces, {fontFamily, fontStyle, fontWeight});
Expand All @@ -39,6 +42,7 @@ function VictoryChartLegend({x, y, entries, gutter, symbolSpacer, chartWidth}: V
const textX = acc.x;
const textY = y - lineHeight / 2;
acc.x += (font?.getGlyphWidths(font.getGlyphIDs(text)).reduce((totalWidth, width) => totalWidth + width, 0) ?? 0) + (gutter ?? 0);
const resolvedColor = resolveChartThemeColor(color, theme);

acc.entries.push({
symbolX,
Expand All @@ -49,7 +53,7 @@ function VictoryChartLegend({x, y, entries, gutter, symbolSpacer, chartWidth}: V
textY,
text,
font,
color,
color: resolvedColor,
});
return acc;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import React from 'react';
import type {TNode} from 'react-native-render-html';
import {HTMLContentModel, useAmbientTRenderEngine} from 'react-native-render-html';
import {Pie} from 'victory-native';
import VictoryTheme from '@components/Charts/VictoryTheme';
import {useVictoryChartContext} from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/context/VictoryChartContext';
import parseShiftedLineSegmentNode from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/parsers/shiftedLineSegmentParser';
import parseVictoryLabelNode from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/parsers/victoryLabelParser';
import type {PolarChartData} from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/types';
import convertAngleToArcLength from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/convertAngleToArcLength';
import {parseAttributeAsNumber, parseAttributeAsStringArray} from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/parseAttribute';
import parseComponent from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/parseComponent';
import resolveChartThemeColor from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/resolveChartThemeColor';
import useTheme from '@hooks/useTheme';
import VictoryChartPieLabel from './VictoryChartPieLabel';

type VictoryChartPieProps = {tnode: TNode};
Expand All @@ -23,6 +24,7 @@ const ODD_SLICE_LABEL_RADIUS_FACTOR = 0.8;

function VictoryChartPie({tnode}: VictoryChartPieProps) {
const {data, chartContainerStyles} = useVictoryChartContext();
const theme = useTheme();
const renderEngine = useAmbientTRenderEngine();
const labelComponentNode = parseComponent(tnode.attributes.labelcomponent, renderEngine, 'victorylabel', HTMLContentModel.textual);
const baseLabelItem = labelComponentNode ? parseVictoryLabelNode(labelComponentNode).labelItems?.at(0) : undefined;
Expand All @@ -33,10 +35,12 @@ function VictoryChartPie({tnode}: VictoryChartPieProps) {
const radius = parseAttributeAsNumber(tnode.attributes.radius);
const size = radius ? radius * 2 : undefined;
const angularStrokeWidth = padAngle && radius ? 2 * convertAngleToArcLength(padAngle, radius) : 0;
const angularStrokeColor = typeof chartContainerStyles.backgroundColor === 'string' ? chartContainerStyles.backgroundColor : VictoryTheme.colors.default;
const resolvedBgColor = resolveChartThemeColor(typeof chartContainerStyles.backgroundColor === 'string' ? chartContainerStyles.backgroundColor : undefined, theme);
const angularStrokeColor = resolvedBgColor ?? theme.cardBG;
const labelIndicatorNode = parseComponent(tnode.attributes.labelindicator, renderEngine, 'shiftedlinesegment', HTMLContentModel.block);
const labelIndicatorStyles = labelIndicatorNode ? parseShiftedLineSegmentNode(labelIndicatorNode) : undefined;
const {xShift: labelIndicatorXShift, yShift: labelIndicatorYShift, stroke: labelIndicatorStroke, strokeWidth: labelIndicatorStrokeWidth} = labelIndicatorStyles ?? {};
const {xShift: labelIndicatorXShift, yShift: labelIndicatorYShift, strokeWidth: labelIndicatorStrokeWidth} = labelIndicatorStyles ?? {};
const labelIndicatorStroke = resolveChartThemeColor(labelIndicatorStyles?.stroke, theme);
const labelIndicatorInnerOffset = parseAttributeAsNumber(tnode.attributes.labelindicatorinneroffset);
const labelIndicatorOuterOffset = parseAttributeAsNumber(tnode.attributes.labelindicatorouteroffset);
const perSliceData = Object.values(data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import getChartDesignWidth from '@components/HTMLEngineProvider/HTMLRenderers/Vi
import getChartLayoutModeProps from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/getChartLayoutModeProps';
import getHierarchyID from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/getHierarchyID';
import useCurrentTimezone from '@hooks/useCurrentTimezone';
import useTheme from '@hooks/useTheme';
import ThemeContext from '@styles/theme/context/ThemeContext';
import VictoryChartCategories from './VictoryChartCategories';
import VictoryChartLabel from './VictoryChartLabel';
import VictoryChartLegend from './VictoryChartLegend';
Expand All @@ -21,6 +23,7 @@ type VictoryChartPolarProps = {
*/
function VictoryChartPolar({explicitSize, headless}: VictoryChartPolarProps) {
const {tnode, data, labelItems, legendItems, chartContentStyles} = useVictoryChartContext();
const theme = useTheme();
const timezone = useCurrentTimezone();
const designWidth = getChartDesignWidth(explicitSize, chartContentStyles.width);

Expand Down Expand Up @@ -57,7 +60,13 @@ function VictoryChartPolar({explicitSize, headless}: VictoryChartPolarProps) {
colorKey={COLOR_KEY}
{...getChartLayoutModeProps(explicitSize, headless)}
>
{headless ? chartContent : <ChartFontsLoaderProvider>{chartContent}</ChartFontsLoaderProvider>}
{headless ? (
chartContent
) : (
<ThemeContext.Provider value={theme}>
<ChartFontsLoaderProvider>{chartContent}</ChartFontsLoaderProvider>
</ThemeContext.Provider>
)}
</PolarChart>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type {Color} from '@shopify/react-native-skia';
import type {ColorValue} from 'react-native';
import colors from '@styles/theme/colors';
import type {ThemeColors} from '@styles/theme/types';

/**
* Only the chart background colors are remapped. Data-visualization colors (series /
* slice fills, legend symbols) are intentionally excluded so they remain
* consistent across themes.
*/
type ChartThemeColorKey = 'cardBG' | 'heading' | 'textSupporting' | 'border';

const CHART_CHROME_COLOR_MAP = new Map<string, ChartThemeColorKey>(
(
[
[colors.productLight200, 'cardBG'],
[colors.productLight900, 'heading'],
[colors.productLight800, 'textSupporting'],
Comment on lines +16 to +18

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Include the existing chart colors in the dark-mode map

Charts already covered by this renderer still use nearby hex values that are not in this exact-match map: server/victory-chart-renderer/tests/fixtures/monthly-spend.xml and the top-categories fixtures set the card background to #F7F2EF, and monthly/top-employees use supporting text #73857E. In dark theme those values fall through unchanged while mapped heading/border colors change, so those existing chart payloads render with a light card instead of a dark card; please add these aliases or normalize the incoming palette.

Useful? React with 👍 / 👎.

[colors.productLight400, 'border'],
] as const
).map(([hex, key]) => [hex.toLowerCase(), key]),
);

function resolveChartThemeColor(color: string | undefined, theme: ThemeColors): string | undefined;
function resolveChartThemeColor(color: Color | undefined, theme: ThemeColors): Color | undefined;
function resolveChartThemeColor(color: Color | undefined, theme: ThemeColors): Color | undefined {
if (typeof color !== 'string') {
return color;
}
const themeKey = CHART_CHROME_COLOR_MAP.get(color.toLowerCase());
return themeKey ? theme[themeKey] : color;
}

/**
* Resolves a `ViewStyle.backgroundColor` (ColorValue) through the chart theme
* color map. Non-string color values pass through unchanged.
*/
function resolveChartContainerBgColor(bgColor: ColorValue | undefined, theme: ThemeColors): ColorValue | undefined {
return typeof bgColor === 'string' ? (resolveChartThemeColor(bgColor, theme) ?? bgColor) : bgColor;
}

export default resolveChartThemeColor;
export {resolveChartContainerBgColor};
Loading