Relationship between linear math transformation and scaleLinear D3 function

Viewed 50

I didn't understand how scaleLinear works.

I know it's about a linear transformation like y = mx + b and I use it in this way:

const scale = scaleLinear().domain([0, 10]).range([0, 100])

but what are b, m and x in that case?

1 Answers

By specifying the domain and the range, you have two points:

  • (x1,y1) = (domain[0], range[0])
  • (x2,y2) = (domain[1], range[1])

So m = (y2 - y1) / (x2 - x1) and b = y1 - m * x1.

In your example from the comment:

const scale = scaleLinear().domain([0, 10]).range([0, 150])

m = 150 / 10 = 15 and b = 0 - 15 * 0 = 0

Related