Gauge.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import {Object2D} from "../../Object2D.js";
  2. import {ColorStyle} from "../style/ColorStyle";
  3. import {LinearGradientStyle} from "../style/LinearGradientStyle";
  4. /**
  5. * Gauge object is used to draw gauge like graphic.
  6. *
  7. * It has a defined range, start angle, end angle and style controls.
  8. *
  9. * @class
  10. * @extends {Object2D}
  11. */
  12. function Gauge()
  13. {
  14. Object2D.call(this);
  15. /**
  16. * Value displayed by this gauge. It is displayed based on min and max values.
  17. *
  18. * @type {number}
  19. */
  20. this.value = 50;
  21. /**
  22. * Minimum value of the gauge. Necessary to display the value correctly to scale.
  23. *
  24. * @type {number}
  25. */
  26. this.min = 0;
  27. /**
  28. * Maximum value of the gauge. Necessary to display the value correctly to scale.
  29. *
  30. * @type {number}
  31. */
  32. this.max = 100;
  33. /**
  34. * Radius of the gauge object.
  35. *
  36. * @type {number}
  37. */
  38. this.radius = 80;
  39. /**
  40. * The line width of the gauge semi-circle.
  41. *
  42. * @type {number}
  43. */
  44. this.lineWidth = 10;
  45. /**
  46. * Start angle of the gauge.
  47. *
  48. * @type {number}
  49. */
  50. this.startAngle = Math.PI;
  51. /**
  52. * End angle of the gauge.
  53. *
  54. * @type {number}
  55. */
  56. this.endAngle = 2 * Math.PI;
  57. /**
  58. * If true draw a circular dial at the end of the gauge bar.
  59. *
  60. * @type {boolean}
  61. */
  62. this.dial = false;
  63. /**
  64. * Style of the base of the gauge object, (the background of the gauge bar).
  65. *
  66. * @type {Style}
  67. */
  68. this.baseStyle = new ColorStyle("#e9ecf1");
  69. /**
  70. * Style of the gauge bar.
  71. *
  72. * @type {Style}
  73. */
  74. this.barStyle = new LinearGradientStyle();
  75. this.barStyle.start.set(-100, 0);
  76. this.barStyle.end.set(100, 0);
  77. this.barStyle.addColorStop(0, "#e5ff50");
  78. this.barStyle.addColorStop(0.5, "#50ff67");
  79. this.barStyle.addColorStop(1, "#32adff");
  80. }
  81. Gauge.prototype = Object.create(Object2D.prototype);
  82. Gauge.prototype.constructor = Gauge;
  83. Gauge.prototype.type = "Gauge";
  84. Object2D.register(Gauge, "Gauge");
  85. Gauge.prototype.isInside = function(point)
  86. {
  87. return point.length() <= this.radius;
  88. };
  89. Gauge.prototype.draw = function(context, viewport, canvas)
  90. {
  91. var percentage = this.value / (this.max - this.min);
  92. var range = [this.startAngle, this.endAngle];
  93. var diff = range[1] - range[0];
  94. var angle = range[0] + diff * percentage;
  95. var center = [0, 0];
  96. //Back
  97. context.lineWidth = this.lineWidth;
  98. context.lineCap = "round";
  99. context.strokeStyle = this.baseStyle.get(context);
  100. context.beginPath();
  101. context.arc(center[0], center[1], this.radius, range[0], range[1]);
  102. context.stroke();
  103. // Fill gradient
  104. var gradient = context.createLinearGradient(-this.radius, 0, this.radius, 0);
  105. context.strokeStyle = this.barStyle.get(context);
  106. context.lineWidth = this.lineWidth;
  107. context.beginPath();
  108. context.arc(center[0], center[1], this.radius, range[0], angle);
  109. context.stroke();
  110. if(this.dial)
  111. {
  112. var dialAngle = (this.startAngle - this.endAngle) * percentage;
  113. var dialCenter = [Math.cos(dialAngle) * this.radius, Math.sin(dialAngle) * this.radius];
  114. dialCenter[0] = dialCenter[0] - center[0];
  115. dialCenter[1] = dialCenter[1] - center[1];
  116. context.fillStyle = "#FFFFFF";
  117. context.beginPath();
  118. context.arc(dialCenter[0], dialCenter[1], this.lineWidth / 2, 0, 2 * Math.PI);
  119. context.fill();
  120. context.fillStyle = gradient;
  121. context.beginPath();
  122. context.arc(dialCenter[0], dialCenter[1], this.lineWidth / 3, 0, 2 * Math.PI);
  123. context.fill();
  124. }
  125. };
  126. Gauge.prototype.serialize = function(recursive)
  127. {
  128. var data = Object2D.prototype.serialize.call(this, recursive);
  129. data.value = this.value;
  130. data.min = this.min;
  131. data.max = this.max;
  132. data.radius = this.radius;
  133. data.lineWidth = this.lineWidth;
  134. data.startAngle = this.startAngle;
  135. data.endAngle = this.endAngle;
  136. data.dial = this.dial;
  137. data.baseStyle = this.baseStyle !== null ? this.baseStyle.serialize() : null;
  138. data.barStyle = this.barStyle !== null ? this.barStyle.serialize() : null;
  139. return data;
  140. };
  141. Gauge.prototype.parse = function(data, root)
  142. {
  143. Object2D.prototype.parse.call(this, data, root);
  144. this.value = data.value;
  145. this.min = data.min;
  146. this.max = data.max;
  147. this.radius = data.radius;
  148. this.lineWidth = data.lineWidth;
  149. this.startAngle = data.startAngle;
  150. this.endAngle = data.endAngle;
  151. this.dial = data.dial;
  152. this.baseStyle = data.baseStyle !== null ? Style.parse(data.baseStyle) : null;
  153. this.barStyle = data.barStyle !== null ? Style.parse(data.barStyle) : null;
  154. };
  155. export {Gauge};