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

fix(layout): valid specified component size #5264

Merged
merged 1 commit into from
Jul 4, 2023
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
Binary file modified __tests__/integration/snapshots/static/bodyPointScatterPlot.png
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.
1 change: 1 addition & 0 deletions __tests__/plots/static/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,4 @@ export { mockAreaMissingData } from './mock-area-missing-data';
export { mockIntervalFacetRectPolar } from './mock-interval-facet-rect-polar';
export { alphabetIntervalAutoRotate } from './alphabet-interval-auto-padding-rotate';
export { mockLineSmallInterval } from './mock-line-small-interval';
export { unemploymentAreaStackedLegendSize } from './unemployment-area-stacked-legend-size';
28 changes: 28 additions & 0 deletions __tests__/plots/static/unemployment-area-stacked-legend-size.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { G2Spec } from '../../../src';

export function unemploymentAreaStackedLegendSize(): G2Spec {
return {
type: 'area',
padding: 'auto',
width: 800,
data: {
type: 'fetch',
value: 'data/unemployment-by-industry.csv',
},
transform: [{ type: 'stackY' }],
encode: {
x: 'date',
y: 'unemployed',
color: 'industry',
},
legend: { color: { size: 50 } },
viewStyle: {
viewFill: '#4e79a7',
plotFill: '#f28e2c',
mainFill: '#e15759',
contentFill: '#76b7b2',
},
};
}

unemploymentAreaStackedLegendSize.maxError = 180;
7 changes: 4 additions & 3 deletions src/runtime/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function inferComponent(
position: defaultPosition,
orientation: defaultOrientation,
order: defaultOrder,
size: defaultSize,
defaultSize,
...titleOptions,
});
}
Expand Down Expand Up @@ -135,7 +135,7 @@ export function inferComponent(
const defaultCrossPadding = isVertical ? DCP[1] : DCP[0];

const {
size = defaultSize,
size,
order = defaultOrder,
length = defaultLength,
padding = defaultPadding,
Expand All @@ -145,6 +145,7 @@ export function inferComponent(
components.push({
title: field,
...partialGuide,
defaultSize,
length,
position,
orientation,
Expand Down Expand Up @@ -523,7 +524,7 @@ function inferScrollableComponents(
const { defaultPosition, defaultSize, defaultOrder } = props;
return {
position: defaultPosition,
size: defaultSize,
defaultSize,
order: defaultOrder,
type: componentType,
...options,
Expand Down
46 changes: 31 additions & 15 deletions src/runtime/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,32 +257,48 @@ function computePadding(
paddingTop,
paddingRight,
};

for (const position of positions) {
const key = `padding${capitalizeFirst(camelCase(position))}`;
const components = positionComponents.get(position) || [];
const value = layout[key];
if (value === undefined || value === 'auto') {
if (!positionComponents.has(position)) {
const defaultSizeOf = (d) => {
if (d.size === undefined) d.size = d.defaultSize;
};
const autoSizeOf = (d) => {
if (d.size) return;
if (value !== 'auto') {
d.size = d.defaultSize;
return;
}
// Compute component size dynamically.
computeComponentSize(
d,
crossSize,
crossPadding,
position,
theme,
library,
);
defaultSizeOf(d);
};

// Specified padding.
if (typeof value === 'number') {
components.forEach(defaultSizeOf);
} else {
// Compute padding dynamically.
if (components.length === 0) {
layout[key] = 30;
} else {
const components = positionComponents.get(position);
const grouped = groupComponents(components, crossSize);
if (value === 'auto') {
grouped.forEach((component) =>
computeComponentSize(
component,
crossSize,
crossPadding,
position,
theme,
library,
),
);
}
grouped.forEach(autoSizeOf);
const totalSize = grouped.reduce((sum, { size }) => sum + size, 0);
layout[key] = totalSize;
}
}
}

return layout;
}

Expand Down