Coder Social home page Coder Social logo

d3.tickIncrement? about d3-array HOT 5 CLOSED

d3 avatar d3 commented on May 1, 2024
d3.tickIncrement?

from d3-array.

Comments (5)

mbostock avatar mbostock commented on May 1, 2024 1

This seems even better, where the range is always computed in integers, and then the step is applied:

function ticks(start, stop, count) {
  var step = tickIncrement(start, stop, count);
  return step >= 0 ? range(
    Math.ceil(start / step) * step,
    Math.floor(stop / step) * step + step / 2, // inclusive
    step
  ) : range(
    Math.floor(start * step),
    (2 * Math.ceil(stop * step) - 1) / 2, // inclusive
    -1
  ).map(x => x / step);
}

Results in: [5.8, 5.85, 5.9, 5.95, 6, 6.05, 6.1, 6.15, 6.2]!

from d3-array.

mbostock avatar mbostock commented on May 1, 2024

Actually, it may not be necessary to deprecate or change d3.tickStep. I believe (but need to verify using a simple for loop) that in any case that d3.tickStep returns a step where |step| < 1, that 1 / step will be an integer. Even if that’s not the case, we could always Math.round it.

That means that the only real change will be in code that rounds according to the tick step. So nice could look like this:

var step = tickStep(start, stop, count);
if (Math.abs(step) >= 1) {
  start = Math.floor(start / step) * step;
  stop = Math.ceil(stop / step) * step;
} else {
  step = 1 / step;
  start = Math.floor(start * step) / step;
  stop = Math.ceil(stop * step) / step;
}

And ticks could look like this (note: abusing the count argument as a temporary):

function ticks(start, stop, count) {
  var step = tickStep(start, stop, count);
  return Math.abs(step) >= 1 ? range(
    Math.ceil(start / step) * step,
    Math.floor(stop / step) * step + step / 2, // inclusive
    step
  ) : count = 1 / step, range(
    Math.ceil(start * count) / count,
    (2 * Math.floor(stop * count) + 1) / (2 * count), // inclusive
    step
  );
}

Which has the same behavior on the OP’s test case.

from d3-array.

mbostock avatar mbostock commented on May 1, 2024

Slightly cleaner?

function ticks(start, stop, count) {
  var step = tickStep(start, stop, count);
  if (Math.abs(step) >= 1) {
    start = Math.ceil(start / step) * step;
    stop = (2 * Math.floor(stop / step) + 1) * step / 2; // inclusive
  } else {
    count = 1 / step;
    start = Math.ceil(start * count) / count;
    stop = (2 * Math.floor(stop * count) + 1) / (2 * count); // inclusive      
  }
  return range(start, stop, step);
}

from d3-array.

mbostock avatar mbostock commented on May 1, 2024

Guess I’m wrong—we can’t assume that 1 / step will be an integer, at least for extreme values. We probably will need a tickIncrement method, then.

https://runkit.com/57d8702e1ff21014007f12d5/58138606783f93001320ab74

from d3-array.

ddotlic avatar ddotlic commented on May 1, 2024

@mbostock Hats off, this is a very elegant solution for the task at hand (stumbled upon this while looking for the reason for having both tickStep and tickIncrement).

from d3-array.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.