Skip to content

Commit

Permalink
fix: axis arrow direction
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcc committed Aug 15, 2023
1 parent bb387ea commit bfd6b44
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 25 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions __tests__/plots/static/alphabet-interval-axis-arrow-position.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { G2Spec } from '../../../src';

export function alphabetIntervalAxisArrowPosition(): G2Spec {
return {
type: 'interval',
transform: [{ type: 'sortX', by: 'y', reverse: true }],
data: {
type: 'fetch',
value: 'data/alphabet.csv',
},
axis: {
x: { line: true, arrow: true, position: 'top' },
y: { labelFormatter: '.0%', line: true, arrow: true, position: 'right' },
},
encode: {
x: 'letter',
y: 'frequency',
color: 'steelblue',
},
};
}
21 changes: 21 additions & 0 deletions __tests__/plots/static/alphabet-interval-axis-arrow-transposed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { G2Spec } from '../../../src';

export function alphabetIntervalAxisArrowTransposed(): G2Spec {
return {
type: 'interval',
coordinate: { transform: [{ type: 'transpose' }] },
data: {
type: 'fetch',
value: 'data/alphabet.csv',
},
axis: {
x: { line: true, arrow: true },
y: { labelFormatter: '.0%', line: true, arrow: true },
},
encode: {
x: 'letter',
y: 'frequency',
color: 'steelblue',
},
};
}
20 changes: 20 additions & 0 deletions __tests__/plots/static/alphabet-interval-axis-arrow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { G2Spec } from '../../../src';

export function alphabetIntervalAxisArrow(): G2Spec {
return {
type: 'interval',
data: {
type: 'fetch',
value: 'data/alphabet.csv',
},
axis: {
x: { line: true, arrow: true },
y: { labelFormatter: '.0%', line: true, arrow: true },
},
encode: {
x: 'letter',
y: 'frequency',
color: 'steelblue',
},
};
}
3 changes: 3 additions & 0 deletions __tests__/plots/static/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ export { mockAxisY } from './mock-axisY';
export { mockAxisXY } from './mock-axisXY';
export { mockAxisXYPolar } from './mock-axisXY-polar';
export { alphabetIntervalAxis } from './alphabet-interval-axis';
export { alphabetIntervalAxisArrow } from './alphabet-interval-axis-arrow';
export { alphabetIntervalAxisArrowTransposed } from './alphabet-interval-axis-arrow-transposed';
export { alphabetIntervalAxisArrowPosition } from './alphabet-interval-axis-arrow-position';
export { commitsPointGroupedLegendFlexCenter } from './commits-point-grouped-legend-flex-center';
export { commitsPointGroupedLegendFlexRight } from './commits-point-grouped-legend-flex-right';
export { commitsPointGroupedLegendPositionRight } from './commits-point-grouped-legend-position-right';
Expand Down
15 changes: 10 additions & 5 deletions src/component/axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Coordinate } from '@antv/coord';
import type { DisplayObject } from '@antv/g';
import { Axis as AxisComponent } from '@antv/gui';
import { Linear as LinearScale } from '@antv/scale';
import { deepMix, has, omit } from '@antv/util';
import { deepMix, omit } from '@antv/util';
import { extent } from 'd3-array';
import { format } from 'd3-format';
import {
Expand All @@ -24,6 +24,7 @@ import {
radiusOf,
} from '../utils/coordinate';
import { capitalizeFirst } from '../utils/helper';
import { isOrdinalScale } from '../utils/scale';
import { adaptor, isVertical, titleContent } from './utils';

export type AxisOptions = {
Expand Down Expand Up @@ -157,6 +158,7 @@ function getData(
const applyFisheye = createFisheye(position, coordinate);
const isHorizontal = (position) =>
['top', 'bottom', 'center', 'outer'].includes(position);
const isVertical = (position) => ['left', 'right'].includes(position);

// @todo GUI should consider the overlap problem for the first
// and label of arc axis.
Expand All @@ -168,7 +170,9 @@ function getData(
(isRadial(coordinate) && position === 'center') ||
(isTranspose(coordinate) &&
scale.getTicks?.() &&
isHorizontal(position));
isHorizontal(position)) ||
(isTranspose(coordinate) && isVertical(position));

return {
value: shouldReverse ? 1 - tick : tick,
label: toString(labelFormatter(prettyNumber(d), i, array)),
Expand All @@ -180,8 +184,9 @@ function getData(
return filteredTicks.map((d, i, array) => {
const offset = scale.getBandWidth?.(d) / 2 || 0;
const tick = applyFisheye(applyInset(scale.map(d) + offset));
const shouldReverse = isVertical(position);
return {
value: tick,
value: shouldReverse ? 1 - tick : tick,
label: toString(labelFormatter(prettyNumber(d), i, array)),
id: String(i),
};
Expand Down Expand Up @@ -291,10 +296,10 @@ function inferAxisLinearOverrideStyle(
return { startPos: [x, y], endPos: [x + width, y] };
}
if (position === 'left') {
return { startPos: [x + width, y], endPos: [x + width, y + height] };
return { startPos: [x + width, y + height], endPos: [x + width, y] };
}
if (position === 'right') {
return { endPos: [x, y + height], startPos: [x, y] };
return { startPos: [x, y + height], endPos: [x, y] };
}
if (position === 'top') {
return { startPos: [x, y + height], endPos: [x + width, y + height] };
Expand Down
12 changes: 6 additions & 6 deletions src/theme/academy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,21 @@ export const Academy: TC<AcademyOptions> = (options) => {
titleTextBaseline: 'bottom',
},
axisLeft: {
gridDirection: 'negative',
labelDirection: 'positive',
gridDirection: 'positive',
labelDirection: 'negative',
labelSpacing: 4,
tickDirection: 'positive',
tickDirection: 'negative',
titlePosition: 'left',
titleSpacing: 4,
titleTextBaseline: 'middle',
titleDirection: 'vertical',
titleTransformOrigin: 'center',
},
axisRight: {
gridDirection: 'positive',
labelDirection: 'negative',
gridDirection: 'negative',
labelDirection: 'positive',
labelSpacing: 4,
tickDirection: 'negative',
tickDirection: 'positive',
titlePosition: 'right',
titleSpacing: 0,
titleTextBaseline: 'top',
Expand Down
13 changes: 6 additions & 7 deletions src/theme/classic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,23 +245,22 @@ export const Classic: TC<ClassicOptions> = (options) => {
titleTextBaseline: 'bottom',
},
axisLeft: {
gridDirection: 'negative',
gridDirection: 'positive',
labelAutoRotate: false,
labelDirection: 'positive',
labelDirection: 'negative',
labelSpacing: 4,
tickDirection: 'positive',
tickDirection: 'negative',
titlePosition: 'left',
titleSpacing: 10,
titleTextBaseline: 'middle',
titleDirection: 'vertical',
titleTransformOrigin: 'center',
},
axisRight: {
gridDirection: 'positive',
labelDirection: 'negative',
gridDirection: 'negative',
labelDirection: 'positive',
labelSpacing: 4,
labelAutoRotate: false,
tickDirection: 'negative',
tickDirection: 'positive',
titlePosition: 'right',
titleSpacing: 0,
titleTextBaseline: 'top',
Expand Down
12 changes: 6 additions & 6 deletions src/theme/classicDark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,22 +244,22 @@ export const ClassicDark: TC<ClassicDarkOptions> = (options) => {
titleTextBaseline: 'bottom',
},
axisLeft: {
gridDirection: 'negative',
gridDirection: 'positive',
labelAutoRotate: false,
labelDirection: 'positive',
labelDirection: 'negative',
labelSpacing: 4,
tickDirection: 'positive',
tickDirection: 'negative',
titlePosition: 'left',
titleSpacing: 10,
titleTextBaseline: 'middle',
titleDirection: 'vertical',
titleTransformOrigin: 'center',
},
axisRight: {
gridDirection: 'positive',
labelDirection: 'negative',
gridDirection: 'negative',
labelDirection: 'positive',
labelSpacing: 4,
tickDirection: 'negative',
tickDirection: 'positive',
titlePosition: 'right',
titleSpacing: 0,
titleTextBaseline: 'top',
Expand Down
2 changes: 1 addition & 1 deletion src/utils/scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function constrain(x, lo, hi) {
}

export function isOrdinalScale(scale) {
return scale.getBandWidth;
return !!scale.getBandWidth;
}

export function invert(scale, x, start) {
Expand Down

0 comments on commit bfd6b44

Please sign in to comment.