Series.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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 =
  58. /** @class */
  59. function (_super) {
  60. __extends(SeriesModel, _super);
  61. function SeriesModel() {
  62. // [Caution]: Because 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 overridden.
  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 overridden 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. var ecModel = this.ecModel; // Disable animation if using echarts in node but not give ssr flag.
  283. // In ssr mode, renderToString will generate svg with css animation.
  284. if (env.node && !(ecModel && ecModel.ssr)) {
  285. return false;
  286. }
  287. var animationEnabled = this.getShallow('animation');
  288. if (animationEnabled) {
  289. if (this.getData().count() > this.getShallow('animationThreshold')) {
  290. animationEnabled = false;
  291. }
  292. }
  293. return !!animationEnabled;
  294. };
  295. SeriesModel.prototype.restoreData = function () {
  296. this.dataTask.dirty();
  297. };
  298. SeriesModel.prototype.getColorFromPalette = function (name, scope, requestColorNum) {
  299. var ecModel = this.ecModel; // PENDING
  300. var color = PaletteMixin.prototype.getColorFromPalette.call(this, name, scope, requestColorNum);
  301. if (!color) {
  302. color = ecModel.getColorFromPalette(name, scope, requestColorNum);
  303. }
  304. return color;
  305. };
  306. /**
  307. * Use `data.mapDimensionsAll(coordDim)` instead.
  308. * @deprecated
  309. */
  310. SeriesModel.prototype.coordDimToDataDim = function (coordDim) {
  311. return this.getRawData().mapDimensionsAll(coordDim);
  312. };
  313. /**
  314. * Get progressive rendering count each step
  315. */
  316. SeriesModel.prototype.getProgressive = function () {
  317. return this.get('progressive');
  318. };
  319. /**
  320. * Get progressive rendering count each step
  321. */
  322. SeriesModel.prototype.getProgressiveThreshold = function () {
  323. return this.get('progressiveThreshold');
  324. }; // PENGING If selectedMode is null ?
  325. SeriesModel.prototype.select = function (innerDataIndices, dataType) {
  326. this._innerSelect(this.getData(dataType), innerDataIndices);
  327. };
  328. SeriesModel.prototype.unselect = function (innerDataIndices, dataType) {
  329. var selectedMap = this.option.selectedMap;
  330. if (!selectedMap) {
  331. return;
  332. }
  333. var selectedMode = this.option.selectedMode;
  334. var data = this.getData(dataType);
  335. if (selectedMode === 'series' || selectedMap === 'all') {
  336. this.option.selectedMap = {};
  337. this._selectedDataIndicesMap = {};
  338. return;
  339. }
  340. for (var i = 0; i < innerDataIndices.length; i++) {
  341. var dataIndex = innerDataIndices[i];
  342. var nameOrId = getSelectionKey(data, dataIndex);
  343. selectedMap[nameOrId] = false;
  344. this._selectedDataIndicesMap[nameOrId] = -1;
  345. }
  346. };
  347. SeriesModel.prototype.toggleSelect = function (innerDataIndices, dataType) {
  348. var tmpArr = [];
  349. for (var i = 0; i < innerDataIndices.length; i++) {
  350. tmpArr[0] = innerDataIndices[i];
  351. this.isSelected(innerDataIndices[i], dataType) ? this.unselect(tmpArr, dataType) : this.select(tmpArr, dataType);
  352. }
  353. };
  354. SeriesModel.prototype.getSelectedDataIndices = function () {
  355. if (this.option.selectedMap === 'all') {
  356. return [].slice.call(this.getData().getIndices());
  357. }
  358. var selectedDataIndicesMap = this._selectedDataIndicesMap;
  359. var nameOrIds = zrUtil.keys(selectedDataIndicesMap);
  360. var dataIndices = [];
  361. for (var i = 0; i < nameOrIds.length; i++) {
  362. var dataIndex = selectedDataIndicesMap[nameOrIds[i]];
  363. if (dataIndex >= 0) {
  364. dataIndices.push(dataIndex);
  365. }
  366. }
  367. return dataIndices;
  368. };
  369. SeriesModel.prototype.isSelected = function (dataIndex, dataType) {
  370. var selectedMap = this.option.selectedMap;
  371. if (!selectedMap) {
  372. return false;
  373. }
  374. var data = this.getData(dataType);
  375. return (selectedMap === 'all' || selectedMap[getSelectionKey(data, dataIndex)]) && !data.getItemModel(dataIndex).get(['select', 'disabled']);
  376. };
  377. SeriesModel.prototype.isUniversalTransitionEnabled = function () {
  378. if (this[SERIES_UNIVERSAL_TRANSITION_PROP]) {
  379. return true;
  380. }
  381. var universalTransitionOpt = this.option.universalTransition; // Quick reject
  382. if (!universalTransitionOpt) {
  383. return false;
  384. }
  385. if (universalTransitionOpt === true) {
  386. return true;
  387. } // Can be simply 'universalTransition: true'
  388. return universalTransitionOpt && universalTransitionOpt.enabled;
  389. };
  390. SeriesModel.prototype._innerSelect = function (data, innerDataIndices) {
  391. var _a, _b;
  392. var option = this.option;
  393. var selectedMode = option.selectedMode;
  394. var len = innerDataIndices.length;
  395. if (!selectedMode || !len) {
  396. return;
  397. }
  398. if (selectedMode === 'series') {
  399. option.selectedMap = 'all';
  400. } else if (selectedMode === 'multiple') {
  401. if (!zrUtil.isObject(option.selectedMap)) {
  402. option.selectedMap = {};
  403. }
  404. var selectedMap = option.selectedMap;
  405. for (var i = 0; i < len; i++) {
  406. var dataIndex = innerDataIndices[i]; // TODO different types of data share same object.
  407. var nameOrId = getSelectionKey(data, dataIndex);
  408. selectedMap[nameOrId] = true;
  409. this._selectedDataIndicesMap[nameOrId] = data.getRawIndex(dataIndex);
  410. }
  411. } else if (selectedMode === 'single' || selectedMode === true) {
  412. var lastDataIndex = innerDataIndices[len - 1];
  413. var nameOrId = getSelectionKey(data, lastDataIndex);
  414. option.selectedMap = (_a = {}, _a[nameOrId] = true, _a);
  415. this._selectedDataIndicesMap = (_b = {}, _b[nameOrId] = data.getRawIndex(lastDataIndex), _b);
  416. }
  417. };
  418. SeriesModel.prototype._initSelectedMapFromData = function (data) {
  419. // Ignore select info in data if selectedMap exists.
  420. // NOTE It's only for legacy usage. edge data is not supported.
  421. if (this.option.selectedMap) {
  422. return;
  423. }
  424. var dataIndices = [];
  425. if (data.hasItemOption) {
  426. data.each(function (idx) {
  427. var rawItem = data.getRawDataItem(idx);
  428. if (rawItem && rawItem.selected) {
  429. dataIndices.push(idx);
  430. }
  431. });
  432. }
  433. if (dataIndices.length > 0) {
  434. this._innerSelect(data, dataIndices);
  435. }
  436. }; // /**
  437. // * @see {module:echarts/stream/Scheduler}
  438. // */
  439. // abstract pipeTask: null
  440. SeriesModel.registerClass = function (clz) {
  441. return ComponentModel.registerClass(clz);
  442. };
  443. SeriesModel.protoInitialize = function () {
  444. var proto = SeriesModel.prototype;
  445. proto.type = 'series.__base__';
  446. proto.seriesIndex = 0;
  447. proto.ignoreStyleOnData = false;
  448. proto.hasSymbolVisual = false;
  449. proto.defaultSymbol = 'circle'; // Make sure the values can be accessed!
  450. proto.visualStyleAccessPath = 'itemStyle';
  451. proto.visualDrawType = 'fill';
  452. }();
  453. return SeriesModel;
  454. }(ComponentModel);
  455. zrUtil.mixin(SeriesModel, DataFormatMixin);
  456. zrUtil.mixin(SeriesModel, PaletteMixin);
  457. mountExtend(SeriesModel, ComponentModel);
  458. /**
  459. * MUST be called after `prepareSource` called
  460. * Here we need to make auto series, especially for auto legend. But we
  461. * do not modify series.name in option to avoid side effects.
  462. */
  463. function autoSeriesName(seriesModel) {
  464. // User specified name has higher priority, otherwise it may cause
  465. // series can not be queried unexpectedly.
  466. var name = seriesModel.name;
  467. if (!modelUtil.isNameSpecified(seriesModel)) {
  468. seriesModel.name = getSeriesAutoName(seriesModel) || name;
  469. }
  470. }
  471. function getSeriesAutoName(seriesModel) {
  472. var data = seriesModel.getRawData();
  473. var dataDims = data.mapDimensionsAll('seriesName');
  474. var nameArr = [];
  475. zrUtil.each(dataDims, function (dataDim) {
  476. var dimInfo = data.getDimensionInfo(dataDim);
  477. dimInfo.displayName && nameArr.push(dimInfo.displayName);
  478. });
  479. return nameArr.join(' ');
  480. }
  481. function dataTaskCount(context) {
  482. return context.model.getRawData().count();
  483. }
  484. function dataTaskReset(context) {
  485. var seriesModel = context.model;
  486. seriesModel.setData(seriesModel.getRawData().cloneShallow());
  487. return dataTaskProgress;
  488. }
  489. function dataTaskProgress(param, context) {
  490. // Avoid repeat cloneShallow when data just created in reset.
  491. if (context.outputData && param.end > context.outputData.count()) {
  492. context.model.getRawData().cloneShallow(context.outputData);
  493. }
  494. } // TODO refactor
  495. function wrapData(data, seriesModel) {
  496. zrUtil.each(zrUtil.concatArray(data.CHANGABLE_METHODS, data.DOWNSAMPLE_METHODS), function (methodName) {
  497. data.wrapMethod(methodName, zrUtil.curry(onDataChange, seriesModel));
  498. });
  499. }
  500. function onDataChange(seriesModel, newList) {
  501. var task = getCurrentTask(seriesModel);
  502. if (task) {
  503. // Consider case: filter, selectRange
  504. task.setOutputEnd((newList || this).count());
  505. }
  506. return newList;
  507. }
  508. function getCurrentTask(seriesModel) {
  509. var scheduler = (seriesModel.ecModel || {}).scheduler;
  510. var pipeline = scheduler && scheduler.getPipeline(seriesModel.uid);
  511. if (pipeline) {
  512. // When pipline finished, the currrentTask keep the last
  513. // task (renderTask).
  514. var task = pipeline.currentTask;
  515. if (task) {
  516. var agentStubMap = task.agentStubMap;
  517. if (agentStubMap) {
  518. task = agentStubMap.get(seriesModel.uid);
  519. }
  520. }
  521. return task;
  522. }
  523. }
  524. export default SeriesModel;