Skip to content

Commit fa64dd6

Browse files
lxfu1liufu.lf
and
liufu.lf
authored
docs: add demos (#1423)
* docs: add demos * docs: review modify * fix: 修复默认配置引起的单测问题 * fix: review modify * fix: 删除多余目录 * fix: 删除多余空格 Co-authored-by: liufu.lf <[email protected]>
1 parent 44dc124 commit fa64dd6

File tree

11 files changed

+195
-19
lines changed

11 files changed

+195
-19
lines changed

__tests__/unit/plots/line/axis-spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe('line', () => {
2828
},
2929
},
3030
yAxis: {
31+
nice: false,
3132
tickCount: 3,
3233
min: 500,
3334
},

__tests__/unit/plots/radar/axis-spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe('radar axis 设置', () => {
8080
const yAxes = getAxes(radar.chart)[1];
8181
expect(xAxes.component.get('label').style.fill).toBe('red');
8282
expect(xAxes.component.get('grid').line.style.lineWidth).toBe(0);
83-
expect(yAxes.component.get('ticks').length).toBe(3);
83+
expect(yAxes.component.get('ticks').length).toBe(4);
8484
expect(yAxes.component.get('tickCount')).toBe(4);
8585
});
8686

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Scatter } from '../../../../src';
2+
import { data } from '../../../data/gender';
3+
import { createDiv } from '../../../utils/dom';
4+
5+
describe('scatter', () => {
6+
it('default config', () => {
7+
const scatter = new Scatter(createDiv(), {
8+
width: 400,
9+
height: 300,
10+
data,
11+
xField: 'weight',
12+
yField: 'height',
13+
size: 10,
14+
xAxis: {
15+
nice: true,
16+
},
17+
});
18+
19+
scatter.render();
20+
const { options } = scatter;
21+
// @ts-ignore
22+
expect(options.tooltip.offset).toBe(20);
23+
});
24+
});

examples/scatter/basic/demo/basic.js renamed to examples/scatter/basic/demo/basic.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,27 @@ fetch('https://gw.alipayobjects.com/os/antfincdn/aao6XnO5pW/IMDB.json')
44
.then((res) => res.json())
55
.then((data) => {
66
const scatterPlot = new Scatter('container', {
7-
width: 400,
8-
height: 300,
97
appendPadding: 10,
108
data,
119
xField: 'Revenue (Millions)',
1210
yField: 'Rating',
1311
shape: 'circle',
12+
yAxis: {
13+
grid: {},
14+
},
1415
xAxis: {
15-
visible: true,
16+
min: -100,
17+
nice: true,
18+
grid: {
19+
line: {
20+
style: {
21+
stroke: '#eee',
22+
},
23+
},
24+
},
1625
},
1726
pointStyle: {
18-
stroke: '#777',
27+
stroke: '#fff',
1928
},
2029
});
2130
scatterPlot.render();

examples/scatter/basic/demo/color-mapping.js renamed to examples/scatter/basic/demo/color-mapping.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,29 @@ fetch('https://gw.alipayobjects.com/os/antfincdn/aao6XnO5pW/IMDB.json')
44
.then((res) => res.json())
55
.then((data) => {
66
const scatterPlot = new Scatter('container', {
7-
width: 400,
8-
height: 300,
97
appendPadding: 10,
108
data,
119
xField: 'Revenue (Millions)',
1210
yField: 'Rating',
1311
shape: 'circle',
1412
colorField: 'Genre',
1513
sizeField: 'Rating',
16-
size: [2, 6],
17-
xAxis: {
14+
size: [2, 5],
15+
yAxis: {
1816
nice: true,
1917
},
18+
xAxis: {
19+
min: -100,
20+
grid: {
21+
line: {
22+
style: {
23+
stroke: '#eee',
24+
},
25+
},
26+
},
27+
},
2028
pointStyle: {
21-
stroke: '#777',
29+
stroke: '#fff',
2230
},
2331
});
2432
scatterPlot.render();

examples/scatter/basic/demo/label.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Scatter } from '@antv/g2plot';
2+
3+
fetch('https://gw.alipayobjects.com/os/bmw-prod/3e4db10a-9da1-4b44-80d8-c128f42764a8.json')
4+
.then((res) => res.json())
5+
.then((data) => {
6+
const scatterPlot = new Scatter('container', {
7+
appendPadding: 30,
8+
data,
9+
xField: 'xG conceded',
10+
yField: 'Shot conceded',
11+
colorField: 'Result',
12+
color: ['#c71e1d', '#ffca76', '#09bb9f'],
13+
size: 5,
14+
shape: 'circle',
15+
pointStyle: {
16+
fillOpacity: 1,
17+
},
18+
xAxis: {
19+
grid: {
20+
line: {
21+
style: {
22+
stroke: '#eee',
23+
},
24+
},
25+
},
26+
},
27+
label: {},
28+
});
29+
scatterPlot.render();
30+
});

examples/scatter/basic/demo/meta.json

+19-3
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,44 @@
55
},
66
"demos": [
77
{
8-
"filename": "basic.js",
8+
"filename": "basic.ts",
99
"title": {
1010
"zh": "基础散点图",
1111
"en": "basic Scatter chart"
1212
},
1313
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*Cu0wSYK_U6YAAAAAAAAAAABkARQnAQ"
1414
},
1515
{
16-
"filename": "color-mapping.js",
16+
"filename": "color-mapping.ts",
1717
"title": {
1818
"zh": "散点图颜色映射",
1919
"en": "the color mapping of Scatter chart"
2020
},
2121
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*Mds9Q7-zeHMAAAAAAAAAAABkARQnAQ"
2222
},
2323
{
24-
"filename": "shape-mapping.js",
24+
"filename": "shape-mapping.ts",
2525
"title": {
2626
"zh": "散点形状色映射",
2727
"en": "the shape mapping of Scatter chart"
2828
},
2929
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/kRFIJ2OlFs/FE726B54-2352-47F5-AAF9-B17D281CE513.png"
30+
},
31+
{
32+
"filename": "label.ts",
33+
"title": {
34+
"zh": "散点图图形标签",
35+
"en": "Scatter chart label"
36+
},
37+
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*XlOeS4YAHbYAAAAAAAAAAABkARQnAQ"
38+
},
39+
{
40+
"filename": "size.ts",
41+
"title": {
42+
"zh": "散点图-气泡",
43+
"en": "Bubble chart"
44+
},
45+
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*_lGkQrhj53gAAAAAAAAAAABkARQnAQ"
3046
}
3147
]
3248
}

examples/scatter/basic/demo/shape-mapping.js renamed to examples/scatter/basic/demo/shape-mapping.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,30 @@ fetch('https://gw.alipayobjects.com/os/antfincdn/aao6XnO5pW/IMDB.json')
44
.then((res) => res.json())
55
.then((data) => {
66
const scatterPlot = new Scatter('container', {
7-
width: 400,
8-
height: 300,
97
appendPadding: 10,
108
data,
119
xField: 'Revenue (Millions)',
1210
yField: 'Rating',
1311
shapeField: 'Genre',
1412
shape: ['circle', 'square', 'bowtie', 'diamond', 'hexagon', 'triangle', 'hyphen', 'line', 'cross'],
1513
colorField: 'Genre',
14+
size: 6,
15+
yAxis: {
16+
nice: true,
17+
},
1618
xAxis: {
19+
min: -100,
1720
nice: true,
21+
grid: {
22+
line: {
23+
style: {
24+
stroke: '#eee',
25+
},
26+
},
27+
},
1828
},
1929
pointStyle: {
20-
stroke: '#777',
30+
stroke: '#fff',
2131
},
2232
});
2333
scatterPlot.render();

examples/scatter/basic/demo/size.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Scatter } from '@antv/g2plot';
2+
3+
fetch('https://gw.alipayobjects.com/os/bmw-prod/0b37279d-1674-42b4-b285-29683747ad9a.json')
4+
.then((res) => res.json())
5+
.then((data) => {
6+
const scatterPlot = new Scatter('container', {
7+
appendPadding: 30,
8+
data,
9+
xField: 'change in female rate',
10+
yField: 'change in male rate',
11+
sizeField: 'pop',
12+
colorField: 'continent',
13+
color: ['#ffd500', '#82cab2', '#193442', '#d18768', '#7e827a'],
14+
size: [4, 30],
15+
shape: 'circle',
16+
pointStyle: {
17+
fillOpacity: 0.8,
18+
},
19+
xAxis: {
20+
min: -25,
21+
max: 5,
22+
grid: {
23+
line: {
24+
style: {
25+
stroke: '#eee',
26+
},
27+
},
28+
},
29+
},
30+
});
31+
scatterPlot.render();
32+
});

src/core/plot.ts

+29-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,39 @@ import { deepMix } from '@antv/util';
33
import EE from '@antv/event-emitter';
44
import { bind } from 'size-sensor';
55
import { Adaptor } from './adaptor';
6-
import { ChartOptions, Data, Size } from '../types';
6+
import { ChartOptions, Options, Data, Size } from '../types';
77
import { getContainerSize } from '../utils';
88

99
/**
1010
* 所有 plot 的基类
1111
*/
1212
export abstract class Plot<O extends ChartOptions> extends EE {
13+
/** 默认配置 */
14+
private defaultOptions: Partial<Options> = {
15+
renderer: 'canvas',
16+
tooltip: {
17+
shared: true,
18+
showCrosshairs: true,
19+
crosshairs: {
20+
type: 'x',
21+
},
22+
offset: 20,
23+
},
24+
xAxis: {
25+
nice: true,
26+
label: {
27+
autoRotate: true,
28+
autoHide: true,
29+
},
30+
},
31+
yAxis: {
32+
nice: true,
33+
label: {
34+
autoHide: true,
35+
autoRotate: false,
36+
},
37+
},
38+
};
1339
/** plot 类型名称 */
1440
public abstract readonly type: string = 'base';
1541
/** plot 的 schema 配置 */
@@ -81,8 +107,8 @@ export abstract class Plot<O extends ChartOptions> extends EE {
81107
* 获取默认的 options 配置项
82108
* 每个组件都可以复写
83109
*/
84-
protected getDefaultOptions(): Partial<O> {
85-
return {};
110+
protected getDefaultOptions(): Partial<Options> {
111+
return this.defaultOptions;
86112
}
87113

88114
/**

src/plots/scatter/index.ts

+20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { deepMix } from '@antv/util';
12
import { Plot } from '../../core/plot';
23
import { ScatterOptions } from './types';
34
import { adaptor } from './adaptor';
@@ -16,4 +17,23 @@ export class Scatter extends Plot<ScatterOptions> {
1617
protected getSchemaAdaptor(): Adaptor<ScatterOptions> {
1718
return adaptor;
1819
}
20+
21+
protected getDefaultOptions() {
22+
return deepMix({}, super.getDefaultOptions(), {
23+
size: 4,
24+
pointStyle: {
25+
lineWidth: 1,
26+
strokeOpacity: 1,
27+
fillOpacity: 0.95,
28+
stroke: '#fff',
29+
},
30+
tooltip: {
31+
shared: null,
32+
showTitle: false,
33+
showMarkers: false,
34+
showCrosshairs: false,
35+
},
36+
shape: 'circle',
37+
});
38+
}
1939
}

0 commit comments

Comments
 (0)