Mask.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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-Mask'>/**
  19. </span> * Defines a mask for a chart's series.
  20. * The 'chart' member must be set prior to rendering.
  21. *
  22. * A Mask can be used to select a certain region in a chart.
  23. * When enabled, the `select` event will be triggered when a
  24. * region is selected by the mask, allowing the user to perform
  25. * other tasks like zooming on that region, etc.
  26. *
  27. * In order to use the mask one has to set the Chart `mask` option to
  28. * `true`, `vertical` or `horizontal`. Then a possible configuration for the
  29. * listener could be:
  30. *
  31. * items: {
  32. * xtype: 'chart',
  33. * animate: true,
  34. * store: store1,
  35. * mask: 'horizontal',
  36. * listeners: {
  37. * select: {
  38. * fn: function(me, selection) {
  39. * me.setZoom(selection);
  40. * me.mask.hide();
  41. * }
  42. * }
  43. * }
  44. * }
  45. *
  46. * In this example we zoom the chart to that particular region. You can also get
  47. * a handle to a mask instance from the chart object. The `chart.mask` element is a
  48. * `Ext.Panel`.
  49. *
  50. */
  51. Ext.define('Ext.chart.Mask', {
  52. requires: [
  53. 'Ext.chart.MaskLayer'
  54. ],
  55. <span id='Ext-chart-Mask-cfg-mask'> /**
  56. </span> * @cfg {Boolean/String} mask
  57. * Enables selecting a region on chart. True to enable any selection,
  58. * 'horizontal' or 'vertical' to restrict the selection to X or Y axis.
  59. *
  60. * The mask in itself will do nothing but fire 'select' event.
  61. * See {@link Ext.chart.Mask} for example.
  62. */
  63. <span id='Ext-chart-Mask-method-constructor'> /**
  64. </span> * Creates new Mask.
  65. * @param {Object} [config] Config object.
  66. */
  67. constructor: function(config) {
  68. var me = this,
  69. resizeHandler;
  70. me.addEvents('select');
  71. if (config) {
  72. Ext.apply(me, config);
  73. }
  74. if (me.enableMask) {
  75. me.on('afterrender', function() {
  76. //create a mask layer component
  77. var comp = new Ext.chart.MaskLayer({
  78. renderTo: me.el,
  79. hidden: true
  80. });
  81. comp.el.on({
  82. 'mousemove': function(e) {
  83. me.onMouseMove(e);
  84. },
  85. 'mouseup': function(e) {
  86. me.resized(e);
  87. }
  88. });
  89. //create a resize handler for the component
  90. resizeHandler = new Ext.resizer.Resizer({
  91. el: comp.el,
  92. handles: 'all',
  93. pinned: true
  94. });
  95. resizeHandler.on({
  96. 'resize': function(e) {
  97. me.resized(e);
  98. }
  99. });
  100. comp.initDraggable();
  101. me.maskType = me.mask;
  102. me.mask = comp;
  103. me.maskSprite = me.surface.add({
  104. type: 'path',
  105. path: ['M', 0, 0],
  106. zIndex: 1001,
  107. opacity: 0.7,
  108. hidden: true,
  109. stroke: '#444'
  110. });
  111. }, me, { single: true });
  112. }
  113. },
  114. resized: function(e) {
  115. var me = this,
  116. bbox = me.bbox || me.chartBBox,
  117. x = bbox.x,
  118. y = bbox.y,
  119. width = bbox.width,
  120. height = bbox.height,
  121. box = me.mask.getBox(true),
  122. max = Math.max,
  123. min = Math.min,
  124. staticX = box.x - x,
  125. staticY = box.y - y;
  126. staticX = max(staticX, x);
  127. staticY = max(staticY, y);
  128. staticX = min(staticX, width);
  129. staticY = min(staticY, height);
  130. box.x = staticX;
  131. box.y = staticY;
  132. me.fireEvent('select', me, box);
  133. },
  134. onMouseUp: function(e) {
  135. var me = this,
  136. bbox = me.bbox || me.chartBBox,
  137. sel = me.maskSelection;
  138. me.maskMouseDown = false;
  139. me.mouseDown = false;
  140. if (me.mouseMoved) {
  141. me.onMouseMove(e);
  142. me.mouseMoved = false;
  143. me.fireEvent('select', me, {
  144. x: sel.x - bbox.x,
  145. y: sel.y - bbox.y,
  146. width: sel.width,
  147. height: sel.height
  148. });
  149. }
  150. },
  151. onMouseDown: function(e) {
  152. var me = this;
  153. me.mouseDown = true;
  154. me.mouseMoved = false;
  155. me.maskMouseDown = {
  156. x: e.getPageX() - me.el.getX(),
  157. y: e.getPageY() - me.el.getY()
  158. };
  159. },
  160. onMouseMove: function(e) {
  161. var me = this,
  162. mask = me.maskType,
  163. bbox = me.bbox || me.chartBBox,
  164. x = bbox.x,
  165. y = bbox.y,
  166. math = Math,
  167. floor = math.floor,
  168. abs = math.abs,
  169. min = math.min,
  170. max = math.max,
  171. height = floor(y + bbox.height),
  172. width = floor(x + bbox.width),
  173. posX = e.getPageX(),
  174. posY = e.getPageY(),
  175. staticX = posX - me.el.getX(),
  176. staticY = posY - me.el.getY(),
  177. maskMouseDown = me.maskMouseDown,
  178. path;
  179. me.mouseMoved = me.mouseDown;
  180. staticX = max(staticX, x);
  181. staticY = max(staticY, y);
  182. staticX = min(staticX, width);
  183. staticY = min(staticY, height);
  184. if (maskMouseDown &amp;&amp; me.mouseDown) {
  185. if (mask == 'horizontal') {
  186. staticY = y;
  187. maskMouseDown.y = height;
  188. posY = me.el.getY() + bbox.height + me.insetPadding;
  189. }
  190. else if (mask == 'vertical') {
  191. staticX = x;
  192. maskMouseDown.x = width;
  193. }
  194. width = maskMouseDown.x - staticX;
  195. height = maskMouseDown.y - staticY;
  196. path = ['M', staticX, staticY, 'l', width, 0, 0, height, -width, 0, 'z'];
  197. me.maskSelection = {
  198. x: width &gt; 0 ? staticX : staticX + width,
  199. y: height &gt; 0 ? staticY : staticY + height,
  200. width: abs(width),
  201. height: abs(height)
  202. };
  203. me.mask.updateBox(me.maskSelection);
  204. me.mask.show();
  205. me.maskSprite.setAttributes({
  206. hidden: true
  207. }, true);
  208. }
  209. else {
  210. if (mask == 'horizontal') {
  211. path = ['M', staticX, y, 'L', staticX, height];
  212. }
  213. else if (mask == 'vertical') {
  214. path = ['M', x, staticY, 'L', width, staticY];
  215. }
  216. else {
  217. path = ['M', staticX, y, 'L', staticX, height, 'M', x, staticY, 'L', width, staticY];
  218. }
  219. me.maskSprite.setAttributes({
  220. path: path,
  221. fill: me.maskMouseDown ? me.maskSprite.stroke : false,
  222. 'stroke-width': mask === true ? 1 : 3,
  223. hidden: false
  224. }, true);
  225. }
  226. },
  227. onMouseLeave: function(e) {
  228. var me = this;
  229. me.mouseMoved = false;
  230. me.mouseDown = false;
  231. me.maskMouseDown = false;
  232. me.mask.hide();
  233. me.maskSprite.hide(true);
  234. }
  235. });
  236. </pre>
  237. </body>
  238. </html>