title: Use scales to remap a number
body: <p>I have a domain of numbers, for example <code>domain = [100, 200]</code> and a number of bands in which to divide the range, for example <code>bands = 5</code>.
  I know that each band corresponds to a value:</p>
  
  <pre><code>band #1 --&gt; v = 0.2
  band #2 --&gt; v = 0.4
  band #3 --&gt; v = 0.6
  band #4 --&gt; v = 0.8
  band #5 --&gt; v = 1.0
  </code></pre>
  
  <p>These values are fixed (hard coded): if bands became <code>bands = 6</code> then is the developer that choose what is the value of <code>band #6</code>.</p>
  
  <p>I want to divide the domain into bands whose size varies according to the scale used. 
  For example I might want to use either the linear or the logarithmic or the pow scale.</p>
  
  <p>Then I want a function that in input takes a number <code>x ∈ domain</code> and must return the value <code>v</code> associated with the band to which the inout number belongs.</p>
  
  <p><a href="https://stackoverflow.com/questions/56538235/how-to-map-a-number-in-some-ranges?noredirect=1#comment99659132_56538235">Here a similar question</a>, but now I want to use different scales (for example I can use <code>d3 scales</code>) but I don't know how..</p>
  
  <p>Here a piece of code:</p>
  
  <pre><code>function getLinearScaledValue(x, min, max, bands) {
    const range = max - min
    if (x === max) {
      return 1
    } else {
      return Math.floor(1 + ((x - min) / range) * bands) / bands
    }
  }
  </code></pre>
  
  <p>where <code>min</code> and <code>max</code> are the min and max value of the domain.</p>
  
  <p>I think sleepwalking's examples was good so I put them here:</p>
  
  <p>if bands = 5:</p>
  
  <pre><code>band #1 --&gt; v = 0.2
  band #2 --&gt; v = 0.4
  band #3 --&gt; v = 0.6
  band #4 --&gt; v = 0.8
  band #5 --&gt; v = 1.0
  </code></pre>
  
  <p>(1) if scale is linear and domain = [0, 100] --> bands are:</p>
  
  <pre><code>band #1 --&gt; v = 0.2 --&gt; [0, 20]
  band #2 --&gt; v = 0.4 --&gt; [21, 40]
  band #3 --&gt; v = 0.6 --&gt; [41, 60]
  band #4 --&gt; v = 0.8 --&gt; [61, 80]
  band #5 --&gt; v = 1.0 --&gt; [81, 100]
  </code></pre>
  
  <p>for example:</p>
  
  <pre><code>if x = 0  --&gt; v = 0.2
  if x = 10 --&gt; v = 0.2
  if x = 21 --&gt; v = 0.4
  if x = 98 --&gt; v = 1.0
  </code></pre>
  
  <p>(2) if scale is linear and domain = [100, 200] --> bands are:</p>
  
  <pre><code>band #1 --&gt; v = 0.2 --&gt; [100, 120]
  band #2 --&gt; v = 0.4 --&gt; [121, 140]
  band #3 --&gt; v = 0.6 --&gt; [141, 160]
  band #4 --&gt; v = 0.8 --&gt; [161, 180]
  band #5 --&gt; v = 1.0 --&gt; [181, 200]
  </code></pre>
  
  <p>for example:</p>
  
  <pre><code>if x = 100 --&gt; v = 0.2
  if x = 110 --&gt; v = 0.2
  if x = 121 --&gt; v = 0.4
  if x = 198 --&gt; v = 1.0
  </code></pre>
  
  <p>(3) if scale is logarithmic and domain = [0, 100] --> bands are:</p>
  
  <pre><code>band #1 --&gt; v = 0.2 --&gt; [?, ?]
  band #2 --&gt; v = 0.4 --&gt; [?, ?]
  band #3 --&gt; v = 0.6 --&gt; [?, ?]
  band #4 --&gt; v = 0.8 --&gt; [?, ?]
  band #5 --&gt; v = 1.0 --&gt; [?, ?]
  </code></pre>
  
  <p>for example:</p>
  
  <pre><code>if x = 0  --&gt; v = ?
  if x = 10 --&gt; v = ?
  if x = 21 --&gt; v = ?
  if x = 98 --&gt; v = ?
  </code></pre>