Skip to content
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

binary ticks increments on linear scale #232

Closed
nickofthyme opened this issue Sep 20, 2021 · 2 comments
Closed

binary ticks increments on linear scale #232

nickofthyme opened this issue Sep 20, 2021 · 2 comments

Comments

@nickofthyme
Copy link

nickofthyme commented Sep 20, 2021

I would like to propose an added option to allow returning ticks on a binary basis from the ticks method.

d3-array/src/ticks.js

Lines 5 to 35 in 6860b19

export default function ticks(start, stop, count) {
var reverse,
i = -1,
n,
ticks,
step;
stop = +stop, start = +start, count = +count;
if (start === stop && count > 0) return [start];
if (reverse = stop < start) n = start, start = stop, stop = n;
if ((step = tickIncrement(start, stop, count)) === 0 || !isFinite(step)) return [];
if (step > 0) {
let r0 = Math.round(start / step), r1 = Math.round(stop / step);
if (r0 * step < start) ++r0;
if (r1 * step > stop) --r1;
ticks = new Array(n = r1 - r0 + 1);
while (++i < n) ticks[i] = (r0 + i) * step;
} else {
step = -step;
let r0 = Math.round(start * step), r1 = Math.round(stop * step);
if (r0 / step < start) ++r0;
if (r1 / step > stop) --r1;
ticks = new Array(n = r1 - r0 + 1);
while (++i < n) ticks[i] = (r0 + i) / step;
}
if (reverse) ticks.reverse();
return ticks;
}

I imagine this could be an additional parameter like base such as...

export default function ticks(start, stop, count, base) {

Then this tickIncrement logic would be the only change

d3-array/src/ticks.js

Lines 37 to 44 in 6860b19

export function tickIncrement(start, stop, count) {
var step = (stop - start) / Math.max(0, count),
power = Math.floor(Math.log(step) / Math.LN10),
error = step / Math.pow(10, power);
return power >= 0
? (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1) * Math.pow(10, power)
: -Math.pow(10, -power) / (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1);
}

The changes would be something like...

-export function tickIncrement(start, stop, count) {
+export function tickIncrement(start, stop, count, base = 10) {
  var step = (stop - start) / Math.max(0, count),
-      power = Math.floor(Math.log(step) / Math.LN10),
+      power = Math.floor(Math.log(step) / Math.log(base) + Number.EPSILON),
-      error = step / Math.pow(10, power);
+      error = step / Math.pow(base, power);
   return power >= 0
-      ? (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1) * Math.pow(10, power)
+      ? (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1) * Math.pow(base, power)
-      : -Math.pow(10, -power) / (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1);
+      : -Math.pow(base, -power) / (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1);
 }

Which would look something like...

Base 2

domain: [0,1882]
ticks: [0,256,512,768,1024,1280,1536,1792]

Image 2021-09-20 at 3 44 05 PM

Base 10 (default)

domain: [0,1882]
ticks: [0,200,400,600,800,1000,1200,1400,1600,1800]

Image 2021-09-20 at 3 44 36 PM

@nickofthyme
Copy link
Author

If this is something that would be desired by the code owners I'd be happy to open a PR.

@nickofthyme
Copy link
Author

No response to PR for this feature

@nickofthyme nickofthyme closed this as not planned Won't fix, can't repro, duplicate, stale Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant