[483] | 1 | define(["dojo/_base/declare", "dojo/Stateful"], function(declare, Stateful){ |
---|
| 2 | return declare("dojox.dgauges.MultiLinearScaler", Stateful, { |
---|
| 3 | // summary: |
---|
| 4 | // The multi-linear scaler. This scaler maps numeric values according |
---|
| 5 | // to the majorTickValues content. |
---|
| 6 | // This allows display of very large value intervals that are difficult to render |
---|
| 7 | // with a linear scale. For example, if majorTickValues contains [0, 10, 50, 500, 2000], |
---|
| 8 | // the scale will show five major ticks with these values. |
---|
| 9 | // Note that this is not a logarithmic scale, the interpolation is linear between |
---|
| 10 | // two contiguous major ticks. |
---|
| 11 | // Scalers are responsible for tick generation and various data-transform operations. |
---|
| 12 | |
---|
| 13 | // majorTickValues: Array |
---|
| 14 | // An array of Number for creating major ticks. |
---|
| 15 | // This array must be sorted in ascendant order. |
---|
| 16 | majorTickValues: null, |
---|
| 17 | // minorTickCount: Array |
---|
| 18 | // The number of minor ticks between two contiguous major ticks. |
---|
| 19 | // The default value is 4. |
---|
| 20 | minorTickCount: 4, |
---|
| 21 | // majorTicks: |
---|
| 22 | // The array of generated major ticks. You should not set this |
---|
| 23 | // property when using the scaler. |
---|
| 24 | majorTicks: null, |
---|
| 25 | // minorTicks: |
---|
| 26 | // The array of generated minor ticks. You should not set this |
---|
| 27 | // property when using the scaler. |
---|
| 28 | minorTicks: null, |
---|
| 29 | _snapIntervalPrecision: null, |
---|
| 30 | _snapCount: 4, |
---|
| 31 | _snapIntervalPrecision: 6, |
---|
| 32 | |
---|
| 33 | constructor: function(){ |
---|
| 34 | this.watchedProperties = ["majorTickValues", "snapCount", "minorTickCount"]; |
---|
| 35 | }, |
---|
| 36 | |
---|
| 37 | computeTicks: function(){ |
---|
| 38 | // summary: |
---|
| 39 | // Creates or re-creates the ticks for this scaler. |
---|
| 40 | // returns: Array |
---|
| 41 | // An array containing all ticks (major then minor ticks). |
---|
| 42 | this.majorTicks = []; |
---|
| 43 | this.minorTicks = []; |
---|
| 44 | var ti; |
---|
| 45 | var step = 1 / (this.majorTickValues.length - 1); |
---|
| 46 | var minorStep = step / (this.minorTickCount + 1); |
---|
| 47 | var currentIndex = 0; |
---|
| 48 | var minorInterval; |
---|
| 49 | var currentMajorValue; |
---|
| 50 | var v; |
---|
| 51 | for(var i = 0; i < this.majorTickValues.length; i++){ |
---|
| 52 | v = this.majorTickValues[i]; |
---|
| 53 | ti = {scaler: this}; |
---|
| 54 | ti.position = currentIndex * step; |
---|
| 55 | ti.value = v; |
---|
| 56 | ti.isMinor = false; |
---|
| 57 | this.majorTicks.push(ti); |
---|
| 58 | if(currentIndex < this.majorTickValues.length - 1){ |
---|
| 59 | currentMajorValue = Number(v); |
---|
| 60 | minorInterval = (Number(this.majorTickValues[i + 1]) - currentMajorValue) / (this.minorTickCount + 1); |
---|
| 61 | for(var j = 1; j <= this.minorTickCount; j++){ |
---|
| 62 | ti = {scaler: this}; |
---|
| 63 | ti.isMinor = true; |
---|
| 64 | ti.position = currentIndex * step + j * minorStep; |
---|
| 65 | ti.value = currentMajorValue + minorInterval * j; |
---|
| 66 | this.minorTicks.push(ti); |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | currentIndex++; |
---|
| 70 | } |
---|
| 71 | return this.majorTicks.concat(this.minorTicks); |
---|
| 72 | }, |
---|
| 73 | |
---|
| 74 | positionForValue: function(value){ |
---|
| 75 | // summary: |
---|
| 76 | // Transforms a value into a relative position between 0 and 1. |
---|
| 77 | // value: Number |
---|
| 78 | // A value to transform. |
---|
| 79 | // returns: Number |
---|
| 80 | // The position between 0 and 1. |
---|
| 81 | if(!this.majorTickValues){ |
---|
| 82 | return 0; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | if(!this.majorTicks){ |
---|
| 86 | this.computeTicks(); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | var minmax = this._getMinMax(value); |
---|
| 90 | var position = (value - minmax[0].value) / (minmax[1].value - minmax[0].value); |
---|
| 91 | return minmax[0].position + position * (minmax[1].position - minmax[0].position); |
---|
| 92 | }, |
---|
| 93 | |
---|
| 94 | valueForPosition: function(position){ |
---|
| 95 | // summary: |
---|
| 96 | // Transforms a relative position (between 0 and 1) into a value. |
---|
| 97 | // position: Number |
---|
| 98 | // A relative position to transform. |
---|
| 99 | // returns: Number |
---|
| 100 | // The transformed value. |
---|
| 101 | if(this.majorTicks == null){ |
---|
| 102 | return 0; |
---|
| 103 | } |
---|
| 104 | var minmax = this._getMinMax(position, "position"); |
---|
| 105 | var value = (position - minmax[0].position) / (minmax[1].position - minmax[0].position); |
---|
| 106 | return minmax[0].value + value * (minmax[1].value - minmax[0].value); |
---|
| 107 | }, |
---|
| 108 | |
---|
| 109 | _getMinMax: function(v, property){ |
---|
| 110 | // summary: |
---|
| 111 | // Internal method. |
---|
| 112 | // tags: |
---|
| 113 | // private |
---|
| 114 | if(!property){ |
---|
| 115 | property = "value"; |
---|
| 116 | } |
---|
| 117 | var res = []; |
---|
| 118 | var pre; |
---|
| 119 | var post; |
---|
| 120 | var left = 0; |
---|
| 121 | var right = this.majorTicks.length - 1; |
---|
| 122 | var center; |
---|
| 123 | |
---|
| 124 | if(v <= this.majorTicks[0][property] || v >= this.majorTicks[right][property]){ |
---|
| 125 | res[0] = this.majorTicks[0]; |
---|
| 126 | res[1] = this.majorTicks[right]; |
---|
| 127 | return res; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | while (true){ |
---|
| 131 | center = Math.floor((left + right) / 2); |
---|
| 132 | |
---|
| 133 | if(this.majorTicks[center][property] <= v){ |
---|
| 134 | if(this.majorTicks[center + 1][property] >= v){ |
---|
| 135 | res[0] = this.majorTicks[center]; |
---|
| 136 | res[1] = this.majorTicks[center + 1]; |
---|
| 137 | return res; |
---|
| 138 | }else{ |
---|
| 139 | left = center + 1; |
---|
| 140 | continue; |
---|
| 141 | } |
---|
| 142 | }else{ |
---|
| 143 | right = center; |
---|
| 144 | continue; |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | }); |
---|
| 149 | }); |
---|