GlossyCircularGaugeBase.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. define("dojox/gauges/GlossyCircularGaugeBase", ["dojo/_base/declare","dojo/_base/lang","dojo/_base/connect","dojox/gfx","./AnalogGauge","./AnalogCircleIndicator","./TextIndicator","./GlossyCircularGaugeNeedle"],
  2. function(declare, lang, connect, gfx, AnalogGauge, AnalogCircleIndicator, TextIndicator, GlossyCircularGaugeNeedle) {
  3. /*=====
  4. AnalogGauge = dojox.gauges.AnalogGauge;
  5. =====*/
  6. return declare("dojox.gauges.GlossyCircularGaugeBase", [AnalogGauge], {
  7. // summary:
  8. // The base class for GlossyCircularGauge and GlossySemiCircularGauge.
  9. //_defaultIndicator : _Indicator
  10. // the type of default indicator to create
  11. _defaultIndicator: AnalogCircleIndicator,
  12. // _needle: dojox.gauges.GlossyCircularGaugeNeedle
  13. // the needle of this circular gauge
  14. _needle: null,
  15. // _textIndicator: dojox.gauges.TextIndicator
  16. // the text displaying the gauge's value
  17. _textIndicator: null,
  18. _textIndicatorAdded: false,
  19. // _range: Object
  20. // the range of this gauge
  21. _range: null,
  22. // value: Number
  23. // The value of the gauge.
  24. value: 0,
  25. // color: String
  26. // The main color of the gauge.
  27. color: 'black',
  28. // needleColor: Color
  29. // The main color of the needle.
  30. needleColor: '#c4c4c4',
  31. // textIndicatorFont: String
  32. // The font of the text indicator
  33. textIndicatorFont: "normal normal normal 20pt serif",
  34. // textIndicatorVisible: Boolean
  35. // Indicates if the text indicator is visible
  36. textIndicatorVisible: true,
  37. // textIndicatorColor: Color
  38. // The color of the text indicator
  39. textIndicatorColor: '#c4c4c4',
  40. // _majorTicksOffset: Number
  41. // Distance, at design, from gauge's center to major ticks
  42. _majorTicksOffset: 130,
  43. // majorTicksInterval: Number
  44. // Interval between major ticks
  45. majorTicksInterval: 10,
  46. // _majorTicksLength: Number
  47. // Major tick size, at design
  48. _majorTicksLength: 5,
  49. // majorTicksColor: Color
  50. // Color of major tick marks
  51. majorTicksColor: '#c4c4c4',
  52. // majorTicksLabelPlacement: String
  53. // Placement of major tick labels
  54. majorTicksLabelPlacement: 'inside',
  55. // _minorTicksOffset: Number
  56. // Distance, at design, from gauge's center to minor ticks
  57. _minorTicksOffset: 130,
  58. // minorTicksInterval: Number
  59. // Interval between minor ticks
  60. minorTicksInterval: 5,
  61. // _minorTicksLength: Number
  62. // Minor tick size, at design
  63. _minorTicksLength: 3,
  64. // minorTicksColor: Color
  65. // Color of minor tick marks
  66. minorTicksColor: '#c4c4c4',
  67. // noChange: Boolean
  68. // Indicates if the gauge reacts to touch events
  69. noChange: false,
  70. // title: String
  71. // The title displayed in the needle's tooltip
  72. title: "",
  73. // font: Object
  74. // The font of the gauge
  75. font: "normal normal normal 10pt serif",
  76. // scalePrecision: Number
  77. // The precision for the formatting of numbers in the scale (default is 0)
  78. scalePrecision: 0,
  79. // textIndicatorPrecision: Number
  80. // The precision for the formatting of numbers in the text indicator (default is 0)
  81. textIndicatorPrecision: 0,
  82. _font: null,
  83. constructor: function(){
  84. this.startAngle = -135;
  85. this.endAngle = 135;
  86. this.min = 0;
  87. this.max = 100;
  88. },
  89. startup: function(){
  90. // summary:
  91. // Overrides AnalogGauge.startup
  92. this.inherited(arguments);
  93. //just in case someone calls the startup twice.
  94. if (this._needle) return;
  95. var scale = Math.min((this.width / this._designWidth), (this.height / this._designHeight));
  96. this.cx = scale * this._designCx + (this.width - scale * this._designWidth) / 2;
  97. this.cy = scale * this._designCy + (this.height - scale * this._designHeight) / 2;
  98. this._range = {
  99. low: this.min ? this.min : 0,
  100. high: this.max ? this.max : 100,
  101. color: [255, 255, 255, 0]
  102. };
  103. this.addRange(this._range);
  104. this._majorTicksOffset = this._minorTicksOffset = scale * this._majorTicksOffset;
  105. this._majorTicksLength = scale * this._majorTicksLength;
  106. this._minorTicksLength = scale * this._minorTicksLength;
  107. // creates and add the major ticks
  108. this.setMajorTicks({
  109. fixedPrecision: true,
  110. precision: this.scalePrecision,
  111. font: this._font,
  112. offset: this._majorTicksOffset,
  113. interval: this.majorTicksInterval,
  114. length: this._majorTicksLength,
  115. color: this.majorTicksColor,
  116. labelPlacement: this.majorTicksLabelPlacement
  117. });
  118. // creates and add the minor ticks
  119. this.setMinorTicks({
  120. offset: this._minorTicksOffset,
  121. interval: this.minorTicksInterval,
  122. length: this._minorTicksLength,
  123. color: this.minorTicksColor
  124. });
  125. // creates and adds the needle
  126. this._needle = new GlossyCircularGaugeNeedle({
  127. hideValue: true,
  128. title: this.title,
  129. noChange: this.noChange,
  130. color: this.needleColor,
  131. value: this.value
  132. });
  133. this.addIndicator(this._needle);
  134. // creates and add the text indicator
  135. this._textIndicator = new TextIndicator({
  136. x: scale * this._designTextIndicatorX + (this.width - scale * this._designWidth) / 2,
  137. y: scale * this._designTextIndicatorY + (this.height - scale * this._designHeight) / 2,
  138. fixedPrecision: true,
  139. precision: this.textIndicatorPrecision,
  140. color: this.textIndicatorColor,
  141. value: this.value ? this.value : this.min,
  142. align: "middle",
  143. font: this._textIndicatorFont
  144. });
  145. if (this.textIndicatorVisible){
  146. this.addIndicator(this._textIndicator);
  147. this._textIndicatorAdded = true;
  148. }
  149. // connect needle and text
  150. connect.connect(this._needle, "valueChanged", lang.hitch(this, function(){
  151. this.value = this._needle.value;
  152. this._textIndicator.update(this._needle.value);
  153. this.onValueChanged();
  154. }));
  155. },
  156. onValueChanged: function(){
  157. // summary:
  158. // Invoked when the value of the gauge has changed.
  159. },
  160. //*******************************************************************************************
  161. //* Property getters and setters
  162. //*******************************************************************************************
  163. _setColorAttr: function(color){
  164. // summary:
  165. // Sets the main color of the gauge
  166. // color: String
  167. // The color
  168. this.color = color ? color : 'black';
  169. if (this._gaugeBackground && this._gaugeBackground.parent)
  170. this._gaugeBackground.parent.remove(this._gaugeBackground);
  171. if (this._foreground && this._foreground.parent)
  172. this._foreground.parent.remove(this._foreground);
  173. this._gaugeBackground = null;
  174. this._foreground = null;
  175. this.draw();
  176. },
  177. _setNeedleColorAttr: function(color){
  178. // summary:
  179. // Sets the main color of the needle
  180. // color: String
  181. // The color
  182. this.needleColor = color;
  183. if (this._needle){
  184. this.removeIndicator(this._needle);
  185. this._needle.color = this.needleColor;
  186. this._needle.shape = null;
  187. this.addIndicator(this._needle);
  188. }
  189. },
  190. _setTextIndicatorColorAttr: function(color){
  191. // summary:
  192. // Sets the color of text indicator display the gauge's value
  193. // color: String
  194. // The color
  195. this.textIndicatorColor = color;
  196. if (this._textIndicator){
  197. this._textIndicator.color = this.textIndicatorColor;
  198. this.draw();
  199. }
  200. },
  201. _setTextIndicatorFontAttr: function(font){
  202. // summary:
  203. // Sets the font of the text indicator
  204. // font: String
  205. // An string representing the font such as 'normal normal normal 10pt Helvetica,Arial,sans-serif'
  206. //
  207. this.textIndicatorFont = font;
  208. this._textIndicatorFont = gfx.splitFontString(font);
  209. if (this._textIndicator){
  210. this._textIndicator.font = this._textIndicatorFont;
  211. this.draw();
  212. }
  213. },
  214. setMajorTicksOffset: function(offset){
  215. // summary:
  216. // Sets the distance from gauge's center to major ticks
  217. this._majorTicksOffset = offset;
  218. this._setMajorTicksProperty({
  219. 'offset': this._majorTicksOffset
  220. });
  221. return this;
  222. },
  223. getMajorTicksOffset: function(){
  224. // summary:
  225. // Return the distance from gauge's center to major ticks
  226. return this._majorTicksOffset;
  227. },
  228. _setMajorTicksIntervalAttr: function(interval){
  229. // summary:
  230. // Sets the interval between major ticks
  231. this.majorTicksInterval = interval;
  232. this._setMajorTicksProperty({
  233. 'interval': this.majorTicksInterval
  234. });
  235. },
  236. setMajorTicksLength: function(length){
  237. // summary:
  238. // Sets the size of the major ticks.
  239. this._majorTicksLength = length;
  240. this._setMajorTicksProperty({
  241. 'length': this._majorTicksLength
  242. });
  243. return this;
  244. },
  245. getMajorTicksLength: function(){
  246. // summary:
  247. // Returns the size of the major ticks.
  248. return this._majorTicksLength;
  249. },
  250. _setMajorTicksColorAttr: function(color){
  251. // summary:
  252. // Sets the color of the major ticks.
  253. this.majorTicksColor = color;
  254. this._setMajorTicksProperty({
  255. 'color': this.majorTicksColor
  256. });
  257. },
  258. _setMajorTicksLabelPlacementAttr: function(placement){
  259. // summary:
  260. // Sets the placement of labels relatively to major ticks.
  261. // placement: String
  262. // 'inside' or 'outside'
  263. this.majorTicksLabelPlacement = placement;
  264. this._setMajorTicksProperty({
  265. 'labelPlacement': this.majorTicksLabelPlacement
  266. });
  267. },
  268. _setMajorTicksProperty: function(prop){
  269. if (this.majorTicks){
  270. lang.mixin(this.majorTicks, prop);
  271. this.setMajorTicks(this.majorTicks);
  272. }
  273. },
  274. setMinorTicksOffset: function(offset){
  275. // summary:
  276. // Sets the distance from gauge's center to minor ticks
  277. this._minorTicksOffset = offset;
  278. this._setMinorTicksProperty({
  279. 'offset': this._minorTicksOffset
  280. });
  281. return this;
  282. },
  283. getMinorTicksOffset: function(){
  284. // summary:
  285. // Returns the distance from gauge's center to minor ticks
  286. return this._minorTicksOffset;
  287. },
  288. _setMinorTicksIntervalAttr: function(interval){
  289. // summary:
  290. // Sets the interval between minor ticks
  291. this.minorTicksInterval = interval;
  292. this._setMinorTicksProperty({
  293. 'interval': this.minorTicksInterval
  294. });
  295. },
  296. setMinorTicksLength: function(length){
  297. // summary:
  298. // Sets the size of the minor ticks.
  299. this._minorTicksLength = length;
  300. this._setMinorTicksProperty({
  301. 'length': this._minorTicksLength
  302. });
  303. return this;
  304. },
  305. getMinorTicksLength: function(){
  306. // summary:
  307. // Return the size of the minor ticks.
  308. return this._minorTicksLength;
  309. },
  310. _setMinorTicksColorAttr: function(color){
  311. // summary:
  312. // Sets the color of the minor ticks.
  313. this.minorTicksColor = color;
  314. this._setMinorTicksProperty({
  315. 'color': this.minorTicksColor
  316. });
  317. },
  318. _setMinorTicksProperty: function(prop){
  319. if (this.minorTicks){
  320. lang.mixin(this.minorTicks, prop);
  321. this.setMinorTicks(this.minorTicks);
  322. }
  323. },
  324. _setMinAttr: function(min){
  325. this.min = min;
  326. if (this.majorTicks != null)
  327. this.setMajorTicks(this.majorTicks);
  328. if (this.minorTicks != null)
  329. this.setMinorTicks(this.minorTicks);
  330. this.draw();
  331. this._updateNeedle();
  332. },
  333. _setMaxAttr: function(max){
  334. this.max = max;
  335. if (this.majorTicks != null)
  336. this.setMajorTicks(this.majorTicks);
  337. if (this.minorTicks != null)
  338. this.setMinorTicks(this.minorTicks);
  339. this.draw();
  340. this._updateNeedle();
  341. },
  342. _setScalePrecisionAttr: function(value){
  343. // summary:
  344. // Changes precision of the numbers in the scale of the gauge
  345. // value: Number
  346. // The new value
  347. this.scalePrecision = value;
  348. this._setMajorTicksProperty({
  349. 'precision': value
  350. });
  351. },
  352. _setTextIndicatorPrecisionAttr: function(value){
  353. // summary:
  354. // Changes precision of the numbers in the text indicator
  355. // value: Number
  356. // The new value
  357. this.textIndicatorPrecision = value;
  358. this._setMajorTicksProperty({
  359. 'precision': value
  360. });
  361. },
  362. _setValueAttr: function(value){
  363. // summary:
  364. // Changes the value of the gauge
  365. // value: Number
  366. // The new value for the gauge.
  367. value = Math.min(this.max, value);
  368. value = Math.max(this.min, value);
  369. this.value = value;
  370. if (this._needle){
  371. // update will not work if noChange is true.
  372. var noChange = this._needle.noChange;
  373. this._needle.noChange = false;
  374. this._needle.update(value);
  375. this._needle.noChange = noChange;
  376. }
  377. },
  378. _setNoChangeAttr: function(value){
  379. // summary:
  380. // Indicates if the value of the gauge can be changed or not
  381. // value: boolean
  382. // true indicates that the gauge's value cannot be changed
  383. this.noChange = value;
  384. if (this._needle)
  385. this._needle.noChange = this.noChange;
  386. },
  387. _setTextIndicatorVisibleAttr: function(value){
  388. // summary:
  389. // Changes the visibility of the text indicator displaying the gauge's value.
  390. // value: boolean
  391. // true to show the indicator, false to hide.
  392. this.textIndicatorVisible = value;
  393. if (this._textIndicator && this._needle){
  394. if (this.textIndicatorVisible && !this._textIndicatorAdded){
  395. this.addIndicator(this._textIndicator);
  396. this._textIndicatorAdded = true;
  397. this.moveIndicatorToFront(this._needle);
  398. }
  399. else
  400. if (!this.textIndicatorVisible && this._textIndicatorAdded){
  401. this.removeIndicator(this._textIndicator);
  402. this._textIndicatorAdded = false;
  403. }
  404. }
  405. },
  406. _setTitleAttr: function(value){
  407. // summary:
  408. // Sets the title displayed by the needle's tooltip .
  409. // value: String
  410. // the title
  411. this.title = value;
  412. if (this._needle){
  413. this._needle.title = this.title;
  414. }
  415. },
  416. _setOrientationAttr: function(orientation){
  417. // summary:
  418. // Sets the orientation of the gauge
  419. // orientation: String
  420. // Either "clockwise" or "cclockwise"
  421. this.orientation = orientation;
  422. if (this.majorTicks != null)
  423. this.setMajorTicks(this.majorTicks);
  424. if (this.minorTicks != null)
  425. this.setMinorTicks(this.minorTicks);
  426. this.draw();
  427. this._updateNeedle();
  428. },
  429. _updateNeedle: function(){
  430. // updates the needle with no animation
  431. this.value = Math.max(this.min, this.value);
  432. this.value = Math.min(this.max, this.value);
  433. if (this._needle){
  434. // update will not work if noChange is true.
  435. var noChange = this._needle.noChange;
  436. this._needle.noChange = false;
  437. this._needle.update(this.value, false);
  438. this._needle.noChange = noChange;
  439. } // to redraw the needle
  440. },
  441. _setFontAttr: function(font){
  442. // summary:
  443. // Sets the font of the gauge
  444. // font: String
  445. // An string representing the font such as 'normal normal normal 10pt Helvetica,Arial,sans-serif'
  446. //
  447. this.font = font;
  448. this._font = gfx.splitFontString(font);
  449. this._setMajorTicksProperty({
  450. 'font': this._font
  451. });
  452. }});
  453. });