Gauge.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-chart-axis-Gauge'>/**
  19. </span> * @class Ext.chart.axis.Gauge
  20. *
  21. * Gauge Axis is the axis to be used with a Gauge series. The Gauge axis
  22. * displays numeric data from an interval defined by the `minimum`, `maximum` and
  23. * `step` configuration properties. The placement of the numeric data can be changed
  24. * by altering the `margin` option that is set to `10` by default.
  25. *
  26. * A possible configuration for this axis would look like:
  27. *
  28. * axes: [{
  29. * type: 'gauge',
  30. * position: 'gauge',
  31. * minimum: 0,
  32. * maximum: 100,
  33. * steps: 10,
  34. * margin: 7
  35. * }],
  36. */
  37. Ext.define('Ext.chart.axis.Gauge', {
  38. /* Begin Definitions */
  39. extend: 'Ext.chart.axis.Abstract',
  40. /* End Definitions */
  41. <span id='Ext-chart-axis-Gauge-cfg-minimum'> /**
  42. </span> * @cfg {Number} minimum (required)
  43. * The minimum value of the interval to be displayed in the axis.
  44. */
  45. <span id='Ext-chart-axis-Gauge-cfg-maximum'> /**
  46. </span> * @cfg {Number} maximum (required)
  47. * The maximum value of the interval to be displayed in the axis.
  48. */
  49. <span id='Ext-chart-axis-Gauge-cfg-steps'> /**
  50. </span> * @cfg {Number} steps (required)
  51. * The number of steps and tick marks to add to the interval.
  52. */
  53. <span id='Ext-chart-axis-Gauge-cfg-margin'> /**
  54. </span> * @cfg {Number} [margin=10]
  55. * The offset positioning of the tick marks and labels in pixels.
  56. */
  57. <span id='Ext-chart-axis-Gauge-cfg-title'> /**
  58. </span> * @cfg {String} title
  59. * The title for the Axis.
  60. */
  61. position: 'gauge',
  62. alias: 'axis.gauge',
  63. drawAxis: function(init) {
  64. var chart = this.chart,
  65. surface = chart.surface,
  66. bbox = chart.chartBBox,
  67. centerX = bbox.x + (bbox.width / 2),
  68. centerY = bbox.y + bbox.height,
  69. margin = this.margin || 10,
  70. rho = Math.min(bbox.width, 2 * bbox.height) /2 + margin,
  71. sprites = [], sprite,
  72. steps = this.steps,
  73. i, pi = Math.PI,
  74. cos = Math.cos,
  75. sin = Math.sin;
  76. if (this.sprites &amp;&amp; !chart.resizing) {
  77. this.drawLabel();
  78. return;
  79. }
  80. if (this.margin &gt;= 0) {
  81. if (!this.sprites) {
  82. //draw circles
  83. for (i = 0; i &lt;= steps; i++) {
  84. sprite = surface.add({
  85. type: 'path',
  86. path: ['M', centerX + (rho - margin) * cos(i / steps * pi - pi),
  87. centerY + (rho - margin) * sin(i / steps * pi - pi),
  88. 'L', centerX + rho * cos(i / steps * pi - pi),
  89. centerY + rho * sin(i / steps * pi - pi), 'Z'],
  90. stroke: '#ccc'
  91. });
  92. sprite.setAttributes({
  93. hidden: false
  94. }, true);
  95. sprites.push(sprite);
  96. }
  97. } else {
  98. sprites = this.sprites;
  99. //draw circles
  100. for (i = 0; i &lt;= steps; i++) {
  101. sprites[i].setAttributes({
  102. path: ['M', centerX + (rho - margin) * cos(i / steps * pi - pi),
  103. centerY + (rho - margin) * sin(i / steps * pi - pi),
  104. 'L', centerX + rho * cos(i / steps * pi - pi),
  105. centerY + rho * sin(i / steps * pi - pi), 'Z'],
  106. stroke: '#ccc'
  107. }, true);
  108. }
  109. }
  110. }
  111. this.sprites = sprites;
  112. this.drawLabel();
  113. if (this.title) {
  114. this.drawTitle();
  115. }
  116. },
  117. drawTitle: function() {
  118. var me = this,
  119. chart = me.chart,
  120. surface = chart.surface,
  121. bbox = chart.chartBBox,
  122. labelSprite = me.titleSprite,
  123. labelBBox;
  124. if (!labelSprite) {
  125. me.titleSprite = labelSprite = surface.add({
  126. type: 'text',
  127. zIndex: 2
  128. });
  129. }
  130. labelSprite.setAttributes(Ext.apply({
  131. text: me.title
  132. }, me.label || {}), true);
  133. labelBBox = labelSprite.getBBox();
  134. labelSprite.setAttributes({
  135. x: bbox.x + (bbox.width / 2) - (labelBBox.width / 2),
  136. y: bbox.y + bbox.height - (labelBBox.height / 2) - 4
  137. }, true);
  138. },
  139. <span id='Ext-chart-axis-Gauge-method-setTitle'> /**
  140. </span> * Updates the {@link #title} of this axis.
  141. * @param {String} title
  142. */
  143. setTitle: function(title) {
  144. this.title = title;
  145. this.drawTitle();
  146. },
  147. drawLabel: function() {
  148. var chart = this.chart,
  149. surface = chart.surface,
  150. bbox = chart.chartBBox,
  151. centerX = bbox.x + (bbox.width / 2),
  152. centerY = bbox.y + bbox.height,
  153. margin = this.margin || 10,
  154. rho = Math.min(bbox.width, 2 * bbox.height) /2 + 2 * margin,
  155. round = Math.round,
  156. labelArray = [], label,
  157. maxValue = this.maximum || 0,
  158. minValue = this.minimum || 0,
  159. steps = this.steps, i = 0,
  160. adjY,
  161. pi = Math.PI,
  162. cos = Math.cos,
  163. sin = Math.sin,
  164. labelConf = this.label,
  165. renderer = labelConf.renderer || function(v) { return v; };
  166. if (!this.labelArray) {
  167. //draw scale
  168. for (i = 0; i &lt;= steps; i++) {
  169. // TODO Adjust for height of text / 2 instead
  170. adjY = (i === 0 || i === steps) ? 7 : 0;
  171. label = surface.add({
  172. type: 'text',
  173. text: renderer(round(minValue + i / steps * (maxValue - minValue))),
  174. x: centerX + rho * cos(i / steps * pi - pi),
  175. y: centerY + rho * sin(i / steps * pi - pi) - adjY,
  176. 'text-anchor': 'middle',
  177. 'stroke-width': 0.2,
  178. zIndex: 10,
  179. stroke: '#333'
  180. });
  181. label.setAttributes({
  182. hidden: false
  183. }, true);
  184. labelArray.push(label);
  185. }
  186. }
  187. else {
  188. labelArray = this.labelArray;
  189. //draw values
  190. for (i = 0; i &lt;= steps; i++) {
  191. // TODO Adjust for height of text / 2 instead
  192. adjY = (i === 0 || i === steps) ? 7 : 0;
  193. labelArray[i].setAttributes({
  194. text: renderer(round(minValue + i / steps * (maxValue - minValue))),
  195. x: centerX + rho * cos(i / steps * pi - pi),
  196. y: centerY + rho * sin(i / steps * pi - pi) - adjY
  197. }, true);
  198. }
  199. }
  200. this.labelArray = labelArray;
  201. }
  202. });</pre>
  203. </body>
  204. </html>