1 | define(["dojo/_base/lang", "dojo/_base/declare", "dojo/Stateful"], function(lang, declare, Stateful){ |
---|
2 | return declare("dojox.dgauges.LogScaler", Stateful, { |
---|
3 | // summary: |
---|
4 | // The LogScaler maps numeric values evenly |
---|
5 | // between a minimum and a maximum value along a gauge scale. |
---|
6 | // If no multiplier is specified, the scale will place |
---|
7 | // a tick on each power of 10 value (1, 10, 100, 1000, and so on) between |
---|
8 | // the minimum and maximum values. |
---|
9 | |
---|
10 | // minimum: Number |
---|
11 | // The minimum value of the scaler. Default is 0. |
---|
12 | minimum: 0, |
---|
13 | // maximum: Number |
---|
14 | // The maximum value of the scaler. Default is 1000. |
---|
15 | maximum: 1000, |
---|
16 | // multiplier: Number |
---|
17 | // The interval between two major ticks. |
---|
18 | multiplier: 10, |
---|
19 | // majorTicks: |
---|
20 | // The array of generated major ticks. You should not set this |
---|
21 | // property when using the scaler. |
---|
22 | majorTicks: null, |
---|
23 | |
---|
24 | _computedMultiplier: NaN, |
---|
25 | |
---|
26 | constructor: function(){ |
---|
27 | this.watchedProperties = ["minimum", "maximum", "multiplier"]; |
---|
28 | }, |
---|
29 | _buildMajorTickItems: function(){ |
---|
30 | // summary: |
---|
31 | // Internal method. |
---|
32 | // tags: |
---|
33 | // private |
---|
34 | var majorTickCache = []; |
---|
35 | this._computedMinimum = this.getComputedMinimum(); |
---|
36 | this._computedMaximum = this.getComputedMaximum(); |
---|
37 | this._computedMultiplier = this.getComputedMultiplier(); |
---|
38 | |
---|
39 | if(this._computedMaximum > this._computedMinimum){ |
---|
40 | var start = Math.max(0, Math.floor(Math.log(this._computedMinimum + 0.000000001) / Math.LN10)); |
---|
41 | var end = Math.max(0, Math.floor(Math.log(this._computedMaximum + 0.000000001) / Math.LN10)); |
---|
42 | var data; |
---|
43 | for(var i = start; i <= end; i += this._computedMultiplier){ |
---|
44 | data = {}; |
---|
45 | data.value = Math.pow(10, i); |
---|
46 | data.position = (i - start) / (end - start); |
---|
47 | majorTickCache.push(data); |
---|
48 | } |
---|
49 | } |
---|
50 | return majorTickCache; |
---|
51 | }, |
---|
52 | |
---|
53 | getComputedMinimum: function(){ |
---|
54 | // summary: |
---|
55 | // The computed minimum value of the scale. If the minimum value is not |
---|
56 | // an even power of 10, the scale computes a new minimum so that it maps to |
---|
57 | // an even power of 10. |
---|
58 | return Math.pow(10, Math.max(0, Math.floor(Math.log(this.minimum + 0.000000001) / Math.LN10))); |
---|
59 | }, |
---|
60 | |
---|
61 | getComputedMaximum: function(){ |
---|
62 | // summary: |
---|
63 | // The computed maximum value of the scale. If the maximum value is not |
---|
64 | // an even power of 10, the scale computes a new maximum so that it maps to |
---|
65 | // an even power of 10. |
---|
66 | return Math.pow(10, Math.max(0, Math.floor(Math.log(this.maximum + 0.000000001) / Math.LN10))); |
---|
67 | }, |
---|
68 | |
---|
69 | |
---|
70 | getComputedMultiplier: function(){ |
---|
71 | // summary: |
---|
72 | // The computed multiplier value of the scale. If the multiplier value is not |
---|
73 | // an even power of 10, the scale computes a new multiplier so that it maps to |
---|
74 | // an even power of 10. |
---|
75 | return Math.max(1, Math.floor(Math.log(this.multiplier + 0.000000001) / Math.LN10)); |
---|
76 | |
---|
77 | }, |
---|
78 | |
---|
79 | computeTicks: function(){ |
---|
80 | // summary: |
---|
81 | // Creates or re-creates the ticks for this scaler. |
---|
82 | // returns: Array |
---|
83 | // An array containing ticks. |
---|
84 | this.majorTicks = this._buildMajorTickItems(); |
---|
85 | return this.majorTicks.concat(); |
---|
86 | }, |
---|
87 | |
---|
88 | positionForValue: function(value){ |
---|
89 | // summary: |
---|
90 | // Transforms a value into a relative position between 0 and 1. |
---|
91 | // value: Number |
---|
92 | // A value to transform. |
---|
93 | // returns: Number |
---|
94 | // The position between 0 and 1. |
---|
95 | |
---|
96 | if(this._computedMaximum < this._computedMinimum || value <= this._computedMinimum || value < 1 || isNaN(value)){ |
---|
97 | value = this._computedMinimum; |
---|
98 | } |
---|
99 | if(value >= this._computedMaximum){ |
---|
100 | value = this._computedMaximum; |
---|
101 | } |
---|
102 | value = Math.log(value) / Math.LN10; |
---|
103 | var sv = Math.log(this._computedMinimum) / Math.LN10; |
---|
104 | var ev = Math.log(this._computedMaximum) / Math.LN10; |
---|
105 | return (value - sv) / (ev - sv); |
---|
106 | }, |
---|
107 | |
---|
108 | valueForPosition: function(position){ |
---|
109 | // summary: |
---|
110 | // Transforms a relative position (between 0 and 1) into a value. |
---|
111 | // position: Number |
---|
112 | // A relative position to transform. |
---|
113 | // returns: Number |
---|
114 | // The transformed value between minimum and maximum. |
---|
115 | var sv = Math.log(this._computedMinimum) / Math.LN10; |
---|
116 | var ev = Math.log(this._computedMaximum) / Math.LN10; |
---|
117 | return Math.pow(10, sv + position * (ev - sv)); |
---|
118 | } |
---|
119 | }); |
---|
120 | }); |
---|