Skip to content

Commit 55842a6

Browse files
authored
feat: add polar attribute to heatmap (#2425)
* feat: add polar attribute to heatmap * feat: add polar attribute to heatmap add heatMap polar examples * feat: add polar attribute to heatmap add heatMap polar examples * feat: add polar attribute to heatmap add heatMap polar examples -2 * feat: add polar attribute add heatMap polar examples add document * feat: add polar attribute add heatMap polar examples add document * feat: add polar attribute add heatMap polar examples add document Co-authored-by: wb-xcf804241 <[email protected]>
1 parent 2589854 commit 55842a6

File tree

8 files changed

+230
-6
lines changed

8 files changed

+230
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { Heatmap } from '../../../../src';
2+
import { semanticBasicHeatmapData } from '../../../data/basic-heatmap';
3+
import { createDiv } from '../../../utils/dom';
4+
5+
describe('heatmap', () => {
6+
it('x*y*color and default coordinate', () => {
7+
const heatmap = new Heatmap(createDiv('default axis'), {
8+
width: 400,
9+
height: 300,
10+
data: semanticBasicHeatmapData,
11+
xField: 'name',
12+
yField: 'day',
13+
colorField: 'sales',
14+
shape: 'circle',
15+
});
16+
17+
heatmap.render();
18+
19+
// @ts-ignore
20+
expect(heatmap.chart.options.coordinate.type).toBe('rect');
21+
22+
heatmap.destroy();
23+
});
24+
25+
it('x*y*color and custom axis', () => {
26+
const heatmap = new Heatmap(createDiv('custom axis'), {
27+
width: 400,
28+
height: 300,
29+
data: semanticBasicHeatmapData,
30+
xField: 'name',
31+
yField: 'day',
32+
colorField: 'sales',
33+
shape: 'circle',
34+
coordinate: {
35+
type: 'polar',
36+
cfg: {
37+
radius: 0.85,
38+
innerRadius: 0.2,
39+
},
40+
},
41+
});
42+
43+
heatmap.render();
44+
45+
// @ts-ignore
46+
expect(heatmap.chart.options.coordinate.type).toBe('polar');
47+
// @ts-ignore
48+
expect(heatmap.chart.options.coordinate.cfg.radius).toBe(0.85);
49+
// @ts-ignore
50+
expect(heatmap.chart.options.coordinate.cfg.innerRadius).toBe(0.2);
51+
52+
heatmap.update({
53+
...heatmap.options,
54+
coordinate: {
55+
type: 'polar',
56+
cfg: {
57+
startAngle: 1,
58+
endAngle: 2,
59+
},
60+
},
61+
});
62+
63+
// @ts-ignore
64+
expect(heatmap.chart.options.coordinate.cfg.startAngle).toBe(1);
65+
// @ts-ignore
66+
expect(heatmap.chart.options.coordinate.cfg.endAngle).toBe(2);
67+
68+
heatmap.destroy();
69+
});
70+
});

docs/api/plots/heatmap.en.md

+27
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,33 @@ Axis mapping.
5555

5656
Shapes in thermal grids, density heat maps are not specified.
5757

58+
#### coordinate
59+
60+
<description>**optional** </description>
61+
62+
Coordinate system configuration property.
63+
64+
| Properties | Type | Description |
65+
| ------- | --------------------- | ------------------------------ |
66+
| type | string | Type of coordinate system |
67+
| cfg | _CoordinateCfg_ | Coordinate system configuration term, currently commonly used in polar coordinates |
68+
69+
_**CoordinateOption.type**_ Type of coordinate system:
70+
71+
- cartesian / rect:Cartesian coordinate system
72+
- polar:Polar coordinates
73+
- helix:Spiral coordinate system, based on Archimedes helix
74+
- theta:A special polar coordinate system with fixed radius lengths that maps data only to angles, often used in pie charts
75+
76+
_**CoordinateCfg**_ Coordinate system configuration term, currently commonly used in polar coordinates:
77+
78+
| Properties | Type | Description |
79+
| ----------- | -------- | ------------------------------------------ |
80+
| startAngle | _number_ | For polar coordinates, configure the starting radian |
81+
| endAngle | _number_ | For polar coordinates, configure end radians |
82+
| radius | _number_ | For polar coordinates, configure polar radius, values in the 0-1 range |
83+
| innerRadius | _number_ | For polar coordinates, radius within polar coordinates, values in the range 0-1 |
84+
5885
#### sizeRatio
5986

6087
<description>**optional** _number_</description>

docs/api/plots/heatmap.zh.md

+27
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,33 @@ order: 23
5555

5656
热力格子中的形状,密度热力图不用指定。
5757

58+
#### coordinate
59+
60+
<description>**可选**</description>
61+
62+
坐标系配置属性。
63+
64+
| 参数名 | 类型 | 描述 |
65+
| ------- | --------------------- | ----------------------------|
66+
| type | string | 坐标系类型 |
67+
| cfg | _CoordinateCfg_ | 坐标系配置项,目前常用于极坐标 |
68+
69+
_**CoordinateOption.type**_ 坐标系类型:
70+
71+
- cartesian / rect:笛卡尔坐标系
72+
- polar:极坐标系
73+
- helix:螺旋坐标系,基于阿基米德螺旋线
74+
- theta:一种特殊的极坐标系,半径长度固定,仅仅将数据映射到角度,常用于饼图的绘制
75+
76+
_**CoordinateCfg**_ 坐标系配置项,目前常用于极坐标:
77+
78+
| 参数名 | 类型 | 描述 |
79+
| ----------- | -------- | ----------------------------------------|
80+
| startAngle | _number_ | 用于极坐标,配置起始弧度 |
81+
| endAngle | _number_ | 用于极坐标,配置结束弧度 |
82+
| radius | _number_ | 用于极坐标,配置极坐标半径,0-1 范围的数值 |
83+
| innerRadius | _number_ | 用于极坐标,极坐标内半径,0 -1 范围的数值 |
84+
5885
#### sizeRatio
5986

6087
<description>**可选** _number_</description>

examples/heatmap/basic/demo/meta.json

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@
3535
"en": "Calendar heatmap plot"
3636
},
3737
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*aLbWR5ioU2UAAAAAAAAAAAAAARQnAQ"
38+
},
39+
{
40+
"filename": "polar.ts",
41+
"title": {
42+
"zh": "极坐标色块图",
43+
"en": "Polar heatmap plot"
44+
},
45+
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*cNrISYzITTcAAAAAAAAAAAAAARQnAQ"
3846
}
3947
]
4048
}

examples/heatmap/basic/demo/polar.ts

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { Heatmap } from '@antv/g2plot';
2+
3+
fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/polar-heatmap.json')
4+
.then((res) => res.json())
5+
.then((data) => {
6+
const heatmapPlot = new Heatmap(document.getElementById('container'), {
7+
data,
8+
xField: 'time',
9+
yField: 'week',
10+
colorField: 'value',
11+
legend: true,
12+
color: '#BAE7FF-#1890FF-#1028ff',
13+
coordinate: {
14+
// 坐标轴属性配置
15+
type: 'polar', // 极坐标
16+
cfg: {
17+
innerRadius: 0.2,
18+
},
19+
},
20+
heatmapStyle: {
21+
stroke: '#f5f5f5',
22+
opacity: 0.8,
23+
},
24+
meta: {
25+
time: {
26+
type: 'cat',
27+
},
28+
value: {
29+
min: 0,
30+
max: 1,
31+
},
32+
},
33+
xAxis: {
34+
line: null,
35+
grid: null,
36+
tickLine: null,
37+
label: {
38+
offset: 12,
39+
style: {
40+
fill: '#666',
41+
fontSize: 12,
42+
textBaseline: 'top',
43+
},
44+
},
45+
},
46+
yAxis: {
47+
top: true,
48+
line: null,
49+
grid: null,
50+
tickLine: null,
51+
label: {
52+
offset: 0,
53+
style: {
54+
fill: '#fff',
55+
textAlign: 'center',
56+
shadowBlur: 2,
57+
shadowColor: 'rgba(0, 0, 0, .45)',
58+
},
59+
},
60+
},
61+
tooltip: {
62+
showMarkers: false,
63+
},
64+
interactions: [{ type: 'element-active' }],
65+
});
66+
heatmapPlot.render();
67+
});

src/plots/heatmap/adaptor.ts

+25-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { HeatmapOptions } from './types';
1313
*/
1414
function field(params: Params<HeatmapOptions>): Params<HeatmapOptions> {
1515
const { chart, options } = params;
16-
const { data, type, reflect, xField, yField, colorField, sizeField, sizeRatio, shape, color } = options;
16+
const { data, type, xField, yField, colorField, sizeField, sizeRatio, shape, color } = options;
1717

1818
chart.data(data);
1919
let geometry: Geometry;
@@ -28,10 +28,6 @@ function field(params: Params<HeatmapOptions>): Params<HeatmapOptions> {
2828
geometry.color(colorField, color || DEFAULT_COLORS.GRADIENT.CONTINUOUS);
2929
}
3030

31-
if (reflect) {
32-
chart.coordinate().reflect(reflect);
33-
}
34-
3531
/**
3632
* The ratio between the actual size and the max available size, must be in range `[0,1]`.
3733
*
@@ -173,6 +169,28 @@ function label(params: Params<HeatmapOptions>): Params<HeatmapOptions> {
173169
return params;
174170
}
175171

172+
/**
173+
* 极坐标
174+
* @param params
175+
*/
176+
function coordinate(params: Params<HeatmapOptions>): Params<HeatmapOptions> {
177+
const { chart, options } = params;
178+
const { coordinate, reflect } = options;
179+
180+
if (coordinate) {
181+
chart.coordinate({
182+
type: coordinate.type || 'rect',
183+
cfg: coordinate.cfg,
184+
});
185+
}
186+
187+
if (reflect) {
188+
chart.coordinate().reflect(reflect);
189+
}
190+
191+
return params;
192+
}
193+
176194
/**
177195
* 热力图适配器
178196
* @param chart
@@ -192,6 +210,7 @@ export function adaptor(params: Params<HeatmapOptions>) {
192210
annotation(),
193211
interaction,
194212
animation,
195-
state
213+
state,
214+
coordinate
196215
)(params);
197216
}

src/plots/heatmap/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ export class Heatmap extends Plot<HeatmapOptions> {
2424
return deepAssign({}, super.getDefaultOptions(), {
2525
type: 'polygon',
2626
legend: false,
27+
coordinate: {
28+
type: 'rect',
29+
},
2730
xAxis: {
2831
tickLine: null,
2932
line: null,

src/plots/heatmap/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Types } from '@antv/g2';
12
import { Options, StyleAttr } from '../../types';
23

34
export interface HeatmapOptions extends Options {
@@ -19,4 +20,6 @@ export interface HeatmapOptions extends Options {
1920
readonly heatmapStyle?: StyleAttr;
2021
/** 坐标轴映射 */
2122
readonly reflect?: 'x' | 'y';
23+
/** 极坐标属性 */
24+
readonly coordinate?: Types.CoordinateOption;
2225
}

0 commit comments

Comments
 (0)