Skip to content

Commit a3021c7

Browse files
authored
feat(line): support lineShape config (#2053)
* feat(line): support lineShape config * feat(line): do not affect stepType config
1 parent d292545 commit a3021c7

File tree

4 files changed

+60
-19
lines changed

4 files changed

+60
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Line } from '../../../../src';
2+
import { createDiv } from '../../../utils/dom';
3+
import { salesByArea, subSalesByArea } from '../../../data/sales';
4+
5+
describe('line shape', () => {
6+
it('undefined shape', () => {
7+
const plot = new Line(createDiv('undefined mapping'), {
8+
data: salesByArea,
9+
xField: 'area',
10+
yField: 'sales',
11+
});
12+
13+
expect(() => plot.render()).not.toThrow();
14+
});
15+
16+
it('shape mapping, value', () => {
17+
const plot = new Line(createDiv('shape mapping'), {
18+
data: salesByArea,
19+
xField: 'area',
20+
yField: 'sales',
21+
lineShape: 'hvh',
22+
});
23+
plot.render();
24+
25+
expect(plot.chart.geometries[0].attributes.shape.values).toEqual(['hvh']);
26+
});
27+
28+
it('shape mapping, callback', () => {
29+
const shapeCallback = (datum) => ({ 公司: 'smooth', 小型企业: 'line', 消费者: 'hv' }[datum.series]);
30+
const plot = new Line(createDiv('shape mapping'), {
31+
data: subSalesByArea,
32+
xField: 'area',
33+
yField: 'sales',
34+
seriesField: 'series',
35+
lineShape: shapeCallback,
36+
});
37+
plot.render();
38+
39+
plot.chart.geometries[0].elements.forEach((element) => {
40+
const model = element.getModel();
41+
expect(model.shape).toBe(shapeCallback(model.data[0]));
42+
});
43+
});
44+
});

src/adaptor/geometries/line.ts

+11-17
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1+
import { deepMix } from '@antv/util';
12
import { Params } from '../../core/adaptor';
2-
import { ColorAttr, StyleAttr, SizeAttr } from '../../types';
3+
import { ColorAttr, StyleAttr, SizeAttr, ShapeAttr } from '../../types';
34
import { getTooltipMapping } from '../../utils/tooltip';
45
import { deepAssign } from '../../utils';
5-
import { GeometryOptions, geometry } from './base';
6-
7-
type LineOption = {
8-
/** line color 映射, 提供回调的方式, 不开放 field 映射配置 */
9-
readonly color?: ColorAttr;
10-
/** 样式映射 */
11-
readonly style?: StyleAttr;
12-
/** 折线宽度 */
13-
readonly size?: SizeAttr;
14-
};
6+
import { GeometryOptions, geometry, MappingOptions } from './base';
157

168
export interface LineGeometryOptions extends GeometryOptions {
179
/** x 轴字段 */
@@ -25,7 +17,7 @@ export interface LineGeometryOptions extends GeometryOptions {
2517
/** 是否连接空数据 */
2618
readonly connectNulls?: boolean;
2719
/** line 映射配置 */
28-
readonly line?: LineOption;
20+
readonly line?: MappingOptions;
2921
/** 阶梯折线图类型 */
3022
readonly stepType?: string;
3123
}
@@ -48,11 +40,13 @@ export function line<O extends LineGeometryOptions>(params: Params<O>): Params<O
4840
type: 'line',
4941
colorField: seriesField,
5042
tooltipFields: fields,
51-
mapping: {
52-
shape: stepType || (smooth ? 'smooth' : 'line'),
53-
tooltip: formatter,
54-
...line,
55-
},
43+
mapping: deepMix(
44+
{
45+
shape: stepType || (smooth ? 'smooth' : 'line'),
46+
tooltip: formatter,
47+
},
48+
line
49+
),
5650
args: { connectNulls },
5751
},
5852
})

src/plots/line/adaptor.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { LineOptions } from './types';
1414
*/
1515
function geometry(params: Params<LineOptions>): Params<LineOptions> {
1616
const { chart, options } = params;
17-
const { data, color, lineStyle, point: pointMapping, seriesField } = options;
17+
const { data, color, lineStyle, lineShape, point: pointMapping, seriesField } = options;
1818

1919
chart.data(data);
2020

@@ -25,6 +25,7 @@ function geometry(params: Params<LineOptions>): Params<LineOptions> {
2525
line: {
2626
color,
2727
style: lineStyle,
28+
shape: lineShape,
2829
},
2930
// 颜色保持一致,因为如果颜色不一致,会导致 tooltip 中元素重复。
3031
// 如果存在,才设置,否则为空

src/plots/line/types.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PointGeometryOptions } from '../../adaptor/geometries';
1+
import { LineGeometryOptions, PointGeometryOptions } from '../../adaptor/geometries';
22
import { Options, StyleAttr } from '../../types';
33

44
export interface LineOptions extends Options {
@@ -18,6 +18,8 @@ export interface LineOptions extends Options {
1818
readonly connectNulls?: boolean;
1919
/** 折线图形样式 */
2020
readonly lineStyle?: StyleAttr;
21+
/** 折线 shape 配置 */
22+
readonly lineShape?: LineGeometryOptions['line']['shape'];
2123
/** 折线数据点图形样式 */
2224
readonly point?: PointGeometryOptions['point'];
2325
}

0 commit comments

Comments
 (0)