-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
optional log base for ticks functions #233
Changes from all commits
a8ac7f9
73a4b62
e983ad4
4ecda5f
13793c9
a764258
f700c34
79c5d11
28d1a03
c6ae425
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,35 +2,42 @@ const e10 = Math.sqrt(50), | |
e5 = Math.sqrt(10), | ||
e2 = Math.sqrt(2); | ||
|
||
function tickSpec(start, stop, count) { | ||
const logParams = { | ||
binary: [Math.log2, 2], | ||
natural: [(n) => Math.log1p(n - 1), Math.E], | ||
common: [Math.log10, 10], | ||
Comment on lines
+6
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Limits the log types to just |
||
} | ||
Comment on lines
+5
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Does this approach work for you to use the respective precise |
||
|
||
function tickSpec(start, stop, count, log = 'common') { | ||
const [logFn, base] = logParams[log] || logParams.common; | ||
const step = (stop - start) / Math.max(0, count), | ||
power = Math.floor(Math.log10(step)), | ||
error = step / Math.pow(10, power), | ||
factor = error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1; | ||
power = Math.floor(logFn(step)), | ||
error = step / Math.pow(base, power), | ||
factor = error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1; | ||
let i1, i2, inc; | ||
if (power < 0) { | ||
inc = Math.pow(10, -power) / factor; | ||
inc = Math.pow(base, -power) / factor; | ||
i1 = Math.round(start * inc); | ||
i2 = Math.round(stop * inc); | ||
if (i1 / inc < start) ++i1; | ||
if (i2 / inc > stop) --i2; | ||
inc = -inc; | ||
} else { | ||
inc = Math.pow(10, power) * factor; | ||
inc = Math.pow(base, power) * factor; | ||
i1 = Math.round(start / inc); | ||
i2 = Math.round(stop / inc); | ||
if (i1 * inc < start) ++i1; | ||
if (i2 * inc > stop) --i2; | ||
} | ||
if (i2 < i1 && 0.5 <= count && count < 2) return tickSpec(start, stop, count * 2); | ||
if (i2 < i1 && 0.5 <= count && count < 2) return tickSpec(start, stop, count * 2, log); | ||
return [i1, i2, inc]; | ||
} | ||
|
||
export default function ticks(start, stop, count) { | ||
export default function ticks(start, stop, count, log) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm happy to separate these added methods into separate methods. But I'd like to avoid duplication of code. Would you be ok with having the So exporting |
||
stop = +stop, start = +start, count = +count; | ||
if (!(count > 0)) return []; | ||
if (start === stop) return [start]; | ||
const reverse = stop < start, [i1, i2, inc] = reverse ? tickSpec(stop, start, count) : tickSpec(start, stop, count); | ||
const reverse = stop < start, [i1, i2, inc] = reverse ? tickSpec(stop, start, count, log) : tickSpec(start, stop, count, log); | ||
if (!(i2 >= i1)) return []; | ||
const n = i2 - i1 + 1, ticks = new Array(n); | ||
if (reverse) { | ||
|
@@ -43,13 +50,16 @@ export default function ticks(start, stop, count) { | |
return ticks; | ||
} | ||
|
||
export function tickIncrement(start, stop, count) { | ||
stop = +stop, start = +start, count = +count; | ||
return tickSpec(start, stop, count)[2]; | ||
export function tickIncrement(start, stop, count, log) { | ||
(stop = +stop), (start = +start), (count = +count); | ||
return tickSpec(start, stop, count, log)[2]; | ||
} | ||
|
||
export function tickStep(start, stop, count) { | ||
export function tickStep(start, stop, count, log) { | ||
stop = +stop, start = +start, count = +count; | ||
const reverse = stop < start, inc = reverse ? tickIncrement(stop, start, count) : tickIncrement(start, stop, count); | ||
const reverse = stop < start, | ||
inc = reverse | ||
? tickIncrement(stop, start, count, log) | ||
: tickIncrement(start, stop, count, log); | ||
return (reverse ? -1 : 1) * (inc < 0 ? 1 / -inc : inc); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you point me to the other parts of the code that would need to be addressed?