Skip to content

Commit

Permalink
Merge pull request #60 from VisActor/fix/no-strict-tick-count
Browse files Browse the repository at this point in the history
Fix/no strict tick count
  • Loading branch information
xile611 authored Aug 9, 2023
2 parents 8c39868 + 00222f5 commit 854f3e8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"comment": "fix: ticks() should not require strict tickCount\n\n",
"type": "none",
"packageName": "@visactor/vscale"
}
],
"packageName": "@visactor/vscale",
"email": "dingling112@gmail.com"
}
4 changes: 2 additions & 2 deletions packages/vscale/__tests__/linear.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ test('linear.ticks(count) will filter ticks when niceMin is true', function () {
expect(s.domain()).toEqual([2, originDomain[1]]);

const newTicks = s.ticks(5);
expect(newTicks).toEqual([3, 6, 9]);
expect(newTicks).toEqual([2, 4, 6, 8, 10]);
expect(s.domain()).toEqual([newTicks[0], originDomain[1]]);
});

Expand All @@ -807,6 +807,6 @@ test('linear.ticks(count) will filter ticks when niceMax is true', function () {
expect(s.domain()).toEqual([originDomain[0], 12]);

const newTicks = s.ticks(5);
expect(newTicks).toEqual([6, 9, 12, 15]);
expect(newTicks).toEqual([4, 6, 8, 10, 12]);
expect(s.domain()).toEqual([originDomain[0], newTicks[newTicks.length - 1]]);
});
4 changes: 2 additions & 2 deletions packages/vscale/__tests__/log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ it('log.ticks() generates the expected power-of-ten ticks for small domains', ()
expect(s.domain([-5, -1]).ticks()).toEqual([-5, -4, -3, -2, -1]);
expect(s.domain([286.9252014, 329.4978332]).ticks(1)).toEqual([300]);
expect(s.domain([286.9252014, 329.4978332]).ticks(2)).toEqual([300]);
expect(s.domain([286.9252014, 329.4978332]).ticks(3)).toEqual([270, 300, 330]);
expect(s.domain([286.9252014, 329.4978332]).ticks(3)).toEqual([280, 300, 320, 340]);
expect(s.domain([286.9252014, 329.4978332]).ticks(4)).toEqual([280, 300, 320, 340]);
expect(s.domain([286.9252014, 329.4978332]).ticks()).toEqual([285, 290, 295, 300, 305, 310, 315, 320, 325, 330]);
});
Expand All @@ -274,7 +274,7 @@ it('log.ticks() generates linear ticks when the domain extent is small', () => {
const s = new LogScale();
expect(s.domain([41, 42]).ticks()).toEqual([41, 41.1, 41.2, 41.3, 41.4, 41.5, 41.6, 41.7, 41.8, 41.9, 42]);
expect(s.domain([42, 41]).ticks()).toEqual([42, 41.9, 41.8, 41.7, 41.6, 41.5, 41.4, 41.3, 41.2, 41.1, 41]);
expect(s.domain([1600, 1400]).ticks()).toEqual([1650, 1620, 1590, 1560, 1530, 1500, 1470, 1440, 1410, 1380]);
expect(s.domain([1600, 1400]).ticks()).toEqual([1600, 1580, 1560, 1540, 1520, 1500, 1480, 1460, 1440, 1420, 1400]);
});

it('log.base(base).ticks() generates the expected power-of-base ticks', () => {
Expand Down
12 changes: 6 additions & 6 deletions packages/vscale/__tests__/tick-sample.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ticks, d3Ticks } from '../src/utils/tick-sample';

test('ticks() of float number', () => {
expect(ticks(-0.1, 15, 5)).toEqual([-5, 0, 5, 10, 15]);
expect(ticks(2, 15, 5)).toEqual([0, 4, 8, 12, 16]);
expect(ticks(12333.233, 233303, 5)).toEqual([0, 60000, 120000, 180000, 240000]);
expect(ticks(-0.1, 15, 5)).toEqual([-4, 0, 4, 8, 12, 16]);
expect(ticks(2, 15, 5)).toEqual([0, 3, 6, 9, 12, 15]);
expect(ticks(12333.233, 233303, 5)).toEqual([0, 50000, 100000, 150000, 200000, 250000]);
expect(ticks(12333.233, 12334, 5)).toEqual([12333.2, 12333.4, 12333.6, 12333.8, 12334]);
});

Expand All @@ -15,9 +15,9 @@ test('ticks() of equal value', () => {
test('ticks() of different tickCount', () => {
expect(ticks(0, 1, 1)).toEqual([0, 1]);
expect(ticks(0, 1, 5)).toEqual([0, 0.2, 0.4, 0.6, 0.8, 1]);
expect(ticks(0, 245.78, 5)).toEqual([0, 70, 140, 210, 280]);
expect(ticks(0, 8412, 5)).toEqual([0, 3000, 6000, 9000, 12000]);
expect(ticks(0, 2167, 5)).toEqual([0, 600, 1200, 1800, 2400]);
expect(ticks(0, 245.78, 5)).toEqual([0, 50, 100, 150, 200, 250]);
expect(ticks(0, 8412, 5)).toEqual([0, 2000, 4000, 6000, 8000, 10000]);
expect(ticks(0, 2167, 5)).toEqual([0, 500, 1000, 1500, 2000, 2500]);
});

test('d3Ticks() when noDecimals is true', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/vscale/src/utils/tick-sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,15 @@ export const ticks = memoize<ContinuousTicksFunc>(
const delatStep = gap === 10 ? 2 * 10 ** power : 1 * 10 ** power;
while (
cur <= maxIterations &&
((ticks = calculateTicksByStep(start, stop, step)), ticks.length > count) &&
((ticks = calculateTicksByStep(start, stop, step)), ticks.length > count + 1) &&
count > 2
) {
step += delatStep;

cur += 1;
}

if (ticks.length < count) {
if (count > 2 && ticks.length < count - 1) {
ticks = appendTicksToCount(ticks, count, step);
}
} else {
Expand Down

0 comments on commit 854f3e8

Please sign in to comment.