echarts.js 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var _config = require("./config");
  20. var __DEV__ = _config.__DEV__;
  21. var zrender = require("zrender/lib/zrender");
  22. var zrUtil = require("zrender/lib/core/util");
  23. var colorTool = require("zrender/lib/tool/color");
  24. var env = require("zrender/lib/core/env");
  25. var timsort = require("zrender/lib/core/timsort");
  26. var Eventful = require("zrender/lib/mixin/Eventful");
  27. var GlobalModel = require("./model/Global");
  28. var ExtensionAPI = require("./ExtensionAPI");
  29. var CoordinateSystemManager = require("./CoordinateSystem");
  30. var OptionManager = require("./model/OptionManager");
  31. var backwardCompat = require("./preprocessor/backwardCompat");
  32. var dataStack = require("./processor/dataStack");
  33. var ComponentModel = require("./model/Component");
  34. var SeriesModel = require("./model/Series");
  35. var ComponentView = require("./view/Component");
  36. var ChartView = require("./view/Chart");
  37. var graphic = require("./util/graphic");
  38. var modelUtil = require("./util/model");
  39. var _throttle = require("./util/throttle");
  40. var throttle = _throttle.throttle;
  41. var seriesColor = require("./visual/seriesColor");
  42. var aria = require("./visual/aria");
  43. var loadingDefault = require("./loading/default");
  44. var Scheduler = require("./stream/Scheduler");
  45. var lightTheme = require("./theme/light");
  46. var darkTheme = require("./theme/dark");
  47. require("./component/dataset");
  48. var mapDataStorage = require("./coord/geo/mapDataStorage");
  49. /*
  50. * Licensed to the Apache Software Foundation (ASF) under one
  51. * or more contributor license agreements. See the NOTICE file
  52. * distributed with this work for additional information
  53. * regarding copyright ownership. The ASF licenses this file
  54. * to you under the Apache License, Version 2.0 (the
  55. * "License"); you may not use this file except in compliance
  56. * with the License. You may obtain a copy of the License at
  57. *
  58. * http://www.apache.org/licenses/LICENSE-2.0
  59. *
  60. * Unless required by applicable law or agreed to in writing,
  61. * software distributed under the License is distributed on an
  62. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  63. * KIND, either express or implied. See the License for the
  64. * specific language governing permissions and limitations
  65. * under the License.
  66. */
  67. var assert = zrUtil.assert;
  68. var each = zrUtil.each;
  69. var isFunction = zrUtil.isFunction;
  70. var isObject = zrUtil.isObject;
  71. var parseClassType = ComponentModel.parseClassType;
  72. var version = '4.9.0';
  73. var dependencies = {
  74. zrender: '4.3.2'
  75. };
  76. var TEST_FRAME_REMAIN_TIME = 1;
  77. var PRIORITY_PROCESSOR_FILTER = 1000;
  78. var PRIORITY_PROCESSOR_SERIES_FILTER = 800;
  79. var PRIORITY_PROCESSOR_DATASTACK = 900;
  80. var PRIORITY_PROCESSOR_STATISTIC = 5000;
  81. var PRIORITY_VISUAL_LAYOUT = 1000;
  82. var PRIORITY_VISUAL_PROGRESSIVE_LAYOUT = 1100;
  83. var PRIORITY_VISUAL_GLOBAL = 2000;
  84. var PRIORITY_VISUAL_CHART = 3000;
  85. var PRIORITY_VISUAL_POST_CHART_LAYOUT = 3500;
  86. var PRIORITY_VISUAL_COMPONENT = 4000; // FIXME
  87. // necessary?
  88. var PRIORITY_VISUAL_BRUSH = 5000;
  89. var PRIORITY = {
  90. PROCESSOR: {
  91. FILTER: PRIORITY_PROCESSOR_FILTER,
  92. SERIES_FILTER: PRIORITY_PROCESSOR_SERIES_FILTER,
  93. STATISTIC: PRIORITY_PROCESSOR_STATISTIC
  94. },
  95. VISUAL: {
  96. LAYOUT: PRIORITY_VISUAL_LAYOUT,
  97. PROGRESSIVE_LAYOUT: PRIORITY_VISUAL_PROGRESSIVE_LAYOUT,
  98. GLOBAL: PRIORITY_VISUAL_GLOBAL,
  99. CHART: PRIORITY_VISUAL_CHART,
  100. POST_CHART_LAYOUT: PRIORITY_VISUAL_POST_CHART_LAYOUT,
  101. COMPONENT: PRIORITY_VISUAL_COMPONENT,
  102. BRUSH: PRIORITY_VISUAL_BRUSH
  103. }
  104. }; // Main process have three entries: `setOption`, `dispatchAction` and `resize`,
  105. // where they must not be invoked nestedly, except the only case: invoke
  106. // dispatchAction with updateMethod "none" in main process.
  107. // This flag is used to carry out this rule.
  108. // All events will be triggered out side main process (i.e. when !this[IN_MAIN_PROCESS]).
  109. var IN_MAIN_PROCESS = '__flagInMainProcess';
  110. var OPTION_UPDATED = '__optionUpdated';
  111. var ACTION_REG = /^[a-zA-Z0-9_]+$/;
  112. function createRegisterEventWithLowercaseName(method, ignoreDisposed) {
  113. return function (eventName, handler, context) {
  114. if (!ignoreDisposed && this._disposed) {
  115. disposedWarning(this.id);
  116. return;
  117. } // Event name is all lowercase
  118. eventName = eventName && eventName.toLowerCase();
  119. Eventful.prototype[method].call(this, eventName, handler, context);
  120. };
  121. }
  122. /**
  123. * @module echarts~MessageCenter
  124. */
  125. function MessageCenter() {
  126. Eventful.call(this);
  127. }
  128. MessageCenter.prototype.on = createRegisterEventWithLowercaseName('on', true);
  129. MessageCenter.prototype.off = createRegisterEventWithLowercaseName('off', true);
  130. MessageCenter.prototype.one = createRegisterEventWithLowercaseName('one', true);
  131. zrUtil.mixin(MessageCenter, Eventful);
  132. /**
  133. * @module echarts~ECharts
  134. */
  135. function ECharts(dom, theme, opts) {
  136. opts = opts || {}; // Get theme by name
  137. if (typeof theme === 'string') {
  138. theme = themeStorage[theme];
  139. }
  140. /**
  141. * @type {string}
  142. */
  143. this.id;
  144. /**
  145. * Group id
  146. * @type {string}
  147. */
  148. this.group;
  149. /**
  150. * @type {HTMLElement}
  151. * @private
  152. */
  153. this._dom = dom;
  154. var defaultRenderer = 'canvas';
  155. /**
  156. * @type {module:zrender/ZRender}
  157. * @private
  158. */
  159. var zr = this._zr = zrender.init(dom, {
  160. renderer: opts.renderer || defaultRenderer,
  161. devicePixelRatio: opts.devicePixelRatio,
  162. width: opts.width,
  163. height: opts.height
  164. });
  165. /**
  166. * Expect 60 fps.
  167. * @type {Function}
  168. * @private
  169. */
  170. this._throttledZrFlush = throttle(zrUtil.bind(zr.flush, zr), 17);
  171. var theme = zrUtil.clone(theme);
  172. theme && backwardCompat(theme, true);
  173. /**
  174. * @type {Object}
  175. * @private
  176. */
  177. this._theme = theme;
  178. /**
  179. * @type {Array.<module:echarts/view/Chart>}
  180. * @private
  181. */
  182. this._chartsViews = [];
  183. /**
  184. * @type {Object.<string, module:echarts/view/Chart>}
  185. * @private
  186. */
  187. this._chartsMap = {};
  188. /**
  189. * @type {Array.<module:echarts/view/Component>}
  190. * @private
  191. */
  192. this._componentsViews = [];
  193. /**
  194. * @type {Object.<string, module:echarts/view/Component>}
  195. * @private
  196. */
  197. this._componentsMap = {};
  198. /**
  199. * @type {module:echarts/CoordinateSystem}
  200. * @private
  201. */
  202. this._coordSysMgr = new CoordinateSystemManager();
  203. /**
  204. * @type {module:echarts/ExtensionAPI}
  205. * @private
  206. */
  207. var api = this._api = createExtensionAPI(this); // Sort on demand
  208. function prioritySortFunc(a, b) {
  209. return a.__prio - b.__prio;
  210. }
  211. timsort(visualFuncs, prioritySortFunc);
  212. timsort(dataProcessorFuncs, prioritySortFunc);
  213. /**
  214. * @type {module:echarts/stream/Scheduler}
  215. */
  216. this._scheduler = new Scheduler(this, api, dataProcessorFuncs, visualFuncs);
  217. Eventful.call(this, this._ecEventProcessor = new EventProcessor());
  218. /**
  219. * @type {module:echarts~MessageCenter}
  220. * @private
  221. */
  222. this._messageCenter = new MessageCenter(); // Init mouse events
  223. this._initEvents(); // In case some people write `window.onresize = chart.resize`
  224. this.resize = zrUtil.bind(this.resize, this); // Can't dispatch action during rendering procedure
  225. this._pendingActions = [];
  226. zr.animation.on('frame', this._onframe, this);
  227. bindRenderedEvent(zr, this); // ECharts instance can be used as value.
  228. zrUtil.setAsPrimitive(this);
  229. }
  230. var echartsProto = ECharts.prototype;
  231. echartsProto._onframe = function () {
  232. if (this._disposed) {
  233. return;
  234. }
  235. var scheduler = this._scheduler; // Lazy update
  236. if (this[OPTION_UPDATED]) {
  237. var silent = this[OPTION_UPDATED].silent;
  238. this[IN_MAIN_PROCESS] = true;
  239. prepare(this);
  240. updateMethods.update.call(this);
  241. this[IN_MAIN_PROCESS] = false;
  242. this[OPTION_UPDATED] = false;
  243. flushPendingActions.call(this, silent);
  244. triggerUpdatedEvent.call(this, silent);
  245. } // Avoid do both lazy update and progress in one frame.
  246. else if (scheduler.unfinished) {
  247. // Stream progress.
  248. var remainTime = TEST_FRAME_REMAIN_TIME;
  249. var ecModel = this._model;
  250. var api = this._api;
  251. scheduler.unfinished = false;
  252. do {
  253. var startTime = +new Date();
  254. scheduler.performSeriesTasks(ecModel); // Currently dataProcessorFuncs do not check threshold.
  255. scheduler.performDataProcessorTasks(ecModel);
  256. updateStreamModes(this, ecModel); // Do not update coordinate system here. Because that coord system update in
  257. // each frame is not a good user experience. So we follow the rule that
  258. // the extent of the coordinate system is determin in the first frame (the
  259. // frame is executed immedietely after task reset.
  260. // this._coordSysMgr.update(ecModel, api);
  261. // console.log('--- ec frame visual ---', remainTime);
  262. scheduler.performVisualTasks(ecModel);
  263. renderSeries(this, this._model, api, 'remain');
  264. remainTime -= +new Date() - startTime;
  265. } while (remainTime > 0 && scheduler.unfinished); // Call flush explicitly for trigger finished event.
  266. if (!scheduler.unfinished) {
  267. this._zr.flush();
  268. } // Else, zr flushing be ensue within the same frame,
  269. // because zr flushing is after onframe event.
  270. }
  271. };
  272. /**
  273. * @return {HTMLElement}
  274. */
  275. echartsProto.getDom = function () {
  276. return this._dom;
  277. };
  278. /**
  279. * @return {module:zrender~ZRender}
  280. */
  281. echartsProto.getZr = function () {
  282. return this._zr;
  283. };
  284. /**
  285. * Usage:
  286. * chart.setOption(option, notMerge, lazyUpdate);
  287. * chart.setOption(option, {
  288. * notMerge: ...,
  289. * lazyUpdate: ...,
  290. * silent: ...
  291. * });
  292. *
  293. * @param {Object} option
  294. * @param {Object|boolean} [opts] opts or notMerge.
  295. * @param {boolean} [opts.notMerge=false]
  296. * @param {boolean} [opts.lazyUpdate=false] Useful when setOption frequently.
  297. */
  298. echartsProto.setOption = function (option, notMerge, lazyUpdate) {
  299. if (this._disposed) {
  300. disposedWarning(this.id);
  301. return;
  302. }
  303. var silent;
  304. if (isObject(notMerge)) {
  305. lazyUpdate = notMerge.lazyUpdate;
  306. silent = notMerge.silent;
  307. notMerge = notMerge.notMerge;
  308. }
  309. this[IN_MAIN_PROCESS] = true;
  310. if (!this._model || notMerge) {
  311. var optionManager = new OptionManager(this._api);
  312. var theme = this._theme;
  313. var ecModel = this._model = new GlobalModel();
  314. ecModel.scheduler = this._scheduler;
  315. ecModel.init(null, null, theme, optionManager);
  316. }
  317. this._model.setOption(option, optionPreprocessorFuncs);
  318. if (lazyUpdate) {
  319. this[OPTION_UPDATED] = {
  320. silent: silent
  321. };
  322. this[IN_MAIN_PROCESS] = false;
  323. } else {
  324. prepare(this);
  325. updateMethods.update.call(this); // Ensure zr refresh sychronously, and then pixel in canvas can be
  326. // fetched after `setOption`.
  327. this._zr.flush();
  328. this[OPTION_UPDATED] = false;
  329. this[IN_MAIN_PROCESS] = false;
  330. flushPendingActions.call(this, silent);
  331. triggerUpdatedEvent.call(this, silent);
  332. }
  333. };
  334. /**
  335. * @DEPRECATED
  336. */
  337. echartsProto.setTheme = function () {
  338. console.error('ECharts#setTheme() is DEPRECATED in ECharts 3.0');
  339. };
  340. /**
  341. * @return {module:echarts/model/Global}
  342. */
  343. echartsProto.getModel = function () {
  344. return this._model;
  345. };
  346. /**
  347. * @return {Object}
  348. */
  349. echartsProto.getOption = function () {
  350. return this._model && this._model.getOption();
  351. };
  352. /**
  353. * @return {number}
  354. */
  355. echartsProto.getWidth = function () {
  356. return this._zr.getWidth();
  357. };
  358. /**
  359. * @return {number}
  360. */
  361. echartsProto.getHeight = function () {
  362. return this._zr.getHeight();
  363. };
  364. /**
  365. * @return {number}
  366. */
  367. echartsProto.getDevicePixelRatio = function () {
  368. return this._zr.painter.dpr || window.devicePixelRatio || 1;
  369. };
  370. /**
  371. * Get canvas which has all thing rendered
  372. * @param {Object} opts
  373. * @param {string} [opts.backgroundColor]
  374. * @return {string}
  375. */
  376. echartsProto.getRenderedCanvas = function (opts) {
  377. if (!env.canvasSupported) {
  378. return;
  379. }
  380. opts = opts || {};
  381. opts.pixelRatio = opts.pixelRatio || 1;
  382. opts.backgroundColor = opts.backgroundColor || this._model.get('backgroundColor');
  383. var zr = this._zr; // var list = zr.storage.getDisplayList();
  384. // Stop animations
  385. // Never works before in init animation, so remove it.
  386. // zrUtil.each(list, function (el) {
  387. // el.stopAnimation(true);
  388. // });
  389. return zr.painter.getRenderedCanvas(opts);
  390. };
  391. /**
  392. * Get svg data url
  393. * @return {string}
  394. */
  395. echartsProto.getSvgDataURL = function () {
  396. if (!env.svgSupported) {
  397. return;
  398. }
  399. var zr = this._zr;
  400. var list = zr.storage.getDisplayList(); // Stop animations
  401. zrUtil.each(list, function (el) {
  402. el.stopAnimation(true);
  403. });
  404. return zr.painter.toDataURL();
  405. };
  406. /**
  407. * @return {string}
  408. * @param {Object} opts
  409. * @param {string} [opts.type='png']
  410. * @param {string} [opts.pixelRatio=1]
  411. * @param {string} [opts.backgroundColor]
  412. * @param {string} [opts.excludeComponents]
  413. */
  414. echartsProto.getDataURL = function (opts) {
  415. if (this._disposed) {
  416. disposedWarning(this.id);
  417. return;
  418. }
  419. opts = opts || {};
  420. var excludeComponents = opts.excludeComponents;
  421. var ecModel = this._model;
  422. var excludesComponentViews = [];
  423. var self = this;
  424. each(excludeComponents, function (componentType) {
  425. ecModel.eachComponent({
  426. mainType: componentType
  427. }, function (component) {
  428. var view = self._componentsMap[component.__viewId];
  429. if (!view.group.ignore) {
  430. excludesComponentViews.push(view);
  431. view.group.ignore = true;
  432. }
  433. });
  434. });
  435. var url = this._zr.painter.getType() === 'svg' ? this.getSvgDataURL() : this.getRenderedCanvas(opts).toDataURL('image/' + (opts && opts.type || 'png'));
  436. each(excludesComponentViews, function (view) {
  437. view.group.ignore = false;
  438. });
  439. return url;
  440. };
  441. /**
  442. * @return {string}
  443. * @param {Object} opts
  444. * @param {string} [opts.type='png']
  445. * @param {string} [opts.pixelRatio=1]
  446. * @param {string} [opts.backgroundColor]
  447. */
  448. echartsProto.getConnectedDataURL = function (opts) {
  449. if (this._disposed) {
  450. disposedWarning(this.id);
  451. return;
  452. }
  453. if (!env.canvasSupported) {
  454. return;
  455. }
  456. var isSvg = opts.type === 'svg';
  457. var groupId = this.group;
  458. var mathMin = Math.min;
  459. var mathMax = Math.max;
  460. var MAX_NUMBER = Infinity;
  461. if (connectedGroups[groupId]) {
  462. var left = MAX_NUMBER;
  463. var top = MAX_NUMBER;
  464. var right = -MAX_NUMBER;
  465. var bottom = -MAX_NUMBER;
  466. var canvasList = [];
  467. var dpr = opts && opts.pixelRatio || 1;
  468. zrUtil.each(instances, function (chart, id) {
  469. if (chart.group === groupId) {
  470. var canvas = isSvg ? chart.getZr().painter.getSvgDom().innerHTML : chart.getRenderedCanvas(zrUtil.clone(opts));
  471. var boundingRect = chart.getDom().getBoundingClientRect();
  472. left = mathMin(boundingRect.left, left);
  473. top = mathMin(boundingRect.top, top);
  474. right = mathMax(boundingRect.right, right);
  475. bottom = mathMax(boundingRect.bottom, bottom);
  476. canvasList.push({
  477. dom: canvas,
  478. left: boundingRect.left,
  479. top: boundingRect.top
  480. });
  481. }
  482. });
  483. left *= dpr;
  484. top *= dpr;
  485. right *= dpr;
  486. bottom *= dpr;
  487. var width = right - left;
  488. var height = bottom - top;
  489. var targetCanvas = zrUtil.createCanvas();
  490. var zr = zrender.init(targetCanvas, {
  491. renderer: isSvg ? 'svg' : 'canvas'
  492. });
  493. zr.resize({
  494. width: width,
  495. height: height
  496. });
  497. if (isSvg) {
  498. var content = '';
  499. each(canvasList, function (item) {
  500. var x = item.left - left;
  501. var y = item.top - top;
  502. content += '<g transform="translate(' + x + ',' + y + ')">' + item.dom + '</g>';
  503. });
  504. zr.painter.getSvgRoot().innerHTML = content;
  505. if (opts.connectedBackgroundColor) {
  506. zr.painter.setBackgroundColor(opts.connectedBackgroundColor);
  507. }
  508. zr.refreshImmediately();
  509. return zr.painter.toDataURL();
  510. } else {
  511. // Background between the charts
  512. if (opts.connectedBackgroundColor) {
  513. zr.add(new graphic.Rect({
  514. shape: {
  515. x: 0,
  516. y: 0,
  517. width: width,
  518. height: height
  519. },
  520. style: {
  521. fill: opts.connectedBackgroundColor
  522. }
  523. }));
  524. }
  525. each(canvasList, function (item) {
  526. var img = new graphic.Image({
  527. style: {
  528. x: item.left * dpr - left,
  529. y: item.top * dpr - top,
  530. image: item.dom
  531. }
  532. });
  533. zr.add(img);
  534. });
  535. zr.refreshImmediately();
  536. return targetCanvas.toDataURL('image/' + (opts && opts.type || 'png'));
  537. }
  538. } else {
  539. return this.getDataURL(opts);
  540. }
  541. };
  542. /**
  543. * Convert from logical coordinate system to pixel coordinate system.
  544. * See CoordinateSystem#convertToPixel.
  545. * @param {string|Object} finder
  546. * If string, e.g., 'geo', means {geoIndex: 0}.
  547. * If Object, could contain some of these properties below:
  548. * {
  549. * seriesIndex / seriesId / seriesName,
  550. * geoIndex / geoId, geoName,
  551. * bmapIndex / bmapId / bmapName,
  552. * xAxisIndex / xAxisId / xAxisName,
  553. * yAxisIndex / yAxisId / yAxisName,
  554. * gridIndex / gridId / gridName,
  555. * ... (can be extended)
  556. * }
  557. * @param {Array|number} value
  558. * @return {Array|number} result
  559. */
  560. echartsProto.convertToPixel = zrUtil.curry(doConvertPixel, 'convertToPixel');
  561. /**
  562. * Convert from pixel coordinate system to logical coordinate system.
  563. * See CoordinateSystem#convertFromPixel.
  564. * @param {string|Object} finder
  565. * If string, e.g., 'geo', means {geoIndex: 0}.
  566. * If Object, could contain some of these properties below:
  567. * {
  568. * seriesIndex / seriesId / seriesName,
  569. * geoIndex / geoId / geoName,
  570. * bmapIndex / bmapId / bmapName,
  571. * xAxisIndex / xAxisId / xAxisName,
  572. * yAxisIndex / yAxisId / yAxisName
  573. * gridIndex / gridId / gridName,
  574. * ... (can be extended)
  575. * }
  576. * @param {Array|number} value
  577. * @return {Array|number} result
  578. */
  579. echartsProto.convertFromPixel = zrUtil.curry(doConvertPixel, 'convertFromPixel');
  580. function doConvertPixel(methodName, finder, value) {
  581. if (this._disposed) {
  582. disposedWarning(this.id);
  583. return;
  584. }
  585. var ecModel = this._model;
  586. var coordSysList = this._coordSysMgr.getCoordinateSystems();
  587. var result;
  588. finder = modelUtil.parseFinder(ecModel, finder);
  589. for (var i = 0; i < coordSysList.length; i++) {
  590. var coordSys = coordSysList[i];
  591. if (coordSys[methodName] && (result = coordSys[methodName](ecModel, finder, value)) != null) {
  592. return result;
  593. }
  594. }
  595. }
  596. /**
  597. * Is the specified coordinate systems or components contain the given pixel point.
  598. * @param {string|Object} finder
  599. * If string, e.g., 'geo', means {geoIndex: 0}.
  600. * If Object, could contain some of these properties below:
  601. * {
  602. * seriesIndex / seriesId / seriesName,
  603. * geoIndex / geoId / geoName,
  604. * bmapIndex / bmapId / bmapName,
  605. * xAxisIndex / xAxisId / xAxisName,
  606. * yAxisIndex / yAxisId / yAxisName,
  607. * gridIndex / gridId / gridName,
  608. * ... (can be extended)
  609. * }
  610. * @param {Array|number} value
  611. * @return {boolean} result
  612. */
  613. echartsProto.containPixel = function (finder, value) {
  614. if (this._disposed) {
  615. disposedWarning(this.id);
  616. return;
  617. }
  618. var ecModel = this._model;
  619. var result;
  620. finder = modelUtil.parseFinder(ecModel, finder);
  621. zrUtil.each(finder, function (models, key) {
  622. key.indexOf('Models') >= 0 && zrUtil.each(models, function (model) {
  623. var coordSys = model.coordinateSystem;
  624. if (coordSys && coordSys.containPoint) {
  625. result |= !!coordSys.containPoint(value);
  626. } else if (key === 'seriesModels') {
  627. var view = this._chartsMap[model.__viewId];
  628. if (view && view.containPoint) {
  629. result |= view.containPoint(value, model);
  630. } else {}
  631. } else {}
  632. }, this);
  633. }, this);
  634. return !!result;
  635. };
  636. /**
  637. * Get visual from series or data.
  638. * @param {string|Object} finder
  639. * If string, e.g., 'series', means {seriesIndex: 0}.
  640. * If Object, could contain some of these properties below:
  641. * {
  642. * seriesIndex / seriesId / seriesName,
  643. * dataIndex / dataIndexInside
  644. * }
  645. * If dataIndex is not specified, series visual will be fetched,
  646. * but not data item visual.
  647. * If all of seriesIndex, seriesId, seriesName are not specified,
  648. * visual will be fetched from first series.
  649. * @param {string} visualType 'color', 'symbol', 'symbolSize'
  650. */
  651. echartsProto.getVisual = function (finder, visualType) {
  652. var ecModel = this._model;
  653. finder = modelUtil.parseFinder(ecModel, finder, {
  654. defaultMainType: 'series'
  655. });
  656. var seriesModel = finder.seriesModel;
  657. var data = seriesModel.getData();
  658. var dataIndexInside = finder.hasOwnProperty('dataIndexInside') ? finder.dataIndexInside : finder.hasOwnProperty('dataIndex') ? data.indexOfRawIndex(finder.dataIndex) : null;
  659. return dataIndexInside != null ? data.getItemVisual(dataIndexInside, visualType) : data.getVisual(visualType);
  660. };
  661. /**
  662. * Get view of corresponding component model
  663. * @param {module:echarts/model/Component} componentModel
  664. * @return {module:echarts/view/Component}
  665. */
  666. echartsProto.getViewOfComponentModel = function (componentModel) {
  667. return this._componentsMap[componentModel.__viewId];
  668. };
  669. /**
  670. * Get view of corresponding series model
  671. * @param {module:echarts/model/Series} seriesModel
  672. * @return {module:echarts/view/Chart}
  673. */
  674. echartsProto.getViewOfSeriesModel = function (seriesModel) {
  675. return this._chartsMap[seriesModel.__viewId];
  676. };
  677. var updateMethods = {
  678. prepareAndUpdate: function (payload) {
  679. prepare(this);
  680. updateMethods.update.call(this, payload);
  681. },
  682. /**
  683. * @param {Object} payload
  684. * @private
  685. */
  686. update: function (payload) {
  687. // console.profile && console.profile('update');
  688. var ecModel = this._model;
  689. var api = this._api;
  690. var zr = this._zr;
  691. var coordSysMgr = this._coordSysMgr;
  692. var scheduler = this._scheduler; // update before setOption
  693. if (!ecModel) {
  694. return;
  695. }
  696. scheduler.restoreData(ecModel, payload);
  697. scheduler.performSeriesTasks(ecModel); // TODO
  698. // Save total ecModel here for undo/redo (after restoring data and before processing data).
  699. // Undo (restoration of total ecModel) can be carried out in 'action' or outside API call.
  700. // Create new coordinate system each update
  701. // In LineView may save the old coordinate system and use it to get the orignal point
  702. coordSysMgr.create(ecModel, api);
  703. scheduler.performDataProcessorTasks(ecModel, payload); // Current stream render is not supported in data process. So we can update
  704. // stream modes after data processing, where the filtered data is used to
  705. // deteming whether use progressive rendering.
  706. updateStreamModes(this, ecModel); // We update stream modes before coordinate system updated, then the modes info
  707. // can be fetched when coord sys updating (consider the barGrid extent fix). But
  708. // the drawback is the full coord info can not be fetched. Fortunately this full
  709. // coord is not requied in stream mode updater currently.
  710. coordSysMgr.update(ecModel, api);
  711. clearColorPalette(ecModel);
  712. scheduler.performVisualTasks(ecModel, payload);
  713. render(this, ecModel, api, payload); // Set background
  714. var backgroundColor = ecModel.get('backgroundColor') || 'transparent'; // In IE8
  715. if (!env.canvasSupported) {
  716. var colorArr = colorTool.parse(backgroundColor);
  717. backgroundColor = colorTool.stringify(colorArr, 'rgb');
  718. if (colorArr[3] === 0) {
  719. backgroundColor = 'transparent';
  720. }
  721. } else {
  722. zr.setBackgroundColor(backgroundColor);
  723. }
  724. performPostUpdateFuncs(ecModel, api); // console.profile && console.profileEnd('update');
  725. },
  726. /**
  727. * @param {Object} payload
  728. * @private
  729. */
  730. updateTransform: function (payload) {
  731. var ecModel = this._model;
  732. var ecIns = this;
  733. var api = this._api; // update before setOption
  734. if (!ecModel) {
  735. return;
  736. } // ChartView.markUpdateMethod(payload, 'updateTransform');
  737. var componentDirtyList = [];
  738. ecModel.eachComponent(function (componentType, componentModel) {
  739. var componentView = ecIns.getViewOfComponentModel(componentModel);
  740. if (componentView && componentView.__alive) {
  741. if (componentView.updateTransform) {
  742. var result = componentView.updateTransform(componentModel, ecModel, api, payload);
  743. result && result.update && componentDirtyList.push(componentView);
  744. } else {
  745. componentDirtyList.push(componentView);
  746. }
  747. }
  748. });
  749. var seriesDirtyMap = zrUtil.createHashMap();
  750. ecModel.eachSeries(function (seriesModel) {
  751. var chartView = ecIns._chartsMap[seriesModel.__viewId];
  752. if (chartView.updateTransform) {
  753. var result = chartView.updateTransform(seriesModel, ecModel, api, payload);
  754. result && result.update && seriesDirtyMap.set(seriesModel.uid, 1);
  755. } else {
  756. seriesDirtyMap.set(seriesModel.uid, 1);
  757. }
  758. });
  759. clearColorPalette(ecModel); // Keep pipe to the exist pipeline because it depends on the render task of the full pipeline.
  760. // this._scheduler.performVisualTasks(ecModel, payload, 'layout', true);
  761. this._scheduler.performVisualTasks(ecModel, payload, {
  762. setDirty: true,
  763. dirtyMap: seriesDirtyMap
  764. }); // Currently, not call render of components. Geo render cost a lot.
  765. // renderComponents(ecIns, ecModel, api, payload, componentDirtyList);
  766. renderSeries(ecIns, ecModel, api, payload, seriesDirtyMap);
  767. performPostUpdateFuncs(ecModel, this._api);
  768. },
  769. /**
  770. * @param {Object} payload
  771. * @private
  772. */
  773. updateView: function (payload) {
  774. var ecModel = this._model; // update before setOption
  775. if (!ecModel) {
  776. return;
  777. }
  778. ChartView.markUpdateMethod(payload, 'updateView');
  779. clearColorPalette(ecModel); // Keep pipe to the exist pipeline because it depends on the render task of the full pipeline.
  780. this._scheduler.performVisualTasks(ecModel, payload, {
  781. setDirty: true
  782. });
  783. render(this, this._model, this._api, payload);
  784. performPostUpdateFuncs(ecModel, this._api);
  785. },
  786. /**
  787. * @param {Object} payload
  788. * @private
  789. */
  790. updateVisual: function (payload) {
  791. updateMethods.update.call(this, payload); // var ecModel = this._model;
  792. // // update before setOption
  793. // if (!ecModel) {
  794. // return;
  795. // }
  796. // ChartView.markUpdateMethod(payload, 'updateVisual');
  797. // clearColorPalette(ecModel);
  798. // // Keep pipe to the exist pipeline because it depends on the render task of the full pipeline.
  799. // this._scheduler.performVisualTasks(ecModel, payload, {visualType: 'visual', setDirty: true});
  800. // render(this, this._model, this._api, payload);
  801. // performPostUpdateFuncs(ecModel, this._api);
  802. },
  803. /**
  804. * @param {Object} payload
  805. * @private
  806. */
  807. updateLayout: function (payload) {
  808. updateMethods.update.call(this, payload); // var ecModel = this._model;
  809. // // update before setOption
  810. // if (!ecModel) {
  811. // return;
  812. // }
  813. // ChartView.markUpdateMethod(payload, 'updateLayout');
  814. // // Keep pipe to the exist pipeline because it depends on the render task of the full pipeline.
  815. // // this._scheduler.performVisualTasks(ecModel, payload, 'layout', true);
  816. // this._scheduler.performVisualTasks(ecModel, payload, {setDirty: true});
  817. // render(this, this._model, this._api, payload);
  818. // performPostUpdateFuncs(ecModel, this._api);
  819. }
  820. };
  821. function prepare(ecIns) {
  822. var ecModel = ecIns._model;
  823. var scheduler = ecIns._scheduler;
  824. scheduler.restorePipelines(ecModel);
  825. scheduler.prepareStageTasks();
  826. prepareView(ecIns, 'component', ecModel, scheduler);
  827. prepareView(ecIns, 'chart', ecModel, scheduler);
  828. scheduler.plan();
  829. }
  830. /**
  831. * @private
  832. */
  833. function updateDirectly(ecIns, method, payload, mainType, subType) {
  834. var ecModel = ecIns._model; // broadcast
  835. if (!mainType) {
  836. // FIXME
  837. // Chart will not be update directly here, except set dirty.
  838. // But there is no such scenario now.
  839. each(ecIns._componentsViews.concat(ecIns._chartsViews), callView);
  840. return;
  841. }
  842. var query = {};
  843. query[mainType + 'Id'] = payload[mainType + 'Id'];
  844. query[mainType + 'Index'] = payload[mainType + 'Index'];
  845. query[mainType + 'Name'] = payload[mainType + 'Name'];
  846. var condition = {
  847. mainType: mainType,
  848. query: query
  849. };
  850. subType && (condition.subType = subType); // subType may be '' by parseClassType;
  851. var excludeSeriesId = payload.excludeSeriesId;
  852. if (excludeSeriesId != null) {
  853. excludeSeriesId = zrUtil.createHashMap(modelUtil.normalizeToArray(excludeSeriesId));
  854. } // If dispatchAction before setOption, do nothing.
  855. ecModel && ecModel.eachComponent(condition, function (model) {
  856. if (!excludeSeriesId || excludeSeriesId.get(model.id) == null) {
  857. callView(ecIns[mainType === 'series' ? '_chartsMap' : '_componentsMap'][model.__viewId]);
  858. }
  859. }, ecIns);
  860. function callView(view) {
  861. view && view.__alive && view[method] && view[method](view.__model, ecModel, ecIns._api, payload);
  862. }
  863. }
  864. /**
  865. * Resize the chart
  866. * @param {Object} opts
  867. * @param {number} [opts.width] Can be 'auto' (the same as null/undefined)
  868. * @param {number} [opts.height] Can be 'auto' (the same as null/undefined)
  869. * @param {boolean} [opts.silent=false]
  870. */
  871. echartsProto.resize = function (opts) {
  872. if (this._disposed) {
  873. disposedWarning(this.id);
  874. return;
  875. }
  876. this._zr.resize(opts);
  877. var ecModel = this._model; // Resize loading effect
  878. this._loadingFX && this._loadingFX.resize();
  879. if (!ecModel) {
  880. return;
  881. }
  882. var optionChanged = ecModel.resetOption('media');
  883. var silent = opts && opts.silent;
  884. this[IN_MAIN_PROCESS] = true;
  885. optionChanged && prepare(this);
  886. updateMethods.update.call(this);
  887. this[IN_MAIN_PROCESS] = false;
  888. flushPendingActions.call(this, silent);
  889. triggerUpdatedEvent.call(this, silent);
  890. };
  891. function updateStreamModes(ecIns, ecModel) {
  892. var chartsMap = ecIns._chartsMap;
  893. var scheduler = ecIns._scheduler;
  894. ecModel.eachSeries(function (seriesModel) {
  895. scheduler.updateStreamModes(seriesModel, chartsMap[seriesModel.__viewId]);
  896. });
  897. }
  898. /**
  899. * Show loading effect
  900. * @param {string} [name='default']
  901. * @param {Object} [cfg]
  902. */
  903. echartsProto.showLoading = function (name, cfg) {
  904. if (this._disposed) {
  905. disposedWarning(this.id);
  906. return;
  907. }
  908. if (isObject(name)) {
  909. cfg = name;
  910. name = '';
  911. }
  912. name = name || 'default';
  913. this.hideLoading();
  914. if (!loadingEffects[name]) {
  915. return;
  916. }
  917. var el = loadingEffects[name](this._api, cfg);
  918. var zr = this._zr;
  919. this._loadingFX = el;
  920. zr.add(el);
  921. };
  922. /**
  923. * Hide loading effect
  924. */
  925. echartsProto.hideLoading = function () {
  926. if (this._disposed) {
  927. disposedWarning(this.id);
  928. return;
  929. }
  930. this._loadingFX && this._zr.remove(this._loadingFX);
  931. this._loadingFX = null;
  932. };
  933. /**
  934. * @param {Object} eventObj
  935. * @return {Object}
  936. */
  937. echartsProto.makeActionFromEvent = function (eventObj) {
  938. var payload = zrUtil.extend({}, eventObj);
  939. payload.type = eventActionMap[eventObj.type];
  940. return payload;
  941. };
  942. /**
  943. * @pubilc
  944. * @param {Object} payload
  945. * @param {string} [payload.type] Action type
  946. * @param {Object|boolean} [opt] If pass boolean, means opt.silent
  947. * @param {boolean} [opt.silent=false] Whether trigger events.
  948. * @param {boolean} [opt.flush=undefined]
  949. * true: Flush immediately, and then pixel in canvas can be fetched
  950. * immediately. Caution: it might affect performance.
  951. * false: Not flush.
  952. * undefined: Auto decide whether perform flush.
  953. */
  954. echartsProto.dispatchAction = function (payload, opt) {
  955. if (this._disposed) {
  956. disposedWarning(this.id);
  957. return;
  958. }
  959. if (!isObject(opt)) {
  960. opt = {
  961. silent: !!opt
  962. };
  963. }
  964. if (!actions[payload.type]) {
  965. return;
  966. } // Avoid dispatch action before setOption. Especially in `connect`.
  967. if (!this._model) {
  968. return;
  969. } // May dispatchAction in rendering procedure
  970. if (this[IN_MAIN_PROCESS]) {
  971. this._pendingActions.push(payload);
  972. return;
  973. }
  974. doDispatchAction.call(this, payload, opt.silent);
  975. if (opt.flush) {
  976. this._zr.flush(true);
  977. } else if (opt.flush !== false && env.browser.weChat) {
  978. // In WeChat embeded browser, `requestAnimationFrame` and `setInterval`
  979. // hang when sliding page (on touch event), which cause that zr does not
  980. // refresh util user interaction finished, which is not expected.
  981. // But `dispatchAction` may be called too frequently when pan on touch
  982. // screen, which impacts performance if do not throttle them.
  983. this._throttledZrFlush();
  984. }
  985. flushPendingActions.call(this, opt.silent);
  986. triggerUpdatedEvent.call(this, opt.silent);
  987. };
  988. function doDispatchAction(payload, silent) {
  989. var payloadType = payload.type;
  990. var escapeConnect = payload.escapeConnect;
  991. var actionWrap = actions[payloadType];
  992. var actionInfo = actionWrap.actionInfo;
  993. var cptType = (actionInfo.update || 'update').split(':');
  994. var updateMethod = cptType.pop();
  995. cptType = cptType[0] != null && parseClassType(cptType[0]);
  996. this[IN_MAIN_PROCESS] = true;
  997. var payloads = [payload];
  998. var batched = false; // Batch action
  999. if (payload.batch) {
  1000. batched = true;
  1001. payloads = zrUtil.map(payload.batch, function (item) {
  1002. item = zrUtil.defaults(zrUtil.extend({}, item), payload);
  1003. item.batch = null;
  1004. return item;
  1005. });
  1006. }
  1007. var eventObjBatch = [];
  1008. var eventObj;
  1009. var isHighDown = payloadType === 'highlight' || payloadType === 'downplay';
  1010. each(payloads, function (batchItem) {
  1011. // Action can specify the event by return it.
  1012. eventObj = actionWrap.action(batchItem, this._model, this._api); // Emit event outside
  1013. eventObj = eventObj || zrUtil.extend({}, batchItem); // Convert type to eventType
  1014. eventObj.type = actionInfo.event || eventObj.type;
  1015. eventObjBatch.push(eventObj); // light update does not perform data process, layout and visual.
  1016. if (isHighDown) {
  1017. // method, payload, mainType, subType
  1018. updateDirectly(this, updateMethod, batchItem, 'series');
  1019. } else if (cptType) {
  1020. updateDirectly(this, updateMethod, batchItem, cptType.main, cptType.sub);
  1021. }
  1022. }, this);
  1023. if (updateMethod !== 'none' && !isHighDown && !cptType) {
  1024. // Still dirty
  1025. if (this[OPTION_UPDATED]) {
  1026. // FIXME Pass payload ?
  1027. prepare(this);
  1028. updateMethods.update.call(this, payload);
  1029. this[OPTION_UPDATED] = false;
  1030. } else {
  1031. updateMethods[updateMethod].call(this, payload);
  1032. }
  1033. } // Follow the rule of action batch
  1034. if (batched) {
  1035. eventObj = {
  1036. type: actionInfo.event || payloadType,
  1037. escapeConnect: escapeConnect,
  1038. batch: eventObjBatch
  1039. };
  1040. } else {
  1041. eventObj = eventObjBatch[0];
  1042. }
  1043. this[IN_MAIN_PROCESS] = false;
  1044. !silent && this._messageCenter.trigger(eventObj.type, eventObj);
  1045. }
  1046. function flushPendingActions(silent) {
  1047. var pendingActions = this._pendingActions;
  1048. while (pendingActions.length) {
  1049. var payload = pendingActions.shift();
  1050. doDispatchAction.call(this, payload, silent);
  1051. }
  1052. }
  1053. function triggerUpdatedEvent(silent) {
  1054. !silent && this.trigger('updated');
  1055. }
  1056. /**
  1057. * Event `rendered` is triggered when zr
  1058. * rendered. It is useful for realtime
  1059. * snapshot (reflect animation).
  1060. *
  1061. * Event `finished` is triggered when:
  1062. * (1) zrender rendering finished.
  1063. * (2) initial animation finished.
  1064. * (3) progressive rendering finished.
  1065. * (4) no pending action.
  1066. * (5) no delayed setOption needs to be processed.
  1067. */
  1068. function bindRenderedEvent(zr, ecIns) {
  1069. zr.on('rendered', function () {
  1070. ecIns.trigger('rendered'); // The `finished` event should not be triggered repeatly,
  1071. // so it should only be triggered when rendering indeed happend
  1072. // in zrender. (Consider the case that dipatchAction is keep
  1073. // triggering when mouse move).
  1074. if ( // Although zr is dirty if initial animation is not finished
  1075. // and this checking is called on frame, we also check
  1076. // animation finished for robustness.
  1077. zr.animation.isFinished() && !ecIns[OPTION_UPDATED] && !ecIns._scheduler.unfinished && !ecIns._pendingActions.length) {
  1078. ecIns.trigger('finished');
  1079. }
  1080. });
  1081. }
  1082. /**
  1083. * @param {Object} params
  1084. * @param {number} params.seriesIndex
  1085. * @param {Array|TypedArray} params.data
  1086. */
  1087. echartsProto.appendData = function (params) {
  1088. if (this._disposed) {
  1089. disposedWarning(this.id);
  1090. return;
  1091. }
  1092. var seriesIndex = params.seriesIndex;
  1093. var ecModel = this.getModel();
  1094. var seriesModel = ecModel.getSeriesByIndex(seriesIndex);
  1095. seriesModel.appendData(params); // Note: `appendData` does not support that update extent of coordinate
  1096. // system, util some scenario require that. In the expected usage of
  1097. // `appendData`, the initial extent of coordinate system should better
  1098. // be fixed by axis `min`/`max` setting or initial data, otherwise if
  1099. // the extent changed while `appendData`, the location of the painted
  1100. // graphic elements have to be changed, which make the usage of
  1101. // `appendData` meaningless.
  1102. this._scheduler.unfinished = true;
  1103. };
  1104. /**
  1105. * Register event
  1106. * @method
  1107. */
  1108. echartsProto.on = createRegisterEventWithLowercaseName('on', false);
  1109. echartsProto.off = createRegisterEventWithLowercaseName('off', false);
  1110. echartsProto.one = createRegisterEventWithLowercaseName('one', false);
  1111. /**
  1112. * Prepare view instances of charts and components
  1113. * @param {module:echarts/model/Global} ecModel
  1114. * @private
  1115. */
  1116. function prepareView(ecIns, type, ecModel, scheduler) {
  1117. var isComponent = type === 'component';
  1118. var viewList = isComponent ? ecIns._componentsViews : ecIns._chartsViews;
  1119. var viewMap = isComponent ? ecIns._componentsMap : ecIns._chartsMap;
  1120. var zr = ecIns._zr;
  1121. var api = ecIns._api;
  1122. for (var i = 0; i < viewList.length; i++) {
  1123. viewList[i].__alive = false;
  1124. }
  1125. isComponent ? ecModel.eachComponent(function (componentType, model) {
  1126. componentType !== 'series' && doPrepare(model);
  1127. }) : ecModel.eachSeries(doPrepare);
  1128. function doPrepare(model) {
  1129. // Consider: id same and type changed.
  1130. var viewId = '_ec_' + model.id + '_' + model.type;
  1131. var view = viewMap[viewId];
  1132. if (!view) {
  1133. var classType = parseClassType(model.type);
  1134. var Clazz = isComponent ? ComponentView.getClass(classType.main, classType.sub) : ChartView.getClass(classType.sub);
  1135. view = new Clazz();
  1136. view.init(ecModel, api);
  1137. viewMap[viewId] = view;
  1138. viewList.push(view);
  1139. zr.add(view.group);
  1140. }
  1141. model.__viewId = view.__id = viewId;
  1142. view.__alive = true;
  1143. view.__model = model;
  1144. view.group.__ecComponentInfo = {
  1145. mainType: model.mainType,
  1146. index: model.componentIndex
  1147. };
  1148. !isComponent && scheduler.prepareView(view, model, ecModel, api);
  1149. }
  1150. for (var i = 0; i < viewList.length;) {
  1151. var view = viewList[i];
  1152. if (!view.__alive) {
  1153. !isComponent && view.renderTask.dispose();
  1154. zr.remove(view.group);
  1155. view.dispose(ecModel, api);
  1156. viewList.splice(i, 1);
  1157. delete viewMap[view.__id];
  1158. view.__id = view.group.__ecComponentInfo = null;
  1159. } else {
  1160. i++;
  1161. }
  1162. }
  1163. } // /**
  1164. // * Encode visual infomation from data after data processing
  1165. // *
  1166. // * @param {module:echarts/model/Global} ecModel
  1167. // * @param {object} layout
  1168. // * @param {boolean} [layoutFilter] `true`: only layout,
  1169. // * `false`: only not layout,
  1170. // * `null`/`undefined`: all.
  1171. // * @param {string} taskBaseTag
  1172. // * @private
  1173. // */
  1174. // function startVisualEncoding(ecIns, ecModel, api, payload, layoutFilter) {
  1175. // each(visualFuncs, function (visual, index) {
  1176. // var isLayout = visual.isLayout;
  1177. // if (layoutFilter == null
  1178. // || (layoutFilter === false && !isLayout)
  1179. // || (layoutFilter === true && isLayout)
  1180. // ) {
  1181. // visual.func(ecModel, api, payload);
  1182. // }
  1183. // });
  1184. // }
  1185. function clearColorPalette(ecModel) {
  1186. ecModel.clearColorPalette();
  1187. ecModel.eachSeries(function (seriesModel) {
  1188. seriesModel.clearColorPalette();
  1189. });
  1190. }
  1191. function render(ecIns, ecModel, api, payload) {
  1192. renderComponents(ecIns, ecModel, api, payload);
  1193. each(ecIns._chartsViews, function (chart) {
  1194. chart.__alive = false;
  1195. });
  1196. renderSeries(ecIns, ecModel, api, payload); // Remove groups of unrendered charts
  1197. each(ecIns._chartsViews, function (chart) {
  1198. if (!chart.__alive) {
  1199. chart.remove(ecModel, api);
  1200. }
  1201. });
  1202. }
  1203. function renderComponents(ecIns, ecModel, api, payload, dirtyList) {
  1204. each(dirtyList || ecIns._componentsViews, function (componentView) {
  1205. var componentModel = componentView.__model;
  1206. componentView.render(componentModel, ecModel, api, payload);
  1207. updateZ(componentModel, componentView);
  1208. });
  1209. }
  1210. /**
  1211. * Render each chart and component
  1212. * @private
  1213. */
  1214. function renderSeries(ecIns, ecModel, api, payload, dirtyMap) {
  1215. // Render all charts
  1216. var scheduler = ecIns._scheduler;
  1217. var unfinished;
  1218. ecModel.eachSeries(function (seriesModel) {
  1219. var chartView = ecIns._chartsMap[seriesModel.__viewId];
  1220. chartView.__alive = true;
  1221. var renderTask = chartView.renderTask;
  1222. scheduler.updatePayload(renderTask, payload);
  1223. if (dirtyMap && dirtyMap.get(seriesModel.uid)) {
  1224. renderTask.dirty();
  1225. }
  1226. unfinished |= renderTask.perform(scheduler.getPerformArgs(renderTask));
  1227. chartView.group.silent = !!seriesModel.get('silent');
  1228. updateZ(seriesModel, chartView);
  1229. updateBlend(seriesModel, chartView);
  1230. });
  1231. scheduler.unfinished |= unfinished; // If use hover layer
  1232. updateHoverLayerStatus(ecIns, ecModel); // Add aria
  1233. aria(ecIns._zr.dom, ecModel);
  1234. }
  1235. function performPostUpdateFuncs(ecModel, api) {
  1236. each(postUpdateFuncs, function (func) {
  1237. func(ecModel, api);
  1238. });
  1239. }
  1240. var MOUSE_EVENT_NAMES = ['click', 'dblclick', 'mouseover', 'mouseout', 'mousemove', 'mousedown', 'mouseup', 'globalout', 'contextmenu'];
  1241. /**
  1242. * @private
  1243. */
  1244. echartsProto._initEvents = function () {
  1245. each(MOUSE_EVENT_NAMES, function (eveName) {
  1246. var handler = function (e) {
  1247. var ecModel = this.getModel();
  1248. var el = e.target;
  1249. var params;
  1250. var isGlobalOut = eveName === 'globalout'; // no e.target when 'globalout'.
  1251. if (isGlobalOut) {
  1252. params = {};
  1253. } else if (el && el.dataIndex != null) {
  1254. var dataModel = el.dataModel || ecModel.getSeriesByIndex(el.seriesIndex);
  1255. params = dataModel && dataModel.getDataParams(el.dataIndex, el.dataType, el) || {};
  1256. } // If element has custom eventData of components
  1257. else if (el && el.eventData) {
  1258. params = zrUtil.extend({}, el.eventData);
  1259. } // Contract: if params prepared in mouse event,
  1260. // these properties must be specified:
  1261. // {
  1262. // componentType: string (component main type)
  1263. // componentIndex: number
  1264. // }
  1265. // Otherwise event query can not work.
  1266. if (params) {
  1267. var componentType = params.componentType;
  1268. var componentIndex = params.componentIndex; // Special handling for historic reason: when trigger by
  1269. // markLine/markPoint/markArea, the componentType is
  1270. // 'markLine'/'markPoint'/'markArea', but we should better
  1271. // enable them to be queried by seriesIndex, since their
  1272. // option is set in each series.
  1273. if (componentType === 'markLine' || componentType === 'markPoint' || componentType === 'markArea') {
  1274. componentType = 'series';
  1275. componentIndex = params.seriesIndex;
  1276. }
  1277. var model = componentType && componentIndex != null && ecModel.getComponent(componentType, componentIndex);
  1278. var view = model && this[model.mainType === 'series' ? '_chartsMap' : '_componentsMap'][model.__viewId];
  1279. params.event = e;
  1280. params.type = eveName;
  1281. this._ecEventProcessor.eventInfo = {
  1282. targetEl: el,
  1283. packedEvent: params,
  1284. model: model,
  1285. view: view
  1286. };
  1287. this.trigger(eveName, params);
  1288. }
  1289. }; // Consider that some component (like tooltip, brush, ...)
  1290. // register zr event handler, but user event handler might
  1291. // do anything, such as call `setOption` or `dispatchAction`,
  1292. // which probably update any of the content and probably
  1293. // cause problem if it is called previous other inner handlers.
  1294. handler.zrEventfulCallAtLast = true;
  1295. this._zr.on(eveName, handler, this);
  1296. }, this);
  1297. each(eventActionMap, function (actionType, eventType) {
  1298. this._messageCenter.on(eventType, function (event) {
  1299. this.trigger(eventType, event);
  1300. }, this);
  1301. }, this);
  1302. };
  1303. /**
  1304. * @return {boolean}
  1305. */
  1306. echartsProto.isDisposed = function () {
  1307. return this._disposed;
  1308. };
  1309. /**
  1310. * Clear
  1311. */
  1312. echartsProto.clear = function () {
  1313. if (this._disposed) {
  1314. disposedWarning(this.id);
  1315. return;
  1316. }
  1317. this.setOption({
  1318. series: []
  1319. }, true);
  1320. };
  1321. /**
  1322. * Dispose instance
  1323. */
  1324. echartsProto.dispose = function () {
  1325. if (this._disposed) {
  1326. disposedWarning(this.id);
  1327. return;
  1328. }
  1329. this._disposed = true;
  1330. modelUtil.setAttribute(this.getDom(), DOM_ATTRIBUTE_KEY, '');
  1331. var api = this._api;
  1332. var ecModel = this._model;
  1333. each(this._componentsViews, function (component) {
  1334. component.dispose(ecModel, api);
  1335. });
  1336. each(this._chartsViews, function (chart) {
  1337. chart.dispose(ecModel, api);
  1338. }); // Dispose after all views disposed
  1339. this._zr.dispose();
  1340. delete instances[this.id];
  1341. };
  1342. zrUtil.mixin(ECharts, Eventful);
  1343. function disposedWarning(id) {}
  1344. function updateHoverLayerStatus(ecIns, ecModel) {
  1345. var zr = ecIns._zr;
  1346. var storage = zr.storage;
  1347. var elCount = 0;
  1348. storage.traverse(function (el) {
  1349. elCount++;
  1350. });
  1351. if (elCount > ecModel.get('hoverLayerThreshold') && !env.node) {
  1352. ecModel.eachSeries(function (seriesModel) {
  1353. if (seriesModel.preventUsingHoverLayer) {
  1354. return;
  1355. }
  1356. var chartView = ecIns._chartsMap[seriesModel.__viewId];
  1357. if (chartView.__alive) {
  1358. chartView.group.traverse(function (el) {
  1359. // Don't switch back.
  1360. el.useHoverLayer = true;
  1361. });
  1362. }
  1363. });
  1364. }
  1365. }
  1366. /**
  1367. * Update chart progressive and blend.
  1368. * @param {module:echarts/model/Series|module:echarts/model/Component} model
  1369. * @param {module:echarts/view/Component|module:echarts/view/Chart} view
  1370. */
  1371. function updateBlend(seriesModel, chartView) {
  1372. var blendMode = seriesModel.get('blendMode') || null;
  1373. chartView.group.traverse(function (el) {
  1374. // FIXME marker and other components
  1375. if (!el.isGroup) {
  1376. // Only set if blendMode is changed. In case element is incremental and don't wan't to rerender.
  1377. if (el.style.blend !== blendMode) {
  1378. el.setStyle('blend', blendMode);
  1379. }
  1380. }
  1381. if (el.eachPendingDisplayable) {
  1382. el.eachPendingDisplayable(function (displayable) {
  1383. displayable.setStyle('blend', blendMode);
  1384. });
  1385. }
  1386. });
  1387. }
  1388. /**
  1389. * @param {module:echarts/model/Series|module:echarts/model/Component} model
  1390. * @param {module:echarts/view/Component|module:echarts/view/Chart} view
  1391. */
  1392. function updateZ(model, view) {
  1393. var z = model.get('z');
  1394. var zlevel = model.get('zlevel'); // Set z and zlevel
  1395. view.group.traverse(function (el) {
  1396. if (el.type !== 'group') {
  1397. z != null && (el.z = z);
  1398. zlevel != null && (el.zlevel = zlevel);
  1399. }
  1400. });
  1401. }
  1402. function createExtensionAPI(ecInstance) {
  1403. var coordSysMgr = ecInstance._coordSysMgr;
  1404. return zrUtil.extend(new ExtensionAPI(ecInstance), {
  1405. // Inject methods
  1406. getCoordinateSystems: zrUtil.bind(coordSysMgr.getCoordinateSystems, coordSysMgr),
  1407. getComponentByElement: function (el) {
  1408. while (el) {
  1409. var modelInfo = el.__ecComponentInfo;
  1410. if (modelInfo != null) {
  1411. return ecInstance._model.getComponent(modelInfo.mainType, modelInfo.index);
  1412. }
  1413. el = el.parent;
  1414. }
  1415. }
  1416. });
  1417. }
  1418. /**
  1419. * @class
  1420. * Usage of query:
  1421. * `chart.on('click', query, handler);`
  1422. * The `query` can be:
  1423. * + The component type query string, only `mainType` or `mainType.subType`,
  1424. * like: 'xAxis', 'series', 'xAxis.category' or 'series.line'.
  1425. * + The component query object, like:
  1426. * `{seriesIndex: 2}`, `{seriesName: 'xx'}`, `{seriesId: 'some'}`,
  1427. * `{xAxisIndex: 2}`, `{xAxisName: 'xx'}`, `{xAxisId: 'some'}`.
  1428. * + The data query object, like:
  1429. * `{dataIndex: 123}`, `{dataType: 'link'}`, `{name: 'some'}`.
  1430. * + The other query object (cmponent customized query), like:
  1431. * `{element: 'some'}` (only available in custom series).
  1432. *
  1433. * Caveat: If a prop in the `query` object is `null/undefined`, it is the
  1434. * same as there is no such prop in the `query` object.
  1435. */
  1436. function EventProcessor() {
  1437. // These info required: targetEl, packedEvent, model, view
  1438. this.eventInfo;
  1439. }
  1440. EventProcessor.prototype = {
  1441. constructor: EventProcessor,
  1442. normalizeQuery: function (query) {
  1443. var cptQuery = {};
  1444. var dataQuery = {};
  1445. var otherQuery = {}; // `query` is `mainType` or `mainType.subType` of component.
  1446. if (zrUtil.isString(query)) {
  1447. var condCptType = parseClassType(query); // `.main` and `.sub` may be ''.
  1448. cptQuery.mainType = condCptType.main || null;
  1449. cptQuery.subType = condCptType.sub || null;
  1450. } // `query` is an object, convert to {mainType, index, name, id}.
  1451. else {
  1452. // `xxxIndex`, `xxxName`, `xxxId`, `name`, `dataIndex`, `dataType` is reserved,
  1453. // can not be used in `compomentModel.filterForExposedEvent`.
  1454. var suffixes = ['Index', 'Name', 'Id'];
  1455. var dataKeys = {
  1456. name: 1,
  1457. dataIndex: 1,
  1458. dataType: 1
  1459. };
  1460. zrUtil.each(query, function (val, key) {
  1461. var reserved = false;
  1462. for (var i = 0; i < suffixes.length; i++) {
  1463. var propSuffix = suffixes[i];
  1464. var suffixPos = key.lastIndexOf(propSuffix);
  1465. if (suffixPos > 0 && suffixPos === key.length - propSuffix.length) {
  1466. var mainType = key.slice(0, suffixPos); // Consider `dataIndex`.
  1467. if (mainType !== 'data') {
  1468. cptQuery.mainType = mainType;
  1469. cptQuery[propSuffix.toLowerCase()] = val;
  1470. reserved = true;
  1471. }
  1472. }
  1473. }
  1474. if (dataKeys.hasOwnProperty(key)) {
  1475. dataQuery[key] = val;
  1476. reserved = true;
  1477. }
  1478. if (!reserved) {
  1479. otherQuery[key] = val;
  1480. }
  1481. });
  1482. }
  1483. return {
  1484. cptQuery: cptQuery,
  1485. dataQuery: dataQuery,
  1486. otherQuery: otherQuery
  1487. };
  1488. },
  1489. filter: function (eventType, query, args) {
  1490. // They should be assigned before each trigger call.
  1491. var eventInfo = this.eventInfo;
  1492. if (!eventInfo) {
  1493. return true;
  1494. }
  1495. var targetEl = eventInfo.targetEl;
  1496. var packedEvent = eventInfo.packedEvent;
  1497. var model = eventInfo.model;
  1498. var view = eventInfo.view; // For event like 'globalout'.
  1499. if (!model || !view) {
  1500. return true;
  1501. }
  1502. var cptQuery = query.cptQuery;
  1503. var dataQuery = query.dataQuery;
  1504. 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));
  1505. function check(query, host, prop, propOnHost) {
  1506. return query[prop] == null || host[propOnHost || prop] === query[prop];
  1507. }
  1508. },
  1509. afterTrigger: function () {
  1510. // Make sure the eventInfo wont be used in next trigger.
  1511. this.eventInfo = null;
  1512. }
  1513. };
  1514. /**
  1515. * @type {Object} key: actionType.
  1516. * @inner
  1517. */
  1518. var actions = {};
  1519. /**
  1520. * Map eventType to actionType
  1521. * @type {Object}
  1522. */
  1523. var eventActionMap = {};
  1524. /**
  1525. * Data processor functions of each stage
  1526. * @type {Array.<Object.<string, Function>>}
  1527. * @inner
  1528. */
  1529. var dataProcessorFuncs = [];
  1530. /**
  1531. * @type {Array.<Function>}
  1532. * @inner
  1533. */
  1534. var optionPreprocessorFuncs = [];
  1535. /**
  1536. * @type {Array.<Function>}
  1537. * @inner
  1538. */
  1539. var postUpdateFuncs = [];
  1540. /**
  1541. * Visual encoding functions of each stage
  1542. * @type {Array.<Object.<string, Function>>}
  1543. */
  1544. var visualFuncs = [];
  1545. /**
  1546. * Theme storage
  1547. * @type {Object.<key, Object>}
  1548. */
  1549. var themeStorage = {};
  1550. /**
  1551. * Loading effects
  1552. */
  1553. var loadingEffects = {};
  1554. var instances = {};
  1555. var connectedGroups = {};
  1556. var idBase = new Date() - 0;
  1557. var groupIdBase = new Date() - 0;
  1558. var DOM_ATTRIBUTE_KEY = '_echarts_instance_';
  1559. function enableConnect(chart) {
  1560. var STATUS_PENDING = 0;
  1561. var STATUS_UPDATING = 1;
  1562. var STATUS_UPDATED = 2;
  1563. var STATUS_KEY = '__connectUpdateStatus';
  1564. function updateConnectedChartsStatus(charts, status) {
  1565. for (var i = 0; i < charts.length; i++) {
  1566. var otherChart = charts[i];
  1567. otherChart[STATUS_KEY] = status;
  1568. }
  1569. }
  1570. each(eventActionMap, function (actionType, eventType) {
  1571. chart._messageCenter.on(eventType, function (event) {
  1572. if (connectedGroups[chart.group] && chart[STATUS_KEY] !== STATUS_PENDING) {
  1573. if (event && event.escapeConnect) {
  1574. return;
  1575. }
  1576. var action = chart.makeActionFromEvent(event);
  1577. var otherCharts = [];
  1578. each(instances, function (otherChart) {
  1579. if (otherChart !== chart && otherChart.group === chart.group) {
  1580. otherCharts.push(otherChart);
  1581. }
  1582. });
  1583. updateConnectedChartsStatus(otherCharts, STATUS_PENDING);
  1584. each(otherCharts, function (otherChart) {
  1585. if (otherChart[STATUS_KEY] !== STATUS_UPDATING) {
  1586. otherChart.dispatchAction(action);
  1587. }
  1588. });
  1589. updateConnectedChartsStatus(otherCharts, STATUS_UPDATED);
  1590. }
  1591. });
  1592. });
  1593. }
  1594. /**
  1595. * @param {HTMLElement} dom
  1596. * @param {Object} [theme]
  1597. * @param {Object} opts
  1598. * @param {number} [opts.devicePixelRatio] Use window.devicePixelRatio by default
  1599. * @param {string} [opts.renderer] Can choose 'canvas' or 'svg' to render the chart.
  1600. * @param {number} [opts.width] Use clientWidth of the input `dom` by default.
  1601. * Can be 'auto' (the same as null/undefined)
  1602. * @param {number} [opts.height] Use clientHeight of the input `dom` by default.
  1603. * Can be 'auto' (the same as null/undefined)
  1604. */
  1605. function init(dom, theme, opts) {
  1606. var existInstance = getInstanceByDom(dom);
  1607. if (existInstance) {
  1608. return existInstance;
  1609. }
  1610. var chart = new ECharts(dom, theme, opts);
  1611. chart.id = 'ec_' + idBase++;
  1612. instances[chart.id] = chart;
  1613. modelUtil.setAttribute(dom, DOM_ATTRIBUTE_KEY, chart.id);
  1614. enableConnect(chart);
  1615. return chart;
  1616. }
  1617. /**
  1618. * @return {string|Array.<module:echarts~ECharts>} groupId
  1619. */
  1620. function connect(groupId) {
  1621. // Is array of charts
  1622. if (zrUtil.isArray(groupId)) {
  1623. var charts = groupId;
  1624. groupId = null; // If any chart has group
  1625. each(charts, function (chart) {
  1626. if (chart.group != null) {
  1627. groupId = chart.group;
  1628. }
  1629. });
  1630. groupId = groupId || 'g_' + groupIdBase++;
  1631. each(charts, function (chart) {
  1632. chart.group = groupId;
  1633. });
  1634. }
  1635. connectedGroups[groupId] = true;
  1636. return groupId;
  1637. }
  1638. /**
  1639. * @DEPRECATED
  1640. * @return {string} groupId
  1641. */
  1642. function disConnect(groupId) {
  1643. connectedGroups[groupId] = false;
  1644. }
  1645. /**
  1646. * @return {string} groupId
  1647. */
  1648. var disconnect = disConnect;
  1649. /**
  1650. * Dispose a chart instance
  1651. * @param {module:echarts~ECharts|HTMLDomElement|string} chart
  1652. */
  1653. function dispose(chart) {
  1654. if (typeof chart === 'string') {
  1655. chart = instances[chart];
  1656. } else if (!(chart instanceof ECharts)) {
  1657. // Try to treat as dom
  1658. chart = getInstanceByDom(chart);
  1659. }
  1660. if (chart instanceof ECharts && !chart.isDisposed()) {
  1661. chart.dispose();
  1662. }
  1663. }
  1664. /**
  1665. * @param {HTMLElement} dom
  1666. * @return {echarts~ECharts}
  1667. */
  1668. function getInstanceByDom(dom) {
  1669. return instances[modelUtil.getAttribute(dom, DOM_ATTRIBUTE_KEY)];
  1670. }
  1671. /**
  1672. * @param {string} key
  1673. * @return {echarts~ECharts}
  1674. */
  1675. function getInstanceById(key) {
  1676. return instances[key];
  1677. }
  1678. /**
  1679. * Register theme
  1680. */
  1681. function registerTheme(name, theme) {
  1682. themeStorage[name] = theme;
  1683. }
  1684. /**
  1685. * Register option preprocessor
  1686. * @param {Function} preprocessorFunc
  1687. */
  1688. function registerPreprocessor(preprocessorFunc) {
  1689. optionPreprocessorFuncs.push(preprocessorFunc);
  1690. }
  1691. /**
  1692. * @param {number} [priority=1000]
  1693. * @param {Object|Function} processor
  1694. */
  1695. function registerProcessor(priority, processor) {
  1696. normalizeRegister(dataProcessorFuncs, priority, processor, PRIORITY_PROCESSOR_FILTER);
  1697. }
  1698. /**
  1699. * Register postUpdater
  1700. * @param {Function} postUpdateFunc
  1701. */
  1702. function registerPostUpdate(postUpdateFunc) {
  1703. postUpdateFuncs.push(postUpdateFunc);
  1704. }
  1705. /**
  1706. * Usage:
  1707. * registerAction('someAction', 'someEvent', function () { ... });
  1708. * registerAction('someAction', function () { ... });
  1709. * registerAction(
  1710. * {type: 'someAction', event: 'someEvent', update: 'updateView'},
  1711. * function () { ... }
  1712. * );
  1713. *
  1714. * @param {(string|Object)} actionInfo
  1715. * @param {string} actionInfo.type
  1716. * @param {string} [actionInfo.event]
  1717. * @param {string} [actionInfo.update]
  1718. * @param {string} [eventName]
  1719. * @param {Function} action
  1720. */
  1721. function registerAction(actionInfo, eventName, action) {
  1722. if (typeof eventName === 'function') {
  1723. action = eventName;
  1724. eventName = '';
  1725. }
  1726. var actionType = isObject(actionInfo) ? actionInfo.type : [actionInfo, actionInfo = {
  1727. event: eventName
  1728. }][0]; // Event name is all lowercase
  1729. actionInfo.event = (actionInfo.event || actionType).toLowerCase();
  1730. eventName = actionInfo.event; // Validate action type and event name.
  1731. assert(ACTION_REG.test(actionType) && ACTION_REG.test(eventName));
  1732. if (!actions[actionType]) {
  1733. actions[actionType] = {
  1734. action: action,
  1735. actionInfo: actionInfo
  1736. };
  1737. }
  1738. eventActionMap[eventName] = actionType;
  1739. }
  1740. /**
  1741. * @param {string} type
  1742. * @param {*} CoordinateSystem
  1743. */
  1744. function registerCoordinateSystem(type, CoordinateSystem) {
  1745. CoordinateSystemManager.register(type, CoordinateSystem);
  1746. }
  1747. /**
  1748. * Get dimensions of specified coordinate system.
  1749. * @param {string} type
  1750. * @return {Array.<string|Object>}
  1751. */
  1752. function getCoordinateSystemDimensions(type) {
  1753. var coordSysCreator = CoordinateSystemManager.get(type);
  1754. if (coordSysCreator) {
  1755. return coordSysCreator.getDimensionsInfo ? coordSysCreator.getDimensionsInfo() : coordSysCreator.dimensions.slice();
  1756. }
  1757. }
  1758. /**
  1759. * Layout is a special stage of visual encoding
  1760. * Most visual encoding like color are common for different chart
  1761. * But each chart has it's own layout algorithm
  1762. *
  1763. * @param {number} [priority=1000]
  1764. * @param {Function} layoutTask
  1765. */
  1766. function registerLayout(priority, layoutTask) {
  1767. normalizeRegister(visualFuncs, priority, layoutTask, PRIORITY_VISUAL_LAYOUT, 'layout');
  1768. }
  1769. /**
  1770. * @param {number} [priority=3000]
  1771. * @param {module:echarts/stream/Task} visualTask
  1772. */
  1773. function registerVisual(priority, visualTask) {
  1774. normalizeRegister(visualFuncs, priority, visualTask, PRIORITY_VISUAL_CHART, 'visual');
  1775. }
  1776. /**
  1777. * @param {Object|Function} fn: {seriesType, createOnAllSeries, performRawSeries, reset}
  1778. */
  1779. function normalizeRegister(targetList, priority, fn, defaultPriority, visualType) {
  1780. if (isFunction(priority) || isObject(priority)) {
  1781. fn = priority;
  1782. priority = defaultPriority;
  1783. }
  1784. var stageHandler = Scheduler.wrapStageHandler(fn, visualType);
  1785. stageHandler.__prio = priority;
  1786. stageHandler.__raw = fn;
  1787. targetList.push(stageHandler);
  1788. return stageHandler;
  1789. }
  1790. /**
  1791. * @param {string} name
  1792. */
  1793. function registerLoading(name, loadingFx) {
  1794. loadingEffects[name] = loadingFx;
  1795. }
  1796. /**
  1797. * @param {Object} opts
  1798. * @param {string} [superClass]
  1799. */
  1800. function extendComponentModel(opts
  1801. /*, superClass*/
  1802. ) {
  1803. // var Clazz = ComponentModel;
  1804. // if (superClass) {
  1805. // var classType = parseClassType(superClass);
  1806. // Clazz = ComponentModel.getClass(classType.main, classType.sub, true);
  1807. // }
  1808. return ComponentModel.extend(opts);
  1809. }
  1810. /**
  1811. * @param {Object} opts
  1812. * @param {string} [superClass]
  1813. */
  1814. function extendComponentView(opts
  1815. /*, superClass*/
  1816. ) {
  1817. // var Clazz = ComponentView;
  1818. // if (superClass) {
  1819. // var classType = parseClassType(superClass);
  1820. // Clazz = ComponentView.getClass(classType.main, classType.sub, true);
  1821. // }
  1822. return ComponentView.extend(opts);
  1823. }
  1824. /**
  1825. * @param {Object} opts
  1826. * @param {string} [superClass]
  1827. */
  1828. function extendSeriesModel(opts
  1829. /*, superClass*/
  1830. ) {
  1831. // var Clazz = SeriesModel;
  1832. // if (superClass) {
  1833. // superClass = 'series.' + superClass.replace('series.', '');
  1834. // var classType = parseClassType(superClass);
  1835. // Clazz = ComponentModel.getClass(classType.main, classType.sub, true);
  1836. // }
  1837. return SeriesModel.extend(opts);
  1838. }
  1839. /**
  1840. * @param {Object} opts
  1841. * @param {string} [superClass]
  1842. */
  1843. function extendChartView(opts
  1844. /*, superClass*/
  1845. ) {
  1846. // var Clazz = ChartView;
  1847. // if (superClass) {
  1848. // superClass = superClass.replace('series.', '');
  1849. // var classType = parseClassType(superClass);
  1850. // Clazz = ChartView.getClass(classType.main, true);
  1851. // }
  1852. return ChartView.extend(opts);
  1853. }
  1854. /**
  1855. * ZRender need a canvas context to do measureText.
  1856. * But in node environment canvas may be created by node-canvas.
  1857. * So we need to specify how to create a canvas instead of using document.createElement('canvas')
  1858. *
  1859. * Be careful of using it in the browser.
  1860. *
  1861. * @param {Function} creator
  1862. * @example
  1863. * var Canvas = require('canvas');
  1864. * var echarts = require('echarts');
  1865. * echarts.setCanvasCreator(function () {
  1866. * // Small size is enough.
  1867. * return new Canvas(32, 32);
  1868. * });
  1869. */
  1870. function setCanvasCreator(creator) {
  1871. zrUtil.$override('createCanvas', creator);
  1872. }
  1873. /**
  1874. * @param {string} mapName
  1875. * @param {Array.<Object>|Object|string} geoJson
  1876. * @param {Object} [specialAreas]
  1877. *
  1878. * @example GeoJSON
  1879. * $.get('USA.json', function (geoJson) {
  1880. * echarts.registerMap('USA', geoJson);
  1881. * // Or
  1882. * echarts.registerMap('USA', {
  1883. * geoJson: geoJson,
  1884. * specialAreas: {}
  1885. * })
  1886. * });
  1887. *
  1888. * $.get('airport.svg', function (svg) {
  1889. * echarts.registerMap('airport', {
  1890. * svg: svg
  1891. * }
  1892. * });
  1893. *
  1894. * echarts.registerMap('eu', [
  1895. * {svg: eu-topographic.svg},
  1896. * {geoJSON: eu.json}
  1897. * ])
  1898. */
  1899. function registerMap(mapName, geoJson, specialAreas) {
  1900. mapDataStorage.registerMap(mapName, geoJson, specialAreas);
  1901. }
  1902. /**
  1903. * @param {string} mapName
  1904. * @return {Object}
  1905. */
  1906. function getMap(mapName) {
  1907. // For backward compatibility, only return the first one.
  1908. var records = mapDataStorage.retrieveMap(mapName);
  1909. return records && records[0] && {
  1910. geoJson: records[0].geoJSON,
  1911. specialAreas: records[0].specialAreas
  1912. };
  1913. }
  1914. registerVisual(PRIORITY_VISUAL_GLOBAL, seriesColor);
  1915. registerPreprocessor(backwardCompat);
  1916. registerProcessor(PRIORITY_PROCESSOR_DATASTACK, dataStack);
  1917. registerLoading('default', loadingDefault); // Default actions
  1918. registerAction({
  1919. type: 'highlight',
  1920. event: 'highlight',
  1921. update: 'highlight'
  1922. }, zrUtil.noop);
  1923. registerAction({
  1924. type: 'downplay',
  1925. event: 'downplay',
  1926. update: 'downplay'
  1927. }, zrUtil.noop); // Default theme
  1928. registerTheme('light', lightTheme);
  1929. registerTheme('dark', darkTheme); // For backward compatibility, where the namespace `dataTool` will
  1930. // be mounted on `echarts` is the extension `dataTool` is imported.
  1931. var dataTool = {};
  1932. exports.version = version;
  1933. exports.dependencies = dependencies;
  1934. exports.PRIORITY = PRIORITY;
  1935. exports.init = init;
  1936. exports.connect = connect;
  1937. exports.disConnect = disConnect;
  1938. exports.disconnect = disconnect;
  1939. exports.dispose = dispose;
  1940. exports.getInstanceByDom = getInstanceByDom;
  1941. exports.getInstanceById = getInstanceById;
  1942. exports.registerTheme = registerTheme;
  1943. exports.registerPreprocessor = registerPreprocessor;
  1944. exports.registerProcessor = registerProcessor;
  1945. exports.registerPostUpdate = registerPostUpdate;
  1946. exports.registerAction = registerAction;
  1947. exports.registerCoordinateSystem = registerCoordinateSystem;
  1948. exports.getCoordinateSystemDimensions = getCoordinateSystemDimensions;
  1949. exports.registerLayout = registerLayout;
  1950. exports.registerVisual = registerVisual;
  1951. exports.registerLoading = registerLoading;
  1952. exports.extendComponentModel = extendComponentModel;
  1953. exports.extendComponentView = extendComponentView;
  1954. exports.extendSeriesModel = extendSeriesModel;
  1955. exports.extendChartView = extendChartView;
  1956. exports.setCanvasCreator = setCanvasCreator;
  1957. exports.registerMap = registerMap;
  1958. exports.getMap = getMap;
  1959. exports.dataTool = dataTool;
  1960. var ___ec_export = require("./export");
  1961. (function () {
  1962. for (var key in ___ec_export) {
  1963. if (___ec_export.hasOwnProperty(key)) {
  1964. exports[key] = ___ec_export[key];
  1965. }
  1966. }
  1967. })();