-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
55 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,42 @@ | ||
export function getYAxisWidth(value: string) { | ||
return value.toString().length * 8.5 + 8.5; | ||
return value.toString().length * 7 + 2; | ||
} | ||
|
||
export function findLargestValue(data) { | ||
return data?.reduce((max, obj) => { | ||
return obj.value > max ? obj.value : max; | ||
}, data[0].value); | ||
export function roundToNearestFactor(numbers: number[]) { | ||
if (numbers.length < 2) return numbers; | ||
|
||
// Sort the array | ||
numbers.sort((a, b) => a - b); | ||
|
||
// Calculate gaps | ||
const gaps = []; | ||
for (let i = 1; i < numbers.length; i++) { | ||
gaps.push(numbers[i] - numbers[i - 1]); | ||
} | ||
|
||
// Determine the rounding factor: using the maximum gap | ||
const maxGap = Math.max(...gaps); | ||
|
||
// Alternatively, you can define a fixed interval, e.g., 250000 | ||
// let roundingFactor = 250000; | ||
|
||
// Rounding factor based on maxGap, rounded to the nearest significant figure | ||
let roundingFactor = Math.pow(10, Math.floor(Math.log10(maxGap))); | ||
|
||
// Adjust rounding factor to align with 250000, 500000, 750000, etc. | ||
if (roundingFactor < maxGap) { | ||
roundingFactor = Math.ceil(maxGap / 250000) * 250000; | ||
} | ||
|
||
// Helper function to round to nearest multiple of factor | ||
function roundToFactor(number, factor) { | ||
return Math.round(number / factor) * factor; | ||
} | ||
|
||
// Round each number to the nearest rounding factor | ||
const roundedNumbers = numbers.map((num) => | ||
roundToFactor(num, roundingFactor) | ||
); | ||
|
||
return Math.max(...roundedNumbers); | ||
} |