Skip to content

Commit a282471

Browse files
authored
Merge pull request #19919 from apache/dvdkon-fix-13627
feat(axis): custom axis tick/label positions
2 parents 0ab5f72 + 0c90824 commit a282471

File tree

3 files changed

+239
-4
lines changed

3 files changed

+239
-4
lines changed

src/coord/axisCommonTypes.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ interface AxisTickOption {
186186
inside?: boolean,
187187
// The length of axisTick.
188188
length?: number,
189-
lineStyle?: LineStyleOption
189+
lineStyle?: LineStyleOption,
190+
customValues?: (number | string | Date)[]
190191
}
191192

192193
type AxisLabelValueFormatter = (value: number, index: number) => string;
@@ -238,9 +239,10 @@ interface AxisLabelBaseOption extends Omit<TextCommonOption, 'color'> {
238239
/**
239240
* If hide overlapping labels.
240241
*/
241-
hideOverlap?: boolean;
242+
hideOverlap?: boolean,
243+
customValues?: (number | string | Date)[],
242244
// Color can be callback
243-
color?: ColorString | ((value?: string | number, index?: number) => ColorString)
245+
color?: ColorString | ((value?: string | number, index?: number) => ColorString),
244246
overflow?: TextStyleProps['overflow']
245247
}
246248
interface AxisLabelOption<TType extends OptionAxisType> extends AxisLabelBaseOption {
@@ -273,6 +275,5 @@ interface SplitAreaOption {
273275
areaStyle?: AreaStyleOption<ZRColor[]>
274276
}
275277

276-
277278
export type AxisBaseOption = ValueAxisBaseOption | LogAxisBaseOption
278279
| CategoryAxisBaseOption | TimeAxisBaseOption | AxisBaseOptionCommon;

src/coord/axisTickLabelBuilder.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ type InnerStore = {
6060

6161
const inner = makeInner<InnerStore, any>();
6262

63+
function tickValuesToNumbers(axis: Axis, values: (number | string | Date)[]) {
64+
const nums = zrUtil.map(values, val => axis.scale.parse(val));
65+
if (axis.type === 'time' && nums.length > 0) {
66+
// Time axis needs duplicate first/last tick (see TimeScale.getTicks())
67+
// The first and last tick/label don't get drawn
68+
nums.sort();
69+
nums.unshift(nums[0]);
70+
nums.push(nums[nums.length - 1]);
71+
}
72+
return nums;
73+
}
74+
6375
export function createAxisLabels(axis: Axis): {
6476
labels: {
6577
level?: number,
@@ -69,6 +81,20 @@ export function createAxisLabels(axis: Axis): {
6981
}[],
7082
labelCategoryInterval?: number
7183
} {
84+
const custom = axis.getLabelModel().get('customValues');
85+
if (custom) {
86+
const labelFormatter = makeLabelFormatter(axis);
87+
return {
88+
labels: tickValuesToNumbers(axis, custom).map(numval => {
89+
const tick = {value: numval};
90+
return {
91+
formattedLabel: labelFormatter(tick),
92+
rawLabel: axis.scale.getLabel(tick),
93+
tickValue: numval
94+
};
95+
})
96+
};
97+
}
7298
// Only ordinal scale support tick interval
7399
return axis.type === 'category'
74100
? makeCategoryLabels(axis)
@@ -87,6 +113,12 @@ export function createAxisTicks(axis: Axis, tickModel: AxisBaseModel): {
87113
ticks: number[],
88114
tickCategoryInterval?: number
89115
} {
116+
const custom = axis.getTickModel().get('customValues');
117+
if (custom) {
118+
return {
119+
ticks: tickValuesToNumbers(axis, custom)
120+
};
121+
}
90122
// Only ordinal scale support tick interval
91123
return axis.type === 'category'
92124
? makeCategoryTicks(axis, tickModel)

test/axis-customTicks.html

Lines changed: 202 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)