Skip to content

Commit e09ed71

Browse files
committed
Internal lib - Add calculateNiceScaleWithExactExtremes utility function
1 parent ae33630 commit e09ed71

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

src/lib.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,31 @@ export function calculateNiceScale(minValue, maxValue, maxTicks, rough = false)
988988
};
989989
}
990990

991+
export function calculateNiceScaleWithExactExtremes(minValue, maxValue, maxTicks, rough = false) {
992+
const range = rough ? (maxValue - minValue) : niceNum(maxValue - minValue, false);
993+
const tickSpacing = rough ? (range / (maxTicks - 1)) : niceNum(range / (maxTicks - 1), true);
994+
const niceMin = Math.floor(minValue / tickSpacing) * tickSpacing;
995+
const niceMax = Math.ceil(maxValue / tickSpacing) * tickSpacing;
996+
let ticks = [];
997+
let tick = niceMin;
998+
999+
while (tick <= niceMax) {
1000+
if (tick >= minValue && tick <= maxValue) {
1001+
ticks.push(tick);
1002+
}
1003+
tick += tickSpacing;
1004+
}
1005+
1006+
if (ticks[0] !== minValue) ticks[0] = minValue;
1007+
if (ticks[ticks.length - 1] !== maxValue) ticks[ticks.length - 1] = maxValue;
1008+
1009+
return {
1010+
min: minValue,
1011+
max: maxValue,
1012+
tickSize: tickSpacing,
1013+
ticks
1014+
};
1015+
}
9911016
export function interpolateColorHex(minColor, maxColor, minValue, maxValue, value) {
9921017
const hexToRgb = (hex) => ({
9931018
r: parseInt(hex.substring(1, 3), 16),
@@ -1730,6 +1755,7 @@ export function applyDataLabel(func, data, fallbackValue, config) {
17301755
}
17311756

17321757
const lib = {
1758+
XMLNS,
17331759
abbreviate,
17341760
adaptColorToBackground,
17351761
addVector,
@@ -1741,8 +1767,11 @@ const lib = {
17411767
calcMedian,
17421768
calcNutArrowPath,
17431769
calcTrend,
1770+
calculateNiceScale,
1771+
calculateNiceScaleWithExactExtremes,
17441772
canShowValue,
17451773
checkArray,
1774+
checkFormatter,
17461775
checkNaN,
17471776
checkObj,
17481777
closestDecimal,
@@ -1752,18 +1781,18 @@ const lib = {
17521781
createCsvContent,
17531782
createPolygonPath,
17541783
createSmoothPath,
1755-
createStraightPath,
17561784
createSpiralPath,
17571785
createStar,
1786+
createStraightPath,
17581787
createTSpans,
17591788
createUid,
17601789
createWordCloudDatasetFromPlainText,
1761-
checkFormatter,
17621790
darkenHexColor,
17631791
dataLabel,
17641792
degreesToRadians,
17651793
downloadCsv,
17661794
error,
1795+
functionReturnsString,
17671796
generateSpiralCoordinates,
17681797
getMissingDatasetAttributes,
17691798
getPalette,
@@ -1772,7 +1801,6 @@ const lib = {
17721801
isFunction,
17731802
isSafeValue,
17741803
isValidUserValue,
1775-
functionReturnsString,
17761804
lightenHexColor,
17771805
makeDonut,
17781806
makePath,
@@ -1784,10 +1812,9 @@ const lib = {
17841812
rotateMatrix,
17851813
shiftHue,
17861814
sumByAttribute,
1815+
sumSeries,
17871816
themePalettes,
17881817
translateSize,
17891818
treeShake,
1790-
XMLNS,
1791-
sumSeries
17921819
};
17931820
export default lib;

tests/lib.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
calcStarPoints,
1313
calcTrend,
1414
calculateNiceScale,
15+
calculateNiceScaleWithExactExtremes,
1516
checkArray,
1617
checkNaN,
1718
checkObj,
@@ -729,6 +730,43 @@ describe('calculateNiceScale', () => {
729730
})
730731
})
731732

733+
describe('calculateNiceScaleWithExactExtremes', () => {
734+
test('returns an object with nice scaling for y axis labels, keeping exact values for extremes', () => {
735+
expect(calculateNiceScaleWithExactExtremes(0, 118, 10)).toStrictEqual({
736+
max: 118,
737+
min: 0,
738+
tickSize: 20,
739+
ticks: [
740+
0,
741+
20,
742+
40,
743+
60,
744+
80,
745+
118,
746+
],
747+
})
748+
749+
expect(calculateNiceScaleWithExactExtremes(0, 1, 10)).toStrictEqual({
750+
max: 1,
751+
min: 0,
752+
tickSize: 0.1,
753+
ticks: [
754+
0,
755+
0.1,
756+
0.2,
757+
0.30000000000000004,
758+
0.4,
759+
0.5,
760+
0.6,
761+
0.7,
762+
0.7999999999999999,
763+
0.8999999999999999,
764+
1
765+
],
766+
})
767+
})
768+
})
769+
732770
describe('niceNum', () => {
733771
test('returns a nice number', () => {
734772
expect(niceNum(1.18, false)).toBe(2)

0 commit comments

Comments
 (0)