Skip to content

Commit e34f0ae

Browse files
fix(axis): add axisTitleHeight to axis increments (#29)
Previously, without the axisTitleHeight offset, multiple axes with the same position would overflow into each other with margins less than the title height. This PR fixes this and also adds a story to the axis group in storybook with multiple axes with the same position. fixes #26
1 parent 1e86c9e commit e34f0ae

File tree

2 files changed

+64
-35
lines changed

2 files changed

+64
-35
lines changed

src/lib/axes/axis_utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,23 +464,24 @@ export function getAxisPosition(
464464

465465
if (isVertical(position)) {
466466
if (position === Position.Left) {
467-
leftIncrement = maxLabelBboxWidth + tickSize + tickPadding + chartMargins.left;
467+
leftIncrement = maxLabelBboxWidth + tickSize + tickPadding + chartMargins.left + axisTitleHeight;
468468
dimensions.left = maxLabelBboxWidth + cumLeftSum + chartMargins.left + axisTitleHeight;
469469
} else {
470-
rightIncrement = maxLabelBboxWidth + tickSize + tickPadding + chartMargins.right;
470+
rightIncrement = maxLabelBboxWidth + tickSize + tickPadding + chartMargins.right + axisTitleHeight;
471471
dimensions.left = left + width + cumRightSum;
472472
}
473473
dimensions.width = maxLabelBboxWidth;
474474
} else {
475475
if (position === Position.Top) {
476-
topIncrement = maxLabelBboxHeight + tickSize + tickPadding + chartMargins.top;
476+
topIncrement = maxLabelBboxHeight + tickSize + tickPadding + chartMargins.top + axisTitleHeight;
477477
dimensions.top = cumTopSum + chartMargins.top + axisTitleHeight;
478478
} else {
479-
bottomIncrement = maxLabelBboxHeight + tickSize + tickPadding + chartMargins.bottom;
479+
bottomIncrement = maxLabelBboxHeight + tickSize + tickPadding + chartMargins.bottom + axisTitleHeight;
480480
dimensions.top = top + height + cumBottomSum;
481481
}
482482
dimensions.height = maxLabelBboxHeight;
483483
}
484+
484485
return { dimensions, topIncrement, bottomIncrement, leftIncrement, rightIncrement };
485486
}
486487

src/stories/axis.tsx

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,37 @@ import {
1313
ScaleType,
1414
Settings,
1515
} from '..';
16+
import { PartialTheme } from '../lib/themes/theme';
1617
import { LineSeries } from '../specs';
1718

19+
function createThemeAction(title: string, min: number, max: number, value: number) {
20+
return number(title, value, {
21+
range: true,
22+
min,
23+
max,
24+
step: 1,
25+
}, 'theme');
26+
}
27+
28+
function renderAxisWithOptions(position: Position, seriesGroup: string, show: boolean) {
29+
const axisTitle = `${position} axis (${seriesGroup})`;
30+
31+
const showAxis = boolean(`show ${axisTitle} axis`, show, `${position} axes`);
32+
33+
if (!showAxis) {
34+
return null;
35+
}
36+
37+
const axisProps = {
38+
id: getAxisId(axisTitle),
39+
position,
40+
title: axisTitle,
41+
showOverlappingTicks: true,
42+
};
43+
44+
return <Axis {...axisProps} />;
45+
}
46+
1847
storiesOf('Axis', module)
1948
.add('basic', () => {
2049
return (
@@ -149,29 +178,37 @@ storiesOf('Axis', module)
149178
</Chart>
150179
);
151180
})
152-
.add('with multi axis (TO FIX)', () => {
181+
.add('with multi axis', () => {
182+
const theme: PartialTheme = {
183+
chart: {
184+
margins: {
185+
left: createThemeAction('margin left', 0, 50, 0),
186+
right: createThemeAction('margin right', 0, 50, 0),
187+
top: createThemeAction('margin top', 0, 50, 0),
188+
bottom: createThemeAction('margin bottom', 0, 50, 0),
189+
},
190+
paddings: {
191+
left: createThemeAction('padding left', 0, 50, 0),
192+
right: createThemeAction('padding right', 0, 50, 0),
193+
top: createThemeAction('padding top', 0, 50, 0),
194+
bottom: createThemeAction('padding bottom', 0, 50, 0),
195+
},
196+
},
197+
};
198+
199+
const seriesGroup1 = 'group1';
200+
const seriesGroup2 = 'group2';
153201
return (
154-
<Chart renderer="canvas" className={'story-chart'}>
155-
<Settings showLegend={false} />
156-
<Axis
157-
id={getAxisId('bottom')}
158-
position={Position.Bottom}
159-
title={'Bottom axis'}
160-
showOverlappingTicks={true}
161-
/>
162-
<Axis
163-
id={getAxisId('left1')}
164-
title={'First left axis'}
165-
position={Position.Left}
166-
tickFormat={(d) => Number(d).toFixed(2)}
167-
/>
168-
<Axis
169-
id={getAxisId('left2')}
170-
title={'Second left axis'}
171-
groupId={getGroupId('group2')}
172-
position={Position.Left}
173-
tickFormat={(d) => Number(d).toFixed(2)}
174-
/>
202+
<Chart renderer="canvas" size={[500, 300]} className={'story-chart'}>
203+
<Settings showLegend={false} theme={theme} debug={boolean('debug', true)} />
204+
{renderAxisWithOptions(Position.Top, seriesGroup1, false)}
205+
{renderAxisWithOptions(Position.Top, seriesGroup2, true)}
206+
{renderAxisWithOptions(Position.Left, seriesGroup1, false)}
207+
{renderAxisWithOptions(Position.Left, seriesGroup2, true)}
208+
{renderAxisWithOptions(Position.Bottom, seriesGroup1, false)}
209+
{renderAxisWithOptions(Position.Bottom, seriesGroup2, true)}
210+
{renderAxisWithOptions(Position.Right, seriesGroup1, false)}
211+
{renderAxisWithOptions(Position.Right, seriesGroup2, true)}
175212
<BarSeries
176213
id={getSpecId('barseries1')}
177214
xScaleType={ScaleType.Linear}
@@ -180,15 +217,6 @@ storiesOf('Axis', module)
180217
yAccessors={['y']}
181218
data={[{ x: 0, y: 1 }, { x: 1, y: 2 }, { x: 2, y: 3 }, { x: 3, y: 4 }]}
182219
/>
183-
<BarSeries
184-
id={getSpecId('barseries2')}
185-
groupId={getGroupId('group2')}
186-
xScaleType={ScaleType.Linear}
187-
yScaleType={ScaleType.Linear}
188-
xAccessor="x"
189-
yAccessors={['y']}
190-
data={[{ x: 0, y: 8 }, { x: 1, y: 7 }, { x: 2, y: 6 }, { x: 3, y: 5 }]}
191-
/>
192220
</Chart>
193221
);
194222
})

0 commit comments

Comments
 (0)