Skip to content

Commit 4948f64

Browse files
authored
feat(heatmap): 热力图主题色默认从 theme 中获取连续色板 (#2535)
* feat(heatmap): 热力图主题色默认从 theme 中获取连续色板 * test: skip uncontrolled case
1 parent 0953303 commit 4948f64

File tree

5 files changed

+51
-16
lines changed

5 files changed

+51
-16
lines changed

__tests__/unit/plots/heatmap/index-spec.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Heatmap } from '../../../../src';
22
import { basicHeatmapData, semanticBasicHeatmapData } from '../../../data/basic-heatmap';
33
import { createDiv } from '../../../utils/dom';
4-
import { DEFAULT_COLORS } from '../../../../src/constant';
54
import { DEFAULT_OPTIONS } from '../../../../src/plots/heatmap/constant';
65

76
describe('heatmap', () => {
@@ -37,7 +36,7 @@ describe('heatmap', () => {
3736
}
3837
});
3938

40-
const colors = DEFAULT_COLORS.GRADIENT.CONTINUOUS.split('-');
39+
const colors = heatmap.chart.getTheme().sequenceColors;
4140
expect(elements[maxElementIndex].getModel().color.toUpperCase()).toBe(colors[colors.length - 1]);
4241
expect(elements[minElementIndex].getModel().color.toUpperCase()).toBe(colors[0]);
4342

@@ -92,7 +91,7 @@ describe('heatmap', () => {
9291
}
9392
});
9493

95-
const colors = DEFAULT_COLORS.GRADIENT.CONTINUOUS.split('-');
94+
const colors = heatmap.chart.getTheme().sequenceColors;
9695
expect(elements[maxElementIndex].getModel().color.toUpperCase()).toBe(colors[colors.length - 1]);
9796
expect(elements[minElementIndex].getModel().color.toUpperCase()).toBe(colors[0]);
9897

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Heatmap } from '../../../../src';
2+
import { semanticBasicHeatmapData } from '../../../data/basic-heatmap';
3+
import { createDiv } from '../../../utils/dom';
4+
5+
describe('heatmap', () => {
6+
it('theme', () => {
7+
const sequenceColors = ['#FF0000', '#00FF00', '#0000FF'];
8+
const heatmap = new Heatmap(createDiv('basic heatmap'), {
9+
width: 400,
10+
height: 300,
11+
data: semanticBasicHeatmapData,
12+
xField: 'name',
13+
yField: 'day',
14+
colorField: 'sales',
15+
theme: {
16+
sequenceColors,
17+
},
18+
});
19+
20+
heatmap.render();
21+
22+
// @ts-ignore
23+
expect(heatmap.getDefaultOptions()).toBe(Heatmap.getDefaultOptions());
24+
25+
const geometry = heatmap.chart.geometries[0];
26+
27+
const { elements } = geometry;
28+
29+
let maxElementIndex = 0;
30+
let minElementIndex = 0;
31+
32+
elements.forEach((e, i) => {
33+
const value = e.getData().sales;
34+
if (elements[maxElementIndex].getData().sales < value) {
35+
maxElementIndex = i;
36+
}
37+
if (elements[minElementIndex].getData().sales > value) {
38+
minElementIndex = i;
39+
}
40+
});
41+
42+
expect(elements[maxElementIndex].getModel().color.toUpperCase()).toBe(sequenceColors[sequenceColors.length - 1]);
43+
expect(elements[minElementIndex].getModel().color.toUpperCase()).toBe(sequenceColors[0]);
44+
45+
heatmap.destroy();
46+
});
47+
});

examples/heatmap/basic/demo/calendar.ts

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/github-commit.json')
6363
xField: 'week',
6464
yField: 'day',
6565
colorField: 'commits',
66-
color: '#BAE7FF-#1890FF-#0050B3',
6766
reflect: 'y',
6867
shape: 'boundary-polygon',
6968
meta: {

src/constant.ts

-9
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,3 @@ export const AXIS_META_CONFIG_KEYS = [
2323
// 是否同步
2424
'sync',
2525
];
26-
27-
/**
28-
* 默认色彩
29-
*/
30-
export const DEFAULT_COLORS = {
31-
GRADIENT: {
32-
CONTINUOUS: '#BAE7FF-#1890FF-#0050B3',
33-
},
34-
};

src/plots/heatmap/adaptor.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Geometry } from '@antv/g2';
33
import { Params } from '../../core/adaptor';
44
import { findGeometry } from '../../utils';
55
import { flow, transformLabel } from '../../utils';
6-
import { DEFAULT_COLORS } from '../../constant';
76
import { tooltip, interaction, animation, theme, scale, annotation, state } from '../../adaptor/common';
87
import { HeatmapOptions } from './types';
98

@@ -25,7 +24,7 @@ function field(params: Params<HeatmapOptions>): Params<HeatmapOptions> {
2524
}
2625

2726
if (colorField) {
28-
geometry.color(colorField, color || DEFAULT_COLORS.GRADIENT.CONTINUOUS);
27+
geometry.color(colorField, color || chart.getTheme().sequenceColors.join('-'));
2928
}
3029

3130
/**
@@ -199,9 +198,9 @@ function coordinate(params: Params<HeatmapOptions>): Params<HeatmapOptions> {
199198
export function adaptor(params: Params<HeatmapOptions>) {
200199
// flow 的方式处理所有的配置到 G2 API
201200
return flow(
201+
theme,
202202
field,
203203
meta,
204-
theme,
205204
axis,
206205
legend,
207206
tooltip,

0 commit comments

Comments
 (0)