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
7 changes: 5 additions & 2 deletions src/components/Charts/BarChart/BarChartContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {Bar, CartesianChart} from 'victory-native';
import ActivityIndicator from '@components/ActivityIndicator';
import ChartHeader from '@components/Charts/components/ChartHeader';
import ChartTooltip from '@components/Charts/components/ChartTooltip';
import {CHART_CONTENT_MIN_HEIGHT, CHART_PADDING, X_AXIS_LINE_WIDTH, Y_AXIS_LABEL_OFFSET, Y_AXIS_LINE_WIDTH, Y_AXIS_TICK_COUNT} from '@components/Charts/constants';
import {AXIS_LABEL_GAP, CHART_CONTENT_MIN_HEIGHT, CHART_PADDING, X_AXIS_LINE_WIDTH, Y_AXIS_LINE_WIDTH, Y_AXIS_TICK_COUNT} from '@components/Charts/constants';
import fontSource from '@components/Charts/font';
import type {HitTestArgs} from '@components/Charts/hooks';
import {useChartInteractions, useChartLabelFormats, useChartLabelLayout, useDynamicYDomain, useTooltipData} from '@components/Charts/hooks';
Expand Down Expand Up @@ -216,6 +216,9 @@ function BarChartContent({data, title, titleIcon, isLoading, yAxisUnit, yAxisUni
tickCount: data.length,
labelColor: theme.textSupporting,
lineWidth: X_AXIS_LINE_WIDTH,
// Victory-native positions x-axis labels at: chartBounds.bottom + labelOffset + fontSize.
// We subtract descent (fontSize - ascent) so the gap from chart to the ascent line equals AXIS_LABEL_GAP.
labelOffset: AXIS_LABEL_GAP - Math.abs(font?.getMetrics().descent ?? 0),
formatXLabel: formatXAxisLabel,
labelRotate: labelRotation,
labelOverflow: 'visible',
Expand All @@ -228,7 +231,7 @@ function BarChartContent({data, title, titleIcon, isLoading, yAxisUnit, yAxisUni
tickCount: Y_AXIS_TICK_COUNT,
lineWidth: Y_AXIS_LINE_WIDTH,
lineColor: theme.border,
labelOffset: Y_AXIS_LABEL_OFFSET,
labelOffset: AXIS_LABEL_GAP,
domain: yAxisDomain,
},
]}
Expand Down
9 changes: 3 additions & 6 deletions src/components/Charts/LineChart/LineChartContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {CartesianChart, Line, Scatter} from 'victory-native';
import ActivityIndicator from '@components/ActivityIndicator';
import ChartHeader from '@components/Charts/components/ChartHeader';
import ChartTooltip from '@components/Charts/components/ChartTooltip';
import {CHART_CONTENT_MIN_HEIGHT, CHART_PADDING, X_AXIS_LINE_WIDTH, Y_AXIS_LABEL_OFFSET, Y_AXIS_LINE_WIDTH, Y_AXIS_TICK_COUNT} from '@components/Charts/constants';
import {AXIS_LABEL_GAP, CHART_CONTENT_MIN_HEIGHT, CHART_PADDING, X_AXIS_LINE_WIDTH, Y_AXIS_LINE_WIDTH, Y_AXIS_TICK_COUNT} from '@components/Charts/constants';
import fontSource from '@components/Charts/font';
import type {HitTestArgs} from '@components/Charts/hooks';
import {useChartInteractions, useChartLabelFormats, useChartLabelLayout, useDynamicYDomain, useTooltipData} from '@components/Charts/hooks';
Expand All @@ -28,9 +28,6 @@ const DOT_HOVER_EXTRA_RADIUS = 2;
/** Base domain padding applied to all sides */
const BASE_DOMAIN_PADDING = {top: 16, bottom: 16, left: 0, right: 0};

/** Consistent gap between x-axis and closest point of label (regardless of rotation) */
const LABEL_GAP = 8;

type LineChartProps = CartesianChartProps & {
/** Callback when a data point is pressed */
onPointPress?: (dataPoint: ChartDataPoint, index: number) => void;
Expand Down Expand Up @@ -163,7 +160,7 @@ function LineChartContent({data, title, titleIcon, isLoading, yAxisUnit, yAxisUn
const fontMetrics = font.getMetrics();
const ascent = Math.abs(fontMetrics.ascent);
const descent = Math.abs(fontMetrics.descent);
const labelY = args.chartBounds.bottom + LABEL_GAP + rotatedLabelYOffset(ascent, descent, angleRad);
const labelY = args.chartBounds.bottom + AXIS_LABEL_GAP + rotatedLabelYOffset(ascent, descent, angleRad);

return truncatedLabels.map((label, i) => {
if (i % labelSkipInterval !== 0) {
Expand Down Expand Up @@ -266,7 +263,7 @@ function LineChartContent({data, title, titleIcon, isLoading, yAxisUnit, yAxisUn
tickCount: Y_AXIS_TICK_COUNT,
lineWidth: Y_AXIS_LINE_WIDTH,
lineColor: theme.border,
labelOffset: Y_AXIS_LABEL_OFFSET,
labelOffset: AXIS_LABEL_GAP,
domain: yAxisDomain,
},
]}
Expand Down
6 changes: 3 additions & 3 deletions src/components/Charts/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/** Number of Y-axis ticks (including zero) */
const Y_AXIS_TICK_COUNT = 5;

/** Distance between Y-axis labels and the chart */
const Y_AXIS_LABEL_OFFSET = 16;
/** Desired visual gap (px) between axis labels and the chart edge, used for both axes */
const AXIS_LABEL_GAP = 12;

/** Chart padding */
const CHART_PADDING = 5;
Expand All @@ -16,4 +16,4 @@ const X_AXIS_LINE_WIDTH = 0;
/** Line width for Y-axis grid lines */
const Y_AXIS_LINE_WIDTH = 1;

export {Y_AXIS_TICK_COUNT, Y_AXIS_LABEL_OFFSET, CHART_PADDING, CHART_CONTENT_MIN_HEIGHT, X_AXIS_LINE_WIDTH, Y_AXIS_LINE_WIDTH};
export {Y_AXIS_TICK_COUNT, AXIS_LABEL_GAP, CHART_PADDING, CHART_CONTENT_MIN_HEIGHT, X_AXIS_LINE_WIDTH, Y_AXIS_LINE_WIDTH};
Loading