Skip to content

Commit

Permalink
fix: auto fit (#5649)
Browse files Browse the repository at this point in the history
  • Loading branch information
lxfu1 authored Oct 17, 2023
1 parent 84f3cf2 commit c9a0d90
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ site/.dumi/tmp-production
!/__tests__/unit/lib

# Package
package-lock.json
package-lock.json
pnpm-lock.yaml
34 changes: 28 additions & 6 deletions src/api/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isNumber } from '@antv/util';
import { G2ViewTree } from '../runtime';
import { getContainerSize } from '../utils/size';
import { deepAssign } from '../utils/helper';
Expand Down Expand Up @@ -33,6 +34,11 @@ export const REMOVE_FLAG = '__remove__';

export const CALLBACK_NODE = '__callback__';

/** Minimum chart width */
export const MIN_CHART_WIDTH = 1;
/** Minimum chart height */
export const MIN_CHART_HEIGHT = 1;

export function normalizeContainer(
container: string | HTMLElement,
): HTMLElement {
Expand Down Expand Up @@ -70,12 +76,28 @@ export function valueOf(node: Node): Record<string, any> {
}

export function sizeOf(options, container) {
const { autoFit } = options;
const { width = 640, height = 480 } = autoFit
? getContainerSize(container)
: options;
const { depth = 0 } = options;
return { width, height, depth };
const { width = 640, height = 480, autoFit, depth = 0 } = options;
let effectiveWidth = width;
let effectiveHeight = height;

if (autoFit) {
const { width: containerWidth, height: containerHeight } =
getContainerSize(container);
effectiveWidth = containerWidth || effectiveWidth;
effectiveHeight = containerHeight || effectiveHeight;
}

return {
width: Math.max(
isNumber(effectiveWidth) ? effectiveWidth : MIN_CHART_WIDTH,
MIN_CHART_WIDTH,
),
height: Math.max(
isNumber(effectiveHeight) ? effectiveHeight : MIN_CHART_HEIGHT,
MIN_CHART_HEIGHT,
),
depth,
};
}

export function optionsOf(node: Node): Record<string, any> {
Expand Down

0 comments on commit c9a0d90

Please sign in to comment.