How to calculate the scale's result?

后端 未结 2 535
耶瑟儿~
耶瑟儿~ 2020-12-11 20:50

I am new to d3.js library, and I am trying to understand the concept of domain and ranges.

I read Scott Murray\'s book, but I don\'t know how to calculate it.

<
相关标签:
2条回答
  • 2020-12-11 21:36

    Yes I will try to explain why you get 180.

    We have that 300 is the value that ideally divide your domain into two subdomains of the same size, so we can do: 500 - 100 / 2 = 200 (this is the subdomain size), this implies that the middle value is located at:
    200 + 100 = 300 (note that we add the starting point 100)

    So with the range we do the same:
    350 - 10 / 2 = 170 and then we add the starting point 170 + 10 = 180

    0 讨论(0)
  • 2020-12-11 21:43

    THE EQUATION OF THE LINE

    To get the equation used in a D3 linear scale you only need to understand that such scale uses a linear interpolation. In simple words, all you need is the equation of the line created by the two points you're passing to D3 scale generator.

    In your example:

    .domain([100, 500])
    .range([10, 350]);
    

    Will give us two points, using the (x,y) coordinates system:

    • The first point is 100,10 (x = 100 and y = 10)
    • The second point is 500,350 (x = 500 and y = 350)

    Keep in mind that in my explanation the x axis represents what in math is called the domain (in D3 lingo, also domain), that is, the set of input values, while the y axis represents what in math is called the image (in D3 lingo, the range), that is, the set of output values.

    So, using those two points, this is the line you get:

    Now let's explain visually what a linear scale like this one does:

    Choose any point in the x axis (it can even be outside the domain). This is your input value (in the domain). Go up (or down) until you cross the red line. The y coordinate of the point where you cross the red line corresponds to your output value (in the image, or range).

    Now, back to the equation:

    Having our 2 points, we can now calculate the equation of the line.

    The general equation of the line is:

    y = mx + b

    Where y (also known as f(x) in most math books) is the range, and x is the domain.

    The first step is finding m, which we can do using our 4 points:

    formula2

    Remember that:

    • x1 is the first value in the domain array (=100)
    • x2 is the last value in the domain array (=500)
    • y1 is the first value in the range array (=10)
    • y2 is the last value in the range array (=350)

    Putting all these four values in the equation, it gives us that m is 17/20.

    Now, solving the equation for b (using any of the two points)...

    formula7

    ... we have that b is -75, which gives us our final equation:

    formula3

    And that's it. Using this equation, you can get any point in the image (range), relative to any domain input.

    Example:

    Let's calculate the output (range) for 125 (as in your comment). Very easy:

    formula4

    And that gives us... 31.25!

    0 讨论(0)
提交回复
热议问题