Series.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. import * as zrUtil from 'zrender/lib/core/util.js';
  42. import env from 'zrender/lib/core/env.js';
  43. import * as modelUtil from '../util/model.js';
  44. import ComponentModel from './Component.js';
  45. import { PaletteMixin } from './mixin/palette.js';
  46. import { DataFormatMixin } from '../model/mixin/dataFormat.js';
  47. import { getLayoutParams, mergeLayoutParam, fetchLayoutMode } from '../util/layout.js';
  48. import { createTask } from '../core/task.js';
  49. import { mountExtend } from '../util/clazz.js';
  50. import { SourceManager } from '../data/helper/sourceManager.js';
  51. import { defaultSeriesFormatTooltip } from '../component/tooltip/seriesFormatTooltip.js';
  52. var inner = modelUtil.makeInner();
  53. function getSelectionKey(data, dataIndex) {
  54. return data.getName(dataIndex) || data.getId(dataIndex);
  55. }
  56. export var SERIES_UNIVERSAL_TRANSITION_PROP = '__universalTransitionEnabled';
  57. var SeriesModel = /** @class */function (_super) {
  58. __extends(SeriesModel, _super);
  59. function SeriesModel() {
  60. // [Caution]: Because this class or desecendants can be used as `XXX.extend(subProto)`,
  61. // the class members must not be initialized in constructor or declaration place.
  62. // Otherwise there is bad case:
  63. // class A {xxx = 1;}
  64. // enableClassExtend(A);
  65. // class B extends A {}
  66. // var C = B.extend({xxx: 5});
  67. // var c = new C();
  68. // console.log(c.xxx); // expect 5 but always 1.
  69. var _this = _super !== null && _super.apply(this, arguments) || this;
  70. // ---------------------------------------
  71. // Props about data selection
  72. // ---------------------------------------
  73. _this._selectedDataIndicesMap = {};
  74. return _this;
  75. }
  76. SeriesModel.prototype.init = function (option, parentModel, ecModel) {
  77. this.seriesIndex = this.componentIndex;
  78. this.dataTask = createTask({
  79. count: dataTaskCount,
  80. reset: dataTaskReset
  81. });
  82. this.dataTask.context = {
  83. model: this
  84. };
  85. this.mergeDefaultAndTheme(option, ecModel);
  86. var sourceManager = inner(this).sourceManager = new SourceManager(this);
  87. sourceManager.prepareSource();
  88. var data = this.getInitialData(option, ecModel);
  89. wrapData(data, this);
  90. this.dataTask.context.data = data;
  91. if (process.env.NODE_ENV !== 'production') {
  92. zrUtil.assert(data, 'getInitialData returned invalid data.');
  93. }
  94. inner(this).dataBeforeProcessed = data;
  95. // If we reverse the order (make data firstly, and then make
  96. // dataBeforeProcessed by cloneShallow), cloneShallow will
  97. // cause data.graph.data !== data when using
  98. // module:echarts/data/Graph or module:echarts/data/Tree.
  99. // See module:echarts/data/helper/linkSeriesData
  100. // Theoretically, it is unreasonable to call `seriesModel.getData()` in the model
  101. // init or merge stage, because the data can be restored. So we do not `restoreData`
  102. // and `setData` here, which forbids calling `seriesModel.getData()` in this stage.
  103. // Call `seriesModel.getRawData()` instead.
  104. // this.restoreData();
  105. autoSeriesName(this);
  106. this._initSelectedMapFromData(data);
  107. };
  108. /**
  109. * Util for merge default and theme to option
  110. */
  111. SeriesModel.prototype.mergeDefaultAndTheme = function (option, ecModel) {
  112. var layoutMode = fetchLayoutMode(this);
  113. var inputPositionParams = layoutMode ? getLayoutParams(option) : {};
  114. // Backward compat: using subType on theme.
  115. // But if name duplicate between series subType
  116. // (for example: parallel) add component mainType,
  117. // add suffix 'Series'.
  118. var themeSubType = this.subType;
  119. if (ComponentModel.hasClass(themeSubType)) {
  120. themeSubType += 'Series';
  121. }
  122. zrUtil.merge(option, ecModel.getTheme().get(this.subType));
  123. zrUtil.merge(option, this.getDefaultOption());
  124. // Default label emphasis `show`
  125. modelUtil.defaultEmphasis(option, 'label', ['show']);
  126. this.fillDataTextStyle(option.data);
  127. if (layoutMode) {
  128. mergeLayoutParam(option, inputPositionParams, layoutMode);
  129. }
  130. };
  131. SeriesModel.prototype.mergeOption = function (newSeriesOption, ecModel) {
  132. // this.settingTask.dirty();
  133. newSeriesOption = zrUtil.merge(this.option, newSeriesOption, true);
  134. this.fillDataTextStyle(newSeriesOption.data);
  135. var layoutMode = fetchLayoutMode(this);
  136. if (layoutMode) {
  137. mergeLayoutParam(this.option, newSeriesOption, layoutMode);
  138. }
  139. var sourceManager = inner(this).sourceManager;
  140. sourceManager.dirty();
  141. sourceManager.prepareSource();
  142. var data = this.getInitialData(newSeriesOption, ecModel);
  143. wrapData(data, this);
  144. this.dataTask.dirty();
  145. this.dataTask.context.data = data;
  146. inner(this).dataBeforeProcessed = data;
  147. autoSeriesName(this);
  148. this._initSelectedMapFromData(data);
  149. };
  150. SeriesModel.prototype.fillDataTextStyle = function (data) {
  151. // Default data label emphasis `show`
  152. // FIXME Tree structure data ?
  153. // FIXME Performance ?
  154. if (data && !zrUtil.isTypedArray(data)) {
  155. var props = ['show'];
  156. for (var i = 0; i < data.length; i++) {
  157. if (data[i] && data[i].label) {
  158. modelUtil.defaultEmphasis(data[i], 'label', props);
  159. }
  160. }
  161. }
  162. };
  163. /**
  164. * Init a data structure from data related option in series
  165. * Must be overridden.
  166. */
  167. SeriesModel.prototype.getInitialData = function (option, ecModel) {
  168. return;
  169. };
  170. /**
  171. * Append data to list
  172. */
  173. SeriesModel.prototype.appendData = function (params) {
  174. // FIXME ???
  175. // (1) If data from dataset, forbidden append.
  176. // (2) support append data of dataset.
  177. var data = this.getRawData();
  178. data.appendData(params.data);
  179. };
  180. /**
  181. * Consider some method like `filter`, `map` need make new data,
  182. * We should make sure that `seriesModel.getData()` get correct
  183. * data in the stream procedure. So we fetch data from upstream
  184. * each time `task.perform` called.
  185. */
  186. SeriesModel.prototype.getData = function (dataType) {
  187. var task = getCurrentTask(this);
  188. if (task) {
  189. var data = task.context.data;
  190. return dataType == null || !data.getLinkedData ? data : data.getLinkedData(dataType);
  191. } else {
  192. // When series is not alive (that may happen when click toolbox
  193. // restore or setOption with not merge mode), series data may
  194. // be still need to judge animation or something when graphic
  195. // elements want to know whether fade out.
  196. return inner(this).data;
  197. }
  198. };
  199. SeriesModel.prototype.getAllData = function () {
  200. var mainData = this.getData();
  201. return mainData && mainData.getLinkedDataAll ? mainData.getLinkedDataAll() : [{
  202. data: mainData
  203. }];
  204. };
  205. SeriesModel.prototype.setData = function (data) {
  206. var task = getCurrentTask(this);
  207. if (task) {
  208. var context = task.context;
  209. // Consider case: filter, data sample.
  210. // FIXME:TS never used, so comment it
  211. // if (context.data !== data && task.modifyOutputEnd) {
  212. // task.setOutputEnd(data.count());
  213. // }
  214. context.outputData = data;
  215. // Caution: setData should update context.data,
  216. // Because getData may be called multiply in a
  217. // single stage and expect to get the data just
  218. // set. (For example, AxisProxy, x y both call
  219. // getData and setDate sequentially).
  220. // So the context.data should be fetched from
  221. // upstream each time when a stage starts to be
  222. // performed.
  223. if (task !== this.dataTask) {
  224. context.data = data;
  225. }
  226. }
  227. inner(this).data = data;
  228. };
  229. SeriesModel.prototype.getEncode = function () {
  230. var encode = this.get('encode', true);
  231. if (encode) {
  232. return zrUtil.createHashMap(encode);
  233. }
  234. };
  235. SeriesModel.prototype.getSourceManager = function () {
  236. return inner(this).sourceManager;
  237. };
  238. SeriesModel.prototype.getSource = function () {
  239. return this.getSourceManager().getSource();
  240. };
  241. /**
  242. * Get data before processed
  243. */
  244. SeriesModel.prototype.getRawData = function () {
  245. return inner(this).dataBeforeProcessed;
  246. };
  247. SeriesModel.prototype.getColorBy = function () {
  248. var colorBy = this.get('colorBy');
  249. return colorBy || 'series';
  250. };
  251. SeriesModel.prototype.isColorBySeries = function () {
  252. return this.getColorBy() === 'series';
  253. };
  254. /**
  255. * Get base axis if has coordinate system and has axis.
  256. * By default use coordSys.getBaseAxis();
  257. * Can be overridden for some chart.
  258. * @return {type} description
  259. */
  260. SeriesModel.prototype.getBaseAxis = function () {
  261. var coordSys = this.coordinateSystem;
  262. // @ts-ignore
  263. return coordSys && coordSys.getBaseAxis && coordSys.getBaseAxis();
  264. };
  265. /**
  266. * Default tooltip formatter
  267. *
  268. * @param dataIndex
  269. * @param multipleSeries
  270. * @param dataType
  271. * @param renderMode valid values: 'html'(by default) and 'richText'.
  272. * 'html' is used for rendering tooltip in extra DOM form, and the result
  273. * string is used as DOM HTML content.
  274. * 'richText' is used for rendering tooltip in rich text form, for those where
  275. * DOM operation is not supported.
  276. * @return formatted tooltip with `html` and `markers`
  277. * Notice: The override method can also return string
  278. */
  279. SeriesModel.prototype.formatTooltip = function (dataIndex, multipleSeries, dataType) {
  280. return defaultSeriesFormatTooltip({
  281. series: this,
  282. dataIndex: dataIndex,
  283. multipleSeries: multipleSeries
  284. });
  285. };
  286. SeriesModel.prototype.isAnimationEnabled = function () {
  287. var ecModel = this.ecModel;
  288. // Disable animation if using echarts in node but not give ssr flag.
  289. // In ssr mode, renderToString will generate svg with css animation.
  290. if (env.node && !(ecModel && ecModel.ssr)) {
  291. return false;
  292. }
  293. var animationEnabled = this.getShallow('animation');
  294. if (animationEnabled) {
  295. if (this.getData().count() > this.getShallow('animationThreshold')) {
  296. animationEnabled = false;
  297. }
  298. }
  299. return !!animationEnabled;
  300. };
  301. SeriesModel.prototype.restoreData = function () {
  302. this.dataTask.dirty();
  303. };
  304. SeriesModel.prototype.getColorFromPalette = function (name, scope, requestColorNum) {
  305. var ecModel = this.ecModel;
  306. // PENDING
  307. var color = PaletteMixin.prototype.getColorFromPalette.call(this, name, scope, requestColorNum);
  308. if (!color) {
  309. color = ecModel.getColorFromPalette(name, scope, requestColorNum);
  310. }
  311. return color;
  312. };
  313. /**
  314. * Use `data.mapDimensionsAll(coordDim)` instead.
  315. * @deprecated
  316. */
  317. SeriesModel.prototype.coordDimToDataDim = function (coordDim) {
  318. return this.getRawData().mapDimensionsAll(coordDim);
  319. };
  320. /**
  321. * Get progressive rendering count each step
  322. */
  323. SeriesModel.prototype.getProgressive = function () {
  324. return this.get('progressive');
  325. };
  326. /**
  327. * Get progressive rendering count each step
  328. */
  329. SeriesModel.prototype.getProgressiveThreshold = function () {
  330. return this.get('progressiveThreshold');
  331. };
  332. // PENGING If selectedMode is null ?
  333. SeriesModel.prototype.select = function (innerDataIndices, dataType) {
  334. this._innerSelect(this.getData(dataType), innerDataIndices);
  335. };
  336. SeriesModel.prototype.unselect = function (innerDataIndices, dataType) {
  337. var selectedMap = this.option.selectedMap;
  338. if (!selectedMap) {
  339. return;
  340. }
  341. var selectedMode = this.option.selectedMode;
  342. var data = this.getData(dataType);
  343. if (selectedMode === 'series' || selectedMap === 'all') {
  344. this.option.selectedMap = {};
  345. this._selectedDataIndicesMap = {};
  346. return;
  347. }
  348. for (var i = 0; i < innerDataIndices.length; i++) {
  349. var dataIndex = innerDataIndices[i];
  350. var nameOrId = getSelectionKey(data, dataIndex);
  351. selectedMap[nameOrId] = false;
  352. this._selectedDataIndicesMap[nameOrId] = -1;
  353. }
  354. };
  355. SeriesModel.prototype.toggleSelect = function (innerDataIndices, dataType) {
  356. var tmpArr = [];
  357. for (var i = 0; i < innerDataIndices.length; i++) {
  358. tmpArr[0] = innerDataIndices[i];
  359. this.isSelected(innerDataIndices[i], dataType) ? this.unselect(tmpArr, dataType) : this.select(tmpArr, dataType);
  360. }
  361. };
  362. SeriesModel.prototype.getSelectedDataIndices = function () {
  363. if (this.option.selectedMap === 'all') {
  364. return [].slice.call(this.getData().getIndices());
  365. }
  366. var selectedDataIndicesMap = this._selectedDataIndicesMap;
  367. var nameOrIds = zrUtil.keys(selectedDataIndicesMap);
  368. var dataIndices = [];
  369. for (var i = 0; i < nameOrIds.length; i++) {
  370. var dataIndex = selectedDataIndicesMap[nameOrIds[i]];
  371. if (dataIndex >= 0) {
  372. dataIndices.push(dataIndex);
  373. }
  374. }
  375. return dataIndices;
  376. };
  377. SeriesModel.prototype.isSelected = function (dataIndex, dataType) {
  378. var selectedMap = this.option.selectedMap;
  379. if (!selectedMap) {
  380. return false;
  381. }
  382. var data = this.getData(dataType);
  383. return (selectedMap === 'all' || selectedMap[getSelectionKey(data, dataIndex)]) && !data.getItemModel(dataIndex).get(['select', 'disabled']);
  384. };
  385. SeriesModel.prototype.isUniversalTransitionEnabled = function () {
  386. if (this[SERIES_UNIVERSAL_TRANSITION_PROP]) {
  387. return true;
  388. }
  389. var universalTransitionOpt = this.option.universalTransition;
  390. // Quick reject
  391. if (!universalTransitionOpt) {
  392. return false;
  393. }
  394. if (universalTransitionOpt === true) {
  395. return true;
  396. }
  397. // Can be simply 'universalTransition: true'
  398. return universalTransitionOpt && universalTransitionOpt.enabled;
  399. };
  400. SeriesModel.prototype._innerSelect = function (data, innerDataIndices) {
  401. var _a, _b;
  402. var option = this.option;
  403. var selectedMode = option.selectedMode;
  404. var len = innerDataIndices.length;
  405. if (!selectedMode || !len) {
  406. return;
  407. }
  408. if (selectedMode === 'series') {
  409. option.selectedMap = 'all';
  410. } else if (selectedMode === 'multiple') {
  411. if (!zrUtil.isObject(option.selectedMap)) {
  412. option.selectedMap = {};
  413. }
  414. var selectedMap = option.selectedMap;
  415. for (var i = 0; i < len; i++) {
  416. var dataIndex = innerDataIndices[i];
  417. // TODO different types of data share same object.
  418. var nameOrId = getSelectionKey(data, dataIndex);
  419. selectedMap[nameOrId] = true;
  420. this._selectedDataIndicesMap[nameOrId] = data.getRawIndex(dataIndex);
  421. }
  422. } else if (selectedMode === 'single' || selectedMode === true) {
  423. var lastDataIndex = innerDataIndices[len - 1];
  424. var nameOrId = getSelectionKey(data, lastDataIndex);
  425. option.selectedMap = (_a = {}, _a[nameOrId] = true, _a);
  426. this._selectedDataIndicesMap = (_b = {}, _b[nameOrId] = data.getRawIndex(lastDataIndex), _b);
  427. }
  428. };
  429. SeriesModel.prototype._initSelectedMapFromData = function (data) {
  430. // Ignore select info in data if selectedMap exists.
  431. // NOTE It's only for legacy usage. edge data is not supported.
  432. if (this.option.selectedMap) {
  433. return;
  434. }
  435. var dataIndices = [];
  436. if (data.hasItemOption) {
  437. data.each(function (idx) {
  438. var rawItem = data.getRawDataItem(idx);
  439. if (rawItem && rawItem.selected) {
  440. dataIndices.push(idx);
  441. }
  442. });
  443. }
  444. if (dataIndices.length > 0) {
  445. this._innerSelect(data, dataIndices);
  446. }
  447. };
  448. // /**
  449. // * @see {module:echarts/stream/Scheduler}
  450. // */
  451. // abstract pipeTask: null
  452. SeriesModel.registerClass = function (clz) {
  453. return ComponentModel.registerClass(clz);
  454. };
  455. SeriesModel.protoInitialize = function () {
  456. var proto = SeriesModel.prototype;
  457. proto.type = 'series.__base__';
  458. proto.seriesIndex = 0;
  459. proto.ignoreStyleOnData = false;
  460. proto.hasSymbolVisual = false;
  461. proto.defaultSymbol = 'circle';
  462. // Make sure the values can be accessed!
  463. proto.visualStyleAccessPath = 'itemStyle';
  464. proto.visualDrawType = 'fill';
  465. }();
  466. return SeriesModel;
  467. }(ComponentModel);
  468. zrUtil.mixin(SeriesModel, DataFormatMixin);
  469. zrUtil.mixin(SeriesModel, PaletteMixin);
  470. mountExtend(SeriesModel, ComponentModel);
  471. /**
  472. * MUST be called after `prepareSource` called
  473. * Here we need to make auto series, especially for auto legend. But we
  474. * do not modify series.name in option to avoid side effects.
  475. */
  476. function autoSeriesName(seriesModel) {
  477. // User specified name has higher priority, otherwise it may cause
  478. // series can not be queried unexpectedly.
  479. var name = seriesModel.name;
  480. if (!modelUtil.isNameSpecified(seriesModel)) {
  481. seriesModel.name = getSeriesAutoName(seriesModel) || name;
  482. }
  483. }
  484. function getSeriesAutoName(seriesModel) {
  485. var data = seriesModel.getRawData();
  486. var dataDims = data.mapDimensionsAll('seriesName');
  487. var nameArr = [];
  488. zrUtil.each(dataDims, function (dataDim) {
  489. var dimInfo = data.getDimensionInfo(dataDim);
  490. dimInfo.displayName && nameArr.push(dimInfo.displayName);
  491. });
  492. return nameArr.join(' ');
  493. }
  494. function dataTaskCount(context) {
  495. return context.model.getRawData().count();
  496. }
  497. function dataTaskReset(context) {
  498. var seriesModel = context.model;
  499. seriesModel.setData(seriesModel.getRawData().cloneShallow());
  500. return dataTaskProgress;
  501. }
  502. function dataTaskProgress(param, context) {
  503. // Avoid repeat cloneShallow when data just created in reset.
  504. if (context.outputData && param.end > context.outputData.count()) {
  505. context.model.getRawData().cloneShallow(context.outputData);
  506. }
  507. }
  508. // TODO refactor
  509. function wrapData(data, seriesModel) {
  510. zrUtil.each(zrUtil.concatArray(data.CHANGABLE_METHODS, data.DOWNSAMPLE_METHODS), function (methodName) {
  511. data.wrapMethod(methodName, zrUtil.curry(onDataChange, seriesModel));
  512. });
  513. }
  514. function onDataChange(seriesModel, newList) {
  515. var task = getCurrentTask(seriesModel);
  516. if (task) {
  517. // Consider case: filter, selectRange
  518. task.setOutputEnd((newList || this).count());
  519. }
  520. return newList;
  521. }
  522. function getCurrentTask(seriesModel) {
  523. var scheduler = (seriesModel.ecModel || {}).scheduler;
  524. var pipeline = scheduler && scheduler.getPipeline(seriesModel.uid);
  525. if (pipeline) {
  526. // When pipline finished, the currrentTask keep the last
  527. // task (renderTask).
  528. var task = pipeline.currentTask;
  529. if (task) {
  530. var agentStubMap = task.agentStubMap;
  531. if (agentStubMap) {
  532. task = agentStubMap.get(seriesModel.uid);
  533. }
  534. }
  535. return task;
  536. }
  537. }
  538. export default SeriesModel;