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