dataZoom.js 42.2 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
    var Base = require('./base');
    
    // 图形依赖
K
kener 已提交
12 13 14 15
    var RectangleShape = require('zrender/shape/Rectangle');
    var PolygonShape = require('zrender/shape/Polygon');
    var IconShape = require('../util/shape/Icon');
    
K
kener 已提交
16 17 18
    var ecConfig = require('../config');
    var zrUtil = require('zrender/tool/util');

K
kener 已提交
19 20 21 22 23 24 25
    /**
     * 构造函数
     * @param {Object} messageCenter echart消息中心
     * @param {ZRender} zr zrender实例
     * @param {Object} option 图表参数
     * @param {Object} component 组件
     */
K
kener 已提交
26 27
    function DataZoom(ecTheme, messageCenter, zr, option, myChart) {
        Base.call(this, ecTheme, messageCenter, zr, option, myChart);
K
kener 已提交
28

K
kener 已提交
29 30
        var self = this;
        self._ondrift = function (dx, dy) {
K
kener 已提交
31
            return self.__ondrift(this, dx, dy);
K
kener 已提交
32 33
        };
        self._ondragend = function () {
K
kener 已提交
34
            return self.__ondragend();
K
kener 已提交
35
        };
K
kener 已提交
36

K
kener 已提交
37 38 39 40 41 42 43 44 45
        this._fillerSize = 28;       // 控件大小,水平布局为高,纵向布局为宽
        this._handleSize = 8;        // 手柄大小
        // this._fillerShae;            // 填充
        // this._startShape;            // 起始手柄
        // this._endShape;              // 结束手柄
        // this._startFrameShape;       // 起始特效边框
        // this._endFrameShape;         // 结束特效边框
        // this._syncTicket;
        this._isSilence = false;
K
kener 已提交
46
        this._zoom = {};
K
kener 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
        // this._originalData;
        
        this.option.dataZoom = this.reformOption(this.option.dataZoom);
        this.zoomOption = this.option.dataZoom;

        // 位置参数,通过计算所得x, y, width, height
        this._location = this._getLocation();
        // 缩放参数
        this._zoom =  this._getZoom();
        this._backupData();
        
        if (this.option.dataZoom.show) {
            this._buildShape();
        }
        this._syncData();
K
kener 已提交
62 63 64 65 66 67 68 69 70
    }
    
    DataZoom.prototype = {
        type : ecConfig.COMPONENT_TYPE_DATAZOOM,
        _buildShape : function () {
            this._buildBackground();
            this._buildFiller();
            this._buildHandle();
            this._buildFrame();
K
kener 已提交
71

K
kener 已提交
72 73
            for (var i = 0, l = this.shapeList.length; i < l; i++) {
                this.zr.addShape(this.shapeList[i]);
K
kener 已提交
74
            }
K
kener 已提交
75 76
            this._syncFrameShape();
        },
K
kener 已提交
77 78 79 80

        /**
         * 根据选项计算实体的位置坐标
         */
K
kener 已提交
81
        _getLocation : function () {
K
kener 已提交
82 83 84 85
            var x;
            var y;
            var width;
            var height;
K
kener 已提交
86
            var grid = this.component.grid;
K
kener 已提交
87 88

            // 不指定则根据grid适配
K
kener 已提交
89
            if (this.zoomOption.orient == 'horizontal') {
K
kener 已提交
90
                // 水平布局
K
kener 已提交
91 92 93 94 95 96
                width = this.zoomOption.width || grid.getWidth();
                height = this.zoomOption.height || this._fillerSize;
                x = typeof this.zoomOption.x != 'undefined'
                    ? this.zoomOption.x : grid.getX();
                y = typeof this.zoomOption.y != 'undefined'
                    ? this.zoomOption.y : (this.zr.getHeight() - height - 2);
K
kener 已提交
97 98 99
            }
            else {
                // 垂直布局
K
kener 已提交
100 101 102 103 104 105
                width = this.zoomOption.width || this._fillerSize;
                height = this.zoomOption.height || grid.getHeight();
                x = typeof this.zoomOption.x != 'undefined'
                    ? this.zoomOption.x : 2;
                y = typeof this.zoomOption.y != 'undefined'
                    ? this.zoomOption.y : grid.getY();
K
kener 已提交
106 107 108 109 110 111 112 113
            }

            return {
                x : x,
                y : y,
                width : width,
                height : height
            };
K
kener 已提交
114
        },
K
kener 已提交
115 116 117 118 119

        /**
         * 计算缩放参数
         * 修正单坐标轴只传对象为数组。
         */
K
kener 已提交
120 121 122
        _getZoom : function () {
            var series = this.option.series;
            var xAxis = this.option.xAxis;
K
kener 已提交
123 124
            if (xAxis && !(xAxis instanceof Array)) {
                xAxis = [xAxis];
K
kener 已提交
125
                this.option.xAxis = xAxis;
K
kener 已提交
126
            }
K
kener 已提交
127
            var yAxis = this.option.yAxis;
K
kener 已提交
128 129
            if (yAxis && !(yAxis instanceof Array)) {
                yAxis = [yAxis];
K
kener 已提交
130
                this.option.yAxis = yAxis;
K
kener 已提交
131 132 133 134 135 136
            }

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

K
kener 已提交
137
            var zOptIdx = this.zoomOption.xAxisIndex;
K
kener 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
            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 = [];
                }
            }

K
kener 已提交
161
            zOptIdx = this.zoomOption.yAxisIndex;
K
kener 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
            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 已提交
184 185 186 187 188 189 190
                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 已提交
191 192 193 194 195 196 197 198 199 200 201 202
                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 已提交
203 204
                // 不指定接管坐标轴,则散点图被纳入接管范围
                if (series[i].type == ecConfig.CHART_TYPE_SCATTER
K
kener 已提交
205 206
                    && typeof this.zoomOption.xAxisIndex == 'undefined'
                    && typeof this.zoomOption.yAxisIndex == 'undefined'
K
kener 已提交
207 208 209
                ) {
                    zoomSeriesIndex.push(i);
                }
K
kener 已提交
210 211
            }

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

K
kener 已提交
252 253
        _backupData : function () {
            this._originalData = {
K
kener 已提交
254 255 256 257
                xAxis : {},
                yAxis : {},
                series : {}
            };
K
kener 已提交
258 259
            var xAxis = this.option.xAxis;
            var xAxisIndex = this._zoom.xAxisIndex;
K
kener 已提交
260
            for (var i = 0, l = xAxisIndex.length; i < l; i++) {
K
kener 已提交
261
                this._originalData.xAxis[xAxisIndex[i]] = xAxis[xAxisIndex[i]].data;
K
kener 已提交
262 263
            }

K
kener 已提交
264 265
            var yAxis = this.option.yAxis;
            var yAxisIndex = this._zoom.yAxisIndex;
K
kener 已提交
266
            for (var i = 0, l = yAxisIndex.length; i < l; i++) {
K
kener 已提交
267
                this._originalData.yAxis[yAxisIndex[i]] = yAxis[yAxisIndex[i]].data;
K
kener 已提交
268 269
            }

K
kener 已提交
270 271
            var series = this.option.series;
            var seriesIndex = this._zoom.seriesIndex;
K
kener 已提交
272
            var serie;
K
kener 已提交
273
            for (var i = 0, l = seriesIndex.length; i < l; i++) {
K
kener 已提交
274
                serie = series[seriesIndex[i]];
K
kener 已提交
275
                this._originalData.series[seriesIndex[i]] = serie.data;
K
kener 已提交
276
                if (serie.type == ecConfig.CHART_TYPE_SCATTER) {
K
kener 已提交
277
                    this._calculScatterMap(seriesIndex[i]);
K
kener 已提交
278
                }
K
kener 已提交
279
            }
K
kener 已提交
280
        },
K
kener 已提交
281
        
K
kener 已提交
282 283 284
        _calculScatterMap : function (seriesIndex) {
            this._zoom.scatterMap = this._zoom.scatterMap || {};
            this._zoom.scatterMap[seriesIndex] = this._zoom.scatterMap[seriesIndex] || {};
K
kener 已提交
285 286 287
            var componentLibrary = require('../component');
            // x轴极值
            var Axis = componentLibrary.get('axis');
K
kener 已提交
288
            var axisOption = zrUtil.clone(this.option.xAxis);
K
kener 已提交
289 290
            if (axisOption instanceof Array) {
                axisOption[0].type = 'value';
K
kener 已提交
291
                axisOption[0].scale = true;
K
kener 已提交
292 293
                axisOption[0].boundary = [0, 0];
                axisOption[1] && (axisOption[1].type = 'value', axisOption[1].boundary = [0, 0]);
K
kener 已提交
294 295 296
            }
            else {
                axisOption.type = 'value';
K
kener 已提交
297
                axisOption.scale = true;
K
kener 已提交
298
                axisOption.boundary = [0, 0];
K
kener 已提交
299
            }
K
kener 已提交
300
            var vAxis = new Axis(
K
kener 已提交
301
                this.ecTheme,
K
kener 已提交
302
                null,   // messageCenter
K
kener 已提交
303
                false,  // this.zr
K
kener 已提交
304
                {
K
kener 已提交
305
                    xAxis: axisOption,
K
kener 已提交
306
                    series : this.option.series
K
kener 已提交
307
                }, 
K
kener 已提交
308
                this,
K
kener 已提交
309 310
                'xAxis'
            );
K
kener 已提交
311
            var axisIndex = this.option.series[seriesIndex].xAxisIndex || 0;
K
kener 已提交
312
            this._zoom.scatterMap[seriesIndex].x = vAxis.getAxis(axisIndex).getExtremum();
K
kener 已提交
313
            vAxis.dispose();
K
kener 已提交
314 315
            
            // y轴极值
K
kener 已提交
316
            axisOption = zrUtil.clone(this.option.yAxis);
K
kener 已提交
317 318
            if (axisOption instanceof Array) {
                axisOption[0].type = 'value';
K
kener 已提交
319
                axisOption[0].scale = true;
K
kener 已提交
320
                axisOption[1] && (axisOption[1].type = 'value', axisOption[1].boundary = [0, 0]);
K
kener 已提交
321 322 323
            }
            else {
                axisOption.type = 'value';
K
kener 已提交
324
                axisOption.scale = true;
K
kener 已提交
325
                axisOption.boundary = [0, 0];
K
kener 已提交
326
            }
K
kener 已提交
327
            vAxis = new Axis(
K
kener 已提交
328
                this.ecTheme,
K
kener 已提交
329
                null,   // messageCenter
K
kener 已提交
330
                false,  // this.zr
K
kener 已提交
331
                {
K
kener 已提交
332
                    yAxis: axisOption,
K
kener 已提交
333
                    series : this.option.series
K
kener 已提交
334
                }, 
K
kener 已提交
335
                this,
K
kener 已提交
336 337
                'yAxis'
            );
K
kener 已提交
338
            axisIndex = this.option.series[seriesIndex].yAxisIndex || 0;
K
kener 已提交
339
            this._zoom.scatterMap[seriesIndex].y = vAxis.getAxis(axisIndex).getExtremum();
K
kener 已提交
340
            vAxis.dispose();
K
kener 已提交
341 342
            // console.log(this._zoom.scatterMap);
        },
K
kener 已提交
343

K
kener 已提交
344
        _buildBackground : function () {
K
kener 已提交
345 346 347
            var width = this._location.width;
            var height = this._location.height;
            
K
kener 已提交
348
            // 背景
K
kener 已提交
349 350
            this.shapeList.push(new RectangleShape({
                zlevel : this._zlevelBase,
K
kener 已提交
351 352
                hoverable :false,
                style : {
K
kener 已提交
353 354
                    x : this._location.x,
                    y : this._location.y,
K
kener 已提交
355 356
                    width : width,
                    height : height,
K
kener 已提交
357
                    color : this.zoomOption.backgroundColor
K
kener 已提交
358
                }
K
kener 已提交
359
            }));
K
kener 已提交
360 361
            
            // 数据阴影
K
kener 已提交
362
            var maxLength = 0;
K
kener 已提交
363 364
            var xAxis = this._originalData.xAxis;
            var xAxisIndex = this._zoom.xAxisIndex;
K
kener 已提交
365 366
            for (var i = 0, l = xAxisIndex.length; i < l; i++) {
                maxLength = Math.max(
K
kener 已提交
367
                    maxLength, xAxis[xAxisIndex[i]].length
K
kener 已提交
368 369
                );
            }
K
kener 已提交
370 371
            var yAxis = this._originalData.yAxis;
            var yAxisIndex = this._zoom.yAxisIndex;
K
kener 已提交
372 373
            for (var i = 0, l = yAxisIndex.length; i < l; i++) {
                maxLength = Math.max(
K
kener 已提交
374
                    maxLength, yAxis[yAxisIndex[i]].length
K
kener 已提交
375 376 377
                );
            }

K
kener 已提交
378 379
            var seriesIndex = this._zoom.seriesIndex[0];
            var data = this._originalData.series[seriesIndex];
K
kener 已提交
380 381 382 383 384
            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'
K
kener 已提交
385
                        ? (typeof data[i].value != 'undefined' ? data[i].value : data[i])
K
kener 已提交
386
                        : 0;
K
kener 已提交
387
                if (this.option.series[seriesIndex].type == ecConfig.CHART_TYPE_K) {
K
kener 已提交
388 389
                    value = value[1];   // 收盘价
                }
K
kener 已提交
390 391 392 393 394 395
                if (isNaN(value)) {
                    value = 0;
                }
                maxValue = Math.max(maxValue, value);
                minValue = Math.min(minValue, value);
            }
K
kener 已提交
396
            var valueRange = maxValue - minValue;
K
kener 已提交
397 398

            var pointList = [];
K
kener 已提交
399 400 401 402 403 404 405 406 407 408 409
            var x = width / (maxLength - (maxLength > 1 ? 1 : 0));
            var y = height / (maxLength - (maxLength > 1 ? 1 : 0));
            var step = 1;
            if (this.zoomOption.orient == 'horizontal' && x < 1) {
                step = Math.floor(maxLength * 3 / width);
            }
            else if (this.zoomOption.orient == 'vertical' && y < 1){
                step = Math.floor(maxLength * 3 / height);
            }
            
            for (var i = 0, l = maxLength; i < l; i += step) {
K
kener 已提交
410 411 412 413
                value = typeof data[i] != 'undefined'
                        ? (typeof data[i].value != 'undefined'
                          ? data[i].value : data[i])
                        : 0;
K
kener 已提交
414
                if (this.option.series[seriesIndex].type == ecConfig.CHART_TYPE_K) {
K
kener 已提交
415 416
                    value = value[1];   // 收盘价
                }
K
kener 已提交
417 418 419
                if (isNaN(value)) {
                    value = 0;
                }
K
kener 已提交
420
                if (this.zoomOption.orient == 'horizontal') {
K
kener 已提交
421
                    pointList.push([
K
kener 已提交
422
                        this._location.x + x * i,
K
kener 已提交
423
                        this._location.y + height - 1 - Math.round(
K
kener 已提交
424
                            (value - minValue) / valueRange * (height - 10)
K
kener 已提交
425 426 427 428 429
                        )
                    ]);
                }
                else {
                    pointList.push([
K
kener 已提交
430
                        this._location.x + 1 + Math.round(
K
kener 已提交
431
                            (value - minValue) / valueRange * (width - 10)
K
kener 已提交
432
                        ),
K
kener 已提交
433
                        this._location.y + y * i
K
kener 已提交
434 435 436
                    ]);
                }
            }
K
kener 已提交
437
            if (this.zoomOption.orient == 'horizontal') {
K
kener 已提交
438
                 pointList.push([
K
kener 已提交
439 440
                    this._location.x + width,
                    this._location.y + height
K
kener 已提交
441 442
                ]);
                pointList.push([
K
kener 已提交
443
                    this._location.x, this._location.y + height
K
kener 已提交
444 445 446 447
                ]);
            }
            else {
                pointList.push([
K
kener 已提交
448
                    this._location.x, this._location.y + height
K
kener 已提交
449 450
                ]);
                pointList.push([
K
kener 已提交
451
                    this._location.x, this._location.y
K
kener 已提交
452 453 454
                ]);
            }

K
kener 已提交
455 456
            this.shapeList.push(new PolygonShape({
                zlevel : this._zlevelBase,
K
kener 已提交
457 458
                style : {
                    pointList : pointList,
K
kener 已提交
459
                    color : this.zoomOption.dataBackgroundColor
K
kener 已提交
460 461
                },
                hoverable : false
K
kener 已提交
462
            }));
K
kener 已提交
463
        },
K
kener 已提交
464 465 466 467

        /**
         * 构建填充物
         */
K
kener 已提交
468 469 470
        _buildFiller : function () {
            this._fillerShae = {
                zlevel : this._zlevelBase,
K
kener 已提交
471
                draggable : true,
K
kener 已提交
472 473
                ondrift : this._ondrift,
                ondragend : this._ondragend,
K
kener 已提交
474 475 476
                _type : 'filler'
            };

K
kener 已提交
477
            if (this.zoomOption.orient == 'horizontal') {
K
kener 已提交
478
                // 横向
K
kener 已提交
479 480 481 482 483 484 485 486 487
                this._fillerShae.style = {
                    x : this._location.x
                        + Math.round(this._zoom.start / 100 * this._location.width)
                        + this._handleSize,
                    y : this._location.y,
                    width : this._zoom.size - this._handleSize * 2,
                    height : this._location.height,
                    color : this.zoomOption.fillerColor,
                    // strokeColor : '#fff', // this.zoomOption.handleColor,
K
kener 已提交
488
                    // lineWidth: 2,
K
kener 已提交
489 490 491 492 493
                    text : ':::',
                    textPosition : 'inside'
                };
            }
            else {
K
kener 已提交
494
                // 纵向
K
kener 已提交
495 496 497 498 499 500 501 502 503
                this._fillerShae.style ={
                    x : this._location.x,
                    y : this._location.y
                        + Math.round(this._zoom.start / 100 * this._location.height)
                        + this._handleSize,
                    width :  this._location.width,
                    height : this._zoom.size - this._handleSize * 2,
                    color : this.zoomOption.fillerColor,
                    // strokeColor : '#fff', // this.zoomOption.handleColor,
K
kener 已提交
504 505
                    // lineWidth: 2,
                    text : '::',
K
kener 已提交
506 507 508
                    textPosition : 'inside'
                };
            }
K
kener 已提交
509
            
K
kener 已提交
510
            this._fillerShae.highlightStyle = {
K
kener 已提交
511 512 513 514
                brushType: 'fill',
                color : 'rgba(0,0,0,0)'
                /*
                color : require('zrender/tool/color').alpha(
K
kener 已提交
515
                            this._fillerShae.style.color, 0
K
kener 已提交
516 517 518
                        )
                */
            };
K
kener 已提交
519 520 521
            this._fillerShae = new RectangleShape(this._fillerShae);
            this.shapeList.push(this._fillerShae);
        },
K
kener 已提交
522 523 524 525

        /**
         * 构建拖拽手柄
         */
K
kener 已提交
526 527 528
        _buildHandle : function () {
            this._startShape = {
                zlevel : this._zlevelBase,
K
kener 已提交
529 530 531
                draggable : true,
                style : {
                    iconType: 'rectangle',
K
kener 已提交
532 533 534 535 536
                    x : this._location.x,
                    y : this._location.y,
                    width : this._handleSize,
                    height : this._handleSize,
                    color : this.zoomOption.handleColor,
K
kener 已提交
537
                    text : '=',
K
kener 已提交
538
                    textPosition : 'inside'
K
kener 已提交
539 540 541 542
                },
                highlightStyle : {
                    brushType: 'fill'
                },
K
kener 已提交
543 544
                ondrift : this._ondrift,
                ondragend : this._ondragend
K
kener 已提交
545 546
            };
            
K
kener 已提交
547 548 549
            if (this.zoomOption.orient == 'horizontal') {
                this._startShape.style.height = this._location.height;
                this._endShape = zrUtil.clone(this._startShape);
K
kener 已提交
550
                
K
kener 已提交
551 552 553
                this._startShape.style.x = this._fillerShae.style.x - this._handleSize,
                this._endShape.style.x = this._fillerShae.style.x  
                                    + this._fillerShae.style.width;
K
kener 已提交
554 555
            }
            else {
K
kener 已提交
556 557
                this._startShape.style.width = this._location.width;
                this._endShape = zrUtil.clone(this._startShape);
K
kener 已提交
558
                
K
kener 已提交
559 560 561 562 563 564 565 566 567
                this._startShape.style.y = this._fillerShae.style.y - this._handleSize;
                this._endShape.style.y = this._fillerShae.style.y 
                                    + this._fillerShae.style.height;
            }
            this._startShape = new IconShape(this._startShape);
            this._endShape = new IconShape(this._endShape);
            this.shapeList.push(this._startShape);
            this.shapeList.push(this._endShape);
        },
K
kener 已提交
568

K
kener 已提交
569 570 571
        /**
         * 构建特效边框
         */
K
kener 已提交
572
        _buildFrame : function () {
K
kener 已提交
573
            // 特效框线,亚像素优化
K
kener 已提交
574 575 576 577
            var x = this.subPixelOptimize(this._location.x, 1);
            var y = this.subPixelOptimize(this._location.y, 1);
            this._startFrameShape = {
                zlevel : this._zlevelBase,
K
kener 已提交
578 579 580 581
                hoverable :false,
                style : {
                    x : x,
                    y : y,
K
kener 已提交
582 583
                    width : this._location.width - (x > this._location.x ? 1 : 0),
                    height : this._location.height - (y > this._location.y ? 1 : 0),
K
kener 已提交
584 585
                    lineWidth: 1,
                    brushType: 'stroke',
K
kener 已提交
586
                    strokeColor : this.zoomOption.handleColor
K
kener 已提交
587 588
                }
            };
K
kener 已提交
589
            this._endFrameShape = zrUtil.clone(this._startFrameShape);
K
kener 已提交
590
            
K
kener 已提交
591 592 593
            this._startFrameShape = new RectangleShape(this._startFrameShape);
            this._endFrameShape = new RectangleShape(this._endFrameShape);
            this.shapeList.push(this._startFrameShape);
K
kener 已提交
594
            this.shapeList.push(this._endFrameShape);
K
kener 已提交
595
            return;
K
kener 已提交
596
        },
K
kener 已提交
597
        
K
kener 已提交
598 599 600 601 602
        _syncHandleShape : function () {
            if (this.zoomOption.orient == 'horizontal') {
                this._startShape.style.x = this._fillerShae.style.x - this._handleSize;
                this._endShape.style.x = this._fillerShae.style.x
                                    + this._fillerShae.style.width;
K
kener 已提交
603
                
K
kener 已提交
604 605 606
                this._zoom.start = Math.floor(
                    (this._startShape.style.x - this._location.x)
                    / this._location.width * 100
K
kener 已提交
607
                );
K
kener 已提交
608 609 610
                this._zoom.end = Math.ceil(
                    (this._endShape.style.x + this._handleSize - this._location.x)
                    / this._location.width * 100
K
kener 已提交
611 612 613
                );
            }
            else {
K
kener 已提交
614 615 616 617 618 619
                this._startShape.style.y = this._fillerShae.style.y - this._handleSize;
                this._endShape.style.y = this._fillerShae.style.y
                                    + this._fillerShae.style.height;
                this._zoom.start = Math.floor(
                    (this._startShape.style.y - this._location.y)
                    / this._location.height * 100
K
kener 已提交
620
                );
K
kener 已提交
621 622 623
                this._zoom.end = Math.ceil(
                    (this._endShape.style.y + this._handleSize - this._location.y)
                    / this._location.height * 100
K
kener 已提交
624 625 626
                );
            }

K
kener 已提交
627 628
            this.zr.modShape(this._startShape.id);
            this.zr.modShape(this._endShape.id);
K
kener 已提交
629 630
            
            // 同步边框
K
kener 已提交
631
            this._syncFrameShape();
K
kener 已提交
632
            
K
kener 已提交
633 634
            this.zr.refresh();
        },
K
kener 已提交
635

K
kener 已提交
636
        _syncFillerShape : function () {
K
kener 已提交
637 638
            var a;
            var b;
K
kener 已提交
639 640 641 642 643 644 645 646
            if (this.zoomOption.orient == 'horizontal') {
                a = this._startShape.style.x;
                b = this._endShape.style.x;
                this._fillerShae.style.x = Math.min(a, b) + this._handleSize;
                this._fillerShae.style.width = Math.abs(a - b) - this._handleSize;
                this._zoom.start = Math.floor(
                    (Math.min(a, b) - this._location.x)
                    / this._location.width * 100
K
kener 已提交
647
                );
K
kener 已提交
648 649 650
                this._zoom.end = Math.ceil(
                    (Math.max(a, b) + this._handleSize - this._location.x)
                    / this._location.width * 100
K
kener 已提交
651 652 653
                );
            }
            else {
K
kener 已提交
654 655 656 657 658 659 660
                a = this._startShape.style.y;
                b = this._endShape.style.y;
                this._fillerShae.style.y = Math.min(a, b) + this._handleSize;
                this._fillerShae.style.height = Math.abs(a - b) - this._handleSize;
                this._zoom.start = Math.floor(
                    (Math.min(a, b) - this._location.y)
                    / this._location.height * 100
K
kener 已提交
661
                );
K
kener 已提交
662 663 664
                this._zoom.end = Math.ceil(
                    (Math.max(a, b) + this._handleSize - this._location.y)
                    / this._location.height * 100
K
kener 已提交
665 666 667
                );
            }

K
kener 已提交
668
            this.zr.modShape(this._fillerShae.id);
K
kener 已提交
669 670
            
            // 同步边框
K
kener 已提交
671
            this._syncFrameShape();
K
kener 已提交
672
            
K
kener 已提交
673 674
            this.zr.refresh();
        },
675
        
K
kener 已提交
676 677 678 679 680 681 682 683
        _syncFrameShape : function () {
            if (this.zoomOption.orient == 'horizontal') {
                this._startFrameShape.style.width = 
                    this._fillerShae.style.x - this._location.x;
                this._endFrameShape.style.x = 
                    this._fillerShae.style.x + this._fillerShae.style.width;
                this._endFrameShape.style.width = 
                    this._location.x + this._location.width - this._endFrameShape.style.x;
K
kener 已提交
684 685
            }
            else {
K
kener 已提交
686 687 688 689 690 691
                this._startFrameShape.style.height = 
                    this._fillerShae.style.y - this._location.y;
                this._endFrameShape.style.y = 
                    this._fillerShae.style.y + this._fillerShae.style.height;
                this._endFrameShape.style.height = 
                    this._location.y + this._location.height - this._endFrameShape.style.y;
K
kener 已提交
692 693
            }
                    
K
kener 已提交
694 695
            this.zr.modShape(this._startFrameShape.id);
            this.zr.modShape(this._endFrameShape.id);
K
kener 已提交
696
        },
K
kener 已提交
697
        
K
kener 已提交
698 699
        _syncShape : function () {
            if (!this.zoomOption.show) {
K
kener 已提交
700 701 702
                // 没有伸缩控件
                return;
            }
K
kener 已提交
703 704 705 706 707 708
            if (this.zoomOption.orient == 'horizontal') {
                this._startShape.style.x = this._location.x 
                                      + this._zoom.start / 100 * this._location.width;
                this._endShape.style.x = this._location.x 
                                    + this._zoom.end / 100 * this._location.width
                                    - this._handleSize;
709
                    
K
kener 已提交
710 711 712 713
                this._fillerShae.style.x = this._startShape.style.x + this._handleSize;
                this._fillerShae.style.width = this._endShape.style.x 
                                          - this._startShape.style.x
                                          - this._handleSize;
714 715
            }
            else {
K
kener 已提交
716 717 718 719 720
                this._startShape.style.y = this._location.y 
                                      + this._zoom.start / 100 * this._location.height;
                this._endShape.style.y = this._location.y 
                                    + this._zoom.end / 100 * this._location.height
                                    - this._handleSize;
721
                    
K
kener 已提交
722 723 724 725
                this._fillerShae.style.y = this._startShape.style.y + this._handleSize;
                this._fillerShae.style.height = this._endShape.style.y 
                                          - this._startShape.style.y
                                          - this._handleSize;
726 727
            }
            
K
kener 已提交
728 729 730
            this.zr.modShape(this._startShape.id);
            this.zr.modShape(this._endShape.id);
            this.zr.modShape(this._fillerShae.id);
K
kener 已提交
731
            // 同步边框
K
kener 已提交
732 733 734 735 736
            this._syncFrameShape();
            this.zr.refresh();
        },
        
         _syncData : function (dispatchNow) {
K
kener 已提交
737 738 739 740 741
            var target;
            var start;
            var end;
            var length;
            var data;
K
kener 已提交
742
            
K
kener 已提交
743 744 745 746
            for (var key in this._originalData) {
                target = this._originalData[key];
                for (var idx in target) {
                    data = target[idx];
K
kener 已提交
747 748 749
                    if (typeof data == 'undefined') {
                        continue;
                    }
K
kener 已提交
750 751 752 753 754 755 756 757 758
                    length = data.length;
                    start = Math.floor(this._zoom.start / 100 * length);
                    end = Math.ceil(this._zoom.end / 100 * length);
                    if (this.option[key][idx].type != ecConfig.CHART_TYPE_SCATTER) {
                        this.option[key][idx].data = data.slice(start, end);
                    }
                    else {
                        // 散点图特殊处理
                        this.option[key][idx].data = this._synScatterData(idx, data);
K
kener 已提交
759
                    }
K
kener 已提交
760 761 762
                }
            }

K
kener 已提交
763 764
            if (!this._isSilence && (this.zoomOption.realtime || dispatchNow)) {
                this.messageCenter.dispatch(
K
kener 已提交
765 766
                    ecConfig.EVENT.DATA_ZOOM,
                    null,
K
kener 已提交
767 768
                    {zoom: this._zoom},
                    this.myChart
K
kener 已提交
769
                );
K
kener 已提交
770 771
            }

K
kener 已提交
772 773
            //this.zoomOption.start = this._zoom.start;
            //this.zoomOption.end = this._zoom.end;
K
kener 已提交
774
        },
K
kener 已提交
775
        
K
kener 已提交
776
        _synScatterData : function (seriesIndex, data) {
K
jshint  
kener 已提交
777
            if (this._zoom.start === 0 
K
kener 已提交
778
                && this._zoom.end == 100
K
jshint  
kener 已提交
779
                && this._zoom.start2 === 0 
K
kener 已提交
780 781 782 783
                && this._zoom.end2 == 100
            ) {
                return data;
            }
K
kener 已提交
784
            var newData = [];
K
kener 已提交
785
            var scale = this._zoom.scatterMap[seriesIndex];
K
kener 已提交
786 787 788 789 790 791
            var total;
            var xStart;
            var xEnd;
            var yStart;
            var yEnd;
            
K
kener 已提交
792
            if (this.zoomOption.orient == 'horizontal') {
K
kener 已提交
793
                total = scale.x.max - scale.x.min;
K
kener 已提交
794 795
                xStart = this._zoom.start / 100 * total + scale.x.min;
                xEnd = this._zoom.end / 100 * total + scale.x.min;
K
kener 已提交
796 797
                
                total = scale.y.max - scale.y.min;
K
kener 已提交
798 799
                yStart = this._zoom.start2 / 100 * total + scale.y.min;
                yEnd = this._zoom.end2 / 100 * total + scale.y.min;
K
kener 已提交
800 801 802
            }
            else {
                total = scale.x.max - scale.x.min;
K
kener 已提交
803 804
                xStart = this._zoom.start2 / 100 * total + scale.x.min;
                xEnd = this._zoom.end2 / 100 * total + scale.x.min;
K
kener 已提交
805 806
                
                total = scale.y.max - scale.y.min;
K
kener 已提交
807 808
                yStart = this._zoom.start / 100 * total + scale.y.min;
                yEnd = this._zoom.end / 100 * total + scale.y.min;
K
kener 已提交
809 810 811
            }
            
            // console.log(xStart,xEnd,yStart,yEnd);
K
kener 已提交
812
            var value;
K
kener 已提交
813
            for (var i = 0, l = data.length; i < l; i++) {
K
kener 已提交
814 815 816 817 818
                value = data[i].value || data[i];
                if (value[0] >= xStart 
                    && value[0] <= xEnd
                    && value[1] >= yStart
                    && value[1] <= yEnd
K
kener 已提交
819 820 821 822 823 824
                ) {
                    newData.push(data[i]);
                }
            }
            
            return newData;
K
kener 已提交
825
        },
K
kener 已提交
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
        /**
         * 拖拽范围控制
         */
        __ondrift : function (shape, dx, dy) {
            if (this.zoomOption.zoomLock) {
                // zoomLock时把handle转成filler的拖拽
                shape = this._fillerShae;
            }
            
            var detailSize = shape._type == 'filler' ? this._handleSize : 0;
            if (this.zoomOption.orient == 'horizontal') {
                if (shape.style.x + dx - detailSize <= this._location.x) {
                    shape.style.x = this._location.x + detailSize;
                }
                else if (shape.style.x + dx + shape.style.width + detailSize
                         >= this._location.x + this._location.width
                ) {
                    shape.style.x = this._location.x + this._location.width
                                - shape.style.width - detailSize;
                }
                else {
                    shape.style.x += dx;
                }
            }
            else {
                if (shape.style.y + dy - detailSize <= this._location.y) {
                    shape.style.y = this._location.y + detailSize;
                }
                else if (shape.style.y + dy + shape.style.height + detailSize
                         >= this._location.y + this._location.height
                ) {
                    shape.style.y = this._location.y + this._location.height
                                - shape.style.height - detailSize;
                }
                else {
                    shape.style.y += dy;
                }
            }
K
kener 已提交
864

K
kener 已提交
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
            if (shape._type == 'filler') {
                this._syncHandleShape();
            }
            else {
                this._syncFillerShape();
            }

            if (this.zoomOption.realtime) {
                this._syncData();
            }

            return true;
        },
        
        __ondragend : function () {
            this.isDragend = true;
        },
        
K
kener 已提交
883 884 885
        /**
         * 数据项被拖拽出去
         */
K
kener 已提交
886 887
        ondragend : function (param, status) {
            if (!this.isDragend || !param.target) {
K
kener 已提交
888 889 890 891
                // 没有在当前实例上发生拖拽行为则直接返回
                return;
            }

K
kener 已提交
892
            !this.zoomOption.realtime && this._syncData();
K
kener 已提交
893 894 895 896

            // 别status = {}赋值啊!!
            status.dragOut = true;
            status.dragIn = true;
K
kener 已提交
897 898
            if (!this._isSilence && !this.zoomOption.realtime) {
                this.messageCenter.dispatch(
K
kener 已提交
899 900
                    ecConfig.EVENT.DATA_ZOOM,
                    null,
K
kener 已提交
901 902
                    {zoom: this._zoom},
                    this.myChart
K
kener 已提交
903
                );
K
kener 已提交
904 905 906
            }
            status.needRefresh = false; // 会有消息触发fresh,不用再刷一遍
            // 处理完拖拽事件后复位
K
kener 已提交
907
            this.isDragend = false;
K
kener 已提交
908 909

            return;
K
kener 已提交
910
        },
K
kener 已提交
911

K
kener 已提交
912
        ondataZoom : function (param, status) {
K
kener 已提交
913 914
            status.needRefresh = true;
            return;
K
kener 已提交
915
        },
916
        
K
kener 已提交
917
        absoluteZoom : function (param) {
K
kener 已提交
918 919 920 921 922 923 924 925
            //this.zoomOption.start = 
            this._zoom.start = param.start;
            //this.zoomOption.end = 
            this._zoom.end = param.end;
            //this.zoomOption.start2 = 
            this._zoom.start2 = param.start2;
            //this.zoomOption.end2 = 
            this._zoom.end2 = param.end2;
K
kener 已提交
926 927
            this._syncShape();
            this._syncData(true);
K
kener 已提交
928
            return;
K
kener 已提交
929
        },
K
kener 已提交
930
        
K
kener 已提交
931
        rectZoom : function (param) {
932 933
            if (!param) {
                // 重置拖拽
K
kener 已提交
934 935
                //this.zoomOption.start = 
                //this.zoomOption.start2 = 
K
kener 已提交
936 937
                this._zoom.start = 
                this._zoom.start2 = 0;
K
kener 已提交
938
                    
K
kener 已提交
939 940
                //this.zoomOption.end =
                //this.zoomOption.end2 = 
K
kener 已提交
941 942
                this._zoom.end = 
                this._zoom.end2 = 100;
K
kener 已提交
943
                
K
kener 已提交
944 945 946
                this._syncShape();
                this._syncData(true);
                return this._zoom;
947
            }
K
kener 已提交
948
            var gridArea = this.component.grid.getArea();
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
            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;
            }
K
kener 已提交
964
            // console.log(rect,this._zoom);
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
            
            // 剔除无效缩放
            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 已提交
984 985
            var total;
            var sdx = (rect.x - gridArea.x) / gridArea.width;
K
kener 已提交
986 987
            var edx = 1 - (rect.x + rect.width - gridArea.x) / gridArea.width;
            var sdy = 1 - (rect.y + rect.height - gridArea.y) / gridArea.height;
K
kener 已提交
988
            var edy = (rect.y - gridArea.y) / gridArea.height;
K
kener 已提交
989 990 991 992 993
            //console.log('this',sdy,edy,this._zoom.start,this._zoom.end)
            if (this.zoomOption.orient == 'horizontal') {
                total = this._zoom.end - this._zoom.start;
                this._zoom.start += total * sdx;
                this._zoom.end -= total * edx;
K
kener 已提交
994
                
K
kener 已提交
995 996 997
                total = this._zoom.end2 - this._zoom.start2;
                this._zoom.start2 += total * sdy;
                this._zoom.end2 -= total * edy;
K
kener 已提交
998 999
            }
            else {
K
kener 已提交
1000 1001 1002
                total = this._zoom.end - this._zoom.start;
                this._zoom.start += total * sdy;
                this._zoom.end -= total * edy;
K
kener 已提交
1003
                
K
kener 已提交
1004 1005 1006 1007 1008
                total = this._zoom.end2 - this._zoom.start2;
                this._zoom.start2 += total * sdx;
                this._zoom.end2 -= total * edx;
            }
            //console.log(this._zoom.start,this._zoom.end,this._zoom.start2,this._zoom.end2)
K
kener 已提交
1009 1010 1011 1012
            //this.zoomOption.start = this._zoom.start;
            //this.zoomOption.end = this._zoom.end;
            //this.zoomOption.start2 = this._zoom.start2;
            //this.zoomOption.end2 = this._zoom.end2;
K
kener 已提交
1013 1014 1015 1016 1017
            //console.log(rect,gridArea,this._zoom,total)
            this._syncShape();
            this._syncData(true);
            return this._zoom;
        },
K
kener 已提交
1018
        
K
kener 已提交
1019
        syncBackupData : function (curOption) {
K
kener 已提交
1020
            var start;
K
kener 已提交
1021
            var target = this._originalData['series'];
K
kener 已提交
1022 1023 1024 1025 1026 1027
            var curSeries = curOption.series;
            var curData;
            for (var i = 0, l = curSeries.length; i < l; i++) {
                curData = curSeries[i].data;
                if (target[i]) {
                    // dataZoom接管的
K
kener 已提交
1028
                    start = Math.floor(this._zoom.start / 100 * target[i].length);
K
kener 已提交
1029 1030 1031 1032 1033 1034
                }
                else {
                    // 非dataZoom接管
                    start = 0;
                }
                for (var j = 0, k = curData.length; j < k; j++) {
K
kener 已提交
1035
                    //optionBackup.series[i].data[j + start] = curData[j];
K
kener 已提交
1036 1037
                    if (target[i]) {
                        // 同步内部备份
K
kener 已提交
1038
                        target[i][j + start] = curData[j];
K
kener 已提交
1039 1040 1041
                    }
                }
            }
K
kener 已提交
1042
        },
1043
        
K
kener 已提交
1044 1045 1046 1047 1048 1049 1050 1051 1052
        syncOption : function(magicOption) {
            this.silence(true);
            this.option = magicOption;
            
            this.clear();
            // 位置参数,通过计算所得x, y, width, height
            this._location = this._getLocation();
            // 缩放参数
            this._zoom =  this._getZoom();
K
kener 已提交
1053 1054
            
            this._backupData();
K
kener 已提交
1055
            if (this.option.dataZoom && this.option.dataZoom.show) {
K
kener 已提交
1056 1057 1058 1059 1060 1061 1062
                this._buildShape();
            }
            this._syncData();
            
            this.silence(false);
        },
        
K
kener 已提交
1063 1064 1065
        silence : function (s) {
            this._isSilence = s;
        },
1066
        
K
kener 已提交
1067
        getRealDataIndex : function (sIdx, dIdx) {
K
jshint  
kener 已提交
1068
            if (!this._originalData || (this._zoom.start === 0 && this._zoom.end == 100)) {
K
kener 已提交
1069 1070
                return dIdx;
            }
K
kener 已提交
1071
            var sreies = this._originalData.series;
1072
            if (sreies[sIdx]) {
K
kener 已提交
1073
                return Math.floor(this._zoom.start / 100 * sreies[sIdx].length) + dIdx;
1074 1075
            }
            return -1;
K
kener 已提交
1076
        },
K
kener 已提交
1077

K
kener 已提交
1078 1079 1080
        /**
         * 避免dataZoom带来两次refresh,不设refresh接口,resize重复一下buildshape逻辑 
         */
K
kener 已提交
1081 1082
        resize : function () {
            this.clear();
K
kener 已提交
1083
            
K
kener 已提交
1084 1085 1086 1087
            // 位置参数,通过计算所得x, y, width, height
            this._location = this._getLocation();
            // 缩放参数
            this._zoom =  this._getZoom();
K
kener 已提交
1088
            
K
kener 已提交
1089 1090
            if (this.option.dataZoom.show) {
                this._buildShape();
K
kener 已提交
1091 1092
            }
        }
K
kener 已提交
1093 1094 1095 1096
    };
    
    zrUtil.inherits(DataZoom, Base);
    
1097 1098
    require('../component').define('dataZoom', DataZoom);
    
K
kener 已提交
1099 1100
    return DataZoom;
});