Skip to content

Commit d8dd8f4

Browse files
committed
test(axis_utils.test): add test for tickLabelPadding style prop or theme
1 parent 776239a commit d8dd8f4

4 files changed

Lines changed: 23 additions & 8 deletions

File tree

src/lib/axes/axis_utils.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { XDomain } from '../series/domains/x_domain';
22
import { YDomain } from '../series/domains/y_domain';
3-
import { AxisSpec, DomainRange, Position } from '../series/specs';
3+
import { AxisSpec, DomainRange, Position, AxisStyle } from '../series/specs';
44
import { LIGHT_THEME } from '../themes/light_theme';
55
import { AxisId, getAxisId, getGroupId, GroupId } from '../utils/ids';
66
import { ScaleType } from '../utils/scales/scales';
@@ -28,6 +28,7 @@ import {
2828
isVertical,
2929
isYDomain,
3030
mergeDomainsByGroupId,
31+
getAxisTickLabelPadding,
3132
} from './axis_utils';
3233
import { CanvasTextBBoxCalculator } from './canvas_text_bbox_calculator';
3334
import { SvgTextBBoxCalculator } from './svg_text_bbox_calculator';
@@ -1283,4 +1284,13 @@ describe('Axis computational utils', () => {
12831284

12841285
expect(JSON.stringify(negativeReducer)).toEqual(JSON.stringify(positiveReducer));
12851286
});
1287+
test('should expect axisSpec.style.tickLabelPadding if specified', () => {
1288+
const axisSpecStyle: AxisStyle = {
1289+
tickLabelPadding: 2,
1290+
};
1291+
1292+
const axisConfigTickLabelPadding = 1;
1293+
1294+
expect(getAxisTickLabelPadding(axisConfigTickLabelPadding, axisSpecStyle)).toEqual(2);
1295+
});
12861296
});

src/lib/axes/axis_utils.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
Rotation,
1111
TickFormatter,
1212
UpperBoundedDomain,
13+
AxisStyle,
1314
} from '../series/specs';
1415
import { AxisConfig, Theme } from '../themes/theme';
1516
import { Dimensions, Margins } from '../utils/dimensions';
@@ -70,10 +71,7 @@ export function computeAxisTicksDimensions(
7071
throw new Error(`Cannot compute scale for axis spec ${axisSpec.id}`);
7172
}
7273

73-
const tickLabelPadding =
74-
axisSpec.style && axisSpec.style.tickLabelPadding
75-
? axisSpec.style.tickLabelPadding
76-
: axisConfig.tickLabelStyle.padding;
74+
const tickLabelPadding = getAxisTickLabelPadding(axisConfig.tickLabelStyle.padding, axisSpec.style);
7775

7876
const dimensions = computeTickDimensions(
7977
scale,
@@ -89,6 +87,13 @@ export function computeAxisTicksDimensions(
8987
};
9088
}
9189

90+
export function getAxisTickLabelPadding(axisConfigTickLabelPadding: number, axisSpecStyle?: AxisStyle): number {
91+
if (axisSpecStyle && axisSpecStyle.tickLabelPadding) {
92+
return axisSpecStyle.tickLabelPadding;
93+
}
94+
return axisConfigTickLabelPadding;
95+
}
96+
9297
export function isYDomain(position: Position, chartRotation: Rotation): boolean {
9398
const isStraightRotation = chartRotation === 0 || chartRotation === 180;
9499
if (isVertical(position)) {

src/lib/axes/canvas_text_bbox_calculator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class CanvasTextBBoxCalculator implements BBoxCalculator {
2222
return none;
2323
}
2424

25-
// Avoid having negative padding that can obscure text
25+
// Padding should be at least one to avoid browser measureText inconsistencies
2626
if (padding < 1) {
2727
padding = 1;
2828
}

src/lib/series/specs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,12 @@ export interface AxisSpec {
219219
/** If specified, it constrains the domain for these values */
220220
domain?: DomainRange;
221221
/** Object to hold custom styling */
222-
style?: CustomStyle;
222+
style?: AxisStyle;
223223
}
224224

225225
export type TickFormatter = (value: any) => string;
226226

227-
interface CustomStyle {
227+
export interface AxisStyle {
228228
/** Specifies the amount of padding on the tick label bounding box */
229229
tickLabelPadding?: number;
230230
}

0 commit comments

Comments
 (0)