Skip to content

Commit

Permalink
fix(Axis): axisTick.interval work correct
Browse files Browse the repository at this point in the history
  • Loading branch information
erweixin committed Dec 7, 2022
1 parent 6fdb47a commit 1fd6116
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
21 changes: 15 additions & 6 deletions src/coord/Axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,13 @@ function fixOnBandTicksCoords(
return;
}

const intervalIsAutoMode = axis.getTickModel().get('interval') === 'auto';

const axisExtent = axis.getExtent();
let last;
let diffSize;
if (ticksLen === 1) {
ticksCoords[0].coord = axisExtent[0];
last = ticksCoords[1] = {coord: axisExtent[0]};
ticksCoords[0].coord -= axis.getBandWidth() / 2;
}
else {
const crossLen = ticksCoords[ticksLen - 1].tickValue - ticksCoords[0].tickValue;
Expand All @@ -312,14 +313,22 @@ function fixOnBandTicksCoords(
ticksItem.coord -= shift / 2;
});

const dataExtent = axis.scale.getExtent();
diffSize = 1 + dataExtent[1] - ticksCoords[ticksLen - 1].tickValue;
// auto mode: need push a normal tick
// function mode: last tick depend on function return value
// number mode: last tick depend on number
if (intervalIsAutoMode) {
const dataExtent = axis.scale.getExtent();
diffSize = 1 + dataExtent[1] - ticksCoords[ticksLen - 1].tickValue;

last = {coord: ticksCoords[ticksLen - 1].coord + shift * diffSize};
last = {coord: ticksCoords[ticksLen - 1].coord + shift * diffSize};

ticksCoords.push(last);
ticksCoords.push(last);
}
}

// TODO: use array.at(-1) when lib target es2022
last = ticksCoords[ticksCoords.length - 1];

const inverse = axisExtent[0] > axisExtent[1];

// Handling clamp.
Expand Down
18 changes: 17 additions & 1 deletion src/coord/axisTickLabelBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,12 @@ function makeLabelsByNumericCategoryInterval(axis: Axis, categoryInterval: numbe
const labelModel = axis.getLabelModel();
const result: (MakeLabelsResultObj | number)[] = [];

// onBand mode, need one more tick
// |---1---|---2---|---3---|---4---| need 5 tick
if (axis.onBand && ordinalExtent[1]) {
ordinalExtent[1]++;
}

// TODO: axisType: ordinalTime, pick the tick from each month/day/year/...

const step = Math.max((categoryInterval || 0) + 1, 1);
Expand Down Expand Up @@ -411,7 +417,17 @@ function makeLabelsByCustomizedCategoryInterval(axis: Axis, categoryInterval: Ca
const labelFormatter = makeLabelFormatter(axis);
const result: (MakeLabelsResultObj | number)[] = [];

zrUtil.each(ordinalScale.getTicks(), function (tick) {
const originTicks = ordinalScale.getTicks();
// onBand mode, need one more tick
if (axis.onBand && originTicks.length) {
// TODO: use array.at(-1) when lib target es2022
const lastTicks = originTicks[originTicks.length - 1];
originTicks.push({
...lastTicks,
value: lastTicks.value + 1
});
}
zrUtil.each(originTicks, function (tick) {
const rawLabel = ordinalScale.getLabel(tick);
const tickValue = tick.value;
if (categoryInterval(tick.value, rawLabel)) {
Expand Down

0 comments on commit 1fd6116

Please sign in to comment.