Skip to content

Commit

Permalink
feat(line): 折线图支持坐标系镜像反转 (#2925)
Browse files Browse the repository at this point in the history
  • Loading branch information
visiky authored Oct 21, 2021
1 parent bf84671 commit 4d4b22b
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 2 deletions.
8 changes: 8 additions & 0 deletions docs/api/plots/line.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ order: 0

### Data Mapping

### Coordinate

#### reflect

<description>**optional** _'x' | 'y' | ['x', 'y']_</description>

Apply `reflect` transform to the coordinate of line plot. When `reflect: 'y'` is set, y-axis can be inverted; in the same way, you can set `reflect: 'x'` to invert x-axis, and invert x-axis and y-axis at the same time is also supported.

#### data

<description>**required** _array object_</description>
Expand Down
10 changes: 9 additions & 1 deletion docs/api/plots/line.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ order: 0

`markdown:docs/common/chart-options.zh.md`

### 坐标系

#### 轴镜像反转 - reflect

<description>**可选** _'x' | 'y' | ['x', 'y']_</description>

折线图坐标系反转。当设置 `reflect: 'y'`时,可以对 y-axis 进行反转;同理,设置 `reflect: 'x'`时,可以对 x-axis 进行反转;也支持同时对 x-axis 和 y-axis 进行反转。

### 数据映射

#### data
Expand Down Expand Up @@ -88,7 +96,7 @@ const data = [

`markdown:docs/common/component.zh.md`

#### 缩略轴
#### 缩略轴 - slider

`markdown:docs/common/slider.zh.md`

Expand Down
9 changes: 9 additions & 0 deletions examples/line/basic/demo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
},
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*VN5SQYsPOaIAAAAAAAAAAAAAARQnAQ"
},
{
"filename": "reflect-y.ts",
"title": {
"zh": "折线图 y 轴镜像反转",
"en": "Invert y-axis of line plot"
},
"new": true,
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/f8CNbPpLQj/a0eaf523-7c9d-4cec-aaaf-74f6e29412e0.png"
},
{
"filename": "line-slider.ts",
"title": {
Expand Down
23 changes: 23 additions & 0 deletions examples/line/basic/demo/reflect-y.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Line } from '@antv/g2plot';

const data = [
{ Date: '2021-10-01', rating: 1 },
{ Date: '2021-10-02', rating: 3 },
{ Date: '2021-10-03', rating: 8 },
{ Date: '2021-10-04', rating: 12 },
{ Date: '2021-10-05', rating: 30 },
];

const line = new Line('container', {
data,
padding: 'auto',
xField: 'Date',
yField: 'rating',
reflect: 'y',
xAxis: {
// because coordinate is reflectY
position: 'top',
},
});

line.render();
23 changes: 22 additions & 1 deletion src/plots/line/adaptor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Geometry } from '@antv/g2';
import { each } from '@antv/util';
import { each, isArray } from '@antv/util';
import { Params } from '../../core/adaptor';
import { tooltip, slider, interaction, animation, theme, scale, annotation, limitInPlot } from '../../adaptor/common';
import { findGeometry, transformLabel, deepAssign } from '../../utils';
Expand Down Expand Up @@ -71,6 +71,26 @@ export function meta(params: Params<LineOptions>): Params<LineOptions> {
)(params);
}

/**
* 坐标系配置. 支持 reflect 镜像处理
* @param params
*/
function coordinate(params: Params<LineOptions>): Params<LineOptions> {
const { chart, options } = params;
const { reflect } = options;
if (reflect) {
let p = reflect as any;
if (!isArray(p)) {
p = [p];
}
const actions = p.map((d) => ['reflect', d]);

chart.coordinate({ type: 'rect', actions });
}

return params;
}

/**
* axis 配置
* @param params
Expand Down Expand Up @@ -174,6 +194,7 @@ export function adaptor(params: Params<LineOptions>) {
meta,
adjust,
theme,
coordinate,
axis,
legend,
tooltip,
Expand Down
3 changes: 3 additions & 0 deletions src/plots/line/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ export interface LineOptions extends Options {
readonly lineShape?: Required<LineGeometryOptions>['line']['shape'];
/** 折线数据点:1、图形映射属性 2、状态样式 */
readonly point?: PointGeometryOptions['point'] & Pick<PointGeometryOptions, 'state'>;

// 支持坐标系配置
readonly reflect?: 'x' | 'y' | ['x', 'y'];
}

0 comments on commit 4d4b22b

Please sign in to comment.