objects_chart_Gauge.js.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: objects/chart/Gauge.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: objects/chart/Gauge.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>import {Object2D} from "../../Object2D.js";
  20. import {ColorStyle} from "../style/ColorStyle";
  21. import {LinearGradientStyle} from "../style/LinearGradientStyle";
  22. /**
  23. * Gauge object is used to draw gauge like graphic.
  24. *
  25. * It has a defined range, start angle, end angle and style controls.
  26. *
  27. * @class
  28. * @extends {Object2D}
  29. */
  30. function Gauge()
  31. {
  32. Object2D.call(this);
  33. /**
  34. * Value displayed by this gauge. It is displayed based on min and max values.
  35. *
  36. * @type {number}
  37. */
  38. this.value = 50;
  39. /**
  40. * Minimum value of the gauge. Necessary to display the value correctly to scale.
  41. *
  42. * @type {number}
  43. */
  44. this.min = 0;
  45. /**
  46. * Maximum value of the gauge. Necessary to display the value correctly to scale.
  47. *
  48. * @type {number}
  49. */
  50. this.max = 100;
  51. /**
  52. * Radius of the gauge object.
  53. *
  54. * @type {number}
  55. */
  56. this.radius = 80;
  57. /**
  58. * The line width of the gauge semi-circle.
  59. *
  60. * @type {number}
  61. */
  62. this.lineWidth = 10;
  63. /**
  64. * Start angle of the gauge.
  65. *
  66. * @type {number}
  67. */
  68. this.startAngle = Math.PI;
  69. /**
  70. * End angle of the gauge.
  71. *
  72. * @type {number}
  73. */
  74. this.endAngle = 2 * Math.PI;
  75. /**
  76. * If true draw a circular dial at the end of the gauge bar.
  77. *
  78. * @type {boolean}
  79. */
  80. this.dial = false;
  81. /**
  82. * Style of the base of the gauge object, (the background of the gauge bar).
  83. *
  84. * @type {Style}
  85. */
  86. this.baseStyle = new ColorStyle("#e9ecf1");
  87. /**
  88. * Style of the gauge bar.
  89. *
  90. * @type {Style}
  91. */
  92. this.barStyle = new LinearGradientStyle();
  93. this.barStyle.start.set(-100, 0);
  94. this.barStyle.end.set(100, 0);
  95. this.barStyle.addColorStop(0, "#e5ff50");
  96. this.barStyle.addColorStop(0.5, "#50ff67");
  97. this.barStyle.addColorStop(1, "#32adff");
  98. }
  99. Gauge.prototype = Object.create(Object2D.prototype);
  100. Gauge.prototype.constructor = Gauge;
  101. Gauge.prototype.type = "Gauge";
  102. Object2D.register(Gauge, "Gauge");
  103. Gauge.prototype.isInside = function(point)
  104. {
  105. return point.length() &lt;= this.radius;
  106. };
  107. Gauge.prototype.draw = function(context, viewport, canvas)
  108. {
  109. var percentage = this.value / (this.max - this.min);
  110. var range = [this.startAngle, this.endAngle];
  111. var diff = range[1] - range[0];
  112. var angle = range[0] + diff * percentage;
  113. var center = [0, 0];
  114. //Back
  115. context.lineWidth = this.lineWidth;
  116. context.lineCap = "round";
  117. context.strokeStyle = this.baseStyle.get(context);
  118. context.beginPath();
  119. context.arc(center[0], center[1], this.radius, range[0], range[1]);
  120. context.stroke();
  121. // Fill gradient
  122. var gradient = context.createLinearGradient(-this.radius, 0, this.radius, 0);
  123. context.strokeStyle = this.barStyle.get(context);
  124. context.lineWidth = this.lineWidth;
  125. context.beginPath();
  126. context.arc(center[0], center[1], this.radius, range[0], angle);
  127. context.stroke();
  128. if(this.dial)
  129. {
  130. var dialAngle = (this.startAngle - this.endAngle) * percentage;
  131. var dialCenter = [Math.cos(dialAngle) * this.radius, Math.sin(dialAngle) * this.radius];
  132. dialCenter[0] = dialCenter[0] - center[0];
  133. dialCenter[1] = dialCenter[1] - center[1];
  134. context.fillStyle = "#FFFFFF";
  135. context.beginPath();
  136. context.arc(dialCenter[0], dialCenter[1], this.lineWidth / 2, 0, 2 * Math.PI);
  137. context.fill();
  138. context.fillStyle = gradient;
  139. context.beginPath();
  140. context.arc(dialCenter[0], dialCenter[1], this.lineWidth / 3, 0, 2 * Math.PI);
  141. context.fill();
  142. }
  143. };
  144. Gauge.prototype.serialize = function(recursive)
  145. {
  146. var data = Object2D.prototype.serialize.call(this, recursive);
  147. data.value = this.value;
  148. data.min = this.min;
  149. data.max = this.max;
  150. data.radius = this.radius;
  151. data.lineWidth = this.lineWidth;
  152. data.startAngle = this.startAngle;
  153. data.endAngle = this.endAngle;
  154. data.dial = this.dial;
  155. data.baseStyle = this.baseStyle !== null ? this.baseStyle.serialize() : null;
  156. data.barStyle = this.barStyle !== null ? this.barStyle.serialize() : null;
  157. return data;
  158. };
  159. Gauge.prototype.parse = function(data, root)
  160. {
  161. Object2D.prototype.parse.call(this, data, root);
  162. this.value = data.value;
  163. this.min = data.min;
  164. this.max = data.max;
  165. this.radius = data.radius;
  166. this.lineWidth = data.lineWidth;
  167. this.startAngle = data.startAngle;
  168. this.endAngle = data.endAngle;
  169. this.dial = data.dial;
  170. this.baseStyle = data.baseStyle !== null ? Style.parse(data.baseStyle) : null;
  171. this.barStyle = data.barStyle !== null ? Style.parse(data.barStyle) : null;
  172. };
  173. export {Gauge};
  174. </code></pre>
  175. </article>
  176. </section>
  177. </div>
  178. <nav>
  179. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="AnimationTimer.html">AnimationTimer</a></li><li><a href="BarGraph.html">BarGraph</a></li><li><a href="BezierCurve.html">BezierCurve</a></li><li><a href="Box.html">Box</a></li><li><a href="Box2.html">Box2</a></li><li><a href="BoxMask.html">BoxMask</a></li><li><a href="Circle.html">Circle</a></li><li><a href="ColorStyle.html">ColorStyle</a></li><li><a href="DOM.html">DOM</a></li><li><a href="EventManager.html">EventManager</a></li><li><a href="FileUtils.html">FileUtils</a></li><li><a href="Gauge.html">Gauge</a></li><li><a href="GradientColorStop.html">GradientColorStop</a></li><li><a href="GradientStyle.html">GradientStyle</a></li><li><a href="Graph.html">Graph</a></li><li><a href="Helpers.html">Helpers</a></li><li><a href="Image.html">Image</a></li><li><a href="Key.html">Key</a></li><li><a href="Line.html">Line</a></li><li><a href="LinearGradientStyle.html">LinearGradientStyle</a></li><li><a href="Mask.html">Mask</a></li><li><a href="Matrix.html">Matrix</a></li><li><a href="MultiLineText.html">MultiLineText</a></li><li><a href="Node.html">Node</a></li><li><a href="NodeConnector.html">NodeConnector</a></li><li><a href="NodeGraph.html">NodeGraph</a></li><li><a href="NodeSocket.html">NodeSocket</a></li><li><a href="Object2D.html">Object2D</a></li><li><a href="Path.html">Path</a></li><li><a href="Pattern.html">Pattern</a></li><li><a href="PatternStyle.html">PatternStyle</a></li><li><a href="PieChart.html">PieChart</a></li><li><a href="Pointer.html">Pointer</a></li><li><a href="QuadraticCurve.html">QuadraticCurve</a></li><li><a href="RadialGradientStyle.html">RadialGradientStyle</a></li><li><a href="Renderer.html">Renderer</a></li><li><a href="RoundedBox.html">RoundedBox</a></li><li><a href="ScatterGraph.html">ScatterGraph</a></li><li><a href="Style.html">Style</a></li><li><a href="Text.html">Text</a></li><li><a href="UUID.html">UUID</a></li><li><a href="Vector2.html">Vector2</a></li><li><a href="Viewport.html">Viewport</a></li><li><a href="ViewportControls.html">ViewportControls</a></li></ul><h3>Global</h3><ul><li><a href="global.html#writeFile">writeFile</a></li></ul>
  180. </nav>
  181. <br class="clear">
  182. <footer>
  183. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Sat Sep 17 2022 14:24:36 GMT+0100 (Hora de verão da Europa Ocidental)
  184. </footer>
  185. <script> prettyPrint(); </script>
  186. <script src="scripts/linenumber.js"> </script>
  187. </body>
  188. </html>