Skip to content

Commit

Permalink
Tweak grid interval calculation to avoid fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
oblador committed Dec 2, 2019
1 parent 923b9a2 commit 29471a2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/lib/getGridIntervals.js
@@ -1,9 +1,13 @@
export const getGridInterval = (maxValue, numberOfIntervals) => {
const granularity = Math.pow(10, Math.ceil(Math.log10(maxValue)) - 2) || 1;
const roundedMax = Math.round(
Math.ceil(maxValue / granularity) * granularity
const magnitude = Math.pow(10, Math.ceil(Math.log10(maxValue)) - 1);
const granularity = Math.max(
1,
magnitude * 3 > maxValue ? magnitude / 20 : magnitude / 10
);

return Math.round(
Math.ceil(maxValue / (numberOfIntervals - 1) / granularity) * granularity
);
return roundedMax / (numberOfIntervals - 1);
};

export const getGridIntervals = (maxValue, numberOfIntervals) => {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/getGridIntervals.spec.js
Expand Up @@ -9,5 +9,7 @@ describe('getGridIntervals', () => {
it('returns nicely rounded intervals', () => {
expect(getGridIntervals(123, 3)).toEqual([0, 65, 130]);
expect(getGridIntervals(2123, 5)).toEqual([0, 550, 1100, 1650, 2200]);
expect(getGridIntervals(8020, 5)).toEqual([0, 2100, 4200, 6300, 8400]);
expect(getGridIntervals(809, 5)).toEqual([0, 210, 420, 630, 840]);
});
});

0 comments on commit 29471a2

Please sign in to comment.