From c9a0d90807ef46bd7b9fd5052fc2c1a1d951dad1 Mon Sep 17 00:00:00 2001 From: Joel Alan <31396322+lxfu1@users.noreply.github.com> Date: Tue, 17 Oct 2023 17:52:51 +0800 Subject: [PATCH] fix: auto fit (#5649) --- .gitignore | 3 ++- src/api/utils.ts | 34 ++++++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 2fa851ea69..10458210b6 100644 --- a/.gitignore +++ b/.gitignore @@ -43,4 +43,5 @@ site/.dumi/tmp-production !/__tests__/unit/lib # Package -package-lock.json \ No newline at end of file +package-lock.json +pnpm-lock.yaml diff --git a/src/api/utils.ts b/src/api/utils.ts index e06a3abea7..e36b0573ae 100644 --- a/src/api/utils.ts +++ b/src/api/utils.ts @@ -1,3 +1,4 @@ +import { isNumber } from '@antv/util'; import { G2ViewTree } from '../runtime'; import { getContainerSize } from '../utils/size'; import { deepAssign } from '../utils/helper'; @@ -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 { @@ -70,12 +76,28 @@ export function valueOf(node: Node): Record { } 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 {