d3-array icon indicating copy to clipboard operation
d3-array copied to clipboard

binary ticks increments on linear scale

Open nickofthyme opened this issue 4 years ago • 1 comments
trafficstars

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

https://github.com/d3/d3-array/blob/6860b190118e29a08fe9848b97aa1cbe967172f2/src/ticks.js#L5-L35

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

https://github.com/d3/d3-array/blob/6860b190118e29a08fe9848b97aa1cbe967172f2/src/ticks.js#L37-L44

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 avatar Sep 20 '21 20:09 nickofthyme

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

nickofthyme avatar Sep 20 '21 20:09 nickofthyme