echarts.js 70.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/
S
sushuang 已提交
19
import {__DEV__} from './config';
S
sushuang 已提交
20 21 22 23 24 25
import * as zrender from 'zrender/src/zrender';
import * as zrUtil from 'zrender/src/core/util';
import * as colorTool from 'zrender/src/tool/color';
import env from 'zrender/src/core/env';
import timsort from 'zrender/src/core/timsort';
import Eventful from 'zrender/src/mixin/Eventful';
S
sushuang 已提交
26 27 28 29 30
import GlobalModel from './model/Global';
import ExtensionAPI from './ExtensionAPI';
import CoordinateSystemManager from './CoordinateSystem';
import OptionManager from './model/OptionManager';
import backwardCompat from './preprocessor/backwardCompat';
S
sushuang 已提交
31
import dataStack from './processor/dataStack';
S
sushuang 已提交
32 33 34 35 36 37 38 39
import ComponentModel from './model/Component';
import SeriesModel from './model/Series';
import ComponentView from './view/Component';
import ChartView from './view/Chart';
import * as graphic from './util/graphic';
import * as modelUtil from './util/model';
import {throttle} from './util/throttle';
import seriesColor from './visual/seriesColor';
O
Ovilia 已提交
40
import aria from './visual/aria';
S
sushuang 已提交
41
import loadingDefault from './loading/default';
S
tweak  
sushuang 已提交
42
import Scheduler from './stream/Scheduler';
P
pissang 已提交
43 44
import lightTheme from './theme/light';
import darkTheme from './theme/dark';
S
sushuang 已提交
45
import './component/dataset';
S
sushuang 已提交
46
import mapDataStorage from './coord/geo/mapDataStorage';
S
sushuang 已提交
47

S
sushuang 已提交
48
var assert = zrUtil.assert;
S
sushuang 已提交
49
var each = zrUtil.each;
S
sushuang 已提交
50 51
var isFunction = zrUtil.isFunction;
var isObject = zrUtil.isObject;
S
sushuang 已提交
52
var parseClassType = ComponentModel.parseClassType;
L
lang 已提交
53

S
SHUANG SU 已提交
54
export var version = '4.6.0';
55

S
sushuang 已提交
56
export var dependencies = {
S
SHUANG SU 已提交
57
    zrender: '4.2.0'
S
sushuang 已提交
58
};
59

S
sushuang 已提交
60
var TEST_FRAME_REMAIN_TIME = 1;
S
sushuang 已提交
61

S
sushuang 已提交
62
var PRIORITY_PROCESSOR_FILTER = 1000;
63 64
var PRIORITY_PROCESSOR_SERIES_FILTER = 800;
var PRIORITY_PROCESSOR_DATASTACK = 900;
S
sushuang 已提交
65 66 67
var PRIORITY_PROCESSOR_STATISTIC = 5000;

var PRIORITY_VISUAL_LAYOUT = 1000;
S
sushuang 已提交
68
var PRIORITY_VISUAL_PROGRESSIVE_LAYOUT = 1100;
S
sushuang 已提交
69 70
var PRIORITY_VISUAL_GLOBAL = 2000;
var PRIORITY_VISUAL_CHART = 3000;
71
var PRIORITY_VISUAL_POST_CHART_LAYOUT = 3500;
S
sushuang 已提交
72 73 74 75 76
var PRIORITY_VISUAL_COMPONENT = 4000;
// FIXME
// necessary?
var PRIORITY_VISUAL_BRUSH = 5000;

S
sushuang 已提交
77 78 79
export var PRIORITY = {
    PROCESSOR: {
        FILTER: PRIORITY_PROCESSOR_FILTER,
80
        SERIES_FILTER: PRIORITY_PROCESSOR_SERIES_FILTER,
S
sushuang 已提交
81 82 83 84
        STATISTIC: PRIORITY_PROCESSOR_STATISTIC
    },
    VISUAL: {
        LAYOUT: PRIORITY_VISUAL_LAYOUT,
S
sushuang 已提交
85
        PROGRESSIVE_LAYOUT: PRIORITY_VISUAL_PROGRESSIVE_LAYOUT,
S
sushuang 已提交
86 87
        GLOBAL: PRIORITY_VISUAL_GLOBAL,
        CHART: PRIORITY_VISUAL_CHART,
88
        POST_CHART_LAYOUT: PRIORITY_VISUAL_POST_CHART_LAYOUT,
S
sushuang 已提交
89 90 91 92
        COMPONENT: PRIORITY_VISUAL_COMPONENT,
        BRUSH: PRIORITY_VISUAL_BRUSH
    }
};
93

S
sushuang 已提交
94 95 96 97 98 99 100 101
// Main process have three entries: `setOption`, `dispatchAction` and `resize`,
// where they must not be invoked nestedly, except the only case: invoke
// dispatchAction with updateMethod "none" in main process.
// This flag is used to carry out this rule.
// All events will be triggered out side main process (i.e. when !this[IN_MAIN_PROCESS]).
var IN_MAIN_PROCESS = '__flagInMainProcess';
var OPTION_UPDATED = '__optionUpdated';
var ACTION_REG = /^[a-zA-Z0-9_]+$/;
L
lang 已提交
102

L
lang 已提交
103

104
function createRegisterEventWithLowercaseName(method, ignoreDisposed) {
S
sushuang 已提交
105
    return function (eventName, handler, context) {
106 107 108 109 110
        if (!ignoreDisposed && this._disposed) {
            disposedWarning(this.id);
            return;
        }

S
sushuang 已提交
111 112 113 114 115
        // Event name is all lowercase
        eventName = eventName && eventName.toLowerCase();
        Eventful.prototype[method].call(this, eventName, handler, context);
    };
}
L
lang 已提交
116

S
sushuang 已提交
117 118 119 120 121 122
/**
 * @module echarts~MessageCenter
 */
function MessageCenter() {
    Eventful.call(this);
}
123 124 125
MessageCenter.prototype.on = createRegisterEventWithLowercaseName('on', true);
MessageCenter.prototype.off = createRegisterEventWithLowercaseName('off', true);
MessageCenter.prototype.one = createRegisterEventWithLowercaseName('one', true);
S
sushuang 已提交
126
zrUtil.mixin(MessageCenter, Eventful);
127

S
sushuang 已提交
128 129 130 131 132
/**
 * @module echarts~ECharts
 */
function ECharts(dom, theme, opts) {
    opts = opts || {};
133

S
sushuang 已提交
134 135 136
    // Get theme by name
    if (typeof theme === 'string') {
        theme = themeStorage[theme];
L
lang 已提交
137
    }
L
lang 已提交
138

139
    /**
S
sushuang 已提交
140
     * @type {string}
141
     */
S
sushuang 已提交
142
    this.id;
S
sushuang 已提交
143

144
    /**
S
sushuang 已提交
145 146
     * Group id
     * @type {string}
147
     */
S
sushuang 已提交
148
    this.group;
S
sushuang 已提交
149

150
    /**
S
sushuang 已提交
151 152
     * @type {HTMLElement}
     * @private
153
     */
S
sushuang 已提交
154
    this._dom = dom;
S
sushuang 已提交
155 156 157 158

    var defaultRenderer = 'canvas';
    if (__DEV__) {
        defaultRenderer = (
P
pissang 已提交
159
            typeof window === 'undefined' ? global : window
S
sushuang 已提交
160 161 162
        ).__ECHARTS__DEFAULT__RENDERER__ || defaultRenderer;
    }

L
Tweak  
lang 已提交
163
    /**
S
sushuang 已提交
164 165
     * @type {module:zrender/ZRender}
     * @private
L
Tweak  
lang 已提交
166
     */
S
sushuang 已提交
167
    var zr = this._zr = zrender.init(dom, {
S
sushuang 已提交
168
        renderer: opts.renderer || defaultRenderer,
S
sushuang 已提交
169 170 171 172
        devicePixelRatio: opts.devicePixelRatio,
        width: opts.width,
        height: opts.height
    });
P
pah100 已提交
173

L
tweak  
lang 已提交
174
    /**
D
deqingli 已提交
175
     * Expect 60 fps.
S
sushuang 已提交
176 177
     * @type {Function}
     * @private
L
tweak  
lang 已提交
178
     */
S
sushuang 已提交
179
    this._throttledZrFlush = throttle(zrUtil.bind(zr.flush, zr), 17);
L
lang 已提交
180

S
sushuang 已提交
181 182
    var theme = zrUtil.clone(theme);
    theme && backwardCompat(theme, true);
L
lang 已提交
183
    /**
S
sushuang 已提交
184 185
     * @type {Object}
     * @private
L
lang 已提交
186
     */
S
sushuang 已提交
187
    this._theme = theme;
L
lang 已提交
188

L
tweak  
lang 已提交
189
    /**
S
sushuang 已提交
190 191
     * @type {Array.<module:echarts/view/Chart>}
     * @private
L
tweak  
lang 已提交
192
     */
S
sushuang 已提交
193
    this._chartsViews = [];
L
lang 已提交
194

L
tweak  
lang 已提交
195
    /**
S
sushuang 已提交
196 197
     * @type {Object.<string, module:echarts/view/Chart>}
     * @private
L
tweak  
lang 已提交
198
     */
S
sushuang 已提交
199
    this._chartsMap = {};
L
lang 已提交
200

201
    /**
S
sushuang 已提交
202 203
     * @type {Array.<module:echarts/view/Component>}
     * @private
204
     */
S
sushuang 已提交
205
    this._componentsViews = [];
206

L
lang 已提交
207
    /**
S
sushuang 已提交
208 209
     * @type {Object.<string, module:echarts/view/Component>}
     * @private
L
lang 已提交
210
     */
S
sushuang 已提交
211 212
    this._componentsMap = {};

L
lang 已提交
213
    /**
S
sushuang 已提交
214 215
     * @type {module:echarts/CoordinateSystem}
     * @private
L
lang 已提交
216
     */
S
sushuang 已提交
217
    this._coordSysMgr = new CoordinateSystemManager();
L
lang 已提交
218 219

    /**
S
sushuang 已提交
220 221
     * @type {module:echarts/ExtensionAPI}
     * @private
L
lang 已提交
222
     */
S
sushuang 已提交
223
    var api = this._api = createExtensionAPI(this);
L
lang 已提交
224

225 226 227 228 229 230 231
    // Sort on demand
    function prioritySortFunc(a, b) {
        return a.__prio - b.__prio;
    }
    timsort(visualFuncs, prioritySortFunc);
    timsort(dataProcessorFuncs, prioritySortFunc);

S
sushuang 已提交
232
    /**
S
tweak  
sushuang 已提交
233
     * @type {module:echarts/stream/Scheduler}
S
sushuang 已提交
234
     */
235
    this._scheduler = new Scheduler(this, api, dataProcessorFuncs, visualFuncs);
S
sushuang 已提交
236

S
sushuang 已提交
237
    Eventful.call(this, this._ecEventProcessor = new EventProcessor());
238

1
100pah 已提交
239
    /**
S
sushuang 已提交
240 241
     * @type {module:echarts~MessageCenter}
     * @private
1
100pah 已提交
242
     */
S
sushuang 已提交
243
    this._messageCenter = new MessageCenter();
1
100pah 已提交
244

S
sushuang 已提交
245 246
    // Init mouse events
    this._initEvents();
1
100pah 已提交
247

S
sushuang 已提交
248 249
    // In case some people write `window.onresize = chart.resize`
    this.resize = zrUtil.bind(this.resize, this);
1
100pah 已提交
250

S
sushuang 已提交
251 252
    // Can't dispatch action during rendering procedure
    this._pendingActions = [];
1
100pah 已提交
253

S
sushuang 已提交
254
    zr.animation.on('frame', this._onframe, this);
1
100pah 已提交
255

S
sushuang 已提交
256 257
    bindRenderedEvent(zr, this);

S
sushuang 已提交
258 259 260
    // ECharts instance can be used as value.
    zrUtil.setAsPrimitive(this);
}
1
100pah 已提交
261

S
sushuang 已提交
262
var echartsProto = ECharts.prototype;
1
100pah 已提交
263

S
sushuang 已提交
264
echartsProto._onframe = function () {
S
tweak  
sushuang 已提交
265
    if (this._disposed) {
S
sushuang 已提交
266 267 268
        return;
    }

S
sushuang 已提交
269 270
    var scheduler = this._scheduler;

S
sushuang 已提交
271 272 273
    // Lazy update
    if (this[OPTION_UPDATED]) {
        var silent = this[OPTION_UPDATED].silent;
1
100pah 已提交
274

S
sushuang 已提交
275
        this[IN_MAIN_PROCESS] = true;
1
100pah 已提交
276

S
sushuang 已提交
277 278
        prepare(this);
        updateMethods.update.call(this);
1
100pah 已提交
279

S
sushuang 已提交
280
        this[IN_MAIN_PROCESS] = false;
281

S
sushuang 已提交
282
        this[OPTION_UPDATED] = false;
283

S
sushuang 已提交
284
        flushPendingActions.call(this, silent);
285

S
sushuang 已提交
286 287
        triggerUpdatedEvent.call(this, silent);
    }
S
sushuang 已提交
288
    // Avoid do both lazy update and progress in one frame.
S
sushuang 已提交
289
    else if (scheduler.unfinished) {
S
sushuang 已提交
290 291 292
        // Stream progress.
        var remainTime = TEST_FRAME_REMAIN_TIME;
        var ecModel = this._model;
S
sushuang 已提交
293 294 295 296
        var api = this._api;
        scheduler.unfinished = false;
        do {
            var startTime = +new Date();
S
tweak  
sushuang 已提交
297

S
sushuang 已提交
298
            scheduler.performSeriesTasks(ecModel);
S
tweak  
sushuang 已提交
299

S
sushuang 已提交
300
            // Currently dataProcessorFuncs do not check threshold.
301
            scheduler.performDataProcessorTasks(ecModel);
S
sushuang 已提交
302

S
sushuang 已提交
303
            updateStreamModes(this, ecModel);
S
sushuang 已提交
304

S
sushuang 已提交
305 306 307 308
            // Do not update coordinate system here. Because that coord system update in
            // each frame is not a good user experience. So we follow the rule that
            // the extent of the coordinate system is determin in the first frame (the
            // frame is executed immedietely after task reset.
S
sushuang 已提交
309
            // this._coordSysMgr.update(ecModel, api);
S
sushuang 已提交
310

S
sushuang 已提交
311
            // console.log('--- ec frame visual ---', remainTime);
312
            scheduler.performVisualTasks(ecModel);
S
tweak  
sushuang 已提交
313

S
sushuang 已提交
314
            renderSeries(this, this._model, api, 'remain');
S
sushuang 已提交
315

S
sushuang 已提交
316 317 318
            remainTime -= (+new Date() - startTime);
        }
        while (remainTime > 0 && scheduler.unfinished);
S
sushuang 已提交
319

S
sushuang 已提交
320
        // Call flush explicitly for trigger finished event.
S
sushuang 已提交
321
        if (!scheduler.unfinished) {
S
sushuang 已提交
322
            this._zr.flush();
S
tweak  
sushuang 已提交
323
        }
S
sushuang 已提交
324 325
        // Else, zr flushing be ensue within the same frame,
        // because zr flushing is after onframe event.
S
sushuang 已提交
326
    }
S
tweak  
sushuang 已提交
327
};
S
sushuang 已提交
328

S
sushuang 已提交
329 330 331 332 333 334
/**
 * @return {HTMLElement}
 */
echartsProto.getDom = function () {
    return this._dom;
};
335

S
sushuang 已提交
336 337 338 339 340 341
/**
 * @return {module:zrender~ZRender}
 */
echartsProto.getZr = function () {
    return this._zr;
};
342

S
sushuang 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
/**
 * Usage:
 * chart.setOption(option, notMerge, lazyUpdate);
 * chart.setOption(option, {
 *     notMerge: ...,
 *     lazyUpdate: ...,
 *     silent: ...
 * });
 *
 * @param {Object} option
 * @param {Object|boolean} [opts] opts or notMerge.
 * @param {boolean} [opts.notMerge=false]
 * @param {boolean} [opts.lazyUpdate=false] Useful when setOption frequently.
 */
echartsProto.setOption = function (option, notMerge, lazyUpdate) {
    if (__DEV__) {
S
sushuang 已提交
359
        assert(!this[IN_MAIN_PROCESS], '`setOption` should not be called during main process.');
S
sushuang 已提交
360
    }
361 362 363 364
    if (this._disposed) {
        disposedWarning(this.id);
        return;
    }
365

S
sushuang 已提交
366
    var silent;
S
sushuang 已提交
367
    if (isObject(notMerge)) {
S
sushuang 已提交
368 369 370 371
        lazyUpdate = notMerge.lazyUpdate;
        silent = notMerge.silent;
        notMerge = notMerge.notMerge;
    }
372

S
sushuang 已提交
373
    this[IN_MAIN_PROCESS] = true;
374

S
sushuang 已提交
375 376 377
    if (!this._model || notMerge) {
        var optionManager = new OptionManager(this._api);
        var theme = this._theme;
378
        var ecModel = this._model = new GlobalModel();
S
tweak  
sushuang 已提交
379
        ecModel.scheduler = this._scheduler;
S
sushuang 已提交
380 381
        ecModel.init(null, null, theme, optionManager);
    }
382

S
sushuang 已提交
383
    this._model.setOption(option, optionPreprocessorFuncs);
1
100pah 已提交
384

S
sushuang 已提交
385 386 387 388 389
    if (lazyUpdate) {
        this[OPTION_UPDATED] = {silent: silent};
        this[IN_MAIN_PROCESS] = false;
    }
    else {
S
sushuang 已提交
390 391 392 393
        prepare(this);

        updateMethods.update.call(this);

S
sushuang 已提交
394 395 396
        // Ensure zr refresh sychronously, and then pixel in canvas can be
        // fetched after `setOption`.
        this._zr.flush();
L
lang 已提交
397

S
sushuang 已提交
398 399
        this[OPTION_UPDATED] = false;
        this[IN_MAIN_PROCESS] = false;
L
lang 已提交
400

S
sushuang 已提交
401 402 403 404 405 406 407 408 409
        flushPendingActions.call(this, silent);
        triggerUpdatedEvent.call(this, silent);
    }
};

/**
 * @DEPRECATED
 */
echartsProto.setTheme = function () {
S
sushuang 已提交
410
    console.error('ECharts#setTheme() is DEPRECATED in ECharts 3.0');
S
sushuang 已提交
411
};
L
lang 已提交
412

S
sushuang 已提交
413 414 415 416 417 418
/**
 * @return {module:echarts/model/Global}
 */
echartsProto.getModel = function () {
    return this._model;
};
419

S
sushuang 已提交
420 421 422 423 424 425
/**
 * @return {Object}
 */
echartsProto.getOption = function () {
    return this._model && this._model.getOption();
};
P
pah100 已提交
426

S
sushuang 已提交
427 428 429 430 431 432
/**
 * @return {number}
 */
echartsProto.getWidth = function () {
    return this._zr.getWidth();
};
433

S
sushuang 已提交
434 435 436 437 438 439
/**
 * @return {number}
 */
echartsProto.getHeight = function () {
    return this._zr.getHeight();
};
L
lang 已提交
440

S
sushuang 已提交
441 442 443 444 445 446
/**
 * @return {number}
 */
echartsProto.getDevicePixelRatio = function () {
    return this._zr.painter.dpr || window.devicePixelRatio || 1;
};
L
lang 已提交
447

S
sushuang 已提交
448 449 450 451
/**
 * Get canvas which has all thing rendered
 * @param {Object} opts
 * @param {string} [opts.backgroundColor]
S
sushuang 已提交
452
 * @return {string}
S
sushuang 已提交
453 454 455 456 457 458 459 460 461 462
 */
echartsProto.getRenderedCanvas = function (opts) {
    if (!env.canvasSupported) {
        return;
    }
    opts = opts || {};
    opts.pixelRatio = opts.pixelRatio || 1;
    opts.backgroundColor = opts.backgroundColor
        || this._model.get('backgroundColor');
    var zr = this._zr;
S
sushuang 已提交
463
    // var list = zr.storage.getDisplayList();
S
sushuang 已提交
464
    // Stop animations
S
sushuang 已提交
465 466 467 468
    // Never works before in init animation, so remove it.
    // zrUtil.each(list, function (el) {
    //     el.stopAnimation(true);
    // });
S
sushuang 已提交
469 470
    return zr.painter.getRenderedCanvas(opts);
};
O
Ovilia 已提交
471

S
sushuang 已提交
472 473 474 475 476 477 478 479
/**
 * Get svg data url
 * @return {string}
 */
echartsProto.getSvgDataUrl = function () {
    if (!env.svgSupported) {
        return;
    }
O
Ovilia 已提交
480

S
sushuang 已提交
481 482 483 484 485 486
    var zr = this._zr;
    var list = zr.storage.getDisplayList();
    // Stop animations
    zrUtil.each(list, function (el) {
        el.stopAnimation(true);
    });
487

488
    return zr.painter.pathToDataUrl();
S
sushuang 已提交
489
};
490

S
sushuang 已提交
491 492 493 494 495 496 497 498 499
/**
 * @return {string}
 * @param {Object} opts
 * @param {string} [opts.type='png']
 * @param {string} [opts.pixelRatio=1]
 * @param {string} [opts.backgroundColor]
 * @param {string} [opts.excludeComponents]
 */
echartsProto.getDataURL = function (opts) {
500 501 502 503 504
    if (this._disposed) {
        disposedWarning(this.id);
        return;
    }

S
sushuang 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
    opts = opts || {};
    var excludeComponents = opts.excludeComponents;
    var ecModel = this._model;
    var excludesComponentViews = [];
    var self = this;

    each(excludeComponents, function (componentType) {
        ecModel.eachComponent({
            mainType: componentType
        }, function (component) {
            var view = self._componentsMap[component.__viewId];
            if (!view.group.ignore) {
                excludesComponentViews.push(view);
                view.group.ignore = true;
            }
        });
    });
L
lang 已提交
522

S
sushuang 已提交
523 524 525 526 527
    var url = this._zr.painter.getType() === 'svg'
        ? this.getSvgDataUrl()
        : this.getRenderedCanvas(opts).toDataURL(
            'image/' + (opts && opts.type || 'png')
        );
528

S
sushuang 已提交
529 530 531
    each(excludesComponentViews, function (view) {
        view.group.ignore = false;
    });
L
lang 已提交
532

S
sushuang 已提交
533 534
    return url;
};
535

536

S
sushuang 已提交
537 538 539 540 541 542 543 544
/**
 * @return {string}
 * @param {Object} opts
 * @param {string} [opts.type='png']
 * @param {string} [opts.pixelRatio=1]
 * @param {string} [opts.backgroundColor]
 */
echartsProto.getConnectedDataURL = function (opts) {
545 546 547 548 549
    if (this._disposed) {
        disposedWarning(this.id);
        return;
    }

S
sushuang 已提交
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
    if (!env.canvasSupported) {
        return;
    }
    var groupId = this.group;
    var mathMin = Math.min;
    var mathMax = Math.max;
    var MAX_NUMBER = Infinity;
    if (connectedGroups[groupId]) {
        var left = MAX_NUMBER;
        var top = MAX_NUMBER;
        var right = -MAX_NUMBER;
        var bottom = -MAX_NUMBER;
        var canvasList = [];
        var dpr = (opts && opts.pixelRatio) || 1;

        zrUtil.each(instances, function (chart, id) {
            if (chart.group === groupId) {
                var canvas = chart.getRenderedCanvas(
                    zrUtil.clone(opts)
                );
                var boundingRect = chart.getDom().getBoundingClientRect();
                left = mathMin(boundingRect.left, left);
                top = mathMin(boundingRect.top, top);
                right = mathMax(boundingRect.right, right);
                bottom = mathMax(boundingRect.bottom, bottom);
                canvasList.push({
                    dom: canvas,
                    left: boundingRect.left,
                    top: boundingRect.top
L
lang 已提交
579 580
                });
            }
S
sushuang 已提交
581
        });
L
lang 已提交
582

S
sushuang 已提交
583 584 585 586 587 588 589 590 591 592 593
        left *= dpr;
        top *= dpr;
        right *= dpr;
        bottom *= dpr;
        var width = right - left;
        var height = bottom - top;
        var targetCanvas = zrUtil.createCanvas();
        targetCanvas.width = width;
        targetCanvas.height = height;
        var zr = zrender.init(targetCanvas);

594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
        // Background between the charts
        if (opts.connectedBackgroundColor) {
            zr.add(new graphic.Rect({
                shape: {
                    x: 0,
                    y: 0,
                    width: width,
                    height: height
                },
                style: {
                    fill: opts.connectedBackgroundColor
                }
            }));
        }

S
sushuang 已提交
609 610 611 612 613 614
        each(canvasList, function (item) {
            var img = new graphic.Image({
                style: {
                    x: item.left * dpr - left,
                    y: item.top * dpr - top,
                    image: item.dom
L
lang 已提交
615
                }
S
sushuang 已提交
616 617 618 619
            });
            zr.add(img);
        });
        zr.refreshImmediately();
L
lang 已提交
620

S
sushuang 已提交
621 622 623 624 625 626
        return targetCanvas.toDataURL('image/' + (opts && opts.type || 'png'));
    }
    else {
        return this.getDataURL(opts);
    }
};
L
lang 已提交
627

S
sushuang 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
/**
 * Convert from logical coordinate system to pixel coordinate system.
 * See CoordinateSystem#convertToPixel.
 * @param {string|Object} finder
 *        If string, e.g., 'geo', means {geoIndex: 0}.
 *        If Object, could contain some of these properties below:
 *        {
 *            seriesIndex / seriesId / seriesName,
 *            geoIndex / geoId, geoName,
 *            bmapIndex / bmapId / bmapName,
 *            xAxisIndex / xAxisId / xAxisName,
 *            yAxisIndex / yAxisId / yAxisName,
 *            gridIndex / gridId / gridName,
 *            ... (can be extended)
 *        }
 * @param {Array|number} value
 * @return {Array|number} result
 */
echartsProto.convertToPixel = zrUtil.curry(doConvertPixel, 'convertToPixel');
647

S
sushuang 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
/**
 * Convert from pixel coordinate system to logical coordinate system.
 * See CoordinateSystem#convertFromPixel.
 * @param {string|Object} finder
 *        If string, e.g., 'geo', means {geoIndex: 0}.
 *        If Object, could contain some of these properties below:
 *        {
 *            seriesIndex / seriesId / seriesName,
 *            geoIndex / geoId / geoName,
 *            bmapIndex / bmapId / bmapName,
 *            xAxisIndex / xAxisId / xAxisName,
 *            yAxisIndex / yAxisId / yAxisName
 *            gridIndex / gridId / gridName,
 *            ... (can be extended)
 *        }
 * @param {Array|number} value
 * @return {Array|number} result
 */
echartsProto.convertFromPixel = zrUtil.curry(doConvertPixel, 'convertFromPixel');
667

S
sushuang 已提交
668
function doConvertPixel(methodName, finder, value) {
669 670 671 672 673
    if (this._disposed) {
        disposedWarning(this.id);
        return;
    }

S
sushuang 已提交
674 675 676
    var ecModel = this._model;
    var coordSysList = this._coordSysMgr.getCoordinateSystems();
    var result;
677

S
sushuang 已提交
678
    finder = modelUtil.parseFinder(ecModel, finder);
679

S
sushuang 已提交
680 681 682 683 684 685 686 687
    for (var i = 0; i < coordSysList.length; i++) {
        var coordSys = coordSysList[i];
        if (coordSys[methodName]
            && (result = coordSys[methodName](ecModel, finder, value)) != null
        ) {
            return result;
        }
    }
P
pah100 已提交
688

S
sushuang 已提交
689 690 691 692 693 694
    if (__DEV__) {
        console.warn(
            'No coordinate system that supports ' + methodName + ' found by the given finder.'
        );
    }
}
695

S
sushuang 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
/**
 * Is the specified coordinate systems or components contain the given pixel point.
 * @param {string|Object} finder
 *        If string, e.g., 'geo', means {geoIndex: 0}.
 *        If Object, could contain some of these properties below:
 *        {
 *            seriesIndex / seriesId / seriesName,
 *            geoIndex / geoId / geoName,
 *            bmapIndex / bmapId / bmapName,
 *            xAxisIndex / xAxisId / xAxisName,
 *            yAxisIndex / yAxisId / yAxisName,
 *            gridIndex / gridId / gridName,
 *            ... (can be extended)
 *        }
 * @param {Array|number} value
 * @return {boolean} result
 */
echartsProto.containPixel = function (finder, value) {
714 715 716 717 718
    if (this._disposed) {
        disposedWarning(this.id);
        return;
    }

S
sushuang 已提交
719 720
    var ecModel = this._model;
    var result;
721

S
sushuang 已提交
722
    finder = modelUtil.parseFinder(ecModel, finder);
723

S
sushuang 已提交
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
    zrUtil.each(finder, function (models, key) {
        key.indexOf('Models') >= 0 && zrUtil.each(models, function (model) {
            var coordSys = model.coordinateSystem;
            if (coordSys && coordSys.containPoint) {
                result |= !!coordSys.containPoint(value);
            }
            else if (key === 'seriesModels') {
                var view = this._chartsMap[model.__viewId];
                if (view && view.containPoint) {
                    result |= view.containPoint(value, model);
                }
                else {
                    if (__DEV__) {
                        console.warn(key + ': ' + (view
                            ? 'The found component do not support containPoint.'
                            : 'No view mapping to the found component.'
                        ));
                    }
                }
743
            }
S
sushuang 已提交
744 745 746 747 748 749 750
            else {
                if (__DEV__) {
                    console.warn(key + ': containPoint is not supported');
                }
            }
        }, this);
    }, this);
751

S
sushuang 已提交
752 753
    return !!result;
};
P
pah100 已提交
754

S
sushuang 已提交
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
/**
 * Get visual from series or data.
 * @param {string|Object} finder
 *        If string, e.g., 'series', means {seriesIndex: 0}.
 *        If Object, could contain some of these properties below:
 *        {
 *            seriesIndex / seriesId / seriesName,
 *            dataIndex / dataIndexInside
 *        }
 *        If dataIndex is not specified, series visual will be fetched,
 *        but not data item visual.
 *        If all of seriesIndex, seriesId, seriesName are not specified,
 *        visual will be fetched from first series.
 * @param {string} visualType 'color', 'symbol', 'symbolSize'
 */
echartsProto.getVisual = function (finder, visualType) {
    var ecModel = this._model;
772

S
sushuang 已提交
773
    finder = modelUtil.parseFinder(ecModel, finder, {defaultMainType: 'series'});
774

S
sushuang 已提交
775
    var seriesModel = finder.seriesModel;
776

S
sushuang 已提交
777 778 779 780 781
    if (__DEV__) {
        if (!seriesModel) {
            console.warn('There is no specified seires model');
        }
    }
782

S
sushuang 已提交
783
    var data = seriesModel.getData();
784

S
sushuang 已提交
785 786 787 788 789
    var dataIndexInside = finder.hasOwnProperty('dataIndexInside')
        ? finder.dataIndexInside
        : finder.hasOwnProperty('dataIndex')
        ? data.indexOfRawIndex(finder.dataIndex)
        : null;
L
lang 已提交
790

S
sushuang 已提交
791 792 793 794
    return dataIndexInside != null
        ? data.getItemVisual(dataIndexInside, visualType)
        : data.getVisual(visualType);
};
795

S
sushuang 已提交
796 797 798 799 800 801 802 803
/**
 * Get view of corresponding component model
 * @param  {module:echarts/model/Component} componentModel
 * @return {module:echarts/view/Component}
 */
echartsProto.getViewOfComponentModel = function (componentModel) {
    return this._componentsMap[componentModel.__viewId];
};
P
pah100 已提交
804

S
sushuang 已提交
805 806 807 808 809 810 811 812
/**
 * Get view of corresponding series model
 * @param  {module:echarts/model/Series} seriesModel
 * @return {module:echarts/view/Chart}
 */
echartsProto.getViewOfSeriesModel = function (seriesModel) {
    return this._chartsMap[seriesModel.__viewId];
};
P
pah100 已提交
813

S
sushuang 已提交
814
var updateMethods = {
815

S
sushuang 已提交
816 817 818 819 820
    prepareAndUpdate: function (payload) {
        prepare(this);
        updateMethods.update.call(this, payload);
    },

821
    /**
S
sushuang 已提交
822
     * @param {Object} payload
823 824
     * @private
     */
S
sushuang 已提交
825 826
    update: function (payload) {
        // console.profile && console.profile('update');
P
pah100 已提交
827

S
sushuang 已提交
828 829 830
        var ecModel = this._model;
        var api = this._api;
        var zr = this._zr;
S
sushuang 已提交
831
        var coordSysMgr = this._coordSysMgr;
S
tweak  
sushuang 已提交
832 833
        var scheduler = this._scheduler;

S
sushuang 已提交
834 835
        // update before setOption
        if (!ecModel) {
P
pah100 已提交
836 837 838
            return;
        }

839
        scheduler.restoreData(ecModel, payload);
S
sushuang 已提交
840

S
sushuang 已提交
841
        scheduler.performSeriesTasks(ecModel);
842

S
sushuang 已提交
843 844 845
        // TODO
        // Save total ecModel here for undo/redo (after restoring data and before processing data).
        // Undo (restoration of total ecModel) can be carried out in 'action' or outside API call.
1
100pah 已提交
846

S
sushuang 已提交
847 848
        // Create new coordinate system each update
        // In LineView may save the old coordinate system and use it to get the orignal point
S
sushuang 已提交
849
        coordSysMgr.create(ecModel, api);
S
tweak  
sushuang 已提交
850

851
        scheduler.performDataProcessorTasks(ecModel, payload);
S
sushuang 已提交
852

S
sushuang 已提交
853 854
        // Current stream render is not supported in data process. So we can update
        // stream modes after data processing, where the filtered data is used to
855
        // deteming whether use progressive rendering.
S
sushuang 已提交
856
        updateStreamModes(this, ecModel);
857

858 859 860 861 862 863
        // We update stream modes before coordinate system updated, then the modes info
        // can be fetched when coord sys updating (consider the barGrid extent fix). But
        // the drawback is the full coord info can not be fetched. Fortunately this full
        // coord is not requied in stream mode updater currently.
        coordSysMgr.update(ecModel, api);

S
sushuang 已提交
864
        clearColorPalette(ecModel);
865
        scheduler.performVisualTasks(ecModel, payload);
866

S
sushuang 已提交
867
        render(this, ecModel, api, payload);
S
tweak  
sushuang 已提交
868

S
sushuang 已提交
869 870
        // Set background
        var backgroundColor = ecModel.get('backgroundColor') || 'transparent';
L
lang 已提交
871

P
pissang 已提交
872 873 874 875 876 877 878
        // In IE8
        if (!env.canvasSupported) {
            var colorArr = colorTool.parse(backgroundColor);
            backgroundColor = colorTool.stringify(colorArr, 'rgb');
            if (colorArr[3] === 0) {
                backgroundColor = 'transparent';
            }
S
sushuang 已提交
879 880
        }
        else {
P
pissang 已提交
881
            zr.setBackgroundColor(backgroundColor);
S
sushuang 已提交
882
        }
883

S
sushuang 已提交
884
        performPostUpdateFuncs(ecModel, api);
885

S
sushuang 已提交
886 887
        // console.profile && console.profileEnd('update');
    },
L
lang 已提交
888

S
sushuang 已提交
889 890 891 892 893 894 895 896 897 898 899 900 901 902
    /**
     * @param {Object} payload
     * @private
     */
    updateTransform: function (payload) {
        var ecModel = this._model;
        var ecIns = this;
        var api = this._api;

        // update before setOption
        if (!ecModel) {
            return;
        }

903
        // ChartView.markUpdateMethod(payload, 'updateTransform');
S
sushuang 已提交
904

S
sushuang 已提交
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
        var componentDirtyList = [];
        ecModel.eachComponent(function (componentType, componentModel) {
            var componentView = ecIns.getViewOfComponentModel(componentModel);
            if (componentView && componentView.__alive) {
                if (componentView.updateTransform) {
                    var result = componentView.updateTransform(componentModel, ecModel, api, payload);
                    result && result.update && componentDirtyList.push(componentView);
                }
                else {
                    componentDirtyList.push(componentView);
                }
            }
        });

        var seriesDirtyMap = zrUtil.createHashMap();
S
sushuang 已提交
920 921 922
        ecModel.eachSeries(function (seriesModel) {
            var chartView = ecIns._chartsMap[seriesModel.__viewId];
            if (chartView.updateTransform) {
S
sushuang 已提交
923 924
                var result = chartView.updateTransform(seriesModel, ecModel, api, payload);
                result && result.update && seriesDirtyMap.set(seriesModel.uid, 1);
S
sushuang 已提交
925 926
            }
            else {
S
sushuang 已提交
927
                seriesDirtyMap.set(seriesModel.uid, 1);
S
sushuang 已提交
928 929 930
            }
        });

S
sushuang 已提交
931
        clearColorPalette(ecModel);
S
sushuang 已提交
932
        // Keep pipe to the exist pipeline because it depends on the render task of the full pipeline.
933
        // this._scheduler.performVisualTasks(ecModel, payload, 'layout', true);
S
sushuang 已提交
934
        this._scheduler.performVisualTasks(
935
            ecModel, payload, {setDirty: true, dirtyMap: seriesDirtyMap}
S
sushuang 已提交
936 937
        );

S
sushuang 已提交
938 939
        // Currently, not call render of components. Geo render cost a lot.
        // renderComponents(ecIns, ecModel, api, payload, componentDirtyList);
S
sushuang 已提交
940
        renderSeries(ecIns, ecModel, api, payload, seriesDirtyMap);
S
sushuang 已提交
941 942 943 944

        performPostUpdateFuncs(ecModel, this._api);
    },

L
lang 已提交
945
    /**
S
sushuang 已提交
946 947
     * @param {Object} payload
     * @private
L
lang 已提交
948
     */
S
sushuang 已提交
949 950
    updateView: function (payload) {
        var ecModel = this._model;
951

S
sushuang 已提交
952 953
        // update before setOption
        if (!ecModel) {
L
lang 已提交
954 955
            return;
        }
L
lang 已提交
956

S
sushuang 已提交
957
        ChartView.markUpdateMethod(payload, 'updateView');
S
tweak  
sushuang 已提交
958

S
sushuang 已提交
959
        clearColorPalette(ecModel);
L
lang 已提交
960

S
tweak  
sushuang 已提交
961
        // Keep pipe to the exist pipeline because it depends on the render task of the full pipeline.
962
        this._scheduler.performVisualTasks(ecModel, payload, {setDirty: true});
P
pah100 已提交
963

S
sushuang 已提交
964
        render(this, this._model, this._api, payload);
S
tweak  
sushuang 已提交
965

S
sushuang 已提交
966
        performPostUpdateFuncs(ecModel, this._api);
S
sushuang 已提交
967
    },
L
lang 已提交
968

L
tweak  
lang 已提交
969 970
    /**
     * @param {Object} payload
S
sushuang 已提交
971
     * @private
L
tweak  
lang 已提交
972
     */
S
sushuang 已提交
973
    updateVisual: function (payload) {
974
        updateMethods.update.call(this, payload);
975

976
        // var ecModel = this._model;
L
lang 已提交
977

978 979 980 981
        // // update before setOption
        // if (!ecModel) {
        //     return;
        // }
S
tweak  
sushuang 已提交
982

983
        // ChartView.markUpdateMethod(payload, 'updateVisual');
984

985
        // clearColorPalette(ecModel);
986

987
        // // Keep pipe to the exist pipeline because it depends on the render task of the full pipeline.
988
        // this._scheduler.performVisualTasks(ecModel, payload, {visualType: 'visual', setDirty: true});
S
tweak  
sushuang 已提交
989

990 991 992
        // render(this, this._model, this._api, payload);

        // performPostUpdateFuncs(ecModel, this._api);
S
sushuang 已提交
993
    },
1
tweak  
100pah 已提交
994

S
sushuang 已提交
995 996 997 998 999
    /**
     * @param {Object} payload
     * @private
     */
    updateLayout: function (payload) {
1000
        updateMethods.update.call(this, payload);
S
sushuang 已提交
1001

1002
        // var ecModel = this._model;
1003

1004 1005 1006 1007
        // // update before setOption
        // if (!ecModel) {
        //     return;
        // }
S
tweak  
sushuang 已提交
1008

1009
        // ChartView.markUpdateMethod(payload, 'updateLayout');
1010

1011
        // // Keep pipe to the exist pipeline because it depends on the render task of the full pipeline.
1012 1013
        // // this._scheduler.performVisualTasks(ecModel, payload, 'layout', true);
        // this._scheduler.performVisualTasks(ecModel, payload, {setDirty: true});
S
tweak  
sushuang 已提交
1014

1015 1016 1017
        // render(this, this._model, this._api, payload);

        // performPostUpdateFuncs(ecModel, this._api);
S
sushuang 已提交
1018 1019
    }
};
S
sushuang 已提交
1020

S
sushuang 已提交
1021
function prepare(ecIns) {
S
sushuang 已提交
1022 1023
    var ecModel = ecIns._model;
    var scheduler = ecIns._scheduler;
1
tweak  
100pah 已提交
1024

S
sushuang 已提交
1025 1026
    scheduler.restorePipelines(ecModel);

1027
    scheduler.prepareStageTasks();
1
tweak  
100pah 已提交
1028

S
sushuang 已提交
1029
    prepareView(ecIns, 'component', ecModel, scheduler);
S
sushuang 已提交
1030

S
sushuang 已提交
1031
    prepareView(ecIns, 'chart', ecModel, scheduler);
S
sushuang 已提交
1032

S
sushuang 已提交
1033
    scheduler.plan();
S
sushuang 已提交
1034
}
L
lang 已提交
1035

S
sushuang 已提交
1036 1037 1038 1039 1040
/**
 * @private
 */
function updateDirectly(ecIns, method, payload, mainType, subType) {
    var ecModel = ecIns._model;
P
pah100 已提交
1041

S
sushuang 已提交
1042 1043
    // broadcast
    if (!mainType) {
1044 1045 1046
        // FIXME
        // Chart will not be update directly here, except set dirty.
        // But there is no such scenario now.
S
sushuang 已提交
1047 1048 1049
        each(ecIns._componentsViews.concat(ecIns._chartsViews), callView);
        return;
    }
1050

S
sushuang 已提交
1051 1052 1053 1054
    var query = {};
    query[mainType + 'Id'] = payload[mainType + 'Id'];
    query[mainType + 'Index'] = payload[mainType + 'Index'];
    query[mainType + 'Name'] = payload[mainType + 'Name'];
1055

S
sushuang 已提交
1056 1057
    var condition = {mainType: mainType, query: query};
    subType && (condition.subType = subType); // subType may be '' by parseClassType;
1058

1059 1060 1061 1062 1063
    var excludeSeriesId = payload.excludeSeriesId;
    if (excludeSeriesId != null) {
        excludeSeriesId = zrUtil.createHashMap(modelUtil.normalizeToArray(excludeSeriesId));
    }

S
sushuang 已提交
1064
    // If dispatchAction before setOption, do nothing.
1065 1066 1067 1068 1069 1070
    ecModel && ecModel.eachComponent(condition, function (model) {
        if (!excludeSeriesId || excludeSeriesId.get(model.id) == null) {
            callView(ecIns[
                mainType === 'series' ? '_chartsMap' : '_componentsMap'
            ][model.__viewId]);
        }
S
sushuang 已提交
1071
    }, ecIns);
1072

S
sushuang 已提交
1073 1074 1075 1076
    function callView(view) {
        view && view.__alive && view[method] && view[method](
            view.__model, ecModel, ecIns._api, payload
        );
1
tweak  
100pah 已提交
1077
    }
S
sushuang 已提交
1078
}
L
tweak  
lang 已提交
1079

S
sushuang 已提交
1080 1081 1082 1083 1084 1085 1086 1087 1088
/**
 * Resize the chart
 * @param {Object} opts
 * @param {number} [opts.width] Can be 'auto' (the same as null/undefined)
 * @param {number} [opts.height] Can be 'auto' (the same as null/undefined)
 * @param {boolean} [opts.silent=false]
 */
echartsProto.resize = function (opts) {
    if (__DEV__) {
S
sushuang 已提交
1089
        assert(!this[IN_MAIN_PROCESS], '`resize` should not be called during main process.');
1
tweak  
100pah 已提交
1090
    }
1091 1092 1093 1094
    if (this._disposed) {
        disposedWarning(this.id);
        return;
    }
L
lang 已提交
1095

S
sushuang 已提交
1096
    this._zr.resize(opts);
L
lang 已提交
1097

S
sushuang 已提交
1098 1099
    var ecModel = this._model;

P
pissang 已提交
1100 1101 1102 1103 1104 1105 1106 1107
    // Resize loading effect
    this._loadingFX && this._loadingFX.resize();

    if (!ecModel) {
        return;
    }

    var optionChanged = ecModel.resetOption('media');
S
sushuang 已提交
1108

S
sushuang 已提交
1109
    var silent = opts && opts.silent;
S
sushuang 已提交
1110

S
sushuang 已提交
1111
    this[IN_MAIN_PROCESS] = true;
S
sushuang 已提交
1112

S
sushuang 已提交
1113 1114
    optionChanged && prepare(this);
    updateMethods.update.call(this);
1115

S
sushuang 已提交
1116
    this[IN_MAIN_PROCESS] = false;
1117

S
sushuang 已提交
1118
    flushPendingActions.call(this, silent);
1119

S
sushuang 已提交
1120 1121
    triggerUpdatedEvent.call(this, silent);
};
1122

S
sushuang 已提交
1123 1124 1125 1126 1127 1128 1129 1130
function updateStreamModes(ecIns, ecModel) {
    var chartsMap = ecIns._chartsMap;
    var scheduler = ecIns._scheduler;
    ecModel.eachSeries(function (seriesModel) {
        scheduler.updateStreamModes(seriesModel, chartsMap[seriesModel.__viewId]);
    });
}

S
sushuang 已提交
1131 1132 1133 1134 1135 1136
/**
 * Show loading effect
 * @param  {string} [name='default']
 * @param  {Object} [cfg]
 */
echartsProto.showLoading = function (name, cfg) {
1137 1138 1139 1140 1141
    if (this._disposed) {
        disposedWarning(this.id);
        return;
    }

S
sushuang 已提交
1142
    if (isObject(name)) {
S
sushuang 已提交
1143 1144
        cfg = name;
        name = '';
1145
    }
S
sushuang 已提交
1146
    name = name || 'default';
L
lang 已提交
1147

S
sushuang 已提交
1148 1149 1150 1151
    this.hideLoading();
    if (!loadingEffects[name]) {
        if (__DEV__) {
            console.warn('Loading effects ' + name + ' not exists.');
L
tweak  
lang 已提交
1152
        }
S
sushuang 已提交
1153 1154 1155 1156 1157
        return;
    }
    var el = loadingEffects[name](this._api, cfg);
    var zr = this._zr;
    this._loadingFX = el;
L
lang 已提交
1158

S
sushuang 已提交
1159 1160
    zr.add(el);
};
L
tweak  
lang 已提交
1161

S
sushuang 已提交
1162 1163 1164 1165
/**
 * Hide loading effect
 */
echartsProto.hideLoading = function () {
1166 1167 1168 1169 1170
    if (this._disposed) {
        disposedWarning(this.id);
        return;
    }

S
sushuang 已提交
1171 1172 1173
    this._loadingFX && this._zr.remove(this._loadingFX);
    this._loadingFX = null;
};
L
Tweak  
lang 已提交
1174

S
sushuang 已提交
1175 1176 1177 1178 1179 1180 1181 1182 1183
/**
 * @param {Object} eventObj
 * @return {Object}
 */
echartsProto.makeActionFromEvent = function (eventObj) {
    var payload = zrUtil.extend({}, eventObj);
    payload.type = eventActionMap[eventObj.type];
    return payload;
};
L
tweak  
lang 已提交
1184

S
sushuang 已提交
1185 1186 1187 1188 1189 1190 1191 1192 1193
/**
 * @pubilc
 * @param {Object} payload
 * @param {string} [payload.type] Action type
 * @param {Object|boolean} [opt] If pass boolean, means opt.silent
 * @param {boolean} [opt.silent=false] Whether trigger events.
 * @param {boolean} [opt.flush=undefined]
 *                  true: Flush immediately, and then pixel in canvas can be fetched
 *                      immediately. Caution: it might affect performance.
1194
 *                  false: Not flush.
S
sushuang 已提交
1195 1196 1197
 *                  undefined: Auto decide whether perform flush.
 */
echartsProto.dispatchAction = function (payload, opt) {
1198 1199 1200 1201 1202
    if (this._disposed) {
        disposedWarning(this.id);
        return;
    }

S
sushuang 已提交
1203
    if (!isObject(opt)) {
S
sushuang 已提交
1204
        opt = {silent: !!opt};
1205 1206
    }

S
sushuang 已提交
1207 1208
    if (!actions[payload.type]) {
        return;
1209
    }
L
lang 已提交
1210

S
sushuang 已提交
1211 1212 1213
    // Avoid dispatch action before setOption. Especially in `connect`.
    if (!this._model) {
        return;
1214
    }
L
lang 已提交
1215

S
sushuang 已提交
1216 1217 1218 1219
    // May dispatchAction in rendering procedure
    if (this[IN_MAIN_PROCESS]) {
        this._pendingActions.push(payload);
        return;
1220
    }
L
lang 已提交
1221

S
sushuang 已提交
1222
    doDispatchAction.call(this, payload, opt.silent);
L
lang 已提交
1223

S
sushuang 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
    if (opt.flush) {
        this._zr.flush(true);
    }
    else if (opt.flush !== false && env.browser.weChat) {
        // In WeChat embeded browser, `requestAnimationFrame` and `setInterval`
        // hang when sliding page (on touch event), which cause that zr does not
        // refresh util user interaction finished, which is not expected.
        // But `dispatchAction` may be called too frequently when pan on touch
        // screen, which impacts performance if do not throttle them.
        this._throttledZrFlush();
    }
L
tweak  
lang 已提交
1235

S
sushuang 已提交
1236
    flushPendingActions.call(this, opt.silent);
L
tweak  
lang 已提交
1237

S
sushuang 已提交
1238 1239
    triggerUpdatedEvent.call(this, opt.silent);
};
L
tweak  
lang 已提交
1240

S
sushuang 已提交
1241 1242 1243 1244 1245
function doDispatchAction(payload, silent) {
    var payloadType = payload.type;
    var escapeConnect = payload.escapeConnect;
    var actionWrap = actions[payloadType];
    var actionInfo = actionWrap.actionInfo;
L
tweak  
lang 已提交
1246

S
sushuang 已提交
1247 1248 1249
    var cptType = (actionInfo.update || 'update').split(':');
    var updateMethod = cptType.pop();
    cptType = cptType[0] != null && parseClassType(cptType[0]);
L
lang 已提交
1250

S
sushuang 已提交
1251
    this[IN_MAIN_PROCESS] = true;
1252

S
sushuang 已提交
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
    var payloads = [payload];
    var batched = false;
    // Batch action
    if (payload.batch) {
        batched = true;
        payloads = zrUtil.map(payload.batch, function (item) {
            item = zrUtil.defaults(zrUtil.extend({}, item), payload);
            item.batch = null;
            return item;
        });
    }
L
lang 已提交
1264

S
sushuang 已提交
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
    var eventObjBatch = [];
    var eventObj;
    var isHighDown = payloadType === 'highlight' || payloadType === 'downplay';

    each(payloads, function (batchItem) {
        // Action can specify the event by return it.
        eventObj = actionWrap.action(batchItem, this._model, this._api);
        // Emit event outside
        eventObj = eventObj || zrUtil.extend({}, batchItem);
        // Convert type to eventType
        eventObj.type = actionInfo.event || eventObj.type;
        eventObjBatch.push(eventObj);

        // light update does not perform data process, layout and visual.
        if (isHighDown) {
            // method, payload, mainType, subType
            updateDirectly(this, updateMethod, batchItem, 'series');
        }
        else if (cptType) {
            updateDirectly(this, updateMethod, batchItem, cptType.main, cptType.sub);
        }
    }, this);
L
tweak  
lang 已提交
1287

S
sushuang 已提交
1288 1289 1290 1291
    if (updateMethod !== 'none' && !isHighDown && !cptType) {
        // Still dirty
        if (this[OPTION_UPDATED]) {
            // FIXME Pass payload ?
S
sushuang 已提交
1292 1293
            prepare(this);
            updateMethods.update.call(this, payload);
S
sushuang 已提交
1294 1295 1296 1297 1298 1299
            this[OPTION_UPDATED] = false;
        }
        else {
            updateMethods[updateMethod].call(this, payload);
        }
    }
1300

S
sushuang 已提交
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
    // Follow the rule of action batch
    if (batched) {
        eventObj = {
            type: actionInfo.event || payloadType,
            escapeConnect: escapeConnect,
            batch: eventObjBatch
        };
    }
    else {
        eventObj = eventObjBatch[0];
1311
    }
L
lang 已提交
1312

S
sushuang 已提交
1313
    this[IN_MAIN_PROCESS] = false;
1
100pah 已提交
1314

S
sushuang 已提交
1315 1316
    !silent && this._messageCenter.trigger(eventObj.type, eventObj);
}
1
100pah 已提交
1317

S
sushuang 已提交
1318 1319 1320 1321 1322 1323 1324
function flushPendingActions(silent) {
    var pendingActions = this._pendingActions;
    while (pendingActions.length) {
        var payload = pendingActions.shift();
        doDispatchAction.call(this, payload, silent);
    }
}
L
lang 已提交
1325

S
sushuang 已提交
1326 1327 1328
function triggerUpdatedEvent(silent) {
    !silent && this.trigger('updated');
}
L
lang 已提交
1329

S
sushuang 已提交
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
/**
 * Event `rendered` is triggered when zr
 * rendered. It is useful for realtime
 * snapshot (reflect animation).
 *
 * Event `finished` is triggered when:
 * (1) zrender rendering finished.
 * (2) initial animation finished.
 * (3) progressive rendering finished.
 * (4) no pending action.
 * (5) no delayed setOption needs to be processed.
 */
function bindRenderedEvent(zr, ecIns) {
    zr.on('rendered', function () {

        ecIns.trigger('rendered');

        // The `finished` event should not be triggered repeatly,
        // so it should only be triggered when rendering indeed happend
        // in zrender. (Consider the case that dipatchAction is keep
        // triggering when mouse move).
        if (
            // Although zr is dirty if initial animation is not finished
            // and this checking is called on frame, we also check
            // animation finished for robustness.
            zr.animation.isFinished()
            && !ecIns[OPTION_UPDATED]
            && !ecIns._scheduler.unfinished
            && !ecIns._pendingActions.length
        ) {
            ecIns.trigger('finished');
        }
    });
}

S
sushuang 已提交
1365 1366 1367 1368 1369 1370
/**
 * @param {Object} params
 * @param {number} params.seriesIndex
 * @param {Array|TypedArray} params.data
 */
echartsProto.appendData = function (params) {
1371 1372 1373 1374 1375
    if (this._disposed) {
        disposedWarning(this.id);
        return;
    }

S
tweak  
sushuang 已提交
1376 1377
    var seriesIndex = params.seriesIndex;
    var ecModel = this.getModel();
S
sushuang 已提交
1378
    var seriesModel = ecModel.getSeriesByIndex(seriesIndex);
S
sushuang 已提交
1379

S
tweak  
sushuang 已提交
1380
    if (__DEV__) {
S
sushuang 已提交
1381
        assert(params.data && seriesModel);
S
tweak  
sushuang 已提交
1382
    }
S
sushuang 已提交
1383

P
pissang 已提交
1384
    seriesModel.appendData(params);
S
sushuang 已提交
1385

1386 1387 1388 1389 1390 1391 1392 1393
    // Note: `appendData` does not support that update extent of coordinate
    // system, util some scenario require that. In the expected usage of
    // `appendData`, the initial extent of coordinate system should better
    // be fixed by axis `min`/`max` setting or initial data, otherwise if
    // the extent changed while `appendData`, the location of the painted
    // graphic elements have to be changed, which make the usage of
    // `appendData` meaningless.

S
sushuang 已提交
1394
    this._scheduler.unfinished = true;
S
tweak  
sushuang 已提交
1395
};
S
sushuang 已提交
1396

S
sushuang 已提交
1397 1398 1399 1400
/**
 * Register event
 * @method
 */
1401 1402 1403
echartsProto.on = createRegisterEventWithLowercaseName('on', false);
echartsProto.off = createRegisterEventWithLowercaseName('off', false);
echartsProto.one = createRegisterEventWithLowercaseName('one', false);
L
lang 已提交
1404

S
sushuang 已提交
1405 1406 1407 1408 1409
/**
 * Prepare view instances of charts and components
 * @param  {module:echarts/model/Global} ecModel
 * @private
 */
S
sushuang 已提交
1410
function prepareView(ecIns, type, ecModel, scheduler) {
S
sushuang 已提交
1411
    var isComponent = type === 'component';
S
sushuang 已提交
1412 1413 1414 1415
    var viewList = isComponent ? ecIns._componentsViews : ecIns._chartsViews;
    var viewMap = isComponent ? ecIns._componentsMap : ecIns._chartsMap;
    var zr = ecIns._zr;
    var api = ecIns._api;
S
sushuang 已提交
1416 1417 1418

    for (var i = 0; i < viewList.length; i++) {
        viewList[i].__alive = false;
1419
    }
1420

S
sushuang 已提交
1421 1422 1423 1424 1425
    isComponent
        ? ecModel.eachComponent(function (componentType, model) {
            componentType !== 'series' && doPrepare(model);
        })
        : ecModel.eachSeries(doPrepare);
1426

S
sushuang 已提交
1427
    function doPrepare(model) {
S
sushuang 已提交
1428 1429 1430 1431 1432 1433 1434 1435
        // Consider: id same and type changed.
        var viewId = '_ec_' + model.id + '_' + model.type;
        var view = viewMap[viewId];
        if (!view) {
            var classType = parseClassType(model.type);
            var Clazz = isComponent
                ? ComponentView.getClass(classType.main, classType.sub)
                : ChartView.getClass(classType.sub);
S
sushuang 已提交
1436 1437

            if (__DEV__) {
S
sushuang 已提交
1438
                assert(Clazz, classType.sub + ' does not exist.');
1439
            }
S
sushuang 已提交
1440 1441

            view = new Clazz();
S
sushuang 已提交
1442
            view.init(ecModel, api);
S
sushuang 已提交
1443 1444 1445
            viewMap[viewId] = view;
            viewList.push(view);
            zr.add(view.group);
S
sushuang 已提交
1446
        }
1447

S
sushuang 已提交
1448 1449 1450 1451 1452 1453 1454
        model.__viewId = view.__id = viewId;
        view.__alive = true;
        view.__model = model;
        view.group.__ecComponentInfo = {
            mainType: model.mainType,
            index: model.componentIndex
        };
S
sushuang 已提交
1455
        !isComponent && scheduler.prepareView(view, model, ecModel, api);
S
sushuang 已提交
1456
    }
S
sushuang 已提交
1457 1458 1459 1460

    for (var i = 0; i < viewList.length;) {
        var view = viewList[i];
        if (!view.__alive) {
S
sushuang 已提交
1461
            !isComponent && view.renderTask.dispose();
S
sushuang 已提交
1462
            zr.remove(view.group);
S
sushuang 已提交
1463
            view.dispose(ecModel, api);
S
sushuang 已提交
1464 1465 1466 1467 1468 1469 1470
            viewList.splice(i, 1);
            delete viewMap[view.__id];
            view.__id = view.group.__ecComponentInfo = null;
        }
        else {
            i++;
        }
L
lang 已提交
1471
    }
S
sushuang 已提交
1472
}
1473

S
sushuang 已提交
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
// /**
//  * Encode visual infomation from data after data processing
//  *
//  * @param {module:echarts/model/Global} ecModel
//  * @param {object} layout
//  * @param {boolean} [layoutFilter] `true`: only layout,
//  *                                 `false`: only not layout,
//  *                                 `null`/`undefined`: all.
//  * @param {string} taskBaseTag
//  * @private
//  */
// function startVisualEncoding(ecIns, ecModel, api, payload, layoutFilter) {
//     each(visualFuncs, function (visual, index) {
//         var isLayout = visual.isLayout;
//         if (layoutFilter == null
//             || (layoutFilter === false && !isLayout)
//             || (layoutFilter === true && isLayout)
//         ) {
//             visual.func(ecModel, api, payload);
//         }
//     });
// }

function clearColorPalette(ecModel) {
    ecModel.clearColorPalette();
    ecModel.eachSeries(function (seriesModel) {
        seriesModel.clearColorPalette();
S
sushuang 已提交
1501
    });
S
sushuang 已提交
1502 1503
}

S
sushuang 已提交
1504 1505
function render(ecIns, ecModel, api, payload) {

S
sushuang 已提交
1506
    renderComponents(ecIns, ecModel, api, payload);
S
sushuang 已提交
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521

    each(ecIns._chartsViews, function (chart) {
        chart.__alive = false;
    });

    renderSeries(ecIns, ecModel, api, payload);

    // Remove groups of unrendered charts
    each(ecIns._chartsViews, function (chart) {
        if (!chart.__alive) {
            chart.remove(ecModel, api);
        }
    });
}

S
sushuang 已提交
1522 1523 1524 1525 1526 1527 1528 1529 1530
function renderComponents(ecIns, ecModel, api, payload, dirtyList) {
    each(dirtyList || ecIns._componentsViews, function (componentView) {
        var componentModel = componentView.__model;
        componentView.render(componentModel, ecModel, api, payload);

        updateZ(componentModel, componentView);
    });
}

S
sushuang 已提交
1531 1532 1533 1534
/**
 * Render each chart and component
 * @private
 */
1535
function renderSeries(ecIns, ecModel, api, payload, dirtyMap) {
S
sushuang 已提交
1536
    // Render all charts
S
sushuang 已提交
1537 1538
    var scheduler = ecIns._scheduler;
    var unfinished;
1539
    ecModel.eachSeries(function (seriesModel) {
S
sushuang 已提交
1540
        var chartView = ecIns._chartsMap[seriesModel.__viewId];
S
sushuang 已提交
1541
        chartView.__alive = true;
L
lang 已提交
1542

S
sushuang 已提交
1543
        var renderTask = chartView.renderTask;
S
sushuang 已提交
1544
        scheduler.updatePayload(renderTask, payload);
1545 1546 1547 1548 1549

        if (dirtyMap && dirtyMap.get(seriesModel.uid)) {
            renderTask.dirty();
        }

S
sushuang 已提交
1550
        unfinished |= renderTask.perform(scheduler.getPerformArgs(renderTask));
L
lang 已提交
1551

S
sushuang 已提交
1552
        chartView.group.silent = !!seriesModel.get('silent');
P
pah100 已提交
1553

S
sushuang 已提交
1554
        updateZ(seriesModel, chartView);
S
sushuang 已提交
1555

P
pissang 已提交
1556
        updateBlend(seriesModel, chartView);
1557
    });
S
sushuang 已提交
1558
    scheduler.unfinished |= unfinished;
S
sushuang 已提交
1559 1560

    // If use hover layer
1561
    updateHoverLayerStatus(ecIns, ecModel);
O
Ovilia 已提交
1562 1563

    // Add aria
1564
    aria(ecIns._zr.dom, ecModel);
S
sushuang 已提交
1565 1566
}

S
sushuang 已提交
1567 1568 1569
function performPostUpdateFuncs(ecModel, api) {
    each(postUpdateFuncs, function (func) {
        func(ecModel, api);
S
sushuang 已提交
1570
    });
S
tweak  
sushuang 已提交
1571 1572
}

S
sushuang 已提交
1573

S
sushuang 已提交
1574 1575 1576 1577
var MOUSE_EVENT_NAMES = [
    'click', 'dblclick', 'mouseover', 'mouseout', 'mousemove',
    'mousedown', 'mouseup', 'globalout', 'contextmenu'
];
S
sushuang 已提交
1578

S
sushuang 已提交
1579 1580 1581 1582 1583
/**
 * @private
 */
echartsProto._initEvents = function () {
    each(MOUSE_EVENT_NAMES, function (eveName) {
1584
        var handler = function (e) {
S
sushuang 已提交
1585 1586 1587
            var ecModel = this.getModel();
            var el = e.target;
            var params;
1588
            var isGlobalOut = eveName === 'globalout';
S
sushuang 已提交
1589 1590

            // no e.target when 'globalout'.
1591
            if (isGlobalOut) {
S
sushuang 已提交
1592 1593 1594 1595
                params = {};
            }
            else if (el && el.dataIndex != null) {
                var dataModel = el.dataModel || ecModel.getSeriesByIndex(el.seriesIndex);
1596
                params = dataModel && dataModel.getDataParams(el.dataIndex, el.dataType, el) || {};
S
sushuang 已提交
1597 1598 1599 1600 1601
            }
            // If element has custom eventData of components
            else if (el && el.eventData) {
                params = zrUtil.extend({}, el.eventData);
            }
P
pah100 已提交
1602

S
sushuang 已提交
1603 1604 1605 1606 1607 1608 1609 1610
            // Contract: if params prepared in mouse event,
            // these properties must be specified:
            // {
            //    componentType: string (component main type)
            //    componentIndex: number
            // }
            // Otherwise event query can not work.

S
sushuang 已提交
1611
            if (params) {
S
sushuang 已提交
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
                var componentType = params.componentType;
                var componentIndex = params.componentIndex;
                // Special handling for historic reason: when trigger by
                // markLine/markPoint/markArea, the componentType is
                // 'markLine'/'markPoint'/'markArea', but we should better
                // enable them to be queried by seriesIndex, since their
                // option is set in each series.
                if (componentType === 'markLine'
                    || componentType === 'markPoint'
                    || componentType === 'markArea'
                ) {
                    componentType = 'series';
                    componentIndex = params.seriesIndex;
                }
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
                var model = componentType && componentIndex != null
                    && ecModel.getComponent(componentType, componentIndex);
                var view = model && this[
                    model.mainType === 'series' ? '_chartsMap' : '_componentsMap'
                ][model.__viewId];

                if (__DEV__) {
                    // `event.componentType` and `event[componentTpype + 'Index']` must not
                    // be missed, otherwise there is no way to distinguish source component.
                    // See `dataFormat.getDataParams`.
S
sushuang 已提交
1636 1637 1638
                    if (!isGlobalOut && !(model && view)) {
                        console.warn('model or view can not be found by params');
                    }
1639 1640
                }

S
sushuang 已提交
1641 1642
                params.event = e;
                params.type = eveName;
1643

S
sushuang 已提交
1644 1645 1646 1647 1648 1649
                this._ecEventProcessor.eventInfo = {
                    targetEl: el,
                    packedEvent: params,
                    model: model,
                    view: view
                };
1650

S
sushuang 已提交
1651
                this.trigger(eveName, params);
L
lang 已提交
1652
            }
1653 1654 1655 1656 1657 1658 1659 1660
        };
        // Consider that some component (like tooltip, brush, ...)
        // register zr event handler, but user event handler might
        // do anything, such as call `setOption` or `dispatchAction`,
        // which probably update any of the content and probably
        // cause problem if it is called previous other inner handlers.
        handler.zrEventfulCallAtLast = true;
        this._zr.on(eveName, handler, this);
S
sushuang 已提交
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
    }, this);

    each(eventActionMap, function (actionType, eventType) {
        this._messageCenter.on(eventType, function (event) {
            this.trigger(eventType, event);
        }, this);
    }, this);
};

/**
 * @return {boolean}
 */
echartsProto.isDisposed = function () {
    return this._disposed;
};

/**
 * Clear
 */
echartsProto.clear = function () {
1681 1682 1683 1684
    if (this._disposed) {
        disposedWarning(this.id);
        return;
    }
S
sushuang 已提交
1685 1686 1687 1688 1689 1690 1691 1692
    this.setOption({ series: [] }, true);
};

/**
 * Dispose instance
 */
echartsProto.dispose = function () {
    if (this._disposed) {
1693
        disposedWarning(this.id);
S
sushuang 已提交
1694 1695 1696
        return;
    }
    this._disposed = true;
P
pah100 已提交
1697

S
sushuang 已提交
1698 1699
    modelUtil.setAttribute(this.getDom(), DOM_ATTRIBUTE_KEY, '');

S
sushuang 已提交
1700 1701
    var api = this._api;
    var ecModel = this._model;
P
pah100 已提交
1702

S
sushuang 已提交
1703 1704 1705 1706 1707 1708
    each(this._componentsViews, function (component) {
        component.dispose(ecModel, api);
    });
    each(this._chartsViews, function (chart) {
        chart.dispose(ecModel, api);
    });
1
100pah 已提交
1709

S
sushuang 已提交
1710 1711
    // Dispose after all views disposed
    this._zr.dispose();
1
100pah 已提交
1712

S
sushuang 已提交
1713 1714 1715 1716 1717
    delete instances[this.id];
};

zrUtil.mixin(ECharts, Eventful);

1718 1719 1720 1721 1722 1723
function disposedWarning(id) {
    if (__DEV__) {
        console.warn('Instance ' + id + ' has been disposed');
    }
}

1724 1725
function updateHoverLayerStatus(ecIns, ecModel) {
    var zr = ecIns._zr;
S
sushuang 已提交
1726 1727
    var storage = zr.storage;
    var elCount = 0;
1728

S
sushuang 已提交
1729
    storage.traverse(function (el) {
1730
        elCount++;
S
sushuang 已提交
1731
    });
1732

S
sushuang 已提交
1733
    if (elCount > ecModel.get('hoverLayerThreshold') && !env.node) {
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
        ecModel.eachSeries(function (seriesModel) {
            if (seriesModel.preventUsingHoverLayer) {
                return;
            }
            var chartView = ecIns._chartsMap[seriesModel.__viewId];
            if (chartView.__alive) {
                chartView.group.traverse(function (el) {
                    // Don't switch back.
                    el.useHoverLayer = true;
                });
S
sushuang 已提交
1744
            }
L
lang 已提交
1745 1746
        });
    }
S
sushuang 已提交
1747
}
P
pah100 已提交
1748

S
sushuang 已提交
1749 1750 1751 1752 1753
/**
 * Update chart progressive and blend.
 * @param {module:echarts/model/Series|module:echarts/model/Component} model
 * @param {module:echarts/view/Component|module:echarts/view/Chart} view
 */
P
pissang 已提交
1754
function updateBlend(seriesModel, chartView) {
S
sushuang 已提交
1755 1756 1757 1758
    var blendMode = seriesModel.get('blendMode') || null;
    if (__DEV__) {
        if (!env.canvasSupported && blendMode && blendMode !== 'source-over') {
            console.warn('Only canvas support blendMode');
P
pah100 已提交
1759
        }
S
sushuang 已提交
1760 1761 1762 1763
    }
    chartView.group.traverse(function (el) {
        // FIXME marker and other components
        if (!el.isGroup) {
1764 1765 1766 1767
            // Only set if blendMode is changed. In case element is incremental and don't wan't to rerender.
            if (el.style.blend !== blendMode) {
                el.setStyle('blend', blendMode);
            }
S
sushuang 已提交
1768
        }
P
pissang 已提交
1769 1770
        if (el.eachPendingDisplayable) {
            el.eachPendingDisplayable(function (displayable) {
P
pissang 已提交
1771
                displayable.setStyle('blend', blendMode);
P
pissang 已提交
1772 1773
            });
        }
S
sushuang 已提交
1774 1775
    });
}
P
pah100 已提交
1776

S
sushuang 已提交
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
/**
 * @param {module:echarts/model/Series|module:echarts/model/Component} model
 * @param {module:echarts/view/Component|module:echarts/view/Chart} view
 */
function updateZ(model, view) {
    var z = model.get('z');
    var zlevel = model.get('zlevel');
    // Set z and zlevel
    view.group.traverse(function (el) {
        if (el.type !== 'group') {
            z != null && (el.z = z);
            zlevel != null && (el.zlevel = zlevel);
P
pah100 已提交
1789
        }
S
sushuang 已提交
1790 1791
    });
}
P
pah100 已提交
1792

S
sushuang 已提交
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
function createExtensionAPI(ecInstance) {
    var coordSysMgr = ecInstance._coordSysMgr;
    return zrUtil.extend(new ExtensionAPI(ecInstance), {
        // Inject methods
        getCoordinateSystems: zrUtil.bind(
            coordSysMgr.getCoordinateSystems, coordSysMgr
        ),
        getComponentByElement: function (el) {
            while (el) {
                var modelInfo = el.__ecComponentInfo;
                if (modelInfo != null) {
                    return ecInstance._model.getComponent(modelInfo.mainType, modelInfo.index);
                }
                el = el.parent;
1807
            }
1808
        }
S
sushuang 已提交
1809 1810
    });
}
L
lang 已提交
1811

S
sushuang 已提交
1812

1813
/**
S
sushuang 已提交
1814
 * @class
1815 1816 1817 1818 1819 1820 1821 1822
 * Usage of query:
 * `chart.on('click', query, handler);`
 * The `query` can be:
 * + The component type query string, only `mainType` or `mainType.subType`,
 *   like: 'xAxis', 'series', 'xAxis.category' or 'series.line'.
 * + The component query object, like:
 *   `{seriesIndex: 2}`, `{seriesName: 'xx'}`, `{seriesId: 'some'}`,
 *   `{xAxisIndex: 2}`, `{xAxisName: 'xx'}`, `{xAxisId: 'some'}`.
1823 1824 1825 1826
 * + The data query object, like:
 *   `{dataIndex: 123}`, `{dataType: 'link'}`, `{name: 'some'}`.
 * + The other query object (cmponent customized query), like:
 *   `{element: 'some'}` (only available in custom series).
1827 1828 1829 1830
 *
 * Caveat: If a prop in the `query` object is `null/undefined`, it is the
 * same as there is no such prop in the `query` object.
 */
S
sushuang 已提交
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
function EventProcessor() {
    // These info required: targetEl, packedEvent, model, view
    this.eventInfo;
}
EventProcessor.prototype = {
    constructor: EventProcessor,

    normalizeQuery: function (query) {
        var cptQuery = {};
        var dataQuery = {};
        var otherQuery = {};

        // `query` is `mainType` or `mainType.subType` of component.
        if (zrUtil.isString(query)) {
            var condCptType = parseClassType(query);
            // `.main` and `.sub` may be ''.
            cptQuery.mainType = condCptType.main || null;
            cptQuery.subType = condCptType.sub || null;
        }
        // `query` is an object, convert to {mainType, index, name, id}.
        else {
            // `xxxIndex`, `xxxName`, `xxxId`, `name`, `dataIndex`, `dataType` is reserved,
            // can not be used in `compomentModel.filterForExposedEvent`.
            var suffixes = ['Index', 'Name', 'Id'];
            var dataKeys = {name: 1, dataIndex: 1, dataType: 1};
            zrUtil.each(query, function (val, key) {
1857
                var reserved = false;
S
sushuang 已提交
1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
                for (var i = 0; i < suffixes.length; i++) {
                    var propSuffix = suffixes[i];
                    var suffixPos = key.lastIndexOf(propSuffix);
                    if (suffixPos > 0 && suffixPos === key.length - propSuffix.length) {
                        var mainType = key.slice(0, suffixPos);
                        // Consider `dataIndex`.
                        if (mainType !== 'data') {
                            cptQuery.mainType = mainType;
                            cptQuery[propSuffix.toLowerCase()] = val;
                            reserved = true;
1868 1869
                        }
                    }
S
sushuang 已提交
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879
                }
                if (dataKeys.hasOwnProperty(key)) {
                    dataQuery[key] = val;
                    reserved = true;
                }
                if (!reserved) {
                    otherQuery[key] = val;
                }
            });
        }
1880

S
sushuang 已提交
1881 1882 1883 1884 1885 1886
        return {
            cptQuery: cptQuery,
            dataQuery: dataQuery,
            otherQuery: otherQuery
        };
    },
1887

S
sushuang 已提交
1888 1889 1890
    filter: function (eventType, query, args) {
        // They should be assigned before each trigger call.
        var eventInfo = this.eventInfo;
S
sushuang 已提交
1891 1892 1893 1894 1895

        if (!eventInfo) {
            return true;
        }

S
sushuang 已提交
1896 1897 1898 1899 1900 1901 1902 1903
        var targetEl = eventInfo.targetEl;
        var packedEvent = eventInfo.packedEvent;
        var model = eventInfo.model;
        var view = eventInfo.view;

        // For event like 'globalout'.
        if (!model || !view) {
            return true;
1904 1905
        }

S
sushuang 已提交
1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
        var cptQuery = query.cptQuery;
        var dataQuery = query.dataQuery;

        return check(cptQuery, model, 'mainType')
            && check(cptQuery, model, 'subType')
            && check(cptQuery, model, 'index', 'componentIndex')
            && check(cptQuery, model, 'name')
            && check(cptQuery, model, 'id')
            && check(dataQuery, packedEvent, 'name')
            && check(dataQuery, packedEvent, 'dataIndex')
            && check(dataQuery, packedEvent, 'dataType')
            && (!view.filterForExposedEvent || view.filterForExposedEvent(
                eventType, query.otherQuery, targetEl, packedEvent
            ));

        function check(query, host, prop, propOnHost) {
            return query[prop] == null || host[propOnHost || prop] === query[prop];
        }
    },

    afterTrigger: function () {
S
sushuang 已提交
1927
        // Make sure the eventInfo wont be used in next trigger.
S
sushuang 已提交
1928
        this.eventInfo = null;
1929
    }
S
sushuang 已提交
1930 1931
};

1932

S
sushuang 已提交
1933 1934 1935 1936 1937
/**
 * @type {Object} key: actionType.
 * @inner
 */
var actions = {};
L
lang 已提交
1938

S
sushuang 已提交
1939 1940 1941 1942 1943
/**
 * Map eventType to actionType
 * @type {Object}
 */
var eventActionMap = {};
L
lang 已提交
1944

S
sushuang 已提交
1945 1946 1947 1948 1949 1950
/**
 * Data processor functions of each stage
 * @type {Array.<Object.<string, Function>>}
 * @inner
 */
var dataProcessorFuncs = [];
L
lang 已提交
1951

S
sushuang 已提交
1952 1953 1954 1955 1956
/**
 * @type {Array.<Function>}
 * @inner
 */
var optionPreprocessorFuncs = [];
L
lang 已提交
1957

S
sushuang 已提交
1958 1959 1960 1961 1962
/**
 * @type {Array.<Function>}
 * @inner
 */
var postUpdateFuncs = [];
L
lang 已提交
1963

S
sushuang 已提交
1964 1965 1966 1967 1968
/**
 * Visual encoding functions of each stage
 * @type {Array.<Object.<string, Function>>}
 */
var visualFuncs = [];
S
sushuang 已提交
1969

S
sushuang 已提交
1970 1971 1972 1973 1974 1975 1976 1977 1978
/**
 * Theme storage
 * @type {Object.<key, Object>}
 */
var themeStorage = {};
/**
 * Loading effects
 */
var loadingEffects = {};
L
lang 已提交
1979

S
sushuang 已提交
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
var instances = {};
var connectedGroups = {};

var idBase = new Date() - 0;
var groupIdBase = new Date() - 0;
var DOM_ATTRIBUTE_KEY = '_echarts_instance_';

function enableConnect(chart) {
    var STATUS_PENDING = 0;
    var STATUS_UPDATING = 1;
    var STATUS_UPDATED = 2;
    var STATUS_KEY = '__connectUpdateStatus';

    function updateConnectedChartsStatus(charts, status) {
        for (var i = 0; i < charts.length; i++) {
            var otherChart = charts[i];
            otherChart[STATUS_KEY] = status;
L
lang 已提交
1997
        }
S
sushuang 已提交
1998 1999
    }

S
sushuang 已提交
2000
    each(eventActionMap, function (actionType, eventType) {
S
sushuang 已提交
2001 2002 2003 2004 2005 2006 2007 2008 2009
        chart._messageCenter.on(eventType, function (event) {
            if (connectedGroups[chart.group] && chart[STATUS_KEY] !== STATUS_PENDING) {
                if (event && event.escapeConnect) {
                    return;
                }

                var action = chart.makeActionFromEvent(event);
                var otherCharts = [];

S
sushuang 已提交
2010
                each(instances, function (otherChart) {
S
sushuang 已提交
2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032
                    if (otherChart !== chart && otherChart.group === chart.group) {
                        otherCharts.push(otherChart);
                    }
                });

                updateConnectedChartsStatus(otherCharts, STATUS_PENDING);
                each(otherCharts, function (otherChart) {
                    if (otherChart[STATUS_KEY] !== STATUS_UPDATING) {
                        otherChart.dispatchAction(action);
                    }
                });
                updateConnectedChartsStatus(otherCharts, STATUS_UPDATED);
            }
        });
    });
}

/**
 * @param {HTMLElement} dom
 * @param {Object} [theme]
 * @param {Object} opts
 * @param {number} [opts.devicePixelRatio] Use window.devicePixelRatio by default
D
deqingli 已提交
2033
 * @param {string} [opts.renderer] Can choose 'canvas' or 'svg' to render the chart.
S
sushuang 已提交
2034 2035 2036 2037 2038
 * @param {number} [opts.width] Use clientWidth of the input `dom` by default.
 *                              Can be 'auto' (the same as null/undefined)
 * @param {number} [opts.height] Use clientHeight of the input `dom` by default.
 *                               Can be 'auto' (the same as null/undefined)
 */
S
sushuang 已提交
2039
export function init(dom, theme, opts) {
S
sushuang 已提交
2040 2041
    if (__DEV__) {
        // Check version
S
sushuang 已提交
2042
        if ((zrender.version.replace('.', '') - 0) < (dependencies.zrender.replace('.', '') - 0)) {
S
sushuang 已提交
2043
            throw new Error(
S
sushuang 已提交
2044
                'zrender/src ' + zrender.version
S
sushuang 已提交
2045
                + ' is too old for ECharts ' + version
S
sushuang 已提交
2046
                + '. Current version need ZRender '
S
sushuang 已提交
2047
                + dependencies.zrender + '+'
S
sushuang 已提交
2048
            );
P
pissang 已提交
2049
        }
S
sushuang 已提交
2050 2051 2052

        if (!dom) {
            throw new Error('Initialize failed: invalid dom.');
L
lang 已提交
2053
        }
S
sushuang 已提交
2054
    }
L
lang 已提交
2055

S
sushuang 已提交
2056
    var existInstance = getInstanceByDom(dom);
S
sushuang 已提交
2057 2058 2059
    if (existInstance) {
        if (__DEV__) {
            console.warn('There is a chart instance already initialized on the dom.');
P
pissang 已提交
2060
        }
S
sushuang 已提交
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071
        return existInstance;
    }

    if (__DEV__) {
        if (zrUtil.isDom(dom)
            && dom.nodeName.toUpperCase() !== 'CANVAS'
            && (
                (!dom.clientWidth && (!opts || opts.width == null))
                || (!dom.clientHeight && (!opts || opts.height == null))
            )
        ) {
2072
            console.warn('Can\'t get DOM width or height. Please check '
2073 2074 2075
            + 'dom.clientWidth and dom.clientHeight. They should not be 0.'
            + 'For example, you may need to call this in the callback '
            + 'of window.onload.');
P
pissang 已提交
2076
        }
S
sushuang 已提交
2077
    }
P
pah100 已提交
2078

S
sushuang 已提交
2079 2080 2081
    var chart = new ECharts(dom, theme, opts);
    chart.id = 'ec_' + idBase++;
    instances[chart.id] = chart;
L
lang 已提交
2082

S
sushuang 已提交
2083
    modelUtil.setAttribute(dom, DOM_ATTRIBUTE_KEY, chart.id);
L
lang 已提交
2084

S
sushuang 已提交
2085
    enableConnect(chart);
2086

S
sushuang 已提交
2087
    return chart;
S
sushuang 已提交
2088
}
S
sushuang 已提交
2089 2090 2091 2092

/**
 * @return {string|Array.<module:echarts~ECharts>} groupId
 */
S
sushuang 已提交
2093
export function connect(groupId) {
S
sushuang 已提交
2094 2095 2096 2097 2098
    // Is array of charts
    if (zrUtil.isArray(groupId)) {
        var charts = groupId;
        groupId = null;
        // If any chart has group
S
sushuang 已提交
2099
        each(charts, function (chart) {
S
sushuang 已提交
2100 2101
            if (chart.group != null) {
                groupId = chart.group;
2102
            }
2103
        });
S
sushuang 已提交
2104
        groupId = groupId || ('g_' + groupIdBase++);
S
sushuang 已提交
2105
        each(charts, function (chart) {
S
sushuang 已提交
2106 2107 2108 2109 2110
            chart.group = groupId;
        });
    }
    connectedGroups[groupId] = true;
    return groupId;
S
sushuang 已提交
2111
}
L
lang 已提交
2112

S
sushuang 已提交
2113 2114 2115 2116
/**
 * @DEPRECATED
 * @return {string} groupId
 */
S
sushuang 已提交
2117
export function disConnect(groupId) {
S
sushuang 已提交
2118
    connectedGroups[groupId] = false;
S
sushuang 已提交
2119
}
2120

S
sushuang 已提交
2121 2122 2123
/**
 * @return {string} groupId
 */
S
sushuang 已提交
2124
export var disconnect = disConnect;
L
lang 已提交
2125

S
sushuang 已提交
2126 2127 2128 2129
/**
 * Dispose a chart instance
 * @param  {module:echarts~ECharts|HTMLDomElement|string} chart
 */
S
sushuang 已提交
2130
export function dispose(chart) {
S
sushuang 已提交
2131 2132 2133
    if (typeof chart === 'string') {
        chart = instances[chart];
    }
S
sushuang 已提交
2134
    else if (!(chart instanceof ECharts)) {
S
sushuang 已提交
2135
        // Try to treat as dom
S
sushuang 已提交
2136
        chart = getInstanceByDom(chart);
S
sushuang 已提交
2137 2138 2139 2140
    }
    if ((chart instanceof ECharts) && !chart.isDisposed()) {
        chart.dispose();
    }
S
sushuang 已提交
2141
}
2142

S
sushuang 已提交
2143 2144 2145 2146
/**
 * @param  {HTMLElement} dom
 * @return {echarts~ECharts}
 */
S
sushuang 已提交
2147
export function getInstanceByDom(dom) {
S
sushuang 已提交
2148
    return instances[modelUtil.getAttribute(dom, DOM_ATTRIBUTE_KEY)];
S
sushuang 已提交
2149
}
1
100pah 已提交
2150

S
sushuang 已提交
2151 2152 2153 2154
/**
 * @param {string} key
 * @return {echarts~ECharts}
 */
S
sushuang 已提交
2155
export function getInstanceById(key) {
S
sushuang 已提交
2156
    return instances[key];
S
sushuang 已提交
2157
}
P
pah100 已提交
2158

S
sushuang 已提交
2159 2160 2161
/**
 * Register theme
 */
S
sushuang 已提交
2162
export function registerTheme(name, theme) {
S
sushuang 已提交
2163
    themeStorage[name] = theme;
S
sushuang 已提交
2164
}
L
lang 已提交
2165

S
sushuang 已提交
2166 2167 2168 2169
/**
 * Register option preprocessor
 * @param {Function} preprocessorFunc
 */
S
sushuang 已提交
2170
export function registerPreprocessor(preprocessorFunc) {
S
sushuang 已提交
2171
    optionPreprocessorFuncs.push(preprocessorFunc);
S
sushuang 已提交
2172
}
2173

S
sushuang 已提交
2174 2175
/**
 * @param {number} [priority=1000]
S
sushuang 已提交
2176
 * @param {Object|Function} processor
S
sushuang 已提交
2177
 */
S
sushuang 已提交
2178 2179
export function registerProcessor(priority, processor) {
    normalizeRegister(dataProcessorFuncs, priority, processor, PRIORITY_PROCESSOR_FILTER);
S
sushuang 已提交
2180
}
L
lang 已提交
2181

S
sushuang 已提交
2182 2183 2184 2185
/**
 * Register postUpdater
 * @param {Function} postUpdateFunc
 */
S
sushuang 已提交
2186
export function registerPostUpdate(postUpdateFunc) {
S
sushuang 已提交
2187
    postUpdateFuncs.push(postUpdateFunc);
S
sushuang 已提交
2188
}
L
Update  
lang 已提交
2189

S
sushuang 已提交
2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205
/**
 * Usage:
 * registerAction('someAction', 'someEvent', function () { ... });
 * registerAction('someAction', function () { ... });
 * registerAction(
 *     {type: 'someAction', event: 'someEvent', update: 'updateView'},
 *     function () { ... }
 * );
 *
 * @param {(string|Object)} actionInfo
 * @param {string} actionInfo.type
 * @param {string} [actionInfo.event]
 * @param {string} [actionInfo.update]
 * @param {string} [eventName]
 * @param {Function} action
 */
S
sushuang 已提交
2206
export function registerAction(actionInfo, eventName, action) {
S
sushuang 已提交
2207 2208 2209 2210
    if (typeof eventName === 'function') {
        action = eventName;
        eventName = '';
    }
S
sushuang 已提交
2211
    var actionType = isObject(actionInfo)
S
sushuang 已提交
2212 2213 2214 2215
        ? actionInfo.type
        : ([actionInfo, actionInfo = {
            event: eventName
        }][0]);
L
lang 已提交
2216

S
sushuang 已提交
2217 2218 2219
    // Event name is all lowercase
    actionInfo.event = (actionInfo.event || actionType).toLowerCase();
    eventName = actionInfo.event;
L
Update  
lang 已提交
2220

S
sushuang 已提交
2221
    // Validate action type and event name.
S
sushuang 已提交
2222
    assert(ACTION_REG.test(actionType) && ACTION_REG.test(eventName));
L
Update  
lang 已提交
2223

S
sushuang 已提交
2224 2225 2226 2227
    if (!actions[actionType]) {
        actions[actionType] = {action: action, actionInfo: actionInfo};
    }
    eventActionMap[eventName] = actionType;
S
sushuang 已提交
2228
}
P
pah100 已提交
2229

S
sushuang 已提交
2230 2231 2232 2233
/**
 * @param {string} type
 * @param {*} CoordinateSystem
 */
S
sushuang 已提交
2234
export function registerCoordinateSystem(type, CoordinateSystem) {
S
sushuang 已提交
2235
    CoordinateSystemManager.register(type, CoordinateSystem);
S
sushuang 已提交
2236
}
L
lang 已提交
2237

S
sushuang 已提交
2238 2239 2240 2241 2242
/**
 * Get dimensions of specified coordinate system.
 * @param {string} type
 * @return {Array.<string|Object>}
 */
S
sushuang 已提交
2243
export function getCoordinateSystemDimensions(type) {
S
sushuang 已提交
2244 2245 2246 2247 2248 2249
    var coordSysCreator = CoordinateSystemManager.get(type);
    if (coordSysCreator) {
        return coordSysCreator.getDimensionsInfo
                ? coordSysCreator.getDimensionsInfo()
                : coordSysCreator.dimensions.slice();
    }
S
sushuang 已提交
2250
}
2251

S
sushuang 已提交
2252 2253 2254 2255 2256 2257
/**
 * Layout is a special stage of visual encoding
 * Most visual encoding like color are common for different chart
 * But each chart has it's own layout algorithm
 *
 * @param {number} [priority=1000]
S
sushuang 已提交
2258
 * @param {Function} layoutTask
S
sushuang 已提交
2259
 */
S
sushuang 已提交
2260
export function registerLayout(priority, layoutTask) {
S
sushuang 已提交
2261
    normalizeRegister(visualFuncs, priority, layoutTask, PRIORITY_VISUAL_LAYOUT, 'layout');
S
sushuang 已提交
2262
}
P
pah100 已提交
2263

S
sushuang 已提交
2264 2265
/**
 * @param {number} [priority=3000]
S
sushuang 已提交
2266
 * @param {module:echarts/stream/Task} visualTask
S
sushuang 已提交
2267
 */
S
sushuang 已提交
2268
export function registerVisual(priority, visualTask) {
S
sushuang 已提交
2269
    normalizeRegister(visualFuncs, priority, visualTask, PRIORITY_VISUAL_CHART, 'visual');
S
sushuang 已提交
2270 2271
}

S
sushuang 已提交
2272
/**
2273
 * @param {Object|Function} fn: {seriesType, createOnAllSeries, performRawSeries, reset}
S
sushuang 已提交
2274
 */
S
sushuang 已提交
2275
function normalizeRegister(targetList, priority, fn, defaultPriority, visualType) {
S
sushuang 已提交
2276
    if (isFunction(priority) || isObject(priority)) {
S
sushuang 已提交
2277 2278
        fn = priority;
        priority = defaultPriority;
S
sushuang 已提交
2279
    }
S
sushuang 已提交
2280

S
sushuang 已提交
2281
    if (__DEV__) {
S
sushuang 已提交
2282 2283
        if (isNaN(priority) || priority == null) {
            throw new Error('Illegal priority');
2284
        }
S
sushuang 已提交
2285
        // Check duplicate
S
sushuang 已提交
2286
        each(targetList, function (wrap) {
S
sushuang 已提交
2287
            assert(wrap.__raw !== fn);
S
sushuang 已提交
2288
        });
S
sushuang 已提交
2289
    }
S
sushuang 已提交
2290

S
sushuang 已提交
2291 2292 2293 2294
    var stageHandler = Scheduler.wrapStageHandler(fn, visualType);

    stageHandler.__prio = priority;
    stageHandler.__raw = fn;
S
sushuang 已提交
2295
    targetList.push(stageHandler);
S
sushuang 已提交
2296

S
sushuang 已提交
2297
    return stageHandler;
S
sushuang 已提交
2298
}
S
sushuang 已提交
2299 2300 2301 2302

/**
 * @param {string} name
 */
S
sushuang 已提交
2303
export function registerLoading(name, loadingFx) {
S
sushuang 已提交
2304
    loadingEffects[name] = loadingFx;
S
sushuang 已提交
2305
}
S
sushuang 已提交
2306 2307 2308 2309 2310

/**
 * @param {Object} opts
 * @param {string} [superClass]
 */
S
sushuang 已提交
2311
export function extendComponentModel(opts/*, superClass*/) {
S
sushuang 已提交
2312 2313 2314 2315 2316 2317
    // var Clazz = ComponentModel;
    // if (superClass) {
    //     var classType = parseClassType(superClass);
    //     Clazz = ComponentModel.getClass(classType.main, classType.sub, true);
    // }
    return ComponentModel.extend(opts);
S
sushuang 已提交
2318
}
S
sushuang 已提交
2319 2320 2321 2322 2323

/**
 * @param {Object} opts
 * @param {string} [superClass]
 */
S
sushuang 已提交
2324
export function extendComponentView(opts/*, superClass*/) {
S
sushuang 已提交
2325 2326 2327 2328 2329 2330
    // var Clazz = ComponentView;
    // if (superClass) {
    //     var classType = parseClassType(superClass);
    //     Clazz = ComponentView.getClass(classType.main, classType.sub, true);
    // }
    return ComponentView.extend(opts);
S
sushuang 已提交
2331
}
S
sushuang 已提交
2332 2333 2334 2335 2336

/**
 * @param {Object} opts
 * @param {string} [superClass]
 */
S
sushuang 已提交
2337
export function extendSeriesModel(opts/*, superClass*/) {
S
sushuang 已提交
2338 2339 2340 2341 2342 2343 2344
    // var Clazz = SeriesModel;
    // if (superClass) {
    //     superClass = 'series.' + superClass.replace('series.', '');
    //     var classType = parseClassType(superClass);
    //     Clazz = ComponentModel.getClass(classType.main, classType.sub, true);
    // }
    return SeriesModel.extend(opts);
S
sushuang 已提交
2345
}
S
sushuang 已提交
2346 2347 2348 2349 2350

/**
 * @param {Object} opts
 * @param {string} [superClass]
 */
S
sushuang 已提交
2351
export function extendChartView(opts/*, superClass*/) {
S
sushuang 已提交
2352 2353 2354 2355 2356 2357 2358
    // var Clazz = ChartView;
    // if (superClass) {
    //     superClass = superClass.replace('series.', '');
    //     var classType = parseClassType(superClass);
    //     Clazz = ChartView.getClass(classType.main, true);
    // }
    return ChartView.extend(opts);
S
sushuang 已提交
2359
}
S
sushuang 已提交
2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376

/**
 * ZRender need a canvas context to do measureText.
 * But in node environment canvas may be created by node-canvas.
 * So we need to specify how to create a canvas instead of using document.createElement('canvas')
 *
 * Be careful of using it in the browser.
 *
 * @param {Function} creator
 * @example
 *     var Canvas = require('canvas');
 *     var echarts = require('echarts');
 *     echarts.setCanvasCreator(function () {
 *         // Small size is enough.
 *         return new Canvas(32, 32);
 *     });
 */
S
sushuang 已提交
2377
export function setCanvasCreator(creator) {
S
sushuang 已提交
2378 2379 2380 2381 2382
    zrUtil.$override('createCanvas', creator);
}

/**
 * @param {string} mapName
S
sushuang 已提交
2383
 * @param {Array.<Object>|Object|string} geoJson
S
sushuang 已提交
2384 2385
 * @param {Object} [specialAreas]
 *
S
sushuang 已提交
2386
 * @example GeoJSON
S
sushuang 已提交
2387 2388 2389 2390 2391 2392 2393 2394
 *     $.get('USA.json', function (geoJson) {
 *         echarts.registerMap('USA', geoJson);
 *         // Or
 *         echarts.registerMap('USA', {
 *             geoJson: geoJson,
 *             specialAreas: {}
 *         })
 *     });
S
sushuang 已提交
2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405
 *
 *     $.get('airport.svg', function (svg) {
 *         echarts.registerMap('airport', {
 *             svg: svg
 *         }
 *     });
 *
 *     echarts.registerMap('eu', [
 *         {svg: eu-topographic.svg},
 *         {geoJSON: eu.json}
 *     ])
S
sushuang 已提交
2406 2407
 */
export function registerMap(mapName, geoJson, specialAreas) {
S
sushuang 已提交
2408
    mapDataStorage.registerMap(mapName, geoJson, specialAreas);
S
sushuang 已提交
2409 2410 2411 2412 2413 2414 2415
}

/**
 * @param {string} mapName
 * @return {Object}
 */
export function getMap(mapName) {
S
sushuang 已提交
2416 2417 2418 2419 2420 2421
    // For backward compatibility, only return the first one.
    var records = mapDataStorage.retrieveMap(mapName);
    return records && records[0] && {
        geoJson: records[0].geoJSON,
        specialAreas: records[0].specialAreas
    };
S
sushuang 已提交
2422
}
S
sushuang 已提交
2423

S
sushuang 已提交
2424 2425
registerVisual(PRIORITY_VISUAL_GLOBAL, seriesColor);
registerPreprocessor(backwardCompat);
2426
registerProcessor(PRIORITY_PROCESSOR_DATASTACK, dataStack);
S
sushuang 已提交
2427
registerLoading('default', loadingDefault);
S
sushuang 已提交
2428

S
sushuang 已提交
2429 2430
// Default actions

S
sushuang 已提交
2431
registerAction({
S
sushuang 已提交
2432 2433 2434 2435
    type: 'highlight',
    event: 'highlight',
    update: 'highlight'
}, zrUtil.noop);
S
sushuang 已提交
2436

S
sushuang 已提交
2437
registerAction({
S
sushuang 已提交
2438 2439 2440 2441 2442
    type: 'downplay',
    event: 'downplay',
    update: 'downplay'
}, zrUtil.noop);

P
pissang 已提交
2443 2444 2445
// Default theme
registerTheme('light', lightTheme);
registerTheme('dark', darkTheme);
S
sushuang 已提交
2446

S
sushuang 已提交
2447 2448 2449
// For backward compatibility, where the namespace `dataTool` will
// be mounted on `echarts` is the extension `dataTool` is imported.
export var dataTool = {};