Skip to content

Commit

Permalink
feat: 增加ticks算法单测
Browse files Browse the repository at this point in the history
  • Loading branch information
cycgit committed Jul 22, 2020
1 parent f17d467 commit 12f6d69
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/chart/controller/scale.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getScale, registerTickMethod } from '@antv/scale';
import { isNil, mix, isObject, each, isArray, isString, isNumber, Array } from '../../util/common';
import Global from '../../global';
import TimeCat from './f2-time-cat';
import TimeCat from './timecat-tick';
// 覆盖0.3.x的 timecat scale算法
registerTickMethod('time-cat', TimeCat);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
export default cfg => {

const { values, tickCount } = cfg;
if (values.length === 0) {
return values[0];
if (values.length === 1) {
return values;
}

// 获取间隔步长
const step = parseInt(values.length / (tickCount - 1));
// 获取间隔步长, 最小是1
const step = parseInt(values.length / (tickCount - 1)) || 1;
const ticks = [];

// 按间隔数取对应节点
Expand Down
37 changes: 37 additions & 0 deletions test/unit/chart/controller/timecat-tick-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import timeCat from '../../../../src/chart/controller/timecat-tick.js';

describe('Scale controller', function() {
it('tickCount 3', () => {
const ticks = timeCat({
tickCount: 3,
values: [ 1590076800000, 1590336000000, 1590422400000, 1590508800000, 1590595200000, 1590681600000, 1590940800000, 1591027200000, 1591113600000, 1591200000000, 1591286400000, 1591545600000, 1591632000000, 1591718400000, 1591804800000, 1591891200000, 1592150400000, 1592236800000, 1592323200000, 1592409600000, 1592496000000, 1592755200000, 1592841600000, 1592928000000 ]
});
expect(ticks).toEqual([ 1590076800000, 1591632000000, 1592928000000 ]);
});

it('tickCount 5', () => {
const ticks = timeCat({
tickCount: 5,
values: [ 1590076800000, 1590336000000, 1590422400000, 1590508800000, 1590595200000, 1590681600000, 1590940800000, 1591027200000, 1591113600000, 1591200000000, 1591286400000, 1591545600000, 1591632000000, 1591718400000, 1591804800000, 1591891200000, 1592150400000, 1592236800000, 1592323200000, 1592409600000, 1592496000000, 1592755200000, 1592841600000, 1592928000000 ]
});
expect(ticks).toEqual([ 1590076800000, 1590940800000, 1591632000000, 1592323200000, 1592928000000 ]);
});

it('values 2', () => {
const ticks = timeCat({
tickCount: 3,
values: [ 1590076800000, 1591632000000 ]
});
expect(ticks).toEqual([ 1590076800000, 1591632000000 ]);
});


it('values 1', () => {
const ticks = timeCat({
tickCount: 3,
values: [ 1590076800000 ]
});
expect(ticks).toEqual([ 1590076800000 ]);
});

});

0 comments on commit 12f6d69

Please sign in to comment.