diff --git a/packages/f2/src/components/axis/types.d.ts b/packages/f2/src/components/axis/types.d.ts index 7b31cf2de..65b08e22e 100644 --- a/packages/f2/src/components/axis/types.d.ts +++ b/packages/f2/src/components/axis/types.d.ts @@ -128,4 +128,12 @@ export interface AxisProps< style?: StyleProps; // 网格线类型 grid?: 'arc' | 'line'; + /** + * 宽度 + */ + width?: number | string; + /** + * 高度 + */ + height?: number | string; } diff --git a/packages/f2/src/components/axis/withAxis.tsx b/packages/f2/src/components/axis/withAxis.tsx index 35bf38406..d0bc00dcc 100644 --- a/packages/f2/src/components/axis/withAxis.tsx +++ b/packages/f2/src/components/axis/withAxis.tsx @@ -1,5 +1,15 @@ import { jsx, isEqual, Component } from '@antv/f-engine'; -import { deepMix, isFunction, mix, each, clone, isString, isNumber, isArray } from '@antv/util'; +import { + deepMix, + isFunction, + mix, + each, + clone, + isString, + isNumber, + isArray, + isNil, +} from '@antv/util'; import { ChartChildProps, PositionLayout } from '../../chart'; import { Style, Tick, AxisProps } from './types'; import { DataRecord } from '../../chart/Data'; @@ -214,8 +224,8 @@ export default (View) => { } measureLayout(): PositionLayout | PositionLayout[] { - const { props } = this; - const { visible, coord } = props; + const { props, context } = this; + const { visible, coord, width: customWidth, height: customHeight } = props; if (visible === false) { return null; } @@ -225,7 +235,9 @@ export default (View) => { const { isPolar } = coord; const dimType = this._getDimType(); - const { width, height } = bbox; + // const { width, height } = bbox; + const width = isNil(customWidth) ? bbox.width : context.px2hd(customWidth); + const height = isNil(customHeight) ? bbox.height : context.px2hd(customHeight); if (isPolar) { // 机坐标系的 y 不占位置 if (dimType === 'y') { diff --git "a/packages/f2/test/components/axis/__image_snapshots__/axis-test-tsx-axis-\350\275\264-\345\256\232\344\271\211\345\256\275\351\253\230-1-snap.png" "b/packages/f2/test/components/axis/__image_snapshots__/axis-test-tsx-axis-\350\275\264-\345\256\232\344\271\211\345\256\275\351\253\230-1-snap.png" new file mode 100644 index 000000000..6bf8077fb Binary files /dev/null and "b/packages/f2/test/components/axis/__image_snapshots__/axis-test-tsx-axis-\350\275\264-\345\256\232\344\271\211\345\256\275\351\253\230-1-snap.png" differ diff --git a/packages/f2/test/components/axis/axis.test.tsx b/packages/f2/test/components/axis/axis.test.tsx index aacfae96d..374c291e8 100644 --- a/packages/f2/test/components/axis/axis.test.tsx +++ b/packages/f2/test/components/axis/axis.test.tsx @@ -744,4 +744,57 @@ describe('Axis 轴', () => { await delay(1000); expect(context).toMatchImageSnapshot(); }); + + // 定义宽高 + it('定义宽高', async () => { + const context = createContext('定义宽高'); + const data = [ + { + time: 'Jan', + value: 551990, + }, + { + time: 'Feb', + value: 513513, + }, + { + time: 'Mar', + value: 538780, + }, + { + time: 'Apr', + value: 419562, + }, + { + time: 'May', + value: 332167, + }, + { + time: 'Jun', + value: 297956, + }, + { + time: 'Jul', + value: 311760, + }, + { + time: 'Aug', + value: 330824, + }, + ]; + const { props } = ( + + + + + + + + ); + const canvas = new Canvas(props); + await canvas.render(); + + await delay(1000); + expect(context).toMatchImageSnapshot(); + }); });