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: auto fit #2294

Merged
merged 3 commits into from
Dec 12, 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
8 changes: 8 additions & 0 deletions packages/plots/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 2.1.2

`2023-12-12`

- 🔥 新增 segment chart 示例,优化 transform 逻辑
- 🐞 修复 autoFit 配置无效


## 2.1.1

`2023-12-08`
Expand Down
2 changes: 1 addition & 1 deletion packages/plots/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ant-design/plots",
"version": "2.1.1",
"version": "2.1.2",
"description": "G2Plot Statistical chart",
"bugs": {
"url": "https://github.com/ant-design/ant-design-charts/issues"
Expand Down
12 changes: 5 additions & 7 deletions packages/plots/src/core/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ChartEvent } from '@antv/g2';
import { Chart } from './chart';
import { Annotaion } from '../annotation';
import { CHART_OPTIONS, ANNOTATION_LIST, SKIP_DEL_CUSTOM_SIGN } from '../constants';
import { deepAssign, omit, pick, deleteCustomKeys, deleteChartOptionKeys } from '../utils';
import { deepAssign, pick } from '../utils';

import type { G2Chart } from './chart';
import type { Adaptor, Options } from '../types';
Expand Down Expand Up @@ -35,11 +35,9 @@ export abstract class Plot<O extends PickOptions> extends EE {
* new Chart 所需配置
*/
private getChartOptions() {
const { autoFit = true } = this.options;
return {
...pick(this.options, CHART_OPTIONS),
container: this.container,
autoFit,
};
}

Expand All @@ -50,7 +48,7 @@ export abstract class Plot<O extends PickOptions> extends EE {
if (this.type === 'base' || this[SKIP_DEL_CUSTOM_SIGN]) {
return { ...this.options, ...this.getChartOptions() };
}
return deleteCustomKeys(omit(this.options, CHART_OPTIONS), true);
return this.options;
}

/**
Expand Down Expand Up @@ -80,13 +78,13 @@ export abstract class Plot<O extends PickOptions> extends EE {
}

private getBaseOptions(): Partial<Options> {
return { type: 'view' };
return { type: 'view', autoFit: true };
}

/**
* 获取默认的 options 配置项,每个组件都可以复写
*/
protected getDefaultOptions(): any { }
protected getDefaultOptions(): any {}

/**
* 绘制
Expand Down Expand Up @@ -182,7 +180,7 @@ export abstract class Plot<O extends PickOptions> extends EE {
// 转化成 G2 Spec
adaptor({
chart: this.chart,
options: deleteChartOptionKeys(this.options),
options: this.options,
});
}

Expand Down
13 changes: 7 additions & 6 deletions packages/plots/src/core/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { isArray, isBoolean } from '../utils';

/** new Chart options */
export const CHART_OPTIONS = [
export const CHART_OPTIONS = ['renderer'];
/** There is only the view layer, no need to pass it down to children */
export const VIEW_OPTIONS = [
'width',
'height',
'renderer',
'autoFit',
'canvas',
'theme',
'inset',
'insetLeft',
Expand All @@ -26,11 +26,12 @@ export const CHART_OPTIONS = [
'depth',
'title',
'clip',
'children',
'type',
'data',
'direction',
];

/** 最终透传给 G2 Spec 的保留字 */
export const RESERVED_KEYS = ['data', 'type', 'children', 'direction'];

/** 特殊标识,用于标识改配置来自于转换逻辑,而非用户配置 */
export const TRANSFORM_SIGN = '__transform__';

Expand Down
43 changes: 24 additions & 19 deletions packages/plots/src/core/plots/dual-axes/adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,32 @@ export function adaptor(params: Params) {
*/
const annotations = (params: Params) => {
const { options } = params;
const { annotations = [], children = [] } = options;
const { annotations = [], children = [], scale } = options;
let sharedScale = false;
if (get(scale, 'y.key')) {
return params;
}
children.forEach((child, index) => {
const scaleKey = `child${index}Scale`;
set(child, 'scale.y.key', scaleKey);
const { annotations: childAnnotations = [] } = child;
/**
* @description If the child has annotations, the scale of the child needs to be assigned scaleKey to connect the annotation.
*/
if (childAnnotations.length > 0) {
set(child, 'scale.y.independent', false);
childAnnotations.forEach((annotation) => {
set(annotation, 'scale.y.key', scaleKey);
});
}
if (!sharedScale && annotations.length > 0 && get(child, 'scale.y.independent') === undefined) {
sharedScale = true;
set(child, 'scale.y.independent', false);
annotations.forEach((annotation) => {
set(annotation, 'scale.y.key', scaleKey);
});
if (!get(child, 'scale.y.key')) {
const scaleKey = `child${index}Scale`;
set(child, 'scale.y.key', scaleKey);
const { annotations: childAnnotations = [] } = child;
/**
* @description If the child has annotations, the scale of the child needs to be assigned scaleKey to connect the annotation.
*/
if (childAnnotations.length > 0) {
set(child, 'scale.y.independent', false);
childAnnotations.forEach((annotation) => {
set(annotation, 'scale.y.key', scaleKey);
});
}
if (!sharedScale && annotations.length > 0 && get(child, 'scale.y.independent') === undefined) {
sharedScale = true;
set(child, 'scale.y.independent', false);
annotations.forEach((annotation) => {
set(annotation, 'scale.y.key', scaleKey);
});
}
}
});
return params;
Expand Down
15 changes: 0 additions & 15 deletions packages/plots/src/core/utils/delete-chart-option-keys.ts

This file was deleted.

14 changes: 1 addition & 13 deletions packages/plots/src/core/utils/delete-custom-keys.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { COORDIANTE_OPTIONS } from '../components';
import { getCustomKeys } from './get-custom-keys';
import { RESERVED_KEYS } from '../constants';
import { Options } from '../types';

/**
* 统一删除已转换的配置项
*/
export const deleteCustomKeys = <O extends Options>(options: O, isRender?: boolean): O => {
export const deleteCustomKeys = <O extends Options>(options: O): O => {
const deleteKeys = getCustomKeys();
[...deleteKeys, ...COORDIANTE_OPTIONS].forEach((key) => {
delete options[key];
Expand All @@ -20,16 +19,5 @@ export const deleteCustomKeys = <O extends Options>(options: O, isRender?: boole
});
});

/**
* @description 最终渲染,除保留配置外,其余全部删除,避免配置污染
*/
if (isRender) {
Object.keys(options).forEach((key) => {
if (!RESERVED_KEYS.includes(key)) {
delete options[key];
}
});
}

return options;
};
1 change: 0 additions & 1 deletion packages/plots/src/core/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export { isCompositePlot } from './is-composite-plot';
export { transformOptions } from './transform';
export { getShapeConfigKeys } from './get-shape-config-keys';
export { deleteCustomKeys } from './delete-custom-keys';
export { deleteChartOptionKeys } from './delete-chart-option-keys';
export { filterTransformed } from './filter-transformed';
export { conversionTagFormatter } from './conversion';
export { deepAssign } from '@ant-design/charts-util';
9 changes: 2 additions & 7 deletions packages/plots/src/core/utils/transform.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SPECIAL_OPTIONS, TRANSFORM_OPTION_KEY, CONFIG_SHAPE } from '../constants';
import { SPECIAL_OPTIONS, TRANSFORM_OPTION_KEY, CONFIG_SHAPE, VIEW_OPTIONS } from '../constants';
import {
omit,
pick,
Expand All @@ -19,12 +19,7 @@ export const transformOptions = (params: Adaptor) => {
const options = filterTransformed(params);
const { children = [] } = options;

const getRest = (o: Adaptor['options']) => {
const { children, type, data, ...rest } = o;
return omit(rest, getShapeConfigKeys());
};

const rest = getRest(options);
const rest = omit(options, [].concat(VIEW_OPTIONS, getShapeConfigKeys()));

const getValue = (newConfig: string | Function, value: unknown, origin: Options) => {
if (typeof newConfig === 'function') {
Expand Down
8 changes: 8 additions & 0 deletions site/examples/statistics/dual-axes/demo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
},
"screenshot": "https://mdn.alipayobjects.com/mdn/huamei_qa8qxu/afts/img/A*GLvKQbqMjTQAAAAAAAAAAAAADmJ7AQ/fmt.webp"
},
{
"filename": "segment.js",
"title": {
"zh": " 分段图",
"en": "Segment Chart"
},
"screenshot": "https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*t5wnSouqkP4AAAAAAAAAAAAADmJ7AQ/original"
},
{
"filename": "multi-line.js",
"title": {
Expand Down
53 changes: 53 additions & 0 deletions site/examples/statistics/dual-axes/demo/segment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { DualAxes } from '@ant-design/plots';
import React from 'react';
import ReactDOM from 'react-dom';

const DemoDualAxes = () => {
const data = [
{ year: '1991', value: null, count: 3 },
{ year: '1992', value: null, count: 3 },
{ year: '1993', value: null, count: 5 },
{ year: '1994', value: 5, count: 5 },
{ year: '1995', value: 6, count: null },
{ year: '1996', value: 8, count: null },
{ year: '1997', value: 7, count: null },
{ year: '1998', value: 9, count: null },
];

const config = {
data,
xField: 'year',
scale: {
y: {
independent: false,
key: 'sameKey',
domain: [0, 10],
},
},
tooltip: {
items: [
(item) => ({
name: '销售额',
value: item.value | item.count,
}),
],
},
children: [
{
type: 'line',
yField: 'count',
},
{
type: 'line',
yField: 'value',
style: {
lineDash: [2, 4],
stroke: 'red',
},
},
],
};
return <DualAxes {...config} />;
};

ReactDOM.render(<DemoDualAxes />, document.getElementById('container'));