[483] | 1 | define(["dojo/_base/lang", "dojo/_base/declare", "dojo/Stateful"], function(lang, declare, Stateful){ |
---|
| 2 | return declare("dojox.dgauges.LinearScaler", Stateful, { |
---|
| 3 | // summary: |
---|
| 4 | // The linear scaler. This scaler creates major and minor ticks regularly between |
---|
| 5 | // a minimum and a maximum. |
---|
| 6 | // Scalers are responsible for tick generation and various data-transform operations. |
---|
| 7 | |
---|
| 8 | // minimum: Number |
---|
| 9 | // The minimum value of the scaler. Default is 0. |
---|
| 10 | minimum: 0, |
---|
| 11 | // maximum: Number |
---|
| 12 | // The maximum value of the scaler. Default is 100. |
---|
| 13 | maximum: 100, |
---|
| 14 | // snapInterval: |
---|
| 15 | // Specifies the increment value to be used as snap values on this scale |
---|
| 16 | // during user interaction. |
---|
| 17 | // Default is 1. |
---|
| 18 | snapInterval: 1, |
---|
| 19 | // majorTickInterval: Number |
---|
| 20 | // The interval between two major ticks. |
---|
| 21 | majorTickInterval: NaN, |
---|
| 22 | // minorTickInterval: Number |
---|
| 23 | // The interval between two minor ticks. |
---|
| 24 | minorTickInterval: NaN, |
---|
| 25 | // minorTicksEnabled: Boolean |
---|
| 26 | // If false, minor ticks are not generated. Default is true. |
---|
| 27 | minorTicksEnabled: true, |
---|
| 28 | // majorTicks: |
---|
| 29 | // The array of generated major ticks. You should not set this |
---|
| 30 | // property when using the scaler. |
---|
| 31 | majorTicks: null, |
---|
| 32 | // minorTicks: |
---|
| 33 | // The array of generated minor ticks. You should not set this |
---|
| 34 | // property when using the scaler. |
---|
| 35 | minorTicks: null, |
---|
| 36 | |
---|
| 37 | _computedMajorTickInterval: NaN, |
---|
| 38 | _computedMinorTickInterval: NaN, |
---|
| 39 | |
---|
| 40 | constructor: function(){ |
---|
| 41 | this.watchedProperties = ["minimum", "maximum", "majorTickInterval", "minorTickInterval", "snapInterval", "minorTicksEnabled"]; |
---|
| 42 | }, |
---|
| 43 | |
---|
| 44 | _buildMinorTickItems: function(){ |
---|
| 45 | // summary: |
---|
| 46 | // Internal method. |
---|
| 47 | // tags: |
---|
| 48 | // private |
---|
| 49 | var mt = this.majorTicks; |
---|
| 50 | var minorTickCache = []; |
---|
| 51 | if(this.maximum > this.minimum){ |
---|
| 52 | var majorTickCount = Math.floor((this.maximum - this.minimum) / this.getComputedMajorTickInterval()) + 1; |
---|
| 53 | var minorTickCount = Math.floor(this.getComputedMajorTickInterval() / this.getComputedMinorTickInterval()); |
---|
| 54 | var data; |
---|
| 55 | for(var i = 0; i < majorTickCount - 1; i++){ |
---|
| 56 | for(var j = 1; j < minorTickCount; j++){ |
---|
| 57 | data = {scaler: this}; |
---|
| 58 | data.isMinor = true; |
---|
| 59 | data.value = mt[i].value + j * this.getComputedMinorTickInterval(); |
---|
| 60 | data.position = (Number(data.value) - this.minimum) / (this.maximum - this.minimum); |
---|
| 61 | minorTickCache.push(data); |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | return minorTickCache; |
---|
| 66 | }, |
---|
| 67 | |
---|
| 68 | _buildMajorTickItems: function(){ |
---|
| 69 | // summary: |
---|
| 70 | // Internal method. |
---|
| 71 | // tags: |
---|
| 72 | // private |
---|
| 73 | var majorTickCache = []; |
---|
| 74 | if(this.maximum > this.minimum){ |
---|
| 75 | var majorTickCount = Math.floor((this.maximum - this.minimum) / this.getComputedMajorTickInterval()) + 1; |
---|
| 76 | var data; |
---|
| 77 | for(var i = 0; i < majorTickCount; i++){ |
---|
| 78 | data = {scaler: this}; |
---|
| 79 | data.isMinor = false; |
---|
| 80 | data.value = this.minimum + i * this.getComputedMajorTickInterval(); |
---|
| 81 | data.position = (Number(data.value) - this.minimum) / (this.maximum - this.minimum); |
---|
| 82 | majorTickCache.push(data); |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | return majorTickCache; |
---|
| 86 | }, |
---|
| 87 | |
---|
| 88 | getComputedMajorTickInterval: function(){ |
---|
| 89 | // summary: |
---|
| 90 | // The computed or user defined major tick interval. |
---|
| 91 | // returns: Number |
---|
| 92 | // The major tick interval used for ticks generation. |
---|
| 93 | if(!isNaN(this.majorTickInterval)){ |
---|
| 94 | return this.majorTickInterval; |
---|
| 95 | } |
---|
| 96 | if(isNaN(this._computedMajorTickInterval)){ |
---|
| 97 | this._computedMajorTickInterval = (this.maximum - this.minimum) / 10; |
---|
| 98 | } |
---|
| 99 | return this._computedMajorTickInterval; |
---|
| 100 | }, |
---|
| 101 | |
---|
| 102 | getComputedMinorTickInterval: function(){ |
---|
| 103 | // summary: |
---|
| 104 | // The computed or user defined minor tick interval. |
---|
| 105 | // returns: Number |
---|
| 106 | // The minor tick interval used for ticks generation. |
---|
| 107 | if(!isNaN(this.minorTickInterval)){ |
---|
| 108 | return this.minorTickInterval; |
---|
| 109 | } |
---|
| 110 | if(isNaN(this._computedMinorTickInterval)){ |
---|
| 111 | this._computedMinorTickInterval = this.getComputedMajorTickInterval() / 5; |
---|
| 112 | } |
---|
| 113 | return this._computedMinorTickInterval; |
---|
| 114 | }, |
---|
| 115 | |
---|
| 116 | computeTicks: function(){ |
---|
| 117 | // summary: |
---|
| 118 | // Creates or re-creates the ticks for this scaler. |
---|
| 119 | // returns: Array |
---|
| 120 | // An array containing all ticks (major then minor ticks). |
---|
| 121 | this.majorTicks = this._buildMajorTickItems(); |
---|
| 122 | this.minorTicks = this.minorTicksEnabled ? this._buildMinorTickItems() : []; |
---|
| 123 | return this.majorTicks.concat(this.minorTicks); |
---|
| 124 | }, |
---|
| 125 | |
---|
| 126 | positionForValue: function(value){ |
---|
| 127 | // summary: |
---|
| 128 | // Transforms a value into a relative position between 0 and 1. |
---|
| 129 | // value: Number |
---|
| 130 | // A value to transform. |
---|
| 131 | // returns: Number |
---|
| 132 | // The position between 0 and 1. |
---|
| 133 | var position; |
---|
| 134 | if(value == null || isNaN(value) || value <= this.minimum){ |
---|
| 135 | position = 0; |
---|
| 136 | } |
---|
| 137 | if(value >= this.maximum){ |
---|
| 138 | position = 1; |
---|
| 139 | } |
---|
| 140 | if(isNaN(position)){ |
---|
| 141 | position = (value - this.minimum) / (this.maximum - this.minimum); |
---|
| 142 | } |
---|
| 143 | return position; |
---|
| 144 | }, |
---|
| 145 | |
---|
| 146 | valueForPosition: function(position){ |
---|
| 147 | // summary: |
---|
| 148 | // Transforms a relative position (between 0 and 1) into a value. |
---|
| 149 | // position: Number |
---|
| 150 | // A relative position to transform. |
---|
| 151 | // returns: Number |
---|
| 152 | // The transformed value between minimum and maximum. |
---|
| 153 | var range = Math.abs(this.minimum - this.maximum); |
---|
| 154 | var value = this.minimum + range * position; |
---|
| 155 | if(!isNaN(this.snapInterval) && this.snapInterval > 0){ |
---|
| 156 | value = Math.round((value - this.minimum) / this.snapInterval) * this.snapInterval + this.minimum; |
---|
| 157 | } |
---|
| 158 | return value; |
---|
| 159 | } |
---|
| 160 | }); |
---|
| 161 | }); |
---|