dataZoom.js 38.6 KB
Newer Older
K
kener 已提交
1 2 3 4 5 6 7 8
/**
 * echarts组件:数据区域缩放
 *
 * @desc echarts基于Canvas,纯Javascript图表库,提供直观,生动,可交互,可个性化定制的数据统计图表。
 * @author Kener (@Kener-林峰, linzhifeng@baidu.com)
 *
 */
define(function (require) {
K
kener 已提交
9 10 11 12
    var RectangleShape = require('zrender/shape/Rectangle');
    var PolygonShape = require('zrender/shape/Polygon');
    var IconShape = require('../util/shape/Icon');
    
K
kener 已提交
13 14 15 16 17 18 19
    /**
     * 构造函数
     * @param {Object} messageCenter echart消息中心
     * @param {ZRender} zr zrender实例
     * @param {Object} option 图表参数
     * @param {Object} component 组件
     */
K
kener 已提交
20
    function DataZoom(ecConfig, messageCenter, zr, option, component) {
K
kener 已提交
21
        var Base = require('./base');
K
kener 已提交
22
        Base.call(this, ecConfig, zr);
K
kener 已提交
23 24 25 26 27 28 29 30

        var self = this;
        self.type = ecConfig.COMPONENT_TYPE_DATAZOOM;

        var _zlevelBase = self.getZlevelBase();

        var zoomOption;

K
kener 已提交
31 32
        var _fillerSize = 28;       // 控件大小,水平布局为高,纵向布局为宽
        var _handleSize = 8;        // 手柄大小
K
kener 已提交
33 34
        var _location;              // 位置参数,通过计算所得x, y, width, height
        var _zoom;                  // 缩放参数
K
kener 已提交
35 36 37 38 39
        var _fillerShae;            // 填充
        var _startShape;            // 起始手柄
        var _endShape;              // 结束手柄
        var _startFrameShape;       // 起始特效边框
        var _endFrameShape;         // 结束特效边框
K
kener 已提交
40 41

        var _syncTicket;
42
        var _isSilence = false;
K
kener 已提交
43 44 45 46 47 48

        var _originalData;

        function _buildShape() {
            _buildBackground();
            _buildFiller();
K
kener 已提交
49 50
            _buildHandle();
            _buildFrame();
K
kener 已提交
51 52 53 54

            for (var i = 0, l = self.shapeList.length; i < l; i++) {
                zr.addShape(self.shapeList[i]);
            }
K
kener 已提交
55
            _syncFrameShape();
K
kener 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
        }

        /**
         * 根据选项计算实体的位置坐标
         */
        function _getLocation() {
            var x;
            var y;
            var width;
            var height;
            var grid = component.grid;

            // 不指定则根据grid适配
            if (zoomOption.orient == 'horizontal') {
                // 水平布局
                width = zoomOption.width || grid.getWidth();
                height = zoomOption.height || _fillerSize;
                x = typeof zoomOption.x != 'undefined'
                    ? zoomOption.x : grid.getX();
                y = typeof zoomOption.y != 'undefined'
K
kener 已提交
76
                    ? zoomOption.y : (zr.getHeight() - height - 2);
K
kener 已提交
77 78 79 80 81 82
            }
            else {
                // 垂直布局
                width = zoomOption.width || _fillerSize;
                height = zoomOption.height || grid.getHeight();
                x = typeof zoomOption.x != 'undefined'
K
kener 已提交
83
                    ? zoomOption.x : 2;
K
kener 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
                y = typeof zoomOption.y != 'undefined'
                    ? zoomOption.y : grid.getY();
            }

            return {
                x : x,
                y : y,
                width : width,
                height : height
            };
        }

        /**
         * 计算缩放参数
         * 修正单坐标轴只传对象为数组。
         */
        function _getZoom() {
            var series = option.series;
            var xAxis = option.xAxis;
            if (xAxis && !(xAxis instanceof Array)) {
                xAxis = [xAxis];
                option.xAxis = xAxis;
            }
            var yAxis = option.yAxis;
            if (yAxis && !(yAxis instanceof Array)) {
                yAxis = [yAxis];
                option.yAxis = yAxis;
            }

            var zoomSeriesIndex = [];
            var xAxisIndex;
            var yAxisIndex;

            var zOptIdx = zoomOption.xAxisIndex;
            if (xAxis && typeof zOptIdx == 'undefined') {
                xAxisIndex = [];
                for (var i = 0, l = xAxis.length; i < l; i++) {
                    // 横纵默认为类目轴
                    if (xAxis[i].type == 'category'
                        || typeof xAxis[i].type == 'undefined'
                    ) {
                        xAxisIndex.push(i);
                    }
                }
            }
            else {
                if (zOptIdx instanceof Array) {
                    xAxisIndex = zOptIdx;
                }
                else if (typeof zOptIdx != 'undefined') {
                    xAxisIndex = [zOptIdx];
                }
                else {
                    xAxisIndex = [];
                }
            }

            zOptIdx = zoomOption.yAxisIndex;
            if (yAxis && typeof zOptIdx == 'undefined') {
                yAxisIndex = [];
                for (var i = 0, l = yAxis.length; i < l; i++) {
                    if (yAxis[i].type == 'category') {
                        yAxisIndex.push(i);
                    }
                }
            }
            else {
                if (zOptIdx instanceof Array) {
                    yAxisIndex = zOptIdx;
                }
                else if (typeof zOptIdx != 'undefined') {
                    yAxisIndex = [zOptIdx];
                }
                else {
                    yAxisIndex = [];
                }
            }

            // 找到缩放控制的所有series
            for (var i = 0, l = series.length; i < l; i++) {
K
kener 已提交
164 165 166 167 168 169 170
                if (series[i].type != ecConfig.CHART_TYPE_LINE
                    && series[i].type != ecConfig.CHART_TYPE_BAR
                    && series[i].type != ecConfig.CHART_TYPE_SCATTER
                    && series[i].type != ecConfig.CHART_TYPE_K
                ) {
                    continue;
                }
K
kener 已提交
171 172 173 174 175 176 177 178 179 180 181 182
                for (var j = 0, k = xAxisIndex.length; j < k; j++) {
                    if (xAxisIndex[j] == (series[i].xAxisIndex || 0)) {
                        zoomSeriesIndex.push(i);
                        break;
                    }
                }
                for (var j = 0, k = yAxisIndex.length; j < k; j++) {
                    if (yAxisIndex[j] == (series[i].yAxisIndex || 0)) {
                        zoomSeriesIndex.push(i);
                        break;
                    }
                }
K
kener 已提交
183 184 185 186 187 188 189
                // 不指定接管坐标轴,则散点图被纳入接管范围
                if (series[i].type == ecConfig.CHART_TYPE_SCATTER
                    && typeof zoomOption.xAxisIndex == 'undefined'
                    && typeof zoomOption.yAxisIndex == 'undefined'
                ) {
                    zoomSeriesIndex.push(i);
                }
K
kener 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
            }

            var start = typeof zoomOption.start != 'undefined'
                        && zoomOption.start >= 0
                        && zoomOption.start <= 100
                        ? zoomOption.start : 0;
            var end = typeof zoomOption.end != 'undefined'
                      && zoomOption.end >= 0
                      && zoomOption.end <= 100
                      ? zoomOption.end : 100;
            if (start > end) {
                // 大小颠倒自动翻转
                start = start + end;
                end = start - end;
                start = start - end;
            }
            var size = Math.round(
                           (end - start) / 100
                           * (zoomOption.orient == 'horizontal'
                             ? _location.width : _location.height)
                       );
            return {
                start : start,
                end : end,
K
kener 已提交
214 215
                start2 : 0,
                end2 : 100,
K
kener 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
                size : size,
                xAxisIndex : xAxisIndex,
                yAxisIndex : yAxisIndex,
                seriesIndex : zoomSeriesIndex
            };
        }

        function _backupData() {
            _originalData = {
                xAxis : {},
                yAxis : {},
                series : {}
            };
            var xAxis = option.xAxis;
            var xAxisIndex = _zoom.xAxisIndex;
            for (var i = 0, l = xAxisIndex.length; i < l; i++) {
                _originalData.xAxis[xAxisIndex[i]] = xAxis[xAxisIndex[i]].data;
            }

            var yAxis = option.yAxis;
            var yAxisIndex = _zoom.yAxisIndex;
            for (var i = 0, l = yAxisIndex.length; i < l; i++) {
                _originalData.yAxis[yAxisIndex[i]] = yAxis[yAxisIndex[i]].data;
            }

            var series = option.series;
            var seriesIndex = _zoom.seriesIndex;
K
kener 已提交
243
            var serie;
K
kener 已提交
244
            for (var i = 0, l = seriesIndex.length; i < l; i++) {
K
kener 已提交
245 246 247
                serie = series[seriesIndex[i]];
                _originalData.series[seriesIndex[i]] = serie.data;
                if (serie.type == ecConfig.CHART_TYPE_SCATTER) {
K
kener 已提交
248
                    _calculScatterMap(seriesIndex[i]);
K
kener 已提交
249
                }
K
kener 已提交
250 251
            }
        }
K
kener 已提交
252 253 254 255 256
        
        function _calculScatterMap(seriesIndex) {
            _zoom.scatterMap = _zoom.scatterMap || {};
            _zoom.scatterMap[seriesIndex] = _zoom.scatterMap[seriesIndex] || {};
            var componentLibrary = require('../component');
K
kener 已提交
257
            var zrUtil = require('zrender/tool/util');
K
kener 已提交
258 259
            // x轴极值
            var Axis = componentLibrary.get('axis');
K
kener 已提交
260 261 262 263 264 265 266 267
            var axisOption = zrUtil.clone(option.xAxis);
            if (axisOption instanceof Array) {
                axisOption[0].type = 'value';
                axisOption[1] && (axisOption[1].type = 'value');
            }
            else {
                axisOption.type = 'value';
            }
K
kener 已提交
268
            var vAxis = new Axis(
K
kener 已提交
269
                ecConfig,
K
kener 已提交
270 271 272
                null,   // messageCenter
                false,  // zr
                {
K
kener 已提交
273
                    xAxis: axisOption,
K
kener 已提交
274 275 276 277 278 279 280 281
                    series : option.series
                }, 
                component,
                'xAxis'
            );
            var axisIndex = option.series[seriesIndex].xAxisIndex || 0;
            _zoom.scatterMap[seriesIndex].x = 
                vAxis.getAxis(axisIndex).getExtremum();
K
kener 已提交
282
            vAxis.dispose();
K
kener 已提交
283 284
            
            // y轴极值
K
kener 已提交
285 286 287 288 289 290 291 292
            axisOption = zrUtil.clone(option.yAxis);
            if (axisOption instanceof Array) {
                axisOption[0].type = 'value';
                axisOption[1] && (axisOption[1].type = 'value');
            }
            else {
                axisOption.type = 'value';
            }
K
kener 已提交
293
            vAxis = new Axis(
K
kener 已提交
294
                ecConfig,
K
kener 已提交
295 296 297
                null,   // messageCenter
                false,  // zr
                {
K
kener 已提交
298
                    yAxis: axisOption,
K
kener 已提交
299 300 301 302 303 304 305 306
                    series : option.series
                }, 
                component,
                'yAxis'
            );
            axisIndex = option.series[seriesIndex].yAxisIndex || 0;
            _zoom.scatterMap[seriesIndex].y = 
                vAxis.getAxis(axisIndex).getExtremum();
K
kener 已提交
307 308
            vAxis.dispose();
            // console.log(_zoom.scatterMap);
K
kener 已提交
309
        }
K
kener 已提交
310 311

        function _buildBackground() {
K
kener 已提交
312
            // 背景
K
kener 已提交
313
            self.shapeList.push(new RectangleShape({
K
kener 已提交
314 315 316 317 318 319 320 321 322
                zlevel : _zlevelBase,
                hoverable :false,
                style : {
                    x : _location.x,
                    y : _location.y,
                    width : _location.width,
                    height : _location.height,
                    color : zoomOption.backgroundColor
                }
K
kener 已提交
323
            }));
K
kener 已提交
324 325
            
            // 数据阴影
K
kener 已提交
326
            var maxLength = 0;
K
kener 已提交
327
            var xAxis = _originalData.xAxis;
K
kener 已提交
328 329 330
            var xAxisIndex = _zoom.xAxisIndex;
            for (var i = 0, l = xAxisIndex.length; i < l; i++) {
                maxLength = Math.max(
K
kener 已提交
331
                    maxLength, xAxis[xAxisIndex[i]].length
K
kener 已提交
332 333
                );
            }
K
kener 已提交
334
            var yAxis = _originalData.yAxis;
K
kener 已提交
335 336 337
            var yAxisIndex = _zoom.yAxisIndex;
            for (var i = 0, l = yAxisIndex.length; i < l; i++) {
                maxLength = Math.max(
K
kener 已提交
338
                    maxLength, yAxis[yAxisIndex[i]].length
K
kener 已提交
339 340 341
                );
            }

K
kener 已提交
342 343
            var seriesIndex = _zoom.seriesIndex[0];
            var data = _originalData.series[seriesIndex];
K
kener 已提交
344 345 346 347 348 349 350 351
            var maxValue = Number.MIN_VALUE;
            var minValue = Number.MAX_VALUE;
            var value;
            for (var i = 0, l = data.length; i < l; i++) {
                value = typeof data[i] != 'undefined'
                        ? (typeof data[i].value != 'undefined'
                          ? data[i].value : data[i])
                        : 0;
K
kener 已提交
352
                if (option.series[seriesIndex].type == ecConfig.CHART_TYPE_K) {
K
kener 已提交
353 354
                    value = value[1];   // 收盘价
                }
K
kener 已提交
355 356 357 358 359 360 361 362
                if (isNaN(value)) {
                    value = 0;
                }
                maxValue = Math.max(maxValue, value);
                minValue = Math.min(minValue, value);
            }

            var pointList = [];
K
kener 已提交
363 364
            var x = _location.width / (maxLength - (maxLength > 1 ? 1 : 0));
            var y = _location.height / (maxLength - (maxLength > 1 ? 1 : 0));
K
kener 已提交
365 366 367 368 369
            for (var i = 0, l = maxLength; i < l; i++) {
                value = typeof data[i] != 'undefined'
                        ? (typeof data[i].value != 'undefined'
                          ? data[i].value : data[i])
                        : 0;
K
kener 已提交
370
                if (option.series[seriesIndex].type == ecConfig.CHART_TYPE_K) {
K
kener 已提交
371 372
                    value = value[1];   // 收盘价
                }
K
kener 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
                if (isNaN(value)) {
                    value = 0;
                }
                if (zoomOption.orient == 'horizontal') {
                    pointList.push([
                        _location.x + x * i,
                        _location.y + _location.height - 5 - Math.round(
                            (value - minValue)
                            / (maxValue - minValue)
                            * (_location.height - 10)
                        )
                    ]);
                }
                else {
                    pointList.push([
                        _location.x + 5 + Math.round(
                            (value - minValue)
                            / (maxValue - minValue)
                            * (_location.width - 10)
                        ),
                        _location.y + y * i
                    ]);
                }
            }
            if (zoomOption.orient == 'horizontal') {
                 pointList.push([
                    _location.x + _location.width,
                    _location.y + _location.height
                ]);
                pointList.push([
                    _location.x, _location.y + _location.height
                ]);
            }
            else {
                pointList.push([
                    _location.x, _location.y + _location.height
                ]);
                pointList.push([
                    _location.x, _location.y
                ]);
            }

K
kener 已提交
415
            self.shapeList.push(new PolygonShape({
K
kener 已提交
416 417 418 419 420 421
                zlevel : _zlevelBase,
                style : {
                    pointList : pointList,
                    color : zoomOption.dataBackgroundColor
                },
                hoverable : false
K
kener 已提交
422
            }));
K
kener 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
        }

        /**
         * 构建填充物
         */
        function _buildFiller() {
            _fillerShae = {
                zlevel : _zlevelBase,
                draggable : true,
                ondrift : _ondrift,
                ondragend : _ondragend,
                _type : 'filler'
            };

            if (zoomOption.orient == 'horizontal') {
                // 横向
                _fillerShae.style = {
                    x : _location.x
                        + Math.round(_zoom.start / 100 * _location.width)
                        + _handleSize,
K
kener 已提交
443
                    y : _location.y,
K
kener 已提交
444
                    width : _zoom.size - _handleSize * 2,
K
kener 已提交
445
                    height : _location.height,
K
kener 已提交
446
                    color : zoomOption.fillerColor,
K
kener 已提交
447 448
                    // strokeColor : '#fff', // zoomOption.handleColor,
                    // lineWidth: 2,
K
kener 已提交
449 450 451 452 453
                    text : ':::',
                    textPosition : 'inside'
                };
            }
            else {
K
kener 已提交
454
                // 纵向
K
kener 已提交
455
                _fillerShae.style ={
K
kener 已提交
456
                    x : _location.x,
K
kener 已提交
457 458 459
                    y : _location.y
                        + Math.round(_zoom.start / 100 * _location.height)
                        + _handleSize,
K
kener 已提交
460
                    width :  _location.width,
K
kener 已提交
461 462
                    height : _zoom.size - _handleSize * 2,
                    color : zoomOption.fillerColor,
K
kener 已提交
463 464 465
                    // strokeColor : '#fff', // zoomOption.handleColor,
                    // lineWidth: 2,
                    text : '::',
K
kener 已提交
466 467 468
                    textPosition : 'inside'
                };
            }
K
kener 已提交
469 470 471 472 473 474 475 476 477 478
            
            _fillerShae.highlightStyle = {
                brushType: 'fill',
                color : 'rgba(0,0,0,0)'
                /*
                color : require('zrender/tool/color').alpha(
                            _fillerShae.style.color, 0
                        )
                */
            };
K
kener 已提交
479

K
kener 已提交
480
            self.shapeList.push(new RectangleShape(_fillerShae));
K
kener 已提交
481 482 483 484 485
        }

        /**
         * 构建拖拽手柄
         */
K
kener 已提交
486
        function _buildHandle() {
K
kener 已提交
487
            var zrUtil = require('zrender/tool/util');
K
kener 已提交
488
            _startShape = {
K
kener 已提交
489 490 491 492 493
                zlevel : _zlevelBase,
                draggable : true,
                style : {
                    iconType: 'rectangle',
                    x : _location.x,
K
kener 已提交
494 495
                    y : _location.y,
                    width : _handleSize,
K
kener 已提交
496
                    height : _handleSize,
K
kener 已提交
497
                    color : zoomOption.handleColor,
K
kener 已提交
498
                    text : '=',
K
kener 已提交
499
                    textPosition : 'inside'
K
kener 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
                },
                highlightStyle : {
                    brushType: 'fill'
                },
                ondrift : _ondrift,
                ondragend : _ondragend
            };
            
            if (zoomOption.orient == 'horizontal') {
                _startShape.style.height = _location.height;
                _endShape = zrUtil.clone(_startShape);
                
                _startShape.style.x = _fillerShae.style.x - _handleSize,
                _endShape.style.x = _fillerShae.style.x  
                                    + _fillerShae.style.width;
K
kener 已提交
515 516
            }
            else {
K
kener 已提交
517 518 519 520 521 522
                _startShape.style.width = _location.width;
                _endShape = zrUtil.clone(_startShape);
                
                _startShape.style.y = _fillerShae.style.y - _handleSize;
                _endShape.style.y = _fillerShae.style.y 
                                    + _fillerShae.style.height;
K
kener 已提交
523
            }
K
kener 已提交
524 525
            self.shapeList.push(new IconShape(_startShape));
            self.shapeList.push(new IconShape(_endShape));
K
kener 已提交
526 527
        }

K
kener 已提交
528 529 530
        /**
         * 构建特效边框
         */
K
kener 已提交
531
        function _buildFrame() {
K
kener 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
            var zrUtil = require('zrender/tool/util');
            // 特效框线,亚像素优化
            var x = self.subPixelOptimize(_location.x, 1);
            var y = self.subPixelOptimize(_location.y, 1);
            _startFrameShape = {
                zlevel : _zlevelBase,
                hoverable :false,
                style : {
                    x : x,
                    y : y,
                    width : _location.width - (x > _location.x ? 1 : 0),
                    height : _location.height - (y > _location.y ? 1 : 0),
                    lineWidth: 1,
                    brushType: 'stroke',
                    strokeColor : zoomOption.handleColor
                }
            };
            _endFrameShape = zrUtil.clone(_startFrameShape);
K
kener 已提交
550 551
            self.shapeList.push(new RectangleShape(_startFrameShape));
            self.shapeList.push(new RectangleShape(_endFrameShape));
K
kener 已提交
552 553 554
            return;
        }
        
K
kener 已提交
555 556 557
        /**
         * 拖拽范围控制
         */
K
kener 已提交
558 559
        function _ondrift(dx, dy) {
            var e = this;
560 561 562 563 564
            if (zoomOption.zoomLock) {
                // zoomLock时把handle转成filler的拖拽
                e = _fillerShae;
            }
            
K
kener 已提交
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
            var detailSize = e._type == 'filler' ? _handleSize : 0;
            if (zoomOption.orient == 'horizontal') {
                if (e.style.x + dx - detailSize <= _location.x) {
                    e.style.x = _location.x + detailSize;
                }
                else if (e.style.x + dx + e.style.width + detailSize
                         >= _location.x + _location.width
                ) {
                    e.style.x = _location.x + _location.width
                                - e.style.width - detailSize;
                }
                else {
                    e.style.x += dx;
                }
            }
            else {
                if (e.style.y + dy - detailSize <= _location.y) {
                    e.style.y = _location.y + detailSize;
                }
                else if (e.style.y + dy + e.style.height + detailSize
                         >= _location.y + _location.height
                ) {
                    e.style.y = _location.y + _location.height
                                - e.style.height - detailSize;
                }
                else {
                    e.style.y += dy;
                }
            }

            if (e._type == 'filler') {
                _syncHandleShape();
            }
            else {
                _syncFillerShape();
            }

            if (zoomOption.realtime) {
                _syncData();
            }
            else {
                clearTimeout(_syncTicket);
                _syncTicket = setTimeout(_syncData, 200);
            }

            return true;
        }

        function _syncHandleShape() {
            if (zoomOption.orient == 'horizontal') {
                _startShape.style.x = _fillerShae.style.x - _handleSize;
                _endShape.style.x = _fillerShae.style.x
                                    + _fillerShae.style.width;
K
kener 已提交
618
                
K
kener 已提交
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
                _zoom.start = Math.floor(
                    (_startShape.style.x - _location.x)
                    / _location.width * 100
                );
                _zoom.end = Math.ceil(
                    (_endShape.style.x + _handleSize - _location.x)
                    / _location.width * 100
                );
            }
            else {
                _startShape.style.y = _fillerShae.style.y - _handleSize;
                _endShape.style.y = _fillerShae.style.y
                                    + _fillerShae.style.height;
                _zoom.start = Math.floor(
                    (_startShape.style.y - _location.y)
                    / _location.height * 100
                );
                _zoom.end = Math.ceil(
                    (_endShape.style.y + _handleSize - _location.y)
                    / _location.height * 100
                );
            }

            zr.modShape(_startShape.id, _startShape);
            zr.modShape(_endShape.id, _endShape);
K
kener 已提交
644 645 646 647
            
            // 同步边框
            _syncFrameShape();
            
K
kener 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
            zr.refresh();
        }

        function _syncFillerShape() {
            var a;
            var b;
            if (zoomOption.orient == 'horizontal') {
                a = _startShape.style.x;
                b = _endShape.style.x;
                _fillerShae.style.x = Math.min(a, b) + _handleSize;
                _fillerShae.style.width = Math.abs(a - b) - _handleSize;
                _zoom.start = Math.floor(
                    (Math.min(a, b) - _location.x)
                    / _location.width * 100
                );
                _zoom.end = Math.ceil(
                    (Math.max(a, b) + _handleSize - _location.x)
                    / _location.width * 100
                );
            }
            else {
                a = _startShape.style.y;
                b = _endShape.style.y;
                _fillerShae.style.y = Math.min(a, b) + _handleSize;
                _fillerShae.style.height = Math.abs(a - b) - _handleSize;
                _zoom.start = Math.floor(
                    (Math.min(a, b) - _location.y)
                    / _location.height * 100
                );
                _zoom.end = Math.ceil(
                    (Math.max(a, b) + _handleSize - _location.y)
                    / _location.height * 100
                );
            }

            zr.modShape(_fillerShae.id, _fillerShae);
K
kener 已提交
684 685 686 687
            
            // 同步边框
            _syncFrameShape();
            
K
kener 已提交
688 689
            zr.refresh();
        }
690
        
K
kener 已提交
691 692 693 694 695 696 697
        function _syncFrameShape() {
            if (zoomOption.orient == 'horizontal') {
                _startFrameShape.style.width = 
                    _fillerShae.style.x - _location.x;
                _endFrameShape.style.x = 
                    _fillerShae.style.x + _fillerShae.style.width;
                _endFrameShape.style.width = 
K
kener 已提交
698
                    _location.x + _location.width - _endFrameShape.style.x;
K
kener 已提交
699 700 701 702 703 704 705
            }
            else {
                _startFrameShape.style.height = 
                    _fillerShae.style.y - _location.y;
                _endFrameShape.style.y = 
                    _fillerShae.style.y + _fillerShae.style.height;
                _endFrameShape.style.height = 
K
kener 已提交
706
                    _location.y + _location.height - _endFrameShape.style.y;
K
kener 已提交
707 708 709 710 711 712
            }
                    
            zr.modShape(_startFrameShape.id, _startFrameShape);
            zr.modShape(_endFrameShape.id, _endFrameShape);
        }
        
713
        function _syncShape() {
K
kener 已提交
714 715 716 717
            if (!zoomOption.show) {
                // 没有伸缩控件
                return;
            }
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
            if (zoomOption.orient == 'horizontal') {
                _startShape.style.x = _location.x 
                                      + _zoom.start / 100 * _location.width;
                _endShape.style.x = _location.x 
                                    + _zoom.end / 100 * _location.width
                                    - _handleSize;
                    
                _fillerShae.style.x = _startShape.style.x + _handleSize;
                _fillerShae.style.width = _endShape.style.x 
                                          - _startShape.style.x
                                          - _handleSize;
            }
            else {
                _startShape.style.y = _location.y 
                                      + _zoom.start / 100 * _location.height;
                _endShape.style.y = _location.y 
                                    + _zoom.end / 100 * _location.height
                                    - _handleSize;
                    
                _fillerShae.style.y = _startShape.style.y + _handleSize;
                _fillerShae.style.height = _endShape.style.y 
                                          - _startShape.style.y
                                          - _handleSize;
            }
            
            zr.modShape(_startShape.id, _startShape);
            zr.modShape(_endShape.id, _endShape);
            zr.modShape(_fillerShae.id, _fillerShae);
K
kener 已提交
746 747
            // 同步边框
            _syncFrameShape();
748 749
            zr.refresh();
        }
K
kener 已提交
750

K
kener 已提交
751
        function  _syncData(dispatchNow) {
K
kener 已提交
752 753 754 755 756 757 758 759 760 761 762 763
            var target;
            var start;
            var end;
            var length;
            var data;
            for (var key in _originalData) {
                target = _originalData[key];
                for (var idx in target) {
                    data = target[idx];
                    length = data.length;
                    start = Math.floor(_zoom.start / 100 * length);
                    end = Math.ceil(_zoom.end / 100 * length);
K
kener 已提交
764 765 766 767 768 769 770
                    if (option[key][idx].type != ecConfig.CHART_TYPE_SCATTER) {
                        option[key][idx].data = data.slice(start, end);
                    }
                    else {
                        // 散点图特殊处理
                        option[key][idx].data = _synScatterData(idx, data);
                    }
K
kener 已提交
771 772 773
                }
            }

774
            if (!_isSilence && (zoomOption.realtime || dispatchNow)) {
K
kener 已提交
775 776 777 778 779
                messageCenter.dispatch(
                    ecConfig.EVENT.DATA_ZOOM,
                    null,
                    {zoom: _zoom}
                );
K
kener 已提交
780 781 782 783 784
            }

            zoomOption.start = _zoom.start;
            zoomOption.end = _zoom.end;
        }
K
kener 已提交
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
        
        function _synScatterData(seriesIndex, data) {
            var newData = [];
            var scale = _zoom.scatterMap[seriesIndex];
            var total;
            var xStart;
            var xEnd;
            var yStart;
            var yEnd;
            
            if (zoomOption.orient == 'horizontal') {
                total = scale.x.max - scale.x.min;
                xStart = _zoom.start / 100 * total + scale.x.min;
                xEnd = _zoom.end / 100 * total + scale.x.min;
                
                total = scale.y.max - scale.y.min;
                yStart = _zoom.start2 / 100 * total + scale.y.min;
                yEnd = _zoom.end2 / 100 * total + scale.y.min;
            }
            else {
                total = scale.x.max - scale.x.min;
                xStart = _zoom.start2 / 100 * total + scale.x.min;
                xEnd = _zoom.end2 / 100 * total + scale.x.min;
                
                total = scale.y.max - scale.y.min;
                yStart = _zoom.start / 100 * total + scale.y.min;
                yEnd = _zoom.end / 100 * total + scale.y.min;
            }
            
            // console.log(xStart,xEnd,yStart,yEnd);
K
kener 已提交
815
            var value;
K
kener 已提交
816
            for (var i = 0, l = data.length; i < l; i++) {
K
kener 已提交
817 818 819 820 821
                value = data[i].value || data[i];
                if (value[0] >= xStart 
                    && value[0] <= xEnd
                    && value[1] >= yStart
                    && value[1] <= yEnd
K
kener 已提交
822 823 824 825 826 827 828
                ) {
                    newData.push(data[i]);
                }
            }
            
            return newData;
        }
K
kener 已提交
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847

        function _ondragend() {
            self.isDragend = true;
        }

        /**
         * 数据项被拖拽出去
         */
        function ondragend(param, status) {
            if (!self.isDragend || !param.target) {
                // 没有在当前实例上发生拖拽行为则直接返回
                return;
            }

             _syncData();

            // 别status = {}赋值啊!!
            status.dragOut = true;
            status.dragIn = true;
848
            if (!_isSilence && !zoomOption.realtime) {
K
kener 已提交
849 850 851 852 853
                messageCenter.dispatch(
                    ecConfig.EVENT.DATA_ZOOM,
                    null,
                    {zoom: _zoom}
                );
K
kener 已提交
854 855 856 857 858 859 860 861 862 863 864 865
            }
            status.needRefresh = false; // 会有消息触发fresh,不用再刷一遍
            // 处理完拖拽事件后复位
            self.isDragend = false;

            return;
        }

        function ondataZoom(param, status) {
            status.needRefresh = true;
            return;
        }
866
        
K
kener 已提交
867 868 869 870 871 872 873 874 875 876 877
        function absoluteZoom(param) {
            zoomOption.start = _zoom.start = param.start;
            zoomOption.end = _zoom.end = param.end;
            zoomOption.start2 = _zoom.start2 = param.start2;
            zoomOption.end2 = _zoom.end2 = param.end2;
            //console.log(rect,gridArea,_zoom,total)
            _syncShape();
            _syncData(true);
            return;
        }
        
878 879 880
        function rectZoom(param) {
            if (!param) {
                // 重置拖拽
K
kener 已提交
881 882 883 884 885 886 887 888 889 890
                zoomOption.start = 
                zoomOption.start2 = 
                _zoom.start = 
                _zoom.start2 = 0;
                    
                zoomOption.end =
                zoomOption.end2 = 
                _zoom.end = 
                _zoom.end2 = 100;
                
891
                _syncShape();
K
kener 已提交
892
                _syncData(true);
K
kener 已提交
893
                return _zoom;
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
            }
            var gridArea = component.grid.getArea();
            var rect = {
                x : param.x,
                y : param.y,
                width : param.width,
                height : param.height
            };
            // 修正方向框选
            if (rect.width < 0) {
                rect.x += rect.width;
                rect.width = -rect.width;
            }
            if (rect.height < 0) {
                rect.y += rect.height;
                rect.height = -rect.height;
            }
            // console.log(rect,_zoom);
            
            // 剔除无效缩放
            if (rect.x > gridArea.x + gridArea.width
                || rect.y > gridArea.y + gridArea.height
            ) {
                return false; // 无效缩放
            }
            
            // 修正框选超出
            if (rect.x < gridArea.x) {
                rect.x = gridArea.x;
            }
            if (rect.x + rect.width > gridArea.x + gridArea.width) {
                rect.width = gridArea.x + gridArea.width - rect.x;
            }
            if (rect.y + rect.height > gridArea.y + gridArea.height) {
                rect.height = gridArea.y + gridArea.height - rect.y;
            }
            
K
kener 已提交
931 932
            var total;
            var sdx = (rect.x - gridArea.x) / gridArea.width;
K
kener 已提交
933 934
            var edx = 1 - (rect.x + rect.width - gridArea.x) / gridArea.width;
            var sdy = 1 - (rect.y + rect.height - gridArea.y) / gridArea.height;
K
kener 已提交
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
            var edy = (rect.y - gridArea.y) / gridArea.height;
            //console.log('this',sdy,edy,_zoom.start,_zoom.end)
            if (zoomOption.orient == 'horizontal') {
                total = _zoom.end - _zoom.start;
                _zoom.start += total * sdx;
                _zoom.end -= total * edx;
                
                total = _zoom.end2 - _zoom.start2;
                _zoom.start2 += total * sdy;
                _zoom.end2 -= total * edy;
            }
            else {
                total = _zoom.end - _zoom.start;
                _zoom.start += total * sdy;
                _zoom.end -= total * edy;
                
                total = _zoom.end2 - _zoom.start2;
                _zoom.start2 += total * sdx;
                _zoom.end2 -= total * edx;
            }
            //console.log(_zoom.start,_zoom.end,_zoom.start2,_zoom.end2)
956 957
            zoomOption.start = _zoom.start;
            zoomOption.end = _zoom.end;
K
kener 已提交
958 959 960
            zoomOption.start2 = _zoom.start2;
            zoomOption.end2 = _zoom.end2;
            //console.log(rect,gridArea,_zoom,total)
961
            _syncShape();
K
kener 已提交
962
            _syncData(true);
K
kener 已提交
963
            return _zoom;
964
        }
K
kener 已提交
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
        
        function syncBackupData(curOption, optionBackup) {
            var start;
            var target = _originalData['series'];
            var curSeries = curOption.series;
            var curData;
            for (var i = 0, l = curSeries.length; i < l; i++) {
                curData = curSeries[i].data;
                if (target[i]) {
                    // dataZoom接管的
                    start = Math.floor(_zoom.start / 100 * target[i].length);
                }
                else {
                    // 非dataZoom接管
                    start = 0;
                }
                for (var j = 0, k = curData.length; j < k; j++) {
K
kener 已提交
982 983 984 985 986
                    optionBackup.series[i].data[j + start] = curData[j];
                    if (target[i]) {
                        // 同步内部备份
                        target[i][j + start] 
                            = curData[j];
K
kener 已提交
987 988 989 990
                    }
                }
            }
        }
991
        
992 993 994
        function silence(s) {
            _isSilence = s;
        }
995 996
        
        function getRealDataIndex(sIdx, dIdx) {
K
kener 已提交
997 998 999
            if (!_originalData) {
                return dIdx;
            }
1000 1001 1002 1003 1004 1005 1006
            var sreies = _originalData.series;
            if (sreies[sIdx]) {
                return Math.floor(_zoom.start / 100 * sreies[sIdx].length) 
                       + dIdx;
            }
            return -1;
        }
K
kener 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015

        function init(newOption) {
            option = newOption;

            option.dataZoom = self.reformOption(option.dataZoom);

            zoomOption = option.dataZoom;

            self.clear();
1016 1017 1018 1019
            
            // 自己show 或者 toolbox启用且dataZoom有效
            if (option.dataZoom.show
                || (
1020
                    self.query(option, 'toolbox.show')
K
kener 已提交
1021
                    && self.query(option, 'toolbox.feature.dataZoom.show')
1022 1023 1024 1025 1026 1027 1028
                )
            ) {
                _location = _getLocation();
                _zoom =  _getZoom();
                _backupData();
            }
            
K
kener 已提交
1029 1030
            if (option.dataZoom.show) {
                _buildShape();
K
kener 已提交
1031
                _syncData();
K
kener 已提交
1032 1033
            }
        }
K
kener 已提交
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043

        /**
         * 避免dataZoom带来两次refresh,不设refresh接口,resize重复一下buildshape逻辑 
         */
        function resize() {
            self.clear();
            
            // 自己show 或者 toolbox启用且dataZoom有效
            if (option.dataZoom.show
                || (
1044
                    self.query(option, 'toolbox.show')
K
kener 已提交
1045
                    && self.query(option, 'toolbox.feature.dataZoom.show')
K
kener 已提交
1046 1047 1048 1049 1050 1051 1052
                )
            ) {
                _location = _getLocation();
                _zoom =  _getZoom();
            }
            
            if (option.dataZoom.show) {
K
kener 已提交
1053
                _buildShape();
K
kener 已提交
1054 1055
            }
        }
1056
        
K
kener 已提交
1057
        self.init = init;
K
kener 已提交
1058
        self.resize = resize;
K
kener 已提交
1059
        self.syncBackupData = syncBackupData;
K
kener 已提交
1060
        self.absoluteZoom = absoluteZoom;
K
kener 已提交
1061
        self.rectZoom = rectZoom;
K
kener 已提交
1062 1063
        self.ondragend = ondragend;
        self.ondataZoom = ondataZoom;
1064
        self.silence = silence;
1065
        self.getRealDataIndex = getRealDataIndex;
K
kener 已提交
1066 1067 1068 1069

        init(option);
    }

1070 1071
    require('../component').define('dataZoom', DataZoom);
    
K
kener 已提交
1072 1073
    return DataZoom;
});