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
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ function HorizontalBarChartContentBody({data, isLoading, yAxisUnit, yAxisUnitPos
<ChartYAxisLabels
yTicks={chartData.map((point) => point.y)}
yScale={args.yScale}
chartBounds={args.chartBounds}
canvasWidth={args.canvasSize.width}
fontSize={variables.iconSizeExtraSmall}
fontManager={fontManager}
labelColor={theme.textSupporting}
Expand Down
28 changes: 19 additions & 9 deletions src/components/Charts/BarChart/VerticalBarChartContent.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ActivityIndicator from '@components/ActivityIndicator';
import BAR_INNER_PADDING, {VERTICAL_BAR_DOMAIN_PADDING} from '@components/Charts/barChartConstants';
import ChartGridLines from '@components/Charts/components/ChartGridLines';
import ChartTooltipLayer from '@components/Charts/components/ChartTooltipLayer';
import ChartXAxisLabels from '@components/Charts/components/ChartXAxisLabels';
import ChartYAxisLabels from '@components/Charts/components/ChartYAxisLabels';
Expand Down Expand Up @@ -196,28 +197,27 @@ function VerticalBarChartContentBody({data, isLoading, yAxisUnit, yAxisUnitPosit
labelSkipInterval={labelSkipInterval}
fontSize={variables.iconSizeExtraSmall}
fontManager={fontManager}
labelColor={theme.textSupporting}
labelColor={theme.icon}
xScale={args.xScale}
chartBoundsBottom={chartBoundsBottom}
/>
<ChartYAxisLabels
yTicks={args.yTicks}
yScale={args.yScale}
chartBounds={args.chartBounds}
canvasWidth={args.canvasSize.width}
fontSize={variables.iconSizeExtraSmall}
fontManager={fontManager}
labelColor={theme.textSupporting}
labelColor={theme.icon}
formatValue={formatCompactValue}
leftAlign
/>
</>
);
};

const labelSpace = VictoryTheme.axis.labelGap + (xAxisLabelHeight ?? 0);
const labelSpace = VictoryTheme.axis.xAxisLabelGap + (xAxisLabelHeight ?? 0);
const dynamicChartStyle = {height: CHART_CONTENT_MIN_HEIGHT + labelSpace};
const yAxisLabelWidth = getYAxisLabelWidth(data, formatCompactValue, fontManager, variables.iconSizeExtraSmall, VERTICAL_BAR_DOMAIN_PADDING);
const chartPadding = {...VictoryTheme.axis.padding, bottom: labelSpace + VictoryTheme.axis.padding.bottom, left: yAxisLabelWidth + GLYPH_PADDING};
const chartPadding = {...VictoryTheme.axis.padding, bottom: labelSpace + VictoryTheme.axis.padding.bottom, right: yAxisLabelWidth + GLYPH_PADDING};

if (isLoading || !fontManager) {
return (
Expand Down Expand Up @@ -259,16 +259,26 @@ function VerticalBarChartContentBody({data, isLoading, yAxisUnit, yAxisUnitPosit
yAxis={[
{
tickCount: VictoryTheme.axis.tickCount,
lineWidth: VictoryTheme.axis.yLineWidth,
lineColor: theme.border,
axisSide: 'right',
lineWidth: 0,
labelOffset: VictoryTheme.axis.labelGap,
domain: yAxisDomain,
},
]}
frame={{lineWidth: 0}}
data={chartData}
>
{({points, chartBounds}) => points.y.map((point) => renderBar(point, chartBounds, points.y.length))}
{({points, chartBounds, yScale, yTicks}) => (
<>
<ChartGridLines
yTicks={yTicks}
yScale={yScale}
chartBounds={chartBounds}
color={theme.border}
/>
{points.y.map((point) => renderBar(point, chartBounds, points.y.length))}
</>
)}
</CartesianChart>
)}
<ChartTooltipLayer
Expand Down
43 changes: 21 additions & 22 deletions src/components/Charts/LineChart/LineChartContent.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import ActivityIndicator from '@components/ActivityIndicator';
import AreaGradient from '@components/Charts/components/AreaGradient';
import ChartGridLines from '@components/Charts/components/ChartGridLines';
import ChartTooltipLayer from '@components/Charts/components/ChartTooltipLayer';
import ChartXAxisLabels from '@components/Charts/components/ChartXAxisLabels';
import ChartYAxisLabels from '@components/Charts/components/ChartYAxisLabels';
import LeftFrameLine from '@components/Charts/components/LeftFrameLine';
import ScatterPoints from '@components/Charts/components/ScatterPoints';
import type {HitTestArgs} from '@components/Charts/hooks';
import {
Expand All @@ -16,7 +16,7 @@ import {
useDynamicYDomain,
useLabelHitTesting,
} from '@components/Charts/hooks';
import {getXAxisLabel, getYAxisLabelWidth, labelOverhang} from '@components/Charts/utils';
import {getDomainPaddingForEdgeSpace, getXAxisLabel, getYAxisLabelWidth, labelOverhang} from '@components/Charts/utils';
import VictoryTheme, {CHART_CONTENT_MIN_HEIGHT, GLYPH_PADDING, LABEL_PADDING, LABEL_ROTATIONS, SIN_45} from '@components/Charts/VictoryTheme';

import useTheme from '@hooks/useTheme';
Expand Down Expand Up @@ -91,7 +91,7 @@ function LineChartContentBody({data, isLoading, yAxisUnit, yAxisUnitPosition = '
const yAxisLabelWidth = getYAxisLabelWidth(data, formatCompactValue, fontManager, variables.iconSizeExtraSmall, BASE_DOMAIN_PADDING);

const tickSpacing = plotAreaWidth > 0 && data.length > 0 ? plotAreaWidth / data.length : 0;
const chartPaddingLeft = yAxisLabelWidth + GLYPH_PADDING;
const chartPaddingRight = yAxisLabelWidth + GLYPH_PADDING;

const domainPadding = (() => {
if (!firstLabelWidth || !lastLabelWidth) {
Expand All @@ -107,11 +107,11 @@ function LineChartContentBody({data, isLoading, yAxisUnit, yAxisUnitPosition = '
rightOverhang = lineHeight / 2;
}

return {
...BASE_DOMAIN_PADDING,
left: Math.max(0, leftOverhang - chartPaddingLeft),
right: rightOverhang,
const edgeSpace = {
left: Math.max(0, leftOverhang - VictoryTheme.axis.padding.left),
right: Math.max(0, rightOverhang - chartPaddingRight - VictoryTheme.axis.labelGap),
};
return {...BASE_DOMAIN_PADDING, ...getDomainPaddingForEdgeSpace(edgeSpace, plotAreaWidth)};
})();

const totalDomainPadding = domainPadding.left + domainPadding.right;
Expand Down Expand Up @@ -178,12 +178,6 @@ function LineChartContentBody({data, isLoading, yAxisUnit, yAxisUnitPosition = '
chartBottom.set(chartBoundsBottom);
return (
<>
<LeftFrameLine
chartBounds={args.chartBounds}
yTicks={args.yTicks}
yScale={args.yScale}
color={theme.border}
/>
<ScatterPoints
points={args.points.y}
radius={DOT_RADIUS}
Expand All @@ -201,7 +195,7 @@ function LineChartContentBody({data, isLoading, yAxisUnit, yAxisUnitPosition = '
labelSkipInterval={labelSkipInterval}
fontSize={variables.iconSizeExtraSmall}
fontManager={fontManager}
labelColor={theme.textSupporting}
labelColor={theme.icon}
xScale={args.xScale}
chartBoundsBottom={chartBoundsBottom}
/>
Expand All @@ -210,24 +204,23 @@ function LineChartContentBody({data, isLoading, yAxisUnit, yAxisUnitPosition = '
<ChartYAxisLabels
yTicks={args.yTicks}
yScale={args.yScale}
chartBounds={args.chartBounds}
canvasWidth={args.canvasSize.width}
fontSize={variables.iconSizeExtraSmall}
fontManager={fontManager}
labelColor={theme.textSupporting}
labelColor={theme.icon}
formatValue={formatCompactValue}
leftAlign
/>
)}
</>
);
};

const labelSpace = VictoryTheme.axis.labelGap + (xAxisLabelHeight ?? 0);
const labelSpace = VictoryTheme.axis.xAxisLabelGap + (xAxisLabelHeight ?? 0);
const dynamicChartStyle = {height: CHART_CONTENT_MIN_HEIGHT + labelSpace};
const chartPadding = {
...VictoryTheme.axis.padding,
bottom: labelSpace + VictoryTheme.axis.padding.bottom,
left: chartPaddingLeft,
right: chartPaddingRight,
};

if (isLoading || !fontManager) {
Expand Down Expand Up @@ -270,17 +263,23 @@ function LineChartContentBody({data, isLoading, yAxisUnit, yAxisUnitPosition = '
yAxis={[
{
tickCount: VictoryTheme.axis.tickCount,
lineWidth: VictoryTheme.axis.yLineWidth,
lineColor: theme.border,
axisSide: 'right',
lineWidth: 0,
labelOffset: VictoryTheme.axis.labelGap,
domain: yAxisDomain,
},
]}
frame={{lineWidth: 0}}
data={chartData}
>
{({points, yScale, yTicks}) => (
{({points, yScale, yTicks, chartBounds}) => (
<>
<ChartGridLines
yTicks={yTicks}
yScale={yScale}
chartBounds={chartBounds}
color={theme.border}
/>
<AreaGradient
points={points.y}
baselineY={yScale(Math.min(...yTicks))}
Expand Down
6 changes: 5 additions & 1 deletion src/components/Charts/VictoryTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ const VictoryTheme = {
xLineWidth: 0,
/** Line width for Y-axis grid lines */
yLineWidth: 1,
/** Desired visual gap (px) between axis labels and the chart edge, used for both axes */
/** Dash and gap lengths (px) of the grid lines */
gridDashIntervals: [4, 8],
/** Desired visual gap (px) between axis labels and the chart edge */
labelGap: 12,
/** Gap (px) between the plot and the X-axis labels */
xAxisLabelGap: 24,
/** Base chart padding applied to all sides */
padding: {top: 5, left: 5, right: 5, bottom: 5},
},
Expand Down
47 changes: 47 additions & 0 deletions src/components/Charts/components/ChartGridLines.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import VictoryTheme from '@components/Charts/VictoryTheme';

import type {Color} from '@shopify/react-native-skia';
import type {ChartBounds, Scale} from 'victory-native';

import {DashPathEffect, Line, vec} from '@shopify/react-native-skia';
import React from 'react';

type ChartGridLinesProps = {
/** Tick values a grid line is drawn at. */
yTicks: number[];

/** Maps a tick value to its y-pixel position. */
yScale: Scale;

/** Chart plot area bounds the lines span. */
chartBounds: ChartBounds;

/** Stroke color of the lines. */
color: Color;
};

const [DASH_LENGTH, DASH_GAP] = VictoryTheme.axis.gridDashIntervals;
const STROKE_WIDTH = VictoryTheme.axis.yLineWidth;

// Round caps add half the stroke width to each end of a dash, so shorten dashes to keep the visible lengths.
const DASH_INTERVALS = [Math.max(0, DASH_LENGTH - STROKE_WIDTH), DASH_GAP + STROKE_WIDTH];

function ChartGridLines({yTicks, yScale, chartBounds, color}: ChartGridLinesProps) {
return yTicks.map((tick) => {
const y = yScale(tick);
return (
<Line
key={`grid-line-${tick}`}
p1={vec(chartBounds.left, y)}
p2={vec(chartBounds.right, y)}
color={color}
strokeWidth={STROKE_WIDTH}
strokeCap="round"
>
{tick !== 0 && <DashPathEffect intervals={DASH_INTERVALS} />}
</Line>
);
});
}

export default ChartGridLines;
2 changes: 1 addition & 1 deletion src/components/Charts/components/ChartXAxisLabels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function ChartXAxisLabels({
const {ascent, descent} = getFontLineMetrics(fontManager, fontSize);

const correction = rotatedLabelCenterCorrection(ascent, descent, angleRad);
const labelY = chartBoundsBottom + VictoryTheme.axis.labelGap + rotatedLabelYOffset(ascent, descent, angleRad);
const labelY = chartBoundsBottom + VictoryTheme.axis.xAxisLabelGap + rotatedLabelYOffset(ascent, descent, angleRad);

return truncatedLabels.map((label, i) => {
if (i % labelSkipInterval !== 0 || label.length === 0) {
Expand Down
15 changes: 7 additions & 8 deletions src/components/Charts/components/ChartYAxisLabels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {getFontLineMetrics, measureTextWidth, truncateLabel} from '@components/C
import {ELLIPSIS, GLYPH_PADDING, LABEL_PADDING, MAX_X_AXIS_LABEL_WIDTH, MAX_Y_AXIS_LABEL_WIDTH} from '@components/Charts/VictoryTheme';

import type {SkTypefaceFontProvider} from '@shopify/react-native-skia';
import type {ChartBounds, Scale} from 'victory-native';
import type {Scale} from 'victory-native';

import {Paragraph} from '@shopify/react-native-skia';
import React from 'react';
Expand All @@ -15,8 +15,8 @@ type ChartYAxisLabelsProps = {
/** Maps a tick value to its y-pixel position. */
yScale: Scale;

/** Chart plot area bounds. */
chartBounds: ChartBounds;
/** Width of the whole canvas, which right-aligned labels end at. */
canvasWidth: number;

/** Font size used for rendering labels. */
fontSize: number;
Expand All @@ -30,7 +30,7 @@ type ChartYAxisLabelsProps = {
/** Formats a tick value to its display string. */
formatValue: (value: number) => string;

/** When true, labels are left-aligned starting at the left edge of the chart instead of right-aligned. */
/** When true, labels are left-aligned at the left edge of the chart instead of right-aligned at its right edge. */
leftAlign?: boolean;

/** When true, thin out labels so vertically stacked rows never overlap (used by the horizontal chart's category axis). */
Expand All @@ -43,7 +43,7 @@ type ChartYAxisLabelsProps = {
function ChartYAxisLabels({
yTicks,
yScale,
chartBounds,
canvasWidth,
fontSize,
fontManager,
labelColor,
Expand Down Expand Up @@ -93,9 +93,8 @@ function ChartYAxisLabels({
return null;
}

// Left-aligned labels (horizontal bar chart) start at the canvas origin so they sit on the same left
// key line as the chart title. Right-aligned labels end just inside the plot's left edge.
const x = leftAlign ? 0 : chartBounds.left + GLYPH_PADDING - paraData.width;
// Labels sit on the canvas edges so they line up with the card content above the chart.
const x = leftAlign ? 0 : canvasWidth - GLYPH_PADDING - paraData.width;
const tickY = yScale(tick);

return (
Expand Down
33 changes: 0 additions & 33 deletions src/components/Charts/components/LeftFrameLine.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion src/components/Charts/hooks/useLabelHitTesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function useLabelHitTesting({fontManager, fontSize, truncatedLabelWidths, labelR
const iconThirdSin = (variables.iconSizeExtraSmall / 3) * sinA;
const additionalOffset = getAdditionalOffset(angleRad);
labelHitGeometry = {
labelYOffset: VictoryTheme.axis.labelGap + rotatedLabelYOffset(ascent, descent, angleRad) - additionalOffset,
labelYOffset: VictoryTheme.axis.xAxisLabelGap + rotatedLabelYOffset(ascent, descent, angleRad) - additionalOffset,
iconSin: variables.iconSizeExtraSmall * sinA,
labelSins: truncatedLabelWidths.map((w) => w * sinA),
halfWidths: truncatedLabelWidths.map((w) => w / 2),
Expand Down
Loading
Loading