Global.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { __extends } from "tslib";
  41. /**
  42. * Caution: If the mechanism should be changed some day, these cases
  43. * should be considered:
  44. *
  45. * (1) In `merge option` mode, if using the same option to call `setOption`
  46. * many times, the result should be the same (try our best to ensure that).
  47. * (2) In `merge option` mode, if a component has no id/name specified, it
  48. * will be merged by index, and the result sequence of the components is
  49. * consistent to the original sequence.
  50. * (3) In `replaceMerge` mode, keep the result sequence of the components is
  51. * consistent to the original sequence, even though there might result in "hole".
  52. * (4) `reset` feature (in toolbox). Find detailed info in comments about
  53. * `mergeOption` in module:echarts/model/OptionManager.
  54. */
  55. import { each, filter, isArray, isObject, isString, createHashMap, assert, clone, merge, extend, mixin, isFunction } from 'zrender/lib/core/util.js';
  56. import * as modelUtil from '../util/model.js';
  57. import Model from './Model.js';
  58. import ComponentModel from './Component.js';
  59. import globalDefault from './globalDefault.js';
  60. import { resetSourceDefaulter } from '../data/helper/sourceHelper.js';
  61. import { concatInternalOptions } from './internalComponentCreator.js';
  62. import { PaletteMixin } from './mixin/palette.js';
  63. import { error, warn } from '../util/log.js';
  64. // -----------------------
  65. // Internal method names:
  66. // -----------------------
  67. var reCreateSeriesIndices;
  68. var assertSeriesInitialized;
  69. var initBase;
  70. var OPTION_INNER_KEY = '\0_ec_inner';
  71. var OPTION_INNER_VALUE = 1;
  72. var BUITIN_COMPONENTS_MAP = {
  73. grid: 'GridComponent',
  74. polar: 'PolarComponent',
  75. geo: 'GeoComponent',
  76. singleAxis: 'SingleAxisComponent',
  77. parallel: 'ParallelComponent',
  78. calendar: 'CalendarComponent',
  79. graphic: 'GraphicComponent',
  80. toolbox: 'ToolboxComponent',
  81. tooltip: 'TooltipComponent',
  82. axisPointer: 'AxisPointerComponent',
  83. brush: 'BrushComponent',
  84. title: 'TitleComponent',
  85. timeline: 'TimelineComponent',
  86. markPoint: 'MarkPointComponent',
  87. markLine: 'MarkLineComponent',
  88. markArea: 'MarkAreaComponent',
  89. legend: 'LegendComponent',
  90. dataZoom: 'DataZoomComponent',
  91. visualMap: 'VisualMapComponent',
  92. // aria: 'AriaComponent',
  93. // dataset: 'DatasetComponent',
  94. // Dependencies
  95. xAxis: 'GridComponent',
  96. yAxis: 'GridComponent',
  97. angleAxis: 'PolarComponent',
  98. radiusAxis: 'PolarComponent'
  99. };
  100. var BUILTIN_CHARTS_MAP = {
  101. line: 'LineChart',
  102. bar: 'BarChart',
  103. pie: 'PieChart',
  104. scatter: 'ScatterChart',
  105. radar: 'RadarChart',
  106. map: 'MapChart',
  107. tree: 'TreeChart',
  108. treemap: 'TreemapChart',
  109. graph: 'GraphChart',
  110. gauge: 'GaugeChart',
  111. funnel: 'FunnelChart',
  112. parallel: 'ParallelChart',
  113. sankey: 'SankeyChart',
  114. boxplot: 'BoxplotChart',
  115. candlestick: 'CandlestickChart',
  116. effectScatter: 'EffectScatterChart',
  117. lines: 'LinesChart',
  118. heatmap: 'HeatmapChart',
  119. pictorialBar: 'PictorialBarChart',
  120. themeRiver: 'ThemeRiverChart',
  121. sunburst: 'SunburstChart',
  122. custom: 'CustomChart'
  123. };
  124. var componetsMissingLogPrinted = {};
  125. function checkMissingComponents(option) {
  126. each(option, function (componentOption, mainType) {
  127. if (!ComponentModel.hasClass(mainType)) {
  128. var componentImportName = BUITIN_COMPONENTS_MAP[mainType];
  129. if (componentImportName && !componetsMissingLogPrinted[componentImportName]) {
  130. error("Component " + mainType + " is used but not imported.\nimport { " + componentImportName + " } from 'echarts/components';\necharts.use([" + componentImportName + "]);");
  131. componetsMissingLogPrinted[componentImportName] = true;
  132. }
  133. }
  134. });
  135. }
  136. var GlobalModel = /** @class */function (_super) {
  137. __extends(GlobalModel, _super);
  138. function GlobalModel() {
  139. return _super !== null && _super.apply(this, arguments) || this;
  140. }
  141. GlobalModel.prototype.init = function (option, parentModel, ecModel, theme, locale, optionManager) {
  142. theme = theme || {};
  143. this.option = null; // Mark as not initialized.
  144. this._theme = new Model(theme);
  145. this._locale = new Model(locale);
  146. this._optionManager = optionManager;
  147. };
  148. GlobalModel.prototype.setOption = function (option, opts, optionPreprocessorFuncs) {
  149. if (process.env.NODE_ENV !== 'production') {
  150. assert(option != null, 'option is null/undefined');
  151. assert(option[OPTION_INNER_KEY] !== OPTION_INNER_VALUE, 'please use chart.getOption()');
  152. }
  153. var innerOpt = normalizeSetOptionInput(opts);
  154. this._optionManager.setOption(option, optionPreprocessorFuncs, innerOpt);
  155. this._resetOption(null, innerOpt);
  156. };
  157. /**
  158. * @param type null/undefined: reset all.
  159. * 'recreate': force recreate all.
  160. * 'timeline': only reset timeline option
  161. * 'media': only reset media query option
  162. * @return Whether option changed.
  163. */
  164. GlobalModel.prototype.resetOption = function (type, opt) {
  165. return this._resetOption(type, normalizeSetOptionInput(opt));
  166. };
  167. GlobalModel.prototype._resetOption = function (type, opt) {
  168. var optionChanged = false;
  169. var optionManager = this._optionManager;
  170. if (!type || type === 'recreate') {
  171. var baseOption = optionManager.mountOption(type === 'recreate');
  172. if (process.env.NODE_ENV !== 'production') {
  173. checkMissingComponents(baseOption);
  174. }
  175. if (!this.option || type === 'recreate') {
  176. initBase(this, baseOption);
  177. } else {
  178. this.restoreData();
  179. this._mergeOption(baseOption, opt);
  180. }
  181. optionChanged = true;
  182. }
  183. if (type === 'timeline' || type === 'media') {
  184. this.restoreData();
  185. }
  186. // By design, if `setOption(option2)` at the second time, and `option2` is a `ECUnitOption`,
  187. // it should better not have the same props with `MediaUnit['option']`.
  188. // Because either `option2` or `MediaUnit['option']` will be always merged to "current option"
  189. // rather than original "baseOption". If they both override a prop, the result might be
  190. // unexpected when media state changed after `setOption` called.
  191. // If we really need to modify a props in each `MediaUnit['option']`, use the full version
  192. // (`{baseOption, media}`) in `setOption`.
  193. // For `timeline`, the case is the same.
  194. if (!type || type === 'recreate' || type === 'timeline') {
  195. var timelineOption = optionManager.getTimelineOption(this);
  196. if (timelineOption) {
  197. optionChanged = true;
  198. this._mergeOption(timelineOption, opt);
  199. }
  200. }
  201. if (!type || type === 'recreate' || type === 'media') {
  202. var mediaOptions = optionManager.getMediaOption(this);
  203. if (mediaOptions.length) {
  204. each(mediaOptions, function (mediaOption) {
  205. optionChanged = true;
  206. this._mergeOption(mediaOption, opt);
  207. }, this);
  208. }
  209. }
  210. return optionChanged;
  211. };
  212. GlobalModel.prototype.mergeOption = function (option) {
  213. this._mergeOption(option, null);
  214. };
  215. GlobalModel.prototype._mergeOption = function (newOption, opt) {
  216. var option = this.option;
  217. var componentsMap = this._componentsMap;
  218. var componentsCount = this._componentsCount;
  219. var newCmptTypes = [];
  220. var newCmptTypeMap = createHashMap();
  221. var replaceMergeMainTypeMap = opt && opt.replaceMergeMainTypeMap;
  222. resetSourceDefaulter(this);
  223. // If no component class, merge directly.
  224. // For example: color, animaiton options, etc.
  225. each(newOption, function (componentOption, mainType) {
  226. if (componentOption == null) {
  227. return;
  228. }
  229. if (!ComponentModel.hasClass(mainType)) {
  230. // globalSettingTask.dirty();
  231. option[mainType] = option[mainType] == null ? clone(componentOption) : merge(option[mainType], componentOption, true);
  232. } else if (mainType) {
  233. newCmptTypes.push(mainType);
  234. newCmptTypeMap.set(mainType, true);
  235. }
  236. });
  237. if (replaceMergeMainTypeMap) {
  238. // If there is a mainType `xxx` in `replaceMerge` but not declared in option,
  239. // we trade it as it is declared in option as `{xxx: []}`. Because:
  240. // (1) for normal merge, `{xxx: null/undefined}` are the same meaning as `{xxx: []}`.
  241. // (2) some preprocessor may convert some of `{xxx: null/undefined}` to `{xxx: []}`.
  242. replaceMergeMainTypeMap.each(function (val, mainTypeInReplaceMerge) {
  243. if (ComponentModel.hasClass(mainTypeInReplaceMerge) && !newCmptTypeMap.get(mainTypeInReplaceMerge)) {
  244. newCmptTypes.push(mainTypeInReplaceMerge);
  245. newCmptTypeMap.set(mainTypeInReplaceMerge, true);
  246. }
  247. });
  248. }
  249. ComponentModel.topologicalTravel(newCmptTypes, ComponentModel.getAllClassMainTypes(), visitComponent, this);
  250. function visitComponent(mainType) {
  251. var newCmptOptionList = concatInternalOptions(this, mainType, modelUtil.normalizeToArray(newOption[mainType]));
  252. var oldCmptList = componentsMap.get(mainType);
  253. var mergeMode =
  254. // `!oldCmptList` means init. See the comment in `mappingToExists`
  255. !oldCmptList ? 'replaceAll' : replaceMergeMainTypeMap && replaceMergeMainTypeMap.get(mainType) ? 'replaceMerge' : 'normalMerge';
  256. var mappingResult = modelUtil.mappingToExists(oldCmptList, newCmptOptionList, mergeMode);
  257. // Set mainType and complete subType.
  258. modelUtil.setComponentTypeToKeyInfo(mappingResult, mainType, ComponentModel);
  259. // Empty it before the travel, in order to prevent `this._componentsMap`
  260. // from being used in the `init`/`mergeOption`/`optionUpdated` of some
  261. // components, which is probably incorrect logic.
  262. option[mainType] = null;
  263. componentsMap.set(mainType, null);
  264. componentsCount.set(mainType, 0);
  265. var optionsByMainType = [];
  266. var cmptsByMainType = [];
  267. var cmptsCountByMainType = 0;
  268. var tooltipExists;
  269. var tooltipWarningLogged;
  270. each(mappingResult, function (resultItem, index) {
  271. var componentModel = resultItem.existing;
  272. var newCmptOption = resultItem.newOption;
  273. if (!newCmptOption) {
  274. if (componentModel) {
  275. // Consider where is no new option and should be merged using {},
  276. // see removeEdgeAndAdd in topologicalTravel and
  277. // ComponentModel.getAllClassMainTypes.
  278. componentModel.mergeOption({}, this);
  279. componentModel.optionUpdated({}, false);
  280. }
  281. // If no both `resultItem.exist` and `resultItem.option`,
  282. // either it is in `replaceMerge` and not matched by any id,
  283. // or it has been removed in previous `replaceMerge` and left a "hole" in this component index.
  284. } else {
  285. var isSeriesType = mainType === 'series';
  286. var ComponentModelClass = ComponentModel.getClass(mainType, resultItem.keyInfo.subType, !isSeriesType // Give a more detailed warn later if series don't exists
  287. );
  288. if (!ComponentModelClass) {
  289. if (process.env.NODE_ENV !== 'production') {
  290. var subType = resultItem.keyInfo.subType;
  291. var seriesImportName = BUILTIN_CHARTS_MAP[subType];
  292. if (!componetsMissingLogPrinted[subType]) {
  293. componetsMissingLogPrinted[subType] = true;
  294. if (seriesImportName) {
  295. error("Series " + subType + " is used but not imported.\nimport { " + seriesImportName + " } from 'echarts/charts';\necharts.use([" + seriesImportName + "]);");
  296. } else {
  297. error("Unknown series " + subType);
  298. }
  299. }
  300. }
  301. return;
  302. }
  303. // TODO Before multiple tooltips get supported, we do this check to avoid unexpected exception.
  304. if (mainType === 'tooltip') {
  305. if (tooltipExists) {
  306. if (process.env.NODE_ENV !== 'production') {
  307. if (!tooltipWarningLogged) {
  308. warn('Currently only one tooltip component is allowed.');
  309. tooltipWarningLogged = true;
  310. }
  311. }
  312. return;
  313. }
  314. tooltipExists = true;
  315. }
  316. if (componentModel && componentModel.constructor === ComponentModelClass) {
  317. componentModel.name = resultItem.keyInfo.name;
  318. // componentModel.settingTask && componentModel.settingTask.dirty();
  319. componentModel.mergeOption(newCmptOption, this);
  320. componentModel.optionUpdated(newCmptOption, false);
  321. } else {
  322. // PENDING Global as parent ?
  323. var extraOpt = extend({
  324. componentIndex: index
  325. }, resultItem.keyInfo);
  326. componentModel = new ComponentModelClass(newCmptOption, this, this, extraOpt);
  327. // Assign `keyInfo`
  328. extend(componentModel, extraOpt);
  329. if (resultItem.brandNew) {
  330. componentModel.__requireNewView = true;
  331. }
  332. componentModel.init(newCmptOption, this, this);
  333. // Call optionUpdated after init.
  334. // newCmptOption has been used as componentModel.option
  335. // and may be merged with theme and default, so pass null
  336. // to avoid confusion.
  337. componentModel.optionUpdated(null, true);
  338. }
  339. }
  340. if (componentModel) {
  341. optionsByMainType.push(componentModel.option);
  342. cmptsByMainType.push(componentModel);
  343. cmptsCountByMainType++;
  344. } else {
  345. // Always do assign to avoid elided item in array.
  346. optionsByMainType.push(void 0);
  347. cmptsByMainType.push(void 0);
  348. }
  349. }, this);
  350. option[mainType] = optionsByMainType;
  351. componentsMap.set(mainType, cmptsByMainType);
  352. componentsCount.set(mainType, cmptsCountByMainType);
  353. // Backup series for filtering.
  354. if (mainType === 'series') {
  355. reCreateSeriesIndices(this);
  356. }
  357. }
  358. // If no series declared, ensure `_seriesIndices` initialized.
  359. if (!this._seriesIndices) {
  360. reCreateSeriesIndices(this);
  361. }
  362. };
  363. /**
  364. * Get option for output (cloned option and inner info removed)
  365. */
  366. GlobalModel.prototype.getOption = function () {
  367. var option = clone(this.option);
  368. each(option, function (optInMainType, mainType) {
  369. if (ComponentModel.hasClass(mainType)) {
  370. var opts = modelUtil.normalizeToArray(optInMainType);
  371. // Inner cmpts need to be removed.
  372. // Inner cmpts might not be at last since ec5.0, but still
  373. // compatible for users: if inner cmpt at last, splice the returned array.
  374. var realLen = opts.length;
  375. var metNonInner = false;
  376. for (var i = realLen - 1; i >= 0; i--) {
  377. // Remove options with inner id.
  378. if (opts[i] && !modelUtil.isComponentIdInternal(opts[i])) {
  379. metNonInner = true;
  380. } else {
  381. opts[i] = null;
  382. !metNonInner && realLen--;
  383. }
  384. }
  385. opts.length = realLen;
  386. option[mainType] = opts;
  387. }
  388. });
  389. delete option[OPTION_INNER_KEY];
  390. return option;
  391. };
  392. GlobalModel.prototype.getTheme = function () {
  393. return this._theme;
  394. };
  395. GlobalModel.prototype.getLocaleModel = function () {
  396. return this._locale;
  397. };
  398. GlobalModel.prototype.setUpdatePayload = function (payload) {
  399. this._payload = payload;
  400. };
  401. GlobalModel.prototype.getUpdatePayload = function () {
  402. return this._payload;
  403. };
  404. /**
  405. * @param idx If not specified, return the first one.
  406. */
  407. GlobalModel.prototype.getComponent = function (mainType, idx) {
  408. var list = this._componentsMap.get(mainType);
  409. if (list) {
  410. var cmpt = list[idx || 0];
  411. if (cmpt) {
  412. return cmpt;
  413. } else if (idx == null) {
  414. for (var i = 0; i < list.length; i++) {
  415. if (list[i]) {
  416. return list[i];
  417. }
  418. }
  419. }
  420. }
  421. };
  422. /**
  423. * @return Never be null/undefined.
  424. */
  425. GlobalModel.prototype.queryComponents = function (condition) {
  426. var mainType = condition.mainType;
  427. if (!mainType) {
  428. return [];
  429. }
  430. var index = condition.index;
  431. var id = condition.id;
  432. var name = condition.name;
  433. var cmpts = this._componentsMap.get(mainType);
  434. if (!cmpts || !cmpts.length) {
  435. return [];
  436. }
  437. var result;
  438. if (index != null) {
  439. result = [];
  440. each(modelUtil.normalizeToArray(index), function (idx) {
  441. cmpts[idx] && result.push(cmpts[idx]);
  442. });
  443. } else if (id != null) {
  444. result = queryByIdOrName('id', id, cmpts);
  445. } else if (name != null) {
  446. result = queryByIdOrName('name', name, cmpts);
  447. } else {
  448. // Return all non-empty components in that mainType
  449. result = filter(cmpts, function (cmpt) {
  450. return !!cmpt;
  451. });
  452. }
  453. return filterBySubType(result, condition);
  454. };
  455. /**
  456. * The interface is different from queryComponents,
  457. * which is convenient for inner usage.
  458. *
  459. * @usage
  460. * let result = findComponents(
  461. * {mainType: 'dataZoom', query: {dataZoomId: 'abc'}}
  462. * );
  463. * let result = findComponents(
  464. * {mainType: 'series', subType: 'pie', query: {seriesName: 'uio'}}
  465. * );
  466. * let result = findComponents(
  467. * {mainType: 'series',
  468. * filter: function (model, index) {...}}
  469. * );
  470. * // result like [component0, componnet1, ...]
  471. */
  472. GlobalModel.prototype.findComponents = function (condition) {
  473. var query = condition.query;
  474. var mainType = condition.mainType;
  475. var queryCond = getQueryCond(query);
  476. var result = queryCond ? this.queryComponents(queryCond)
  477. // Retrieve all non-empty components.
  478. : filter(this._componentsMap.get(mainType), function (cmpt) {
  479. return !!cmpt;
  480. });
  481. return doFilter(filterBySubType(result, condition));
  482. function getQueryCond(q) {
  483. var indexAttr = mainType + 'Index';
  484. var idAttr = mainType + 'Id';
  485. var nameAttr = mainType + 'Name';
  486. return q && (q[indexAttr] != null || q[idAttr] != null || q[nameAttr] != null) ? {
  487. mainType: mainType,
  488. // subType will be filtered finally.
  489. index: q[indexAttr],
  490. id: q[idAttr],
  491. name: q[nameAttr]
  492. } : null;
  493. }
  494. function doFilter(res) {
  495. return condition.filter ? filter(res, condition.filter) : res;
  496. }
  497. };
  498. GlobalModel.prototype.eachComponent = function (mainType, cb, context) {
  499. var componentsMap = this._componentsMap;
  500. if (isFunction(mainType)) {
  501. var ctxForAll_1 = cb;
  502. var cbForAll_1 = mainType;
  503. componentsMap.each(function (cmpts, componentType) {
  504. for (var i = 0; cmpts && i < cmpts.length; i++) {
  505. var cmpt = cmpts[i];
  506. cmpt && cbForAll_1.call(ctxForAll_1, componentType, cmpt, cmpt.componentIndex);
  507. }
  508. });
  509. } else {
  510. var cmpts = isString(mainType) ? componentsMap.get(mainType) : isObject(mainType) ? this.findComponents(mainType) : null;
  511. for (var i = 0; cmpts && i < cmpts.length; i++) {
  512. var cmpt = cmpts[i];
  513. cmpt && cb.call(context, cmpt, cmpt.componentIndex);
  514. }
  515. }
  516. };
  517. /**
  518. * Get series list before filtered by name.
  519. */
  520. GlobalModel.prototype.getSeriesByName = function (name) {
  521. var nameStr = modelUtil.convertOptionIdName(name, null);
  522. return filter(this._componentsMap.get('series'), function (oneSeries) {
  523. return !!oneSeries && nameStr != null && oneSeries.name === nameStr;
  524. });
  525. };
  526. /**
  527. * Get series list before filtered by index.
  528. */
  529. GlobalModel.prototype.getSeriesByIndex = function (seriesIndex) {
  530. return this._componentsMap.get('series')[seriesIndex];
  531. };
  532. /**
  533. * Get series list before filtered by type.
  534. * FIXME: rename to getRawSeriesByType?
  535. */
  536. GlobalModel.prototype.getSeriesByType = function (subType) {
  537. return filter(this._componentsMap.get('series'), function (oneSeries) {
  538. return !!oneSeries && oneSeries.subType === subType;
  539. });
  540. };
  541. /**
  542. * Get all series before filtered.
  543. */
  544. GlobalModel.prototype.getSeries = function () {
  545. return filter(this._componentsMap.get('series'), function (oneSeries) {
  546. return !!oneSeries;
  547. });
  548. };
  549. /**
  550. * Count series before filtered.
  551. */
  552. GlobalModel.prototype.getSeriesCount = function () {
  553. return this._componentsCount.get('series');
  554. };
  555. /**
  556. * After filtering, series may be different
  557. * from raw series.
  558. */
  559. GlobalModel.prototype.eachSeries = function (cb, context) {
  560. assertSeriesInitialized(this);
  561. each(this._seriesIndices, function (rawSeriesIndex) {
  562. var series = this._componentsMap.get('series')[rawSeriesIndex];
  563. cb.call(context, series, rawSeriesIndex);
  564. }, this);
  565. };
  566. /**
  567. * Iterate raw series before filtered.
  568. *
  569. * @param {Function} cb
  570. * @param {*} context
  571. */
  572. GlobalModel.prototype.eachRawSeries = function (cb, context) {
  573. each(this._componentsMap.get('series'), function (series) {
  574. series && cb.call(context, series, series.componentIndex);
  575. });
  576. };
  577. /**
  578. * After filtering, series may be different.
  579. * from raw series.
  580. */
  581. GlobalModel.prototype.eachSeriesByType = function (subType, cb, context) {
  582. assertSeriesInitialized(this);
  583. each(this._seriesIndices, function (rawSeriesIndex) {
  584. var series = this._componentsMap.get('series')[rawSeriesIndex];
  585. if (series.subType === subType) {
  586. cb.call(context, series, rawSeriesIndex);
  587. }
  588. }, this);
  589. };
  590. /**
  591. * Iterate raw series before filtered of given type.
  592. */
  593. GlobalModel.prototype.eachRawSeriesByType = function (subType, cb, context) {
  594. return each(this.getSeriesByType(subType), cb, context);
  595. };
  596. GlobalModel.prototype.isSeriesFiltered = function (seriesModel) {
  597. assertSeriesInitialized(this);
  598. return this._seriesIndicesMap.get(seriesModel.componentIndex) == null;
  599. };
  600. GlobalModel.prototype.getCurrentSeriesIndices = function () {
  601. return (this._seriesIndices || []).slice();
  602. };
  603. GlobalModel.prototype.filterSeries = function (cb, context) {
  604. assertSeriesInitialized(this);
  605. var newSeriesIndices = [];
  606. each(this._seriesIndices, function (seriesRawIdx) {
  607. var series = this._componentsMap.get('series')[seriesRawIdx];
  608. cb.call(context, series, seriesRawIdx) && newSeriesIndices.push(seriesRawIdx);
  609. }, this);
  610. this._seriesIndices = newSeriesIndices;
  611. this._seriesIndicesMap = createHashMap(newSeriesIndices);
  612. };
  613. GlobalModel.prototype.restoreData = function (payload) {
  614. reCreateSeriesIndices(this);
  615. var componentsMap = this._componentsMap;
  616. var componentTypes = [];
  617. componentsMap.each(function (components, componentType) {
  618. if (ComponentModel.hasClass(componentType)) {
  619. componentTypes.push(componentType);
  620. }
  621. });
  622. ComponentModel.topologicalTravel(componentTypes, ComponentModel.getAllClassMainTypes(), function (componentType) {
  623. each(componentsMap.get(componentType), function (component) {
  624. if (component && (componentType !== 'series' || !isNotTargetSeries(component, payload))) {
  625. component.restoreData();
  626. }
  627. });
  628. });
  629. };
  630. GlobalModel.internalField = function () {
  631. reCreateSeriesIndices = function (ecModel) {
  632. var seriesIndices = ecModel._seriesIndices = [];
  633. each(ecModel._componentsMap.get('series'), function (series) {
  634. // series may have been removed by `replaceMerge`.
  635. series && seriesIndices.push(series.componentIndex);
  636. });
  637. ecModel._seriesIndicesMap = createHashMap(seriesIndices);
  638. };
  639. assertSeriesInitialized = function (ecModel) {
  640. // Components that use _seriesIndices should depends on series component,
  641. // which make sure that their initialization is after series.
  642. if (process.env.NODE_ENV !== 'production') {
  643. if (!ecModel._seriesIndices) {
  644. throw new Error('Option should contains series.');
  645. }
  646. }
  647. };
  648. initBase = function (ecModel, baseOption) {
  649. // Using OPTION_INNER_KEY to mark that this option cannot be used outside,
  650. // i.e. `chart.setOption(chart.getModel().option);` is forbidden.
  651. ecModel.option = {};
  652. ecModel.option[OPTION_INNER_KEY] = OPTION_INNER_VALUE;
  653. // Init with series: [], in case of calling findSeries method
  654. // before series initialized.
  655. ecModel._componentsMap = createHashMap({
  656. series: []
  657. });
  658. ecModel._componentsCount = createHashMap();
  659. // If user spefied `option.aria`, aria will be enable. This detection should be
  660. // performed before theme and globalDefault merge.
  661. var airaOption = baseOption.aria;
  662. if (isObject(airaOption) && airaOption.enabled == null) {
  663. airaOption.enabled = true;
  664. }
  665. mergeTheme(baseOption, ecModel._theme.option);
  666. // TODO Needs clone when merging to the unexisted property
  667. merge(baseOption, globalDefault, false);
  668. ecModel._mergeOption(baseOption, null);
  669. };
  670. }();
  671. return GlobalModel;
  672. }(Model);
  673. function isNotTargetSeries(seriesModel, payload) {
  674. if (payload) {
  675. var index = payload.seriesIndex;
  676. var id = payload.seriesId;
  677. var name_1 = payload.seriesName;
  678. return index != null && seriesModel.componentIndex !== index || id != null && seriesModel.id !== id || name_1 != null && seriesModel.name !== name_1;
  679. }
  680. }
  681. function mergeTheme(option, theme) {
  682. // PENDING
  683. // NOT use `colorLayer` in theme if option has `color`
  684. var notMergeColorLayer = option.color && !option.colorLayer;
  685. each(theme, function (themeItem, name) {
  686. if (name === 'colorLayer' && notMergeColorLayer) {
  687. return;
  688. }
  689. // If it is component model mainType, the model handles that merge later.
  690. // otherwise, merge them here.
  691. if (!ComponentModel.hasClass(name)) {
  692. if (typeof themeItem === 'object') {
  693. option[name] = !option[name] ? clone(themeItem) : merge(option[name], themeItem, false);
  694. } else {
  695. if (option[name] == null) {
  696. option[name] = themeItem;
  697. }
  698. }
  699. }
  700. });
  701. }
  702. function queryByIdOrName(attr, idOrName, cmpts) {
  703. // Here is a break from echarts4: string and number are
  704. // treated as equal.
  705. if (isArray(idOrName)) {
  706. var keyMap_1 = createHashMap();
  707. each(idOrName, function (idOrNameItem) {
  708. if (idOrNameItem != null) {
  709. var idName = modelUtil.convertOptionIdName(idOrNameItem, null);
  710. idName != null && keyMap_1.set(idOrNameItem, true);
  711. }
  712. });
  713. return filter(cmpts, function (cmpt) {
  714. return cmpt && keyMap_1.get(cmpt[attr]);
  715. });
  716. } else {
  717. var idName_1 = modelUtil.convertOptionIdName(idOrName, null);
  718. return filter(cmpts, function (cmpt) {
  719. return cmpt && idName_1 != null && cmpt[attr] === idName_1;
  720. });
  721. }
  722. }
  723. function filterBySubType(components, condition) {
  724. // Using hasOwnProperty for restrict. Consider
  725. // subType is undefined in user payload.
  726. return condition.hasOwnProperty('subType') ? filter(components, function (cmpt) {
  727. return cmpt && cmpt.subType === condition.subType;
  728. }) : components;
  729. }
  730. function normalizeSetOptionInput(opts) {
  731. var replaceMergeMainTypeMap = createHashMap();
  732. opts && each(modelUtil.normalizeToArray(opts.replaceMerge), function (mainType) {
  733. if (process.env.NODE_ENV !== 'production') {
  734. assert(ComponentModel.hasClass(mainType), '"' + mainType + '" is not valid component main type in "replaceMerge"');
  735. }
  736. replaceMergeMainTypeMap.set(mainType, true);
  737. });
  738. return {
  739. replaceMergeMainTypeMap: replaceMergeMainTypeMap
  740. };
  741. }
  742. mixin(GlobalModel, PaletteMixin);
  743. export default GlobalModel;