Sergei Basharov
Sergei Basharov

Reputation: 53850

Create a step scale in D3

I have a basic scale looking like this:

        var yScale = d3.scale.linear()
          .domain([0, totalCount])
          .range([0, containerHeight]);

Now when I put something in it, like yScale(6), it can return a float number, but I want it to return a number representing the row number in my grid. So, if the value is in some range, return 1, if it's in another range, return 2 and so on. I suppose I need to calculate if the value is inside the range and return custom value.

How can I do this?

Upvotes: 1

Views: 3522

Answers (2)

andrey
andrey

Reputation: 1977

The range function has the third argument, which is step. Hope that helps.

d3.range([start, ]stop[, step])

d3.range(0, 1, 0.2) // [0, 0.2, 0.4, 0.6000000000000001, 0.8

Upvotes: 0

prashant
prashant

Reputation: 1815

d3 provides for quantize scales for discrete range values and continuous domains.

var yScale = d3.scale.quantize()
          .domain([0, 50])
          .range([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

In this case for values from [0, 5) you would get 1, for values ranging from [5, 10) you would get 2 and so on.

[0, 5) means 0 is inclusive but 5 is exclusive i.e. 4.9999 would return 1 but 5 would return 2

Upvotes: 1

Related Questions