Skip to content

Commit 089b123

Browse files
authored
fix(gauge): 仪表盘的 ticks 默认不均匀,且自定义 scale 无效 (#2085)
* fix(gauge): 仪表盘的 ticks 默认不均匀,且自定义 scale 无效 * fix: lint
1 parent 4be91f9 commit 089b123

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { Gauge } from '../../src';
2+
import { createDiv } from '../utils/dom';
3+
4+
describe('charts #367', () => {
5+
it('gauge axis ticks', () => {
6+
const gauge = new Gauge(createDiv(), {
7+
percent: 0.75,
8+
range: {
9+
ticks: [0, 0.6, 0.8, 0.9, 1],
10+
color: ['#F4664A', '#FAAD14', '#F2EAEA', '#96dcb0'],
11+
},
12+
indicator: {
13+
pointer: {
14+
style: {
15+
stroke: '#D0D0D0',
16+
},
17+
},
18+
pin: {
19+
style: {
20+
stroke: '#D0D0D0',
21+
},
22+
},
23+
},
24+
});
25+
26+
gauge.render();
27+
28+
expect(gauge.chart.views[0].getXScale().tickCount).toBe(5);
29+
expect(gauge.chart.views[0].getXScale().tickInterval).toBe(0.2);
30+
expect(
31+
gauge.chart.views[0]
32+
.getXScale()
33+
.getTicks()
34+
.map((t) => t.value)
35+
).toEqual([0, 0.2, 0.4, 0.6, 0.8, 1.0]);
36+
37+
const TICKS = [0, 0.6, 0.8, 0.9, 1];
38+
const LABELS = ['差', '及格', '中', '良', '优'];
39+
gauge.update({
40+
axis: {
41+
tickMethod: () => TICKS,
42+
label: {
43+
formatter: (v) => {
44+
return LABELS[TICKS.indexOf(Number(v))];
45+
},
46+
},
47+
subTickLine: null,
48+
},
49+
});
50+
51+
expect(
52+
gauge.chart.views[0]
53+
.getController('axis')
54+
.getComponents()[0]
55+
.component.getContainer()
56+
// @ts-ignore
57+
.getChildByIndex(0)
58+
.getChildByIndex(0)
59+
.getChildren()
60+
.map((v) => v.attr('text'))
61+
).toEqual(LABELS);
62+
});
63+
});

src/plots/gauge/adaptor.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { isString, clamp, size } from '@antv/util';
22
import { interaction, animation, theme, scale } from '../../adaptor/common';
3+
import { AXIS_META_CONFIG_KEYS } from '../../constant';
34
import { Params } from '../../core/adaptor';
45
import { Data } from '../../types';
5-
import { deepAssign, flow, renderGaugeStatistic } from '../../utils';
6+
import { deepAssign, flow, pick, renderGaugeStatistic } from '../../utils';
67
import { RANGE_TYPE, RANGE_VALUE, PERCENT, DEFAULT_COLOR } from './constant';
78
import { GaugeOptions } from './types';
89
import { processRangeData } from './utils';
@@ -41,6 +42,8 @@ function geometry(params: Params<GaugeOptions>): Params<GaugeOptions> {
4142
});
4243

4344
v1.axis(PERCENT, axis);
45+
// 一部分应用到 scale 中
46+
v1.scale(PERCENT, pick(axis, AXIS_META_CONFIG_KEYS));
4447
}
4548

4649
// 辅助 range

src/plots/gauge/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ export class Gauge extends Plot<GaugeOptions> {
6868
},
6969
[PERCENT]: {
7070
sync: 'v',
71+
tickCount: 5,
72+
tickInterval: 0.2,
7173
},
7274
},
7375
animation: false,

0 commit comments

Comments
 (0)