Series.js 19 KB

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