Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions __tests__/bugs/issue-3262-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Gauge } from '../../src';
import { createDiv } from '../utils/dom';

describe('#3262', () => {
it('gauge percent 0', () => {
const gauge = new Gauge(createDiv(), {
percent: 0,
});

gauge.render();

expect(gauge.chart.views[1].getOptions().data.length).toBe(2);
});
});
22 changes: 15 additions & 7 deletions __tests__/unit/plots/gauge/utils-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,27 @@ describe('gauge utils to getData', () => {

it('get rangeData', () => {
expect(getRangeData(0.5, { ticks: [0, 0.3, 1] })).toEqual([
{ [RANGE_VALUE]: 0.3, [RANGE_TYPE]: '1', [PERCENT]: 0.5 },
{ [RANGE_VALUE]: 0.7, [RANGE_TYPE]: '2', [PERCENT]: 0.5 },
{ [RANGE_VALUE]: 0.3, [RANGE_TYPE]: '0', [PERCENT]: 0.5 },
{ [RANGE_VALUE]: 0.7, [RANGE_TYPE]: '1', [PERCENT]: 0.5 },
]);

expect(getRangeData(0.5)).toEqual([
{ [RANGE_VALUE]: 0.5, [RANGE_TYPE]: '0', [PERCENT]: 0.5 },
{ [RANGE_VALUE]: 0.5, [RANGE_TYPE]: '1', [PERCENT]: 0.5 },
{ [RANGE_VALUE]: 0.5, [RANGE_TYPE]: '2', [PERCENT]: 0.5 },
]);

expect(getRangeData(-0.5)).toEqual([{ [RANGE_VALUE]: 1, [RANGE_TYPE]: '2', [PERCENT]: -0.5 }]);
expect(getRangeData(1.5)).toEqual([{ [RANGE_VALUE]: 1, [RANGE_TYPE]: '1', [PERCENT]: 1.5 }]);
expect(getRangeData(1.5)).toEqual([
{ [RANGE_VALUE]: 1, [RANGE_TYPE]: '0', [PERCENT]: 1.5 },
{ [RANGE_VALUE]: 0, [RANGE_TYPE]: '1', [PERCENT]: 1.5 },
]);

expect(getRangeData(0)).toEqual([{ [RANGE_VALUE]: 1, [RANGE_TYPE]: '2', [PERCENT]: 0 }]);
expect(getRangeData(1)).toEqual([{ [RANGE_VALUE]: 1, [RANGE_TYPE]: '1', [PERCENT]: 1 }]);
expect(getRangeData(0)).toEqual([
{ [RANGE_VALUE]: 0, [RANGE_TYPE]: '0', [PERCENT]: 0 },
{ [RANGE_VALUE]: 1, [RANGE_TYPE]: '1', [PERCENT]: 0 },
]);
expect(getRangeData(1)).toEqual([
{ [RANGE_VALUE]: 1, [RANGE_TYPE]: '0', [PERCENT]: 1 },
{ [RANGE_VALUE]: 0, [RANGE_TYPE]: '1', [PERCENT]: 1 },
]);
});
});
9 changes: 5 additions & 4 deletions src/plots/gauge/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { clamp, get, size } from '@antv/util';
import { clamp, get, size, uniq } from '@antv/util';
import { Data, Datum } from '../../types';
import { RANGE_VALUE, RANGE_TYPE, PERCENT } from './constants';
import { GaugeOptions, GaugeRangeData } from './types';
Expand All @@ -16,8 +16,6 @@ export function processRangeData(range: number[], percent: GaugeOptions['percent
.map((r: number, idx: number) => {
return { [RANGE_VALUE]: r - (range[idx - 1] || 0), [RANGE_TYPE]: `${idx}`, [PERCENT]: percent };
})
// 去掉 0 的数据
.filter((d: Datum) => !!d[RANGE_VALUE])
);
}

Expand All @@ -37,6 +35,9 @@ export function getIndicatorData(percent: GaugeOptions['percent']): Data {
export function getRangeData(percent: GaugeOptions['percent'], range?: GaugeOptions['range']): GaugeRangeData {
const ticks = get(range, ['ticks'], []);

const clampTicks = size(ticks) ? ticks : [0, clamp(percent, 0, 1), 1];
const clampTicks = size(ticks) ? uniq(ticks) : [0, clamp(percent, 0, 1), 1];
if (!clampTicks[0]) {
clampTicks.shift();
}
return processRangeData(clampTicks as number[], percent);
}