Skip to content

Commit 86f110b

Browse files
authored
fix(indicator): when indicator > 1 or < 0 in gauge, progress, ring (#1673)
* fix(indicator): when indicator > 1 or < 0 in gauge, progress, ring * chore: remove unused console.log
1 parent 79fcdc9 commit 86f110b

File tree

7 files changed

+114
-6
lines changed

7 files changed

+114
-6
lines changed

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

+28
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,32 @@ describe('gauge', () => {
113113

114114
expect(gauge.options.range.ticks).toEqual([0, 0.65, 1]);
115115
});
116+
117+
it('> 1, < 0', () => {
118+
const gauge = new Gauge(createDiv(), {
119+
width: 600,
120+
height: 300,
121+
autoFit: false,
122+
percent: 1.65,
123+
range: {
124+
color: ['l(0) 0:#5d7cef 1:#e35767'],
125+
},
126+
});
127+
128+
gauge.render();
129+
// @ts-ignore
130+
window.gauge = gauge;
131+
132+
expect(gauge.chart.views[0].getData()).toEqual([{ percent: 1 }]);
133+
134+
gauge.update({
135+
...gauge.options,
136+
percent: -1.65,
137+
});
138+
139+
expect(gauge.chart.views[0].getData()).toEqual([{ percent: 0 }]);
140+
141+
// 多 view 的时候,防止 update 的时候 view 泄露
142+
expect(gauge.chart.views.length).toBe(2);
143+
});
116144
});

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

+38
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,42 @@ describe('progress', () => {
198198

199199
expect(progress.chart.geometries[0].getAttribute('color').values).toEqual(['green', '#E8EDF3']);
200200
});
201+
202+
it('> 1, < 0', () => {
203+
const progress = new Progress(createDiv(), {
204+
width: 600,
205+
height: 300,
206+
autoFit: false,
207+
percent: 1.65,
208+
});
209+
210+
progress.render();
211+
212+
expect(progress.chart.getData()).toEqual([
213+
{
214+
type: 'current',
215+
percent: 1,
216+
},
217+
{
218+
type: 'target',
219+
percent: 0,
220+
},
221+
]);
222+
223+
progress.update({
224+
...progress.options,
225+
percent: -1.65,
226+
});
227+
228+
expect(progress.chart.getData()).toEqual([
229+
{
230+
type: 'current',
231+
percent: 0,
232+
},
233+
{
234+
type: 'target',
235+
percent: 1,
236+
},
237+
]);
238+
});
201239
});

__tests__/unit/plots/ring-progress/index-spec.ts

+38
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,42 @@ describe('ring-progress', () => {
221221

222222
expect(ring.chart.geometries[0].getAttribute('color').values).toEqual(['green', '#E8EDF3']);
223223
});
224+
225+
it('> 1, < 0', () => {
226+
const ring = new RingProgress(createDiv(), {
227+
width: 600,
228+
height: 300,
229+
autoFit: false,
230+
percent: 1.65,
231+
});
232+
233+
ring.render();
234+
235+
expect(ring.chart.getData()).toEqual([
236+
{
237+
type: 'current',
238+
percent: 1,
239+
},
240+
{
241+
type: 'target',
242+
percent: 0,
243+
},
244+
]);
245+
246+
ring.update({
247+
...ring.options,
248+
percent: -1.65,
249+
});
250+
251+
expect(ring.chart.getData()).toEqual([
252+
{
253+
type: 'current',
254+
percent: 0,
255+
},
256+
{
257+
type: 'target',
258+
percent: 1,
259+
},
260+
]);
261+
});
224262
});

src/core/plot.ts

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export abstract class Plot<O extends PickOptions> extends EE {
140140
public render() {
141141
// 暴力处理,先清空再渲染,需要 G2 层自行做好更新渲染
142142
this.chart.clear();
143+
this.chart.views = []; // 删除已有的 views
143144
// 执行 adaptor
144145
this.execAdaptor();
145146
// 渲染

src/plots/gauge/adaptor.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isString, isArray, isFunction } from '@antv/util';
1+
import { isString, isArray, isFunction, clamp } from '@antv/util';
22
import { interaction, animation, theme, scale } from '../../adaptor/common';
33
import { Params } from '../../core/adaptor';
44
import { Data } from '../../types';
@@ -17,7 +17,8 @@ function geometry(params: Params<GaugeOptions>): Params<GaugeOptions> {
1717
const { ticks, color } = range;
1818

1919
// 指标 & 指针
20-
const indicatorData = [{ [PERCENT]: percent }];
20+
const indicatorData = [{ [PERCENT]: clamp(percent, 0, 1) }];
21+
2122
const v1 = chart.createView();
2223
v1.data(indicatorData);
2324

src/plots/gauge/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { clamp } from '@antv/util';
12
import { Plot } from '../../core/plot';
23
import { Adaptor } from '../../core/adaptor';
34
import { GaugeOptions } from './types';
@@ -20,7 +21,7 @@ export class Gauge extends Plot<GaugeOptions> {
2021
return {
2122
percent: 0, // 当前指标值
2223
range: {
23-
ticks: [0, percent, 1],
24+
ticks: [0, clamp(percent, 0, 1), 1],
2425
}, // 默认的刻度
2526
innerRadius: 0.9,
2627
radius: 0.95,

src/plots/progress/adaptor.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { deepMix, isString } from '@antv/util';
1+
import { deepMix, isString, clamp } from '@antv/util';
22
import { Params } from '../../core/adaptor';
33
import { flow } from '../../utils';
44
import { scale, animation, theme, annotation } from '../../adaptor/common';
@@ -14,14 +14,15 @@ export function geometry(params: Params<ProgressOptions>): Params<ProgressOption
1414
const { chart, options } = params;
1515
const { percent, progressStyle, color, barWidthRatio } = options;
1616

17+
const clampPercent = clamp(percent, 0, 1);
1718
const data = [
1819
{
1920
type: 'current',
20-
percent: percent,
21+
percent: clampPercent,
2122
},
2223
{
2324
type: 'target',
24-
percent: 1 - percent,
25+
percent: 1 - clampPercent,
2526
},
2627
];
2728

0 commit comments

Comments
 (0)