Skip to content

Commit 48d94e4

Browse files
committed
chipping away at header + gridline alignments
1 parent 7a2cc26 commit 48d94e4

File tree

4 files changed

+76
-41
lines changed

4 files changed

+76
-41
lines changed

packages/gamut/src/BarChart/BarRow.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const rowBaseStyles = css({
2020
width: '100%',
2121
gap: 16,
2222
py: 8,
23-
px: 0,
23+
px: 16,
2424
bg: 'transparent',
2525
border: 'none',
2626
textDecoration: 'none',
@@ -72,19 +72,22 @@ export const BarRow = forwardRef<
7272
},
7373
ref
7474
) => {
75-
const { maxRange, unit, styleConfig, animate } = useBarChartContext();
75+
const { minRange, maxRange, unit, styleConfig, animate } =
76+
useBarChartContext();
7677

7778
const isStacked = seriesTwoValue !== undefined;
7879
const displayValue = isStacked ? seriesTwoValue : seriesOneValue;
7980

8081
const backgroundBarWidth = calculateBarWidth({
8182
value: displayValue,
83+
minRange,
8284
maxRange,
8385
});
8486

8587
const foregroundBarWidth = isStacked
8688
? calculateBarWidth({
8789
value: seriesOneValue,
90+
minRange,
8891
maxRange,
8992
})
9093
: 0;
@@ -111,7 +114,7 @@ export const BarRow = forwardRef<
111114
minWidth="200px"
112115
>
113116
{Icon && <Icon size={16} />}
114-
<Text fontWeight='bold' truncate="ellipsis" truncateLines={1}>
117+
<Text fontWeight="bold" truncate="ellipsis" truncateLines={1}>
115118
{yLabel}
116119
</Text>
117120
</FlexBox>

packages/gamut/src/BarChart/GridLines.tsx

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,34 @@ import { Box } from '../Box';
55

66
const GridLineContainer = styled(Box)(
77
css({
8-
position: 'absolute',
9-
top: 0,
10-
left: 0,
11-
right: 0,
128
bottom: 0,
139
display: { _: 'none', sm: 'flex' },
10+
left: '200px',
1411
pointerEvents: 'none',
12+
position: 'absolute',
13+
right: '60px',
14+
top: 0,
15+
borderLeft: 1,
16+
borderRight: 1,
17+
borderColor: 'background-disabled',
1518
zIndex: 0,
1619
})
1720
);
1821

22+
const GridLineWrapper = styled(Box)(
23+
css({
24+
flex: 1,
25+
display: 'flex',
26+
height: '100%',
27+
})
28+
);
29+
1930
const GridLine = styled(Box)(
2031
css({
2132
borderLeft: 1,
2233
borderColorLeft: 'background-disabled',
2334
height: '100%',
24-
flex: 1,
25-
'&:first-of-type': {
26-
borderLeft: 'none',
27-
},
35+
width: 0,
2836
})
2937
);
3038

@@ -33,9 +41,13 @@ export interface GridLinesProps {
3341
}
3442

3543
export const GridLines: React.FC<GridLinesProps> = ({ tickCount }) => {
36-
const lines = Array.from({ length: tickCount }, (_, i) => (
37-
<GridLine aria-hidden key={i} />
38-
));
44+
const lines = Array.from({ length: tickCount - 2 }, (_, i) => {
45+
return (
46+
<GridLineWrapper aria-hidden justifyContent="center" key={i}>
47+
<GridLine />
48+
</GridLineWrapper>
49+
);
50+
});
3951

4052
return <GridLineContainer aria-hidden>{lines}</GridLineContainer>;
4153
};
Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { css } from '@codecademy/gamut-styles';
22
import styled from '@emotion/styled';
33

4-
import { FlexBox } from '../Box';
4+
import { Box, FlexBox } from '../Box';
55
import { Text } from '../Typography';
66
import { useBarChartContext } from './BarChartProvider';
77
import { formatNumberUSCompact, getLabel } from './utils';
@@ -21,37 +21,51 @@ const StyledLabelText = styled(Text)(
2121
})
2222
);
2323

24+
const StyledHeaderContainer = styled(FlexBox)(
25+
css({
26+
marginLeft: '200px',
27+
paddingRight: '60px',
28+
width: 'calc(100% - 200px - 60px)',
29+
})
30+
);
31+
2432
export const ScaleChartHeader: React.FC<ScaleChartHeaderProps> = ({
2533
labelCount,
34+
min,
2635
max,
2736
}) => {
2837
const { styleConfig } = useBarChartContext();
2938

30-
const scaleLabels = Array.from({ length: labelCount }, (_, i) => (
31-
<StyledLabelText
32-
data-testid="chart-header-label"
33-
key={i}
34-
textAlign="center"
35-
textColor={styleConfig.textColor}
36-
variant="p-small"
37-
>
38-
{formatNumberUSCompact({
39-
num: getLabel({ labelCount, labelIndex: i, max }),
40-
})}
41-
</StyledLabelText>
42-
));
39+
const scaleLabels = Array.from({ length: labelCount }, (_, i) => {
40+
const isFirst = i === 0;
41+
const isLast = i === labelCount - 1;
42+
const textAlign = isFirst ? 'left' : isLast ? 'right' : 'center';
43+
44+
return (
45+
<StyledLabelText
46+
data-testid="chart-header-label"
47+
key={i}
48+
textAlign={textAlign}
49+
textColor={styleConfig.textColor}
50+
variant="p-small"
51+
>
52+
{formatNumberUSCompact({
53+
num: getLabel({ labelCount, labelIndex: i, min, max }),
54+
})}
55+
</StyledLabelText>
56+
);
57+
});
4358

4459
return (
45-
<FlexBox
46-
aria-hidden="true"
47-
display={{ _: 'none', sm: 'flex' }}
48-
justifyContent="space-between"
49-
mb={8}
50-
pl={{ _: 64, sm: 96 }}
51-
pr={{ _: 40, sm: 64 }}
52-
width="100%"
53-
>
54-
{scaleLabels}
55-
</FlexBox>
60+
<Box bg="red" width={1}>
61+
<StyledHeaderContainer
62+
aria-hidden="true"
63+
display={{ _: 'none', sm: 'flex' }}
64+
justifyContent="space-between"
65+
mb={8}
66+
>
67+
{scaleLabels}
68+
</StyledHeaderContainer>
69+
</Box>
5670
);
5771
};

packages/gamut/src/BarChart/utils/index.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ export const calculatePercent = ({ value, total }: { value: number; total: numbe
2020

2121
export const calculateBarWidth = ({
2222
value,
23+
minRange,
2324
maxRange,
2425
}: {
2526
value: number;
27+
minRange: number;
2628
maxRange: number;
2729
}) => {
28-
return Math.floor(calculatePercent({ value, total: maxRange }));
30+
const range = maxRange - minRange;
31+
const adjustedValue = value - minRange;
32+
return Math.floor(calculatePercent({ value: adjustedValue, total: range }));
2933
};
3034

3135
// Calculate tick spacing and nice minimum and maximum data points on the axis.
@@ -141,13 +145,15 @@ export const getValuesSummary = ({
141145
export const getLabel = ({
142146
labelCount,
143147
labelIndex,
148+
min,
144149
max,
145150
}: {
146151
labelCount: number;
147152
labelIndex: number;
153+
min: number;
148154
max: number;
149155
}): number => {
150156
if (labelCount <= 1) return max;
151157
const incrementalDecimal = labelIndex / (labelCount - 1);
152-
return Math.floor(incrementalDecimal * max);
158+
return Math.floor(min + incrementalDecimal * (max - min));
153159
};

0 commit comments

Comments
 (0)