Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(axis): add customValues option to customize axis tick/label positions #19919

Merged
merged 3 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/coord/axisCommonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ interface AxisTickOption {
inside?: boolean,
// The length of axisTick.
length?: number,
lineStyle?: LineStyleOption
lineStyle?: LineStyleOption,
customValues?: (number | string | Date)[]
}

type AxisLabelValueFormatter = (value: number, index: number) => string;
Expand Down Expand Up @@ -237,9 +238,10 @@ interface AxisLabelBaseOption extends Omit<TextCommonOption, 'color'> {
/**
* If hide overlapping labels.
*/
hideOverlap?: boolean;
hideOverlap?: boolean,
customValues?: (number | string | Date)[],
// Color can be callback
color?: ColorString | ((value?: string | number, index?: number) => ColorString)
color?: ColorString | ((value?: string | number, index?: number) => ColorString),
overflow?: TextStyleProps['overflow']
}
interface AxisLabelOption<TType extends OptionAxisType> extends AxisLabelBaseOption {
Expand Down Expand Up @@ -272,6 +274,5 @@ interface SplitAreaOption {
areaStyle?: AreaStyleOption<ZRColor[]>
}


export type AxisBaseOption = ValueAxisBaseOption | LogAxisBaseOption
| CategoryAxisBaseOption | TimeAxisBaseOption | AxisBaseOptionCommon;
32 changes: 32 additions & 0 deletions src/coord/axisTickLabelBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ type InnerStore = {

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

function tickValuesToNumbers(axis: Axis, values: (number | string | Date)[]) {
const nums = zrUtil.map(values, val => axis.scale.parse(val));
if (axis.type === 'time' && nums.length > 0) {
// Time axis needs duplicate first/last tick (see TimeScale.getTicks())
// The first and last tick/label don't get drawn
nums.sort();
nums.unshift(nums[0]);
nums.push(nums[nums.length - 1]);
}
return nums;
}

export function createAxisLabels(axis: Axis): {
labels: {
level?: number,
Expand All @@ -69,6 +81,20 @@ export function createAxisLabels(axis: Axis): {
}[],
labelCategoryInterval?: number
} {
const custom = axis.getLabelModel().get('customValues');
if (custom) {
const labelFormatter = makeLabelFormatter(axis);
return {
labels: tickValuesToNumbers(axis, custom).map(numval => {
const tick = {value: numval};
return {
formattedLabel: labelFormatter(tick),
rawLabel: axis.scale.getLabel(tick),
tickValue: numval
};
})
};
}
// Only ordinal scale support tick interval
return axis.type === 'category'
? makeCategoryLabels(axis)
Expand All @@ -87,6 +113,12 @@ export function createAxisTicks(axis: Axis, tickModel: AxisBaseModel): {
ticks: number[],
tickCategoryInterval?: number
} {
const custom = axis.getTickModel().get('customValues');
if (custom) {
return {
ticks: tickValuesToNumbers(axis, custom)
};
}
// Only ordinal scale support tick interval
return axis.type === 'category'
? makeCategoryTicks(axis, tickModel)
Expand Down
202 changes: 202 additions & 0 deletions test/axis-customTicks.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.