Skip to content

Commit

Permalink
feat: 坐标轴支持自定义宽高 (#1906)
Browse files Browse the repository at this point in the history
  • Loading branch information
zengyue authored Dec 28, 2023
1 parent cf9f566 commit d20ca53
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/f2/src/components/axis/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export interface Style<Type = void> {
}

export interface StyleProps<Type = void> extends Omit<Style, 'label' | 'grid' | 'labelOffset'> {
width?: number | string;
height?: number | string;
label?: StyleText<Type> | LabelCallback<Type>;
grid?: LineStyleProps | GridCallBack;
labelOffset?: number | string;
Expand Down
21 changes: 17 additions & 4 deletions packages/f2/src/components/axis/withAxis.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -214,18 +224,21 @@ export default (View) => {
}

measureLayout(): PositionLayout | PositionLayout[] {
const { props } = this;
const { visible, coord } = props;
const { props, context } = this;
const { visible, coord, style } = props;
if (visible === false) {
return null;
}
const { width: customWidth, height: customHeight } = style || {};

const ticks = this.getTicks();
const bbox = this.getMaxBBox(ticks, this.axisStyle);

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') {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions packages/f2/test/components/axis/axis.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 } = (
<Canvas context={context} pixelRatio={1}>
<Chart data={data}>
<Axis field="time" style={{ height: '100px' }} />
<Axis field="value" style={{ width: '100px' }} />
<Interval x="time" y="value" color="#2FC25B" />
</Chart>
</Canvas>
);
const canvas = new Canvas(props);
await canvas.render();

await delay(1000);
expect(context).toMatchImageSnapshot();
});
});

0 comments on commit d20ca53

Please sign in to comment.