UNPKG

12 kBJavaScriptView Raw
1(function webpackUniversalModuleDefinition(root, factory) {
2 if(typeof exports === 'object' && typeof module === 'object')
3 module.exports = factory();
4 else if(typeof define === 'function' && define.amd)
5 define([], factory);
6 else if(typeof exports === 'object')
7 exports["Brush"] = factory();
8 else
9 root["Brush"] = factory();
10})(typeof self !== 'undefined' ? self : this, function() {
11return /******/ (function(modules) { // webpackBootstrap
12/******/ // The module cache
13/******/ var installedModules = {};
14/******/
15/******/ // The require function
16/******/ function __webpack_require__(moduleId) {
17/******/
18/******/ // Check if module is in cache
19/******/ if(installedModules[moduleId]) {
20/******/ return installedModules[moduleId].exports;
21/******/ }
22/******/ // Create a new module (and put it into the cache)
23/******/ var module = installedModules[moduleId] = {
24/******/ i: moduleId,
25/******/ l: false,
26/******/ exports: {}
27/******/ };
28/******/
29/******/ // Execute the module function
30/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
31/******/
32/******/ // Flag the module as loaded
33/******/ module.l = true;
34/******/
35/******/ // Return the exports of the module
36/******/ return module.exports;
37/******/ }
38/******/
39/******/
40/******/ // expose the modules object (__webpack_modules__)
41/******/ __webpack_require__.m = modules;
42/******/
43/******/ // expose the module cache
44/******/ __webpack_require__.c = installedModules;
45/******/
46/******/ // define getter function for harmony exports
47/******/ __webpack_require__.d = function(exports, name, getter) {
48/******/ if(!__webpack_require__.o(exports, name)) {
49/******/ Object.defineProperty(exports, name, {
50/******/ configurable: false,
51/******/ enumerable: true,
52/******/ get: getter
53/******/ });
54/******/ }
55/******/ };
56/******/
57/******/ // getDefaultExport function for compatibility with non-harmony modules
58/******/ __webpack_require__.n = function(module) {
59/******/ var getter = module && module.__esModule ?
60/******/ function getDefault() { return module['default']; } :
61/******/ function getModuleExports() { return module; };
62/******/ __webpack_require__.d(getter, 'a', getter);
63/******/ return getter;
64/******/ };
65/******/
66/******/ // Object.prototype.hasOwnProperty.call
67/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
68/******/
69/******/ // __webpack_public_path__
70/******/ __webpack_require__.p = "";
71/******/
72/******/ // Load entry module and return exports
73/******/ return __webpack_require__(__webpack_require__.s = 0);
74/******/ })
75/************************************************************************/
76/******/ ([
77/* 0 */
78/***/ (function(module, exports, __webpack_require__) {
79
80/**
81 * [exports description]
82 * @type {Object}
83 */
84var Brush = __webpack_require__(1);
85module.exports = Brush;
86
87/***/ }),
88/* 1 */
89/***/ (function(module, exports, __webpack_require__) {
90
91function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
92
93/**
94 * [exports description]
95 * @type {Object}
96 */
97var Util = __webpack_require__(2);
98
99var Brush = function () {
100 function Brush(chart, type) {
101 _classCallCheck(this, Brush);
102
103 this.startPoint = null;
104 this.isBrushing = false;
105 this.brushShape = null;
106 this.container = null;
107 this.polygonPath = null;
108 this.polygonPoints = null;
109
110 this.type = type || 'XY';
111 this.chart = chart;
112 // TODO
113 this.canvas = chart.get('canvas');
114 this.plotRange = chart.get('plotRange');
115 this.frontPlot = chart.get('frontPlot');
116
117 this.bindEvent();
118 }
119
120 Brush.prototype.bindEvent = function bindEvent() {
121 var me = this;
122 var chart = me.chart,
123 frontPlot = me.frontPlot,
124 type = me.type;
125
126
127 chart.on('mousedown', function (ev) {
128 var x = ev.x,
129 y = ev.y;
130
131 var container = me.container;
132 me.startPoint = {
133 x: x,
134 y: y
135 };
136 me.isBrushing = true; // 开始框选
137 if (!container) {
138 container = frontPlot.addGroup();
139 container.initTransform();
140 } else {
141 container.clear();
142 }
143 me.container = container;
144
145 if (type === 'polygon') {
146 // 不规则筛选
147 me.polygonPoints = [];
148 me.polygonPath = 'M ' + x + ' ' + y;
149 me.polygonPoints.push([x, y]);
150 }
151 // 这里抛出 brush start 事件
152
153 // const originEvent = ev.event;
154 // originEvent.stopPropagation();
155 // originEvent.preventDefault();
156 me._bindCanvasEvent();
157 });
158 };
159
160 Brush.prototype._bindCanvasEvent = function _bindCanvasEvent() {
161 var canvas = this.canvas;
162
163 var canvasDOM = canvas.get('canvasDOM');
164 // canvasDOM.addEventListener('mousemove', Util.wrapBehavior(this, '_onCanvasMouseMove'), false);
165 // canvasDOM.addEventListener('mouseup', Util.wrapBehavior(this, '_onCanvasMouseUp'), false);
166 this.onMouseMoveListener = Util.addEventListener(canvasDOM, 'mousemove', Util.wrapBehavior(this, '_onCanvasMouseMove'));
167 this.onMouseUpListener = Util.addEventListener(canvasDOM, 'mouseup', Util.wrapBehavior(this, '_onCanvasMouseUp'));
168 };
169
170 Brush.prototype._limitCoordScope = function _limitCoordScope(point) {
171 var plotRange = this.plotRange;
172 var tl = plotRange.tl,
173 br = plotRange.br;
174
175
176 if (point.x < tl.x) {
177 point.x = tl.x;
178 }
179 if (point.x > br.x) {
180 point.x = br.x;
181 }
182 if (point.y < tl.y) {
183 point.y = tl.y;
184 }
185 if (point.y > br.y) {
186 point.y = br.y;
187 }
188 return point;
189 };
190
191 Brush.prototype._onCanvasMouseMove = function _onCanvasMouseMove(ev) {
192 var me = this;
193 var isBrushing = me.isBrushing,
194 type = me.type,
195 plotRange = me.plotRange,
196 startPoint = me.startPoint;
197
198 if (!isBrushing) {
199 return;
200 }
201 var canvas = me.canvas;
202 var tl = plotRange.tl,
203 tr = plotRange.tr,
204 bl = plotRange.bl;
205
206 var polygonPath = me.polygonPath;
207 var polygonPoints = me.polygonPoints;
208 var brushShape = me.brushShape;
209 var container = me.container;
210 var pointX = ev.offsetX;
211 var pointY = ev.offsetY;
212 var currentPoint = me._limitCoordScope({
213 x: pointX,
214 y: pointY
215 });
216 var rectStartX = void 0;
217 var rectStartY = void 0;
218 var rectWidth = void 0;
219 var rectHeight = void 0;
220
221 if (type === 'Y') {
222 rectStartX = tl.x;
223 rectStartY = currentPoint.y >= startPoint.y ? startPoint.y : currentPoint.y;
224 rectWidth = Math.abs(tl.x - tr.x);
225 rectHeight = Math.abs(startPoint.y - currentPoint.y);
226 } else if (type === 'X') {
227 rectStartX = currentPoint.x >= startPoint.x ? startPoint.x : currentPoint.x;
228 rectStartY = tl.y;
229 rectWidth = Math.abs(startPoint.x - currentPoint.x);
230 rectHeight = Math.abs(tl.y - bl.y);
231 } else if (type === 'XY') {
232 if (currentPoint.x >= startPoint.x) {
233 rectStartX = startPoint.x;
234 rectStartY = pointY >= startPoint.y ? startPoint.y : currentPoint.y;
235 } else {
236 rectStartX = currentPoint.x;
237 rectStartY = currentPoint.y >= startPoint.y ? startPoint.y : currentPoint.y;
238 }
239 rectWidth = Math.abs(startPoint.x - currentPoint.x);
240 rectHeight = Math.abs(startPoint.y - currentPoint.y);
241 } else if (type === 'polygon') {
242 // 不规则框选
243 polygonPath += 'L ' + pointX + ' ' + pointY;
244 polygonPoints.push([pointX, pointY]);
245 me.polygonPath = polygonPath;
246 me.polygonPoints = polygonPoints;
247 if (!brushShape) {
248 brushShape = container.addShape('path', {
249 attrs: {
250 path: polygonPath,
251 stroke: '#979797',
252 lineWidth: 2,
253 fill: '#D8D8D8',
254 fillOpacity: 0.5,
255 lineDash: [5, 5]
256 }
257 });
258 } else {
259 brushShape.attr(Util.mix({}, brushShape.__attrs, {
260 path: polygonPath
261 }));
262 }
263 }
264 if (type !== 'polygon') {
265 if (!brushShape) {
266 brushShape = container.addShape('rect', {
267 attrs: {
268 x: rectStartX,
269 y: rectStartY,
270 width: rectWidth,
271 height: rectHeight,
272 fill: '#CCD7EB',
273 opacity: 0.4
274 }
275 });
276 } else {
277 brushShape.attr(Util.mix({}, brushShape.__attrs, {
278 x: rectStartX,
279 y: rectStartY,
280 width: rectWidth,
281 height: rectHeight
282 }));
283 }
284 }
285
286 me.brushShape = brushShape;
287
288 canvas.draw();
289
290 ev.cancelBubble = true;
291 ev.returnValue = false;
292 };
293
294 Brush.prototype._onCanvasMouseUp = function _onCanvasMouseUp() {
295 var me = this;
296 var canvas = me.canvas,
297 type = me.type;
298
299 var canvasDOM = canvas.get('canvasDOM');
300
301 // canvasDOM.removeEventListener('mousemove', Util.getWrapBehavior(me, '_onCanvasMouseMove'), false);
302 // canvasDOM.removeEventListener('mouseup', Util.getWrapBehavior(me, '_onCanvasMouseUp'), false);
303 me.onMouseMoveListener.remove();
304 me.onMouseUpListener.remove();
305 // selectionPlot.clear(); // 一期先默认清楚
306 me.isBrushing = false;
307 // this.brushShape = null;
308 var brushShape = me.brushShape;
309 var polygonPath = me.polygonPath;
310 var polygonPoints = me.polygonPoints;
311
312 if (type === 'polygon') {
313 polygonPath += 'z';
314 polygonPoints.push([polygonPoints[0][0], polygonPoints[0][1]]);
315
316 brushShape.attr(Util.mix({}, brushShape.__attrs, {
317 path: polygonPath
318 }));
319 me.polygonPath = polygonPath;
320 me.polygonPoints = polygonPoints;
321 canvas.draw();
322 } else {
323 me.brushShape = null;
324 }
325
326 // 抛出 brush end 事件
327 };
328
329 // setMode(type) {
330 // // TODO: 框选模式转变
331 // }
332
333
334 return Brush;
335}();
336
337module.exports = Brush;
338
339/***/ }),
340/* 2 */
341/***/ (function(module, exports) {
342
343
344function _mix(dist, obj) {
345 for (var k in obj) {
346 if (obj.hasOwnProperty(k) && k !== 'constructor' && obj[k] !== undefined) {
347 dist[k] = obj[k];
348 }
349 }
350}
351
352var Util = {
353 mix: function mix(dist, obj1, obj2, obj3) {
354 if (obj1) {
355 _mix(dist, obj1);
356 }
357
358 if (obj2) {
359 _mix(dist, obj2);
360 }
361
362 if (obj3) {
363 _mix(dist, obj3);
364 }
365 return dist;
366 },
367
368 /**
369 * 添加事件监听器
370 * @param {Object} target DOM对象
371 * @param {String} eventType 事件名
372 * @param {Funtion} callback 回调函数
373 * @return {Object} 返回对象
374 */
375 addEventListener: function addEventListener(target, eventType, callback) {
376 if (target.addEventListener) {
377 target.addEventListener(eventType, callback, false);
378 return {
379 remove: function remove() {
380 target.removeEventListener(eventType, callback, false);
381 }
382 };
383 } else if (target.attachEvent) {
384 target.attachEvent('on' + eventType, callback);
385 return {
386 remove: function remove() {
387 target.detachEvent('on' + eventType, callback);
388 }
389 };
390 }
391 },
392
393 /**
394 * 封装事件,便于使用上下文this,和便于解除事件时使用
395 * @protected
396 * @param {Object} obj 对象
397 * @param {String} action 事件名称
398 * @return {Function} 返回事件处理函数
399 */
400 wrapBehavior: function wrapBehavior(obj, action) {
401 if (obj['_wrap_' + action]) {
402 return obj['_wrap_' + action];
403 }
404 var method = function method(e) {
405 obj[action](e);
406 };
407 obj['_wrap_' + action] = method;
408 return method;
409 },
410
411 /**
412 * 获取封装的事件
413 * @protected
414 * @param {Object} obj 对象
415 * @param {String} action 事件名称
416 * @return {Function} 返回事件处理函数
417 */
418 getWrapBehavior: function getWrapBehavior(obj, action) {
419 return obj['_wrap_' + action];
420 }
421};
422
423module.exports = Util;
424
425/***/ })
426/******/ ]);
427});
428//# sourceMappingURL=g2-plugin-brush.js.map
\No newline at end of file