Skip to content

Commit

Permalink
fix(axis): auto hide when necessary (#5660)
Browse files Browse the repository at this point in the history
* fix(axis): auto hide when necessary

* fix: ci
  • Loading branch information
pearmini authored Oct 20, 2023
1 parent 79283f4 commit a02fb8e
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 21 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.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function indicesLineChartFacetBrushShared(): Promise<G2Spec> {
type: 'line',
frame: false,
scale: { y: { nice: true, facet: false } },
axis: { y: { labelAutoRotate: false } },
axis: { y: { labelAutoHide: false } },
encode: {
x: 'Date',
y: 'Close',
Expand Down
2 changes: 1 addition & 1 deletion __tests__/plots/interaction/indices-line-chart-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function indicesLineChartFacet(): Promise<G2Spec> {
type: 'line',
frame: false,
scale: { y: { nice: true, facet: false } },
axis: { y: { labelAutoRotate: false } },
axis: { y: { labelAutoHide: false } },
encode: {
x: 'Date',
y: 'Close',
Expand Down
26 changes: 26 additions & 0 deletions __tests__/plots/static/alphabet-interval-rotate-padding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { G2Spec } from '../../../src';

export function alphabetIntervalRotatePadding(): G2Spec {
return {
type: 'interval',
transform: [{ type: 'sortX', by: 'y', reverse: true }],
data: {
type: 'fetch',
value: 'data/alphabet.csv',
},
encode: {
x: 'letter',
y: 'frequency',
color: 'steelblue',
},
axis: {
x: { labelFormatter: (d) => d + '0000', labelAutoRotate: false },
},
viewStyle: {
viewFill: '#4e79a7',
plotFill: '#f28e2c',
mainFill: '#e15759',
contentFill: '#76b7b2',
},
};
}
27 changes: 27 additions & 0 deletions __tests__/plots/static/alphabet-interval-sized-padding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { G2Spec } from '../../../src';

export function alphabetIntervalSizedPadding(): G2Spec {
return {
type: 'interval',
paddingBottom: 20,
transform: [{ type: 'sortX', by: 'y', reverse: true }],
data: {
type: 'fetch',
value: 'data/alphabet.csv',
},
encode: {
x: 'letter',
y: 'frequency',
color: 'steelblue',
},
axis: {
x: { labelFormatter: (d) => d + '0000' },
},
viewStyle: {
viewFill: '#4e79a7',
plotFill: '#f28e2c',
mainFill: '#e15759',
contentFill: '#76b7b2',
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function barleyPointFacetRectColZIndex(): Promise<G2Spec> {
color: 'year',
shape: 'hollow',
},
axis: { y: { labelAutoRotate: false } },
axis: { y: { labelAutoHide: false } },
},
{
type: 'lineY',
Expand Down
2 changes: 1 addition & 1 deletion __tests__/plots/static/barley-point-facet-rect-col.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function barleyPointFacetRectCol(): Promise<G2Spec> {
color: 'year',
shape: 'hollow',
},
axis: { y: { labelAutoRotate: false } },
axis: { y: { labelAutoHide: false } },
},
],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export function incomeStatementByRegionIntervalCustom() {
data: incomeStatementByRegion,
axis: {
x: {
labelAutoRotate: false,
labelAutoEllipsis: true,
title: false,
labelFontSize: 10,
Expand Down
4 changes: 3 additions & 1 deletion __tests__/plots/static/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export { housePricePointShapes } from './house-price-point-shapes';
export { aaplLineSlider } from './aapl-line-slider';
export { aaplLineZIndex } from './aapl-line-z-index';
export { aaplLineSliderTransposed } from './aapl-line-slider-transposed';
export { indicesLineChartFacetAxis } from './indices-line-chart-fact-axis';
export { indicesLineChartFacetAxis } from './indices-line-chart-facet-axis';
export { indicesLineChartScaleRelations } from './indices-line-chart-scale-relations';
export { aaplLineClip } from './aapl-line-clip';
export { githubStarIntervalPalette } from './github-star-interval-palette';
Expand Down Expand Up @@ -295,3 +295,5 @@ export { intervalPointBullet } from './interval-point-bullet';
export { intervalPointBullets } from './interval-point-bullets';
export { intervalPointBulletDatas } from './interval-point-bullet-datas';
export { mockIntervalLine } from './mock-interval-line';
export { alphabetIntervalSizedPadding } from './alphabet-interval-sized-padding';
export { alphabetIntervalRotatePadding } from './alphabet-interval-rotate-padding';
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function indicesLineChartFacetAxis(): Promise<G2Spec> {
frame: false,
scale: { y: { nice: true, facet: false } },
axis: {
y: { labelAutoRotate: false },
y: { labelAutoHide: false },
x: ({ rowIndex }) =>
rowIndex === 0
? { position: 'top' }
Expand Down
2 changes: 2 additions & 0 deletions __tests__/plots/static/liquid-default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ export function liquidDefault(): G2Spec {
},
};
}

liquidDefault.maxError = 10;
10 changes: 3 additions & 7 deletions __tests__/plots/static/month-interval-facet-rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,15 @@ export function monthIntervalFacetRect(): G2Spec {
width: 800,
data: mockData(),
encode: { x: 'day', y: 'week' },
scale: {
x: { domain: days },
},
legend: {
color: { position: 'right', size: 50 },
},
scale: { x: { domain: days } },
legend: { color: { position: 'right', size: 50 } },
paddingLeft: 40,
paddingBottom: 40,
children: [
{
type: 'interval',
transform: [{ type: 'stackY' }],
axis: { x: { labelAutoRotate: false } },
axis: { x: { labelAutoHide: false }, y: { labelAutoHide: false } },
encode: {
x: 'activity',
y: 'value',
Expand Down
12 changes: 10 additions & 2 deletions src/runtime/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,13 @@ function computeAxisSize(

// Get styles to be applied.
const style = styleOf(component, position, theme);
const { tickLength = 0, labelSpacing = 0, titleSpacing = 0, ...rest } = style;
const {
tickLength = 0,
labelSpacing = 0,
titleSpacing = 0,
labelAutoRotate,
...rest
} = style;

// Compute Labels.
const scale = createScale(component, library);
Expand All @@ -727,7 +733,9 @@ function computeAxisSize(
// rotate 90 deg to display them.
if (
overflowX(scale, labelBBoxes, crossSize, crossPadding, tickFilter) &&
!labelTransform
!labelTransform &&
labelAutoRotate !== false &&
labelAutoRotate !== null
) {
component.labelTransform = 'rotate(90)';
component.size = maxLabelWidth + paddingTick;
Expand Down
7 changes: 6 additions & 1 deletion src/runtime/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ function computePadding(

for (const position of positions) {
const key = `padding${capitalizeFirst(camelCase(position))}`;
const insetKey = `inset${capitalizeFirst(camelCase(position))}`;
const components = positionComponents.get(position) || [];
const value = layout[key];
const defaultSizeOf = (d) => {
Expand Down Expand Up @@ -342,6 +341,11 @@ function computePadding(
}
};

const maybeHide = (d) => {
if (!d.type.startsWith('axis')) return;
if (d.labelAutoHide === undefined) d.labelAutoHide = true;
};

const isHorizontal = position === 'bottom' || position === 'top';

// !!!Note
Expand All @@ -356,6 +360,7 @@ function computePadding(
// Specified padding.
if (typeof value === 'number') {
components.forEach(defaultSizeOf);
components.forEach(maybeHide);
} else {
// Compute padding dynamically.
if (components.length === 0) {
Expand Down
4 changes: 0 additions & 4 deletions src/theme/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,13 @@ export function create({
labelDirection: 'negative',
tickDirection: 'negative',
titlePosition: 'top',
labelAutoRotate: false,
titleSpacing: 12,
labelSpacing: 4,
titleTextBaseline: 'middle',
},
axisBottom: {
gridDirection: 'negative',
labelDirection: 'positive',
labelAutoRotate: false,
tickDirection: 'positive',
titlePosition: 'bottom',
titleSpacing: 12,
Expand All @@ -253,7 +251,6 @@ export function create({
},
axisLeft: {
gridDirection: 'positive',
labelAutoRotate: false,
labelDirection: 'negative',
labelSpacing: 4,
tickDirection: 'negative',
Expand All @@ -269,7 +266,6 @@ export function create({
labelSpacing: 4,
tickDirection: 'positive',
titlePosition: 'right',
labelAutoRotate: false,
titleSpacing: 12,
titleTextBaseline: 'top',
titleDirection: 'vertical',
Expand Down

0 comments on commit a02fb8e

Please sign in to comment.