<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"> <style>
	.KEYW {color: #933;}
	.COMM {color: #bbb; font-style: italic;}
	.NUMB {color: #393;}
	.STRN {color: #393;}
	.REGX {color: #339;}
	.line {border-right: 1px dotted #666; color: #666; font-style: normal;}
	</style></head><body><pre><span class='line'>  1</span> <span class="COMM">/**
<span class='line'>  2</span>  * Abstract; see the various scale implementations.
<span class='line'>  3</span>  *
<span class='line'>  4</span>  * @class Represents a scale; a function that performs a transformation from
<span class='line'>  5</span>  * data domain to visual range. For quantitative and quantile scales, the domain
<span class='line'>  6</span>  * is expressed as numbers; for ordinal scales, the domain is expressed as
<span class='line'>  7</span>  * strings (or equivalently objects with unique string representations). The
<span class='line'>  8</span>  * "visual range" may correspond to pixel space, colors, font sizes, and the
<span class='line'>  9</span>  * like.
<span class='line'> 10</span>  *
<span class='line'> 11</span>  * &lt;p>Note that scales are functions, and thus can be used as properties
<span class='line'> 12</span>  * directly, assuming that the data associated with a mark is a number. While
<span class='line'> 13</span>  * this is convenient for single-use scales, frequently it is desirable to
<span class='line'> 14</span>  * define scales globally:
<span class='line'> 15</span>  *
<span class='line'> 16</span>  * &lt;pre>var y = pv.Scale.linear(0, 100).range(0, 640);&lt;/pre>
<span class='line'> 17</span>  *
<span class='line'> 18</span>  * The &lt;tt>y&lt;/tt> scale can now be equivalently referenced within a property:
<span class='line'> 19</span>  *
<span class='line'> 20</span>  * &lt;pre>    .height(function(d) y(d))&lt;/pre>
<span class='line'> 21</span>  *
<span class='line'> 22</span>  * Alternatively, if the data are not simple numbers, the appropriate value can
<span class='line'> 23</span>  * be passed to the &lt;tt>y&lt;/tt> scale (e.g., &lt;tt>d.foo&lt;/tt>). The {@link #by}
<span class='line'> 24</span>  * method similarly allows the data to be mapped to a numeric value before
<span class='line'> 25</span>  * performing the linear transformation.
<span class='line'> 26</span>  *
<span class='line'> 27</span>  * @see pv.Scale.quantitative
<span class='line'> 28</span>  * @see pv.Scale.quantile
<span class='line'> 29</span>  * @see pv.Scale.ordinal
<span class='line'> 30</span>  * @extends function
<span class='line'> 31</span>  */</span><span class="WHIT">
<span class='line'> 32</span> </span><span class="NAME">pv.Scale</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 33</span> 
<span class='line'> 34</span> </span><span class="COMM">/**
<span class='line'> 35</span>  * @private Returns a function that interpolators from the start value to the
<span class='line'> 36</span>  * end value, given a parameter &lt;i>t&lt;/i> in [0, 1].
<span class='line'> 37</span>  *
<span class='line'> 38</span>  * @param start the start value.
<span class='line'> 39</span>  * @param end the end value.
<span class='line'> 40</span>  */</span><span class="WHIT">
<span class='line'> 41</span> </span><span class="NAME">pv.Scale.interpolator</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">start</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">end</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 42</span> </span><span class="WHIT">  </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">typeof</span><span class="WHIT"> </span><span class="NAME">start</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="STRN">"number"</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 43</span> </span><span class="WHIT">    </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">t</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 44</span> </span><span class="WHIT">      </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">t</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">end</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">start</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">start</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 45</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 46</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 47</span> 
<span class='line'> 48</span> </span><span class="WHIT">  </span><span class="COMM">/* For now, assume color. */</span><span class="WHIT">
<span class='line'> 49</span> </span><span class="WHIT">  </span><span class="NAME">start</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">pv.color</span><span class="PUNC">(</span><span class="NAME">start</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">rgb</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 50</span> </span><span class="WHIT">  </span><span class="NAME">end</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">pv.color</span><span class="PUNC">(</span><span class="NAME">end</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">rgb</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 51</span> </span><span class="WHIT">  </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">t</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 52</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">a</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">start.a</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NUMB">1</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">t</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">end.a</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="WHIT"> </span><span class="NAME">t</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 53</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">a</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="NAME">e</span><span class="PUNC">-</span><span class="NUMB">5</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="NAME">a</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="COMM">// avoid scientific notation</span><span class="WHIT">
<span class='line'> 54</span> </span><span class="WHIT">    </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">start.a</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">?</span><span class="WHIT"> </span><span class="NAME">pv.rgb</span><span class="PUNC">(</span><span class="NAME">end.r</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">end.g</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">end.b</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">a</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'> 55</span> </span><span class="WHIT">        </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">end.a</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">?</span><span class="WHIT"> </span><span class="NAME">pv.rgb</span><span class="PUNC">(</span><span class="NAME">start.r</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">start.g</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">start.b</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">a</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'> 56</span> </span><span class="WHIT">        </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">pv.rgb</span><span class="PUNC">(</span><span class="WHIT">
<span class='line'> 57</span> </span><span class="WHIT">            </span><span class="NAME">Math.round</span><span class="PUNC">(</span><span class="NAME">start.r</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NUMB">1</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">t</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">end.r</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="WHIT"> </span><span class="NAME">t</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'> 58</span> </span><span class="WHIT">            </span><span class="NAME">Math.round</span><span class="PUNC">(</span><span class="NAME">start.g</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NUMB">1</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">t</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">end.g</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="WHIT"> </span><span class="NAME">t</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'> 59</span> </span><span class="WHIT">            </span><span class="NAME">Math.round</span><span class="PUNC">(</span><span class="NAME">start.b</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NUMB">1</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">t</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">end.b</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="WHIT"> </span><span class="NAME">t</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">a</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 60</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 61</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 62</span> 
<span class='line'> 63</span> </span><span class="COMM">/**
<span class='line'> 64</span>  * Returns a view of this scale by the specified accessor function &lt;tt>f&lt;/tt>.
<span class='line'> 65</span>  * Given a scale &lt;tt>y&lt;/tt>, &lt;tt>y.by(function(d) d.foo)&lt;/tt> is equivalent to
<span class='line'> 66</span>  * &lt;tt>function(d) y(d.foo)&lt;/tt>.
<span class='line'> 67</span>  *
<span class='line'> 68</span>  * &lt;p>This method is provided for convenience, such that scales can be
<span class='line'> 69</span>  * succinctly defined inline. For example, given an array of data elements that
<span class='line'> 70</span>  * have a &lt;tt>score&lt;/tt> attribute with the domain [0, 1], the height property
<span class='line'> 71</span>  * could be specified as:
<span class='line'> 72</span>  *
<span class='line'> 73</span>  * &lt;pre>    .height(pv.Scale.linear().range(0, 480).by(function(d) d.score))&lt;/pre>
<span class='line'> 74</span>  *
<span class='line'> 75</span>  * This is equivalent to:
<span class='line'> 76</span>  *
<span class='line'> 77</span>  * &lt;pre>    .height(function(d) d.score * 480)&lt;/pre>
<span class='line'> 78</span>  *
<span class='line'> 79</span>  * This method should be used judiciously; it is typically more clear to invoke
<span class='line'> 80</span>  * the scale directly, passing in the value to be scaled.
<span class='line'> 81</span>  *
<span class='line'> 82</span>  * @function
<span class='line'> 83</span>  * @name pv.Scale.prototype.by
<span class='line'> 84</span>  * @param {function} f an accessor function.
<span class='line'> 85</span>  * @returns {pv.Scale} a view of this scale by the specified accessor function.
<span class='line'> 86</span>  */</span><span class="WHIT">
<span class='line'> 87</span> </span></pre></body></html>