Skip to content

Commit 776239a

Browse files
committed
refactor(axis_utils specs): create style object
1 parent fac9f17 commit 776239a

4 files changed

Lines changed: 34 additions & 15 deletions

File tree

src/lib/axes/axis_utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ export function computeAxisTicksDimensions(
7070
throw new Error(`Cannot compute scale for axis spec ${axisSpec.id}`);
7171
}
7272

73-
const tickLabelPadding = axisSpec.tickLabelPadding || axisConfig.tickLabelStyle.padding;
73+
const tickLabelPadding =
74+
axisSpec.style && axisSpec.style.tickLabelPadding
75+
? axisSpec.style.tickLabelPadding
76+
: axisConfig.tickLabelStyle.padding;
7477

7578
const dimensions = computeTickDimensions(
7679
scale,

src/lib/series/specs.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,17 @@ export interface AxisSpec {
218218
title?: string;
219219
/** If specified, it constrains the domain for these values */
220220
domain?: DomainRange;
221-
/** Specifies the amount of padding on the tick label bounding box */
222-
tickLabelPadding?: number;
221+
/** Object to hold custom styling */
222+
style?: CustomStyle;
223223
}
224224

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

227+
interface CustomStyle {
228+
/** Specifies the amount of padding on the tick label bounding box */
229+
tickLabelPadding?: number;
230+
}
231+
227232
/**
228233
* The position of the axis relative to the chart.
229234
* A left or right positioned axis is a vertical axis.

stories/axis.tsx

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ function renderAxisWithOptions(position: Position, seriesGroup: string, show: bo
5454

5555
storiesOf('Axis', module)
5656
.add('basic', () => {
57-
const tickLabelPadding = number('Tick Label Padding', 0);
57+
const customStyle = {
58+
tickLabelPadding: number('Tick Label Padding', 0),
59+
};
60+
5861
return (
5962
<Chart className={'story-chart'}>
6063
<Settings debug={boolean('debug', false)} />
@@ -63,14 +66,14 @@ storiesOf('Axis', module)
6366
position={Position.Bottom}
6467
title={'Bottom axis'}
6568
showOverlappingTicks={true}
66-
tickLabelPadding={tickLabelPadding}
69+
style={customStyle}
6770
/>
6871
<Axis
6972
id={getAxisId('left2')}
7073
title={'Left axis'}
7174
position={Position.Left}
7275
tickFormat={(d) => Number(d).toFixed(2)}
73-
tickLabelPadding={tickLabelPadding}
76+
style={customStyle}
7477
/>
7578

7679
<AreaSeries
@@ -85,7 +88,10 @@ storiesOf('Axis', module)
8588
);
8689
})
8790
.add('tick label rotation', () => {
88-
const tickLabelPadding = number('Axis Tick Label Padding', 0);
91+
const customStyle = {
92+
tickLabelPadding: number('Tick Label Padding', 0),
93+
};
94+
8995
return (
9096
<Chart className={'story-chart'}>
9197
<Axis
@@ -99,7 +105,7 @@ storiesOf('Axis', module)
99105
max: 90,
100106
step: 1,
101107
})}
102-
tickLabelPadding={tickLabelPadding}
108+
style={customStyle}
103109
/>
104110
<Axis
105111
id={getAxisId('left')}
@@ -112,7 +118,7 @@ storiesOf('Axis', module)
112118
step: 1,
113119
})}
114120
tickFormat={(d) => Number(d).toFixed(2)}
115-
tickLabelPadding={tickLabelPadding}
121+
style={customStyle}
116122
/>
117123
<Axis
118124
id={getAxisId('top')}
@@ -125,7 +131,7 @@ storiesOf('Axis', module)
125131
step: 1,
126132
})}
127133
tickFormat={(d) => Number(d).toFixed(2)}
128-
tickLabelPadding={tickLabelPadding}
134+
style={customStyle}
129135
/>
130136
<Axis
131137
id={getAxisId('right')}
@@ -138,7 +144,7 @@ storiesOf('Axis', module)
138144
step: 1,
139145
})}
140146
tickFormat={(d) => Number(d).toFixed(2)}
141-
tickLabelPadding={tickLabelPadding}
147+
style={customStyle}
142148
/>
143149
<AreaSeries
144150
id={getSpecId('lines')}
@@ -323,7 +329,10 @@ storiesOf('Axis', module)
323329
.add('w many tick labels', () => {
324330
const dg = new DataGenerator();
325331
const data = dg.generateSimpleSeries(31);
326-
const tickLabelPadding = number('Axis Tick Label Padding', 0);
332+
const customStyle = {
333+
tickLabelPadding: number('Tick Label Padding', 0),
334+
};
335+
327336
return (
328337
<Chart className={'story-chart'}>
329338
<Settings debug={true} />
@@ -332,7 +341,7 @@ storiesOf('Axis', module)
332341
position={Position.Bottom}
333342
title={'Bottom axis'}
334343
showOverlappingTicks={true}
335-
tickLabelPadding={tickLabelPadding}
344+
style={customStyle}
336345
/>
337346
<AreaSeries
338347
id={getSpecId('lines')}

stories/styling.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,9 @@ storiesOf('Stylings', module)
686686
},
687687
};
688688
const customTheme = mergeWithDefaultTheme(theme, LIGHT_THEME);
689-
const tickLabelPadding = number('Tick Label Padding Axis Spec', 0);
689+
const customStyle = {
690+
tickLabelPadding: number('Tick Label Padding Axis Spec', 0),
691+
};
690692
return (
691693
<Chart className={'story-chart'}>
692694
<Settings theme={customTheme} debug={boolean('debug', true)} />
@@ -695,7 +697,7 @@ storiesOf('Stylings', module)
695697
position={Position.Bottom}
696698
title={'Bottom axis'}
697699
showOverlappingTicks={true}
698-
tickLabelPadding={tickLabelPadding}
700+
style={customStyle}
699701
/>
700702
<Axis
701703
id={getAxisId('left2')}

0 commit comments

Comments
 (0)