gauge.js 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. // Copyright 2007 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Gauge UI component, using browser vector graphics.
  16. * @see ../demos/gauge.html
  17. */
  18. goog.provide('goog.ui.Gauge');
  19. goog.provide('goog.ui.GaugeColoredRange');
  20. goog.require('goog.a11y.aria');
  21. goog.require('goog.asserts');
  22. goog.require('goog.dom.TagName');
  23. goog.require('goog.events');
  24. goog.require('goog.fx.Animation');
  25. goog.require('goog.fx.Transition');
  26. goog.require('goog.fx.easing');
  27. goog.require('goog.graphics');
  28. goog.require('goog.graphics.Font');
  29. goog.require('goog.graphics.Path');
  30. goog.require('goog.graphics.SolidFill');
  31. goog.require('goog.math');
  32. goog.require('goog.ui.Component');
  33. goog.require('goog.ui.GaugeTheme');
  34. /**
  35. * Information on how to decorate a range in the gauge.
  36. * This is an internal-only class.
  37. * @param {number} fromValue The range start (minimal) value.
  38. * @param {number} toValue The range end (maximal) value.
  39. * @param {string} backgroundColor Color to fill the range background with.
  40. * @constructor
  41. * @final
  42. */
  43. goog.ui.GaugeColoredRange = function(fromValue, toValue, backgroundColor) {
  44. /**
  45. * The range start (minimal) value.
  46. * @type {number}
  47. */
  48. this.fromValue = fromValue;
  49. /**
  50. * The range end (maximal) value.
  51. * @type {number}
  52. */
  53. this.toValue = toValue;
  54. /**
  55. * Color to fill the range background with.
  56. * @type {string}
  57. */
  58. this.backgroundColor = backgroundColor;
  59. };
  60. /**
  61. * A UI component that displays a gauge.
  62. * A gauge displayes a current value within a round axis that represents a
  63. * given range.
  64. * The gauge is built from an external border, and internal border inside it,
  65. * ticks and labels inside the internal border, and a needle that points to
  66. * the current value.
  67. * @param {number} width The width in pixels.
  68. * @param {number} height The height in pixels.
  69. * @param {goog.dom.DomHelper=} opt_domHelper The DOM helper object for the
  70. * document we want to render in.
  71. * @constructor
  72. * @extends {goog.ui.Component}
  73. * @final
  74. */
  75. goog.ui.Gauge = function(width, height, opt_domHelper) {
  76. goog.ui.Component.call(this, opt_domHelper);
  77. /**
  78. * The width in pixels of this component.
  79. * @type {number}
  80. * @private
  81. */
  82. this.width_ = width;
  83. /**
  84. * The height in pixels of this component.
  85. * @type {number}
  86. * @private
  87. */
  88. this.height_ = height;
  89. /**
  90. * The underlying graphics.
  91. * @type {goog.graphics.AbstractGraphics}
  92. * @private
  93. */
  94. this.graphics_ =
  95. goog.graphics.createGraphics(width, height, null, null, opt_domHelper);
  96. /**
  97. * Colors to paint the background of certain ranges (optional).
  98. * @type {Array<goog.ui.GaugeColoredRange>}
  99. * @private
  100. */
  101. this.rangeColors_ = [];
  102. };
  103. goog.inherits(goog.ui.Gauge, goog.ui.Component);
  104. /**
  105. * Constant for a background color for a gauge area.
  106. */
  107. goog.ui.Gauge.RED = '#ffc0c0';
  108. /**
  109. * Constant for a background color for a gauge area.
  110. */
  111. goog.ui.Gauge.GREEN = '#c0ffc0';
  112. /**
  113. * Constant for a background color for a gauge area.
  114. */
  115. goog.ui.Gauge.YELLOW = '#ffffa0';
  116. /**
  117. * The radius of the entire gauge from the canvas size.
  118. * @type {number}
  119. */
  120. goog.ui.Gauge.FACTOR_RADIUS_FROM_SIZE = 0.45;
  121. /**
  122. * The ratio of internal gauge radius from entire radius.
  123. * The remaining area is the border around the gauge.
  124. * @type {number}
  125. */
  126. goog.ui.Gauge.FACTOR_MAIN_AREA = 0.9;
  127. /**
  128. * The ratio of the colored background area for value ranges.
  129. * The colored area width is computed as
  130. * InternalRadius * (1 - FACTOR_COLOR_RADIUS)
  131. * @type {number}
  132. */
  133. goog.ui.Gauge.FACTOR_COLOR_RADIUS = 0.75;
  134. /**
  135. * The ratio of the major ticks length start position, from the radius.
  136. * The major ticks length width is computed as
  137. * InternalRadius * (1 - FACTOR_MAJOR_TICKS)
  138. * @type {number}
  139. */
  140. goog.ui.Gauge.FACTOR_MAJOR_TICKS = 0.8;
  141. /**
  142. * The ratio of the minor ticks length start position, from the radius.
  143. * The minor ticks length width is computed as
  144. * InternalRadius * (1 - FACTOR_MINOR_TICKS)
  145. * @type {number}
  146. */
  147. goog.ui.Gauge.FACTOR_MINOR_TICKS = 0.9;
  148. /**
  149. * The length of the needle front (value facing) from the internal radius.
  150. * The needle front is the part of the needle that points to the value.
  151. * @type {number}
  152. */
  153. goog.ui.Gauge.FACTOR_NEEDLE_FRONT = 0.95;
  154. /**
  155. * The length of the needle back relative to the internal radius.
  156. * The needle back is the part of the needle that points away from the value.
  157. * @type {number}
  158. */
  159. goog.ui.Gauge.FACTOR_NEEDLE_BACK = 0.3;
  160. /**
  161. * The width of the needle front at the hinge.
  162. * This is the width of the curve control point, the actual width is
  163. * computed by the curve itself.
  164. * @type {number}
  165. */
  166. goog.ui.Gauge.FACTOR_NEEDLE_WIDTH = 0.07;
  167. /**
  168. * The width (radius) of the needle hinge from the gauge radius.
  169. * @type {number}
  170. */
  171. goog.ui.Gauge.FACTOR_NEEDLE_HINGE = 0.15;
  172. /**
  173. * The title font size (height) for titles relative to the internal radius.
  174. * @type {number}
  175. */
  176. goog.ui.Gauge.FACTOR_TITLE_FONT_SIZE = 0.16;
  177. /**
  178. * The offset of the title from the center, relative to the internal radius.
  179. * @type {number}
  180. */
  181. goog.ui.Gauge.FACTOR_TITLE_OFFSET = 0.35;
  182. /**
  183. * The formatted value font size (height) relative to the internal radius.
  184. * @type {number}
  185. */
  186. goog.ui.Gauge.FACTOR_VALUE_FONT_SIZE = 0.18;
  187. /**
  188. * The title font size (height) for tick labels relative to the internal radius.
  189. * @type {number}
  190. */
  191. goog.ui.Gauge.FACTOR_TICK_LABEL_FONT_SIZE = 0.14;
  192. /**
  193. * The offset of the formatted value down from the center, relative to the
  194. * internal radius.
  195. * @type {number}
  196. */
  197. goog.ui.Gauge.FACTOR_VALUE_OFFSET = 0.75;
  198. /**
  199. * The font name for title text.
  200. * @type {string}
  201. */
  202. goog.ui.Gauge.TITLE_FONT_NAME = 'arial';
  203. /**
  204. * The maximal size of a step the needle can move (percent from size of range).
  205. * If the needle needs to move more, it will be moved in animated steps, to
  206. * show a smooth transition between values.
  207. * @type {number}
  208. */
  209. goog.ui.Gauge.NEEDLE_MOVE_MAX_STEP = 0.02;
  210. /**
  211. * Time in miliseconds for animating a move of the value pointer.
  212. * @type {number}
  213. */
  214. goog.ui.Gauge.NEEDLE_MOVE_TIME = 400;
  215. /**
  216. * Tolerance factor for how much values can exceed the range (being too
  217. * low or too high). The value is presented as a position (percentage).
  218. * @type {number}
  219. */
  220. goog.ui.Gauge.MAX_EXCEED_POSITION_POSITION = 0.02;
  221. /**
  222. * The minimal value that can be displayed.
  223. * @private
  224. * @type {number}
  225. */
  226. goog.ui.Gauge.prototype.minValue_ = 0;
  227. /**
  228. * The maximal value that can be displayed.
  229. * @private
  230. * @type {number}
  231. */
  232. goog.ui.Gauge.prototype.maxValue_ = 100;
  233. /**
  234. * The number of major tick sections.
  235. * @private
  236. * @type {number}
  237. */
  238. goog.ui.Gauge.prototype.majorTicks_ = 5;
  239. /**
  240. * The number of minor tick sections in each major tick section.
  241. * @private
  242. * @type {number}
  243. */
  244. goog.ui.Gauge.prototype.minorTicks_ = 2;
  245. /**
  246. * The current value that needs to be displayed in the gauge.
  247. * @private
  248. * @type {number}
  249. */
  250. goog.ui.Gauge.prototype.value_ = 0;
  251. /**
  252. * The current value formatted into a String.
  253. * @private
  254. * @type {?string}
  255. */
  256. goog.ui.Gauge.prototype.formattedValue_ = null;
  257. /**
  258. * The current colors theme.
  259. * @private
  260. * @type {goog.ui.GaugeTheme?}
  261. */
  262. goog.ui.Gauge.prototype.theme_ = null;
  263. /**
  264. * Title to display above the gauge center.
  265. * @private
  266. * @type {?string}
  267. */
  268. goog.ui.Gauge.prototype.titleTop_ = null;
  269. /**
  270. * Title to display below the gauge center.
  271. * @private
  272. * @type {?string}
  273. */
  274. goog.ui.Gauge.prototype.titleBottom_ = null;
  275. /**
  276. * Font to use for drawing titles.
  277. * If null (default), computed dynamically with a size relative to the
  278. * gauge radius.
  279. * @private
  280. * @type {goog.graphics.Font?}
  281. */
  282. goog.ui.Gauge.prototype.titleFont_ = null;
  283. /**
  284. * Font to use for drawing the formatted value.
  285. * If null (default), computed dynamically with a size relative to the
  286. * gauge radius.
  287. * @private
  288. * @type {goog.graphics.Font?}
  289. */
  290. goog.ui.Gauge.prototype.valueFont_ = null;
  291. /**
  292. * Font to use for drawing tick labels.
  293. * If null (default), computed dynamically with a size relative to the
  294. * gauge radius.
  295. * @private
  296. * @type {goog.graphics.Font?}
  297. */
  298. goog.ui.Gauge.prototype.tickLabelFont_ = null;
  299. /**
  300. * The size in angles of the gauge axis area.
  301. * @private
  302. * @type {number}
  303. */
  304. goog.ui.Gauge.prototype.angleSpan_ = 270;
  305. /**
  306. * The radius for drawing the needle.
  307. * Computed on full redraw, and used on every animation step of moving
  308. * the needle.
  309. * @type {number}
  310. * @private
  311. */
  312. goog.ui.Gauge.prototype.needleRadius_ = 0;
  313. /**
  314. * The group elemnt of the needle. Contains all elements that change when the
  315. * gauge value changes.
  316. * @type {goog.graphics.GroupElement?}
  317. * @private
  318. */
  319. goog.ui.Gauge.prototype.needleGroup_ = null;
  320. /**
  321. * The current position (0-1) of the visible needle.
  322. * Initially set to null to prevent animation on first opening of the gauge.
  323. * @type {?number}
  324. * @private
  325. */
  326. goog.ui.Gauge.prototype.needleValuePosition_ = null;
  327. /**
  328. * Text labels to display by major tick marks.
  329. * @type {Array<string>?}
  330. * @private
  331. */
  332. goog.ui.Gauge.prototype.majorTickLabels_ = null;
  333. /**
  334. * Animation object while needle is being moved (animated).
  335. * @type {goog.fx.Animation?}
  336. * @private
  337. */
  338. goog.ui.Gauge.prototype.animation_ = null;
  339. /**
  340. * @return {number} The minimum value of the range.
  341. */
  342. goog.ui.Gauge.prototype.getMinimum = function() {
  343. return this.minValue_;
  344. };
  345. /**
  346. * Sets the minimum value of the range
  347. * @param {number} min The minimum value of the range.
  348. */
  349. goog.ui.Gauge.prototype.setMinimum = function(min) {
  350. this.minValue_ = min;
  351. var element = this.getElement();
  352. if (element) {
  353. goog.a11y.aria.setState(element, 'valuemin', min);
  354. }
  355. };
  356. /**
  357. * @return {number} The maximum value of the range.
  358. */
  359. goog.ui.Gauge.prototype.getMaximum = function() {
  360. return this.maxValue_;
  361. };
  362. /**
  363. * Sets the maximum number of the range
  364. * @param {number} max The maximum value of the range.
  365. */
  366. goog.ui.Gauge.prototype.setMaximum = function(max) {
  367. this.maxValue_ = max;
  368. var element = this.getElement();
  369. if (element) {
  370. goog.a11y.aria.setState(element, 'valuemax', max);
  371. }
  372. };
  373. /**
  374. * Sets the current value range displayed by the gauge.
  375. * @param {number} value The current value for the gauge. This value
  376. * determines the position of the needle of the gauge.
  377. * @param {string=} opt_formattedValue The string value to show in the gauge.
  378. * If not specified, no string value will be displayed.
  379. */
  380. goog.ui.Gauge.prototype.setValue = function(value, opt_formattedValue) {
  381. this.value_ = value;
  382. this.formattedValue_ = opt_formattedValue || null;
  383. this.stopAnimation_(); // Stop the active animation if exists
  384. // Compute desired value position (normalize value to range 0-1)
  385. var valuePosition = this.valueToRangePosition_(value);
  386. if (this.needleValuePosition_ == null) {
  387. // No animation on initial display
  388. this.needleValuePosition_ = valuePosition;
  389. this.drawValue_();
  390. } else {
  391. // Animate move
  392. this.animation_ = new goog.fx.Animation(
  393. [this.needleValuePosition_], [valuePosition],
  394. goog.ui.Gauge.NEEDLE_MOVE_TIME, goog.fx.easing.inAndOut);
  395. var events = [
  396. goog.fx.Transition.EventType.BEGIN, goog.fx.Animation.EventType.ANIMATE,
  397. goog.fx.Transition.EventType.END
  398. ];
  399. goog.events.listen(this.animation_, events, this.onAnimate_, false, this);
  400. goog.events.listen(
  401. this.animation_, goog.fx.Transition.EventType.END, this.onAnimateEnd_,
  402. false, this);
  403. // Start animation
  404. this.animation_.play(false);
  405. }
  406. var element = this.getElement();
  407. if (element) {
  408. goog.a11y.aria.setState(element, 'valuenow', this.value_);
  409. }
  410. };
  411. /**
  412. * Sets the number of major tick sections and minor tick sections.
  413. * @param {number} majorUnits The number of major tick sections.
  414. * @param {number} minorUnits The number of minor tick sections for each major
  415. * tick section.
  416. */
  417. goog.ui.Gauge.prototype.setTicks = function(majorUnits, minorUnits) {
  418. this.majorTicks_ = Math.max(1, majorUnits);
  419. this.minorTicks_ = Math.max(1, minorUnits);
  420. this.draw_();
  421. };
  422. /**
  423. * Sets the labels of the major ticks.
  424. * @param {Array<string>} tickLabels A text label for each major tick value.
  425. */
  426. goog.ui.Gauge.prototype.setMajorTickLabels = function(tickLabels) {
  427. this.majorTickLabels_ = tickLabels;
  428. this.draw_();
  429. };
  430. /**
  431. * Sets the top title of the gauge.
  432. * The top title is displayed above the center.
  433. * @param {string} text The top title text.
  434. */
  435. goog.ui.Gauge.prototype.setTitleTop = function(text) {
  436. this.titleTop_ = text;
  437. this.draw_();
  438. };
  439. /**
  440. * Sets the bottom title of the gauge.
  441. * The top title is displayed below the center.
  442. * @param {string} text The bottom title text.
  443. */
  444. goog.ui.Gauge.prototype.setTitleBottom = function(text) {
  445. this.titleBottom_ = text;
  446. this.draw_();
  447. };
  448. /**
  449. * Sets the font for displaying top and bottom titles.
  450. * @param {goog.graphics.Font} font The font for titles.
  451. */
  452. goog.ui.Gauge.prototype.setTitleFont = function(font) {
  453. this.titleFont_ = font;
  454. this.draw_();
  455. };
  456. /**
  457. * Sets the font for displaying the formatted value.
  458. * @param {goog.graphics.Font} font The font for displaying the value.
  459. */
  460. goog.ui.Gauge.prototype.setValueFont = function(font) {
  461. this.valueFont_ = font;
  462. this.drawValue_();
  463. };
  464. /**
  465. * Sets the color theme for drawing the gauge.
  466. * @param {goog.ui.GaugeTheme} theme The color theme to use.
  467. */
  468. goog.ui.Gauge.prototype.setTheme = function(theme) {
  469. this.theme_ = theme;
  470. this.draw_();
  471. };
  472. /**
  473. * Set the background color for a range of values on the gauge.
  474. * @param {number} fromValue The lower (start) value of the colored range.
  475. * @param {number} toValue The higher (end) value of the colored range.
  476. * @param {string} color The color name to paint the range with. For example
  477. * 'red', '#ffcc00' or constants like goog.ui.Gauge.RED.
  478. */
  479. goog.ui.Gauge.prototype.addBackgroundColor = function(
  480. fromValue, toValue, color) {
  481. this.rangeColors_.push(
  482. new goog.ui.GaugeColoredRange(fromValue, toValue, color));
  483. this.draw_();
  484. };
  485. /**
  486. * Creates the DOM representation of the graphics area.
  487. * @override
  488. */
  489. goog.ui.Gauge.prototype.createDom = function() {
  490. this.setElementInternal(
  491. this.getDomHelper().createDom(
  492. goog.dom.TagName.DIV, goog.getCssName('goog-gauge'),
  493. this.graphics_.getElement()));
  494. };
  495. /**
  496. * Clears the entire graphics area.
  497. * @private
  498. */
  499. goog.ui.Gauge.prototype.clear_ = function() {
  500. this.graphics_.clear();
  501. this.needleGroup_ = null;
  502. };
  503. /**
  504. * Redraw the entire gauge.
  505. * @private
  506. */
  507. goog.ui.Gauge.prototype.draw_ = function() {
  508. if (!this.isInDocument()) {
  509. return;
  510. }
  511. this.clear_();
  512. var x, y;
  513. var size = Math.min(this.width_, this.height_);
  514. var r = Math.round(goog.ui.Gauge.FACTOR_RADIUS_FROM_SIZE * size);
  515. var cx = this.width_ / 2;
  516. var cy = this.height_ / 2;
  517. var theme = this.theme_;
  518. if (!theme) {
  519. // Lazy allocation of default theme, common to all instances
  520. theme = goog.ui.Gauge.prototype.theme_ = new goog.ui.GaugeTheme();
  521. }
  522. // Draw main circle frame around gauge
  523. var graphics = this.graphics_;
  524. var stroke = this.theme_.getExternalBorderStroke();
  525. var fill = theme.getExternalBorderFill(cx, cy, r);
  526. graphics.drawCircle(cx, cy, r, stroke, fill);
  527. r -= stroke.getWidth();
  528. r = Math.round(r * goog.ui.Gauge.FACTOR_MAIN_AREA);
  529. stroke = theme.getInternalBorderStroke();
  530. fill = theme.getInternalBorderFill(cx, cy, r);
  531. graphics.drawCircle(cx, cy, r, stroke, fill);
  532. r -= stroke.getWidth() * 2;
  533. // Draw Background with external and internal borders
  534. var rBackgroundInternal = r * goog.ui.Gauge.FACTOR_COLOR_RADIUS;
  535. for (var i = 0; i < this.rangeColors_.length; i++) {
  536. var rangeColor = this.rangeColors_[i];
  537. var fromValue = rangeColor.fromValue;
  538. var toValue = rangeColor.toValue;
  539. var path = new goog.graphics.Path();
  540. var fromAngle = this.valueToAngle_(fromValue);
  541. var toAngle = this.valueToAngle_(toValue);
  542. // Move to outer point at "from" angle
  543. path.moveTo(
  544. cx + goog.math.angleDx(fromAngle, r),
  545. cy + goog.math.angleDy(fromAngle, r));
  546. // Arc to outer point at "to" angle
  547. path.arcTo(r, r, fromAngle, toAngle - fromAngle);
  548. // Line to inner point at "to" angle
  549. path.lineTo(
  550. cx + goog.math.angleDx(toAngle, rBackgroundInternal),
  551. cy + goog.math.angleDy(toAngle, rBackgroundInternal));
  552. // Arc to inner point at "from" angle
  553. path.arcTo(
  554. rBackgroundInternal, rBackgroundInternal, toAngle, fromAngle - toAngle);
  555. path.close();
  556. fill = new goog.graphics.SolidFill(rangeColor.backgroundColor);
  557. graphics.drawPath(path, null, fill);
  558. }
  559. // Draw titles
  560. if (this.titleTop_ || this.titleBottom_) {
  561. var font = this.titleFont_;
  562. if (!font) {
  563. // Lazy creation of font
  564. var fontSize = Math.round(r * goog.ui.Gauge.FACTOR_TITLE_FONT_SIZE);
  565. font = new goog.graphics.Font(fontSize, goog.ui.Gauge.TITLE_FONT_NAME);
  566. this.titleFont_ = font;
  567. }
  568. fill = new goog.graphics.SolidFill(theme.getTitleColor());
  569. if (this.titleTop_) {
  570. y = cy - Math.round(r * goog.ui.Gauge.FACTOR_TITLE_OFFSET);
  571. graphics.drawTextOnLine(
  572. this.titleTop_, 0, y, this.width_, y, 'center', font, null, fill);
  573. }
  574. if (this.titleBottom_) {
  575. y = cy + Math.round(r * goog.ui.Gauge.FACTOR_TITLE_OFFSET);
  576. graphics.drawTextOnLine(
  577. this.titleBottom_, 0, y, this.width_, y, 'center', font, null, fill);
  578. }
  579. }
  580. // Draw tick marks
  581. var majorTicks = this.majorTicks_;
  582. var minorTicks = this.minorTicks_;
  583. var rMajorTickInternal = r * goog.ui.Gauge.FACTOR_MAJOR_TICKS;
  584. var rMinorTickInternal = r * goog.ui.Gauge.FACTOR_MINOR_TICKS;
  585. var ticks = majorTicks * minorTicks;
  586. var valueRange = this.maxValue_ - this.minValue_;
  587. var tickValueSpan = valueRange / ticks;
  588. var majorTicksPath = new goog.graphics.Path();
  589. var minorTicksPath = new goog.graphics.Path();
  590. var tickLabelFill = new goog.graphics.SolidFill(theme.getTickLabelColor());
  591. var tickLabelFont = this.tickLabelFont_;
  592. if (!tickLabelFont) {
  593. tickLabelFont = new goog.graphics.Font(
  594. Math.round(r * goog.ui.Gauge.FACTOR_TICK_LABEL_FONT_SIZE),
  595. goog.ui.Gauge.TITLE_FONT_NAME);
  596. }
  597. var tickLabelFontSize = tickLabelFont.size;
  598. for (var i = 0; i <= ticks; i++) {
  599. var angle = this.valueToAngle_(i * tickValueSpan + this.minValue_);
  600. var isMajorTick = i % minorTicks == 0;
  601. var rInternal = isMajorTick ? rMajorTickInternal : rMinorTickInternal;
  602. var path = isMajorTick ? majorTicksPath : minorTicksPath;
  603. x = cx + goog.math.angleDx(angle, rInternal);
  604. y = cy + goog.math.angleDy(angle, rInternal);
  605. path.moveTo(x, y);
  606. x = cx + goog.math.angleDx(angle, r);
  607. y = cy + goog.math.angleDy(angle, r);
  608. path.lineTo(x, y);
  609. // Draw the tick's label for major ticks
  610. if (isMajorTick && this.majorTickLabels_) {
  611. var tickIndex = Math.floor(i / minorTicks);
  612. var label = this.majorTickLabels_[tickIndex];
  613. if (label) {
  614. x = cx + goog.math.angleDx(angle, rInternal - tickLabelFontSize / 2);
  615. y = cy + goog.math.angleDy(angle, rInternal - tickLabelFontSize / 2);
  616. var x1, x2;
  617. var align = 'center';
  618. if (angle > 280 || angle < 90) {
  619. align = 'right';
  620. x1 = 0;
  621. x2 = x;
  622. } else if (angle >= 90 && angle < 260) {
  623. align = 'left';
  624. x1 = x;
  625. x2 = this.width_;
  626. } else {
  627. // Values around top (angle 260-280) are centered around point
  628. var dw = Math.min(x, this.width_ - x); // Nearest side border
  629. x1 = x - dw;
  630. x2 = x + dw;
  631. y += Math.round(tickLabelFontSize / 4); // Movea bit down
  632. }
  633. graphics.drawTextOnLine(
  634. label, x1, y, x2, y, align, tickLabelFont, null, tickLabelFill);
  635. }
  636. }
  637. }
  638. stroke = theme.getMinorTickStroke();
  639. graphics.drawPath(minorTicksPath, stroke, null);
  640. stroke = theme.getMajorTickStroke();
  641. graphics.drawPath(majorTicksPath, stroke, null);
  642. // Draw the needle and the value label. Stop animation when doing
  643. // full redraw and jump to the final value position.
  644. this.stopAnimation_();
  645. this.needleRadius_ = r;
  646. this.drawValue_();
  647. };
  648. /**
  649. * Handle animation events while the hand is moving.
  650. * @param {goog.fx.AnimationEvent} e The event.
  651. * @private
  652. */
  653. goog.ui.Gauge.prototype.onAnimate_ = function(e) {
  654. this.needleValuePosition_ = e.x;
  655. this.drawValue_();
  656. };
  657. /**
  658. * Handle animation events when hand move is complete.
  659. * @private
  660. */
  661. goog.ui.Gauge.prototype.onAnimateEnd_ = function() {
  662. this.stopAnimation_();
  663. };
  664. /**
  665. * Stop the current animation, if it is active.
  666. * @private
  667. */
  668. goog.ui.Gauge.prototype.stopAnimation_ = function() {
  669. if (this.animation_) {
  670. goog.events.removeAll(this.animation_);
  671. this.animation_.stop(false);
  672. this.animation_ = null;
  673. }
  674. };
  675. /**
  676. * Convert a value to the position in the range. The returned position
  677. * is a value between 0 and 1, where 0 indicates the lowest range value,
  678. * 1 is the highest range, and any value in between is proportional
  679. * to mapping the range to (0-1).
  680. * If the value is not within the range, the returned value may be a bit
  681. * lower than 0, or a bit higher than 1. This is done so that values out
  682. * of range will be displayed just a bit outside of the gauge axis.
  683. * @param {number} value The value to convert.
  684. * @private
  685. * @return {number} The range position.
  686. */
  687. goog.ui.Gauge.prototype.valueToRangePosition_ = function(value) {
  688. var valueRange = this.maxValue_ - this.minValue_;
  689. var valuePct = (value - this.minValue_) / valueRange; // 0 to 1
  690. // If value is out of range, trim it not to be too much out of range
  691. valuePct = Math.max(valuePct, -goog.ui.Gauge.MAX_EXCEED_POSITION_POSITION);
  692. valuePct = Math.min(valuePct, 1 + goog.ui.Gauge.MAX_EXCEED_POSITION_POSITION);
  693. return valuePct;
  694. };
  695. /**
  696. * Convert a value to an angle based on the value range and angle span
  697. * @param {number} value The value.
  698. * @return {number} The angle where this value is located on the round
  699. * axis, based on the range and angle span.
  700. * @private
  701. */
  702. goog.ui.Gauge.prototype.valueToAngle_ = function(value) {
  703. var valuePct = this.valueToRangePosition_(value);
  704. return this.valuePositionToAngle_(valuePct);
  705. };
  706. /**
  707. * Convert a value-position (percent in the range) to an angle based on
  708. * the angle span. A value-position is a value that has been proportinally
  709. * adjusted to a value betwwen 0-1, proportionaly to the range.
  710. * @param {number} valuePct The value.
  711. * @return {number} The angle where this value is located on the round
  712. * axis, based on the range and angle span.
  713. * @private
  714. */
  715. goog.ui.Gauge.prototype.valuePositionToAngle_ = function(valuePct) {
  716. var startAngle = goog.math.standardAngle((360 - this.angleSpan_) / 2 + 90);
  717. return this.angleSpan_ * valuePct + startAngle;
  718. };
  719. /**
  720. * Draw the elements that depend on the current value (the needle and
  721. * the formatted value). This function is called whenever a value is changed
  722. * or when the entire gauge is redrawn.
  723. * @private
  724. */
  725. goog.ui.Gauge.prototype.drawValue_ = function() {
  726. if (!this.isInDocument()) {
  727. return;
  728. }
  729. var r = this.needleRadius_;
  730. var graphics = this.graphics_;
  731. var theme = this.theme_;
  732. var cx = this.width_ / 2;
  733. var cy = this.height_ / 2;
  734. var angle = this.valuePositionToAngle_(
  735. /** @type {number} */ (this.needleValuePosition_));
  736. // Compute the needle path
  737. var frontRadius = Math.round(r * goog.ui.Gauge.FACTOR_NEEDLE_FRONT);
  738. var backRadius = Math.round(r * goog.ui.Gauge.FACTOR_NEEDLE_BACK);
  739. var frontDx = goog.math.angleDx(angle, frontRadius);
  740. var frontDy = goog.math.angleDy(angle, frontRadius);
  741. var backDx = goog.math.angleDx(angle, backRadius);
  742. var backDy = goog.math.angleDy(angle, backRadius);
  743. var angleRight = goog.math.standardAngle(angle + 90);
  744. var distanceControlPointBase = r * goog.ui.Gauge.FACTOR_NEEDLE_WIDTH;
  745. var controlPointMidDx =
  746. goog.math.angleDx(angleRight, distanceControlPointBase);
  747. var controlPointMidDy =
  748. goog.math.angleDy(angleRight, distanceControlPointBase);
  749. var path = new goog.graphics.Path();
  750. path.moveTo(cx + frontDx, cy + frontDy);
  751. path.curveTo(
  752. cx + controlPointMidDx, cy + controlPointMidDy,
  753. cx - backDx + (controlPointMidDx / 2),
  754. cy - backDy + (controlPointMidDy / 2), cx - backDx, cy - backDy);
  755. path.curveTo(
  756. cx - backDx - (controlPointMidDx / 2),
  757. cy - backDy - (controlPointMidDy / 2), cx - controlPointMidDx,
  758. cy - controlPointMidDy, cx + frontDx, cy + frontDy);
  759. // Draw the needle hinge
  760. var rh = Math.round(r * goog.ui.Gauge.FACTOR_NEEDLE_HINGE);
  761. // Clean previous needle
  762. var needleGroup = this.needleGroup_;
  763. if (needleGroup) {
  764. needleGroup.clear();
  765. } else {
  766. needleGroup = this.needleGroup_ = graphics.createGroup();
  767. }
  768. // Draw current formatted value if provided.
  769. if (this.formattedValue_) {
  770. var font = this.valueFont_;
  771. if (!font) {
  772. var fontSize = Math.round(r * goog.ui.Gauge.FACTOR_VALUE_FONT_SIZE);
  773. font = new goog.graphics.Font(fontSize, goog.ui.Gauge.TITLE_FONT_NAME);
  774. font.bold = true;
  775. this.valueFont_ = font;
  776. }
  777. var fill = new goog.graphics.SolidFill(theme.getValueColor());
  778. var y = cy + Math.round(r * goog.ui.Gauge.FACTOR_VALUE_OFFSET);
  779. graphics.drawTextOnLine(
  780. this.formattedValue_, 0, y, this.width_, y, 'center', font, null, fill,
  781. needleGroup);
  782. }
  783. // Draw the needle
  784. var stroke = theme.getNeedleStroke();
  785. var fill = theme.getNeedleFill(cx, cy, rh);
  786. graphics.drawPath(path, stroke, fill, needleGroup);
  787. stroke = theme.getHingeStroke();
  788. fill = theme.getHingeFill(cx, cy, rh);
  789. graphics.drawCircle(cx, cy, rh, stroke, fill, needleGroup);
  790. };
  791. /**
  792. * Redraws the entire gauge.
  793. * Should be called after theme colors have been changed.
  794. */
  795. goog.ui.Gauge.prototype.redraw = function() {
  796. this.draw_();
  797. };
  798. /** @override */
  799. goog.ui.Gauge.prototype.enterDocument = function() {
  800. goog.ui.Gauge.superClass_.enterDocument.call(this);
  801. // set roles and states
  802. var el = this.getElement();
  803. goog.asserts.assert(el, 'The DOM element for the gauge cannot be null.');
  804. goog.a11y.aria.setRole(el, 'progressbar');
  805. goog.a11y.aria.setState(el, 'live', 'polite');
  806. goog.a11y.aria.setState(el, 'valuemin', this.minValue_);
  807. goog.a11y.aria.setState(el, 'valuemax', this.maxValue_);
  808. goog.a11y.aria.setState(el, 'valuenow', this.value_);
  809. this.draw_();
  810. };
  811. /** @override */
  812. goog.ui.Gauge.prototype.exitDocument = function() {
  813. goog.ui.Gauge.superClass_.exitDocument.call(this);
  814. this.stopAnimation_();
  815. };
  816. /** @override */
  817. goog.ui.Gauge.prototype.disposeInternal = function() {
  818. this.stopAnimation_();
  819. this.graphics_.dispose();
  820. delete this.graphics_;
  821. delete this.needleGroup_;
  822. delete this.theme_;
  823. delete this.rangeColors_;
  824. goog.ui.Gauge.superClass_.disposeInternal.call(this);
  825. };