universalTransition.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. // Universal transitions that can animate between any shapes(series) and any properties in any amounts.
  41. import { SERIES_UNIVERSAL_TRANSITION_PROP } from '../model/Series.js';
  42. import { createHashMap, each, map, filter, isArray, extend } from 'zrender/lib/core/util.js';
  43. import { applyMorphAnimation, getPathList } from './morphTransitionHelper.js';
  44. import Path from 'zrender/lib/graphic/Path.js';
  45. import { initProps } from '../util/graphic.js';
  46. import DataDiffer from '../data/DataDiffer.js';
  47. import { makeInner, normalizeToArray } from '../util/model.js';
  48. import { warn } from '../util/log.js';
  49. import { getAnimationConfig, getOldStyle } from './basicTransition.js';
  50. import Displayable from 'zrender/lib/graphic/Displayable.js';
  51. var DATA_COUNT_THRESHOLD = 1e4;
  52. ;
  53. var getUniversalTransitionGlobalStore = makeInner();
  54. function getGroupIdDimension(data) {
  55. var dimensions = data.dimensions;
  56. for (var i = 0; i < dimensions.length; i++) {
  57. var dimInfo = data.getDimensionInfo(dimensions[i]);
  58. if (dimInfo && dimInfo.otherDims.itemGroupId === 0) {
  59. return dimensions[i];
  60. }
  61. }
  62. }
  63. function flattenDataDiffItems(list) {
  64. var items = [];
  65. each(list, function (seriesInfo) {
  66. var data = seriesInfo.data;
  67. if (data.count() > DATA_COUNT_THRESHOLD) {
  68. if (process.env.NODE_ENV !== 'production') {
  69. warn('Universal transition is disabled on large data > 10k.');
  70. }
  71. return;
  72. }
  73. var indices = data.getIndices();
  74. var groupDim = getGroupIdDimension(data);
  75. for (var dataIndex = 0; dataIndex < indices.length; dataIndex++) {
  76. items.push({
  77. data: data,
  78. dim: seriesInfo.dim || groupDim,
  79. divide: seriesInfo.divide,
  80. dataIndex: dataIndex
  81. });
  82. }
  83. });
  84. return items;
  85. }
  86. function fadeInElement(newEl, newSeries, newIndex) {
  87. newEl.traverse(function (el) {
  88. if (el instanceof Path) {
  89. // TODO use fade in animation for target element.
  90. initProps(el, {
  91. style: {
  92. opacity: 0
  93. }
  94. }, newSeries, {
  95. dataIndex: newIndex,
  96. isFrom: true
  97. });
  98. }
  99. });
  100. }
  101. function removeEl(el) {
  102. if (el.parent) {
  103. // Bake parent transform to element.
  104. // So it can still have proper transform to transition after it's removed.
  105. var computedTransform = el.getComputedTransform();
  106. el.setLocalTransform(computedTransform);
  107. el.parent.remove(el);
  108. }
  109. }
  110. function stopAnimation(el) {
  111. el.stopAnimation();
  112. if (el.isGroup) {
  113. el.traverse(function (child) {
  114. child.stopAnimation();
  115. });
  116. }
  117. }
  118. function animateElementStyles(el, dataIndex, seriesModel) {
  119. var animationConfig = getAnimationConfig('update', seriesModel, dataIndex);
  120. animationConfig && el.traverse(function (child) {
  121. if (child instanceof Displayable) {
  122. var oldStyle = getOldStyle(child);
  123. if (oldStyle) {
  124. child.animateFrom({
  125. style: oldStyle
  126. }, animationConfig);
  127. }
  128. }
  129. });
  130. }
  131. function isAllIdSame(oldDiffItems, newDiffItems) {
  132. var len = oldDiffItems.length;
  133. if (len !== newDiffItems.length) {
  134. return false;
  135. }
  136. for (var i = 0; i < len; i++) {
  137. var oldItem = oldDiffItems[i];
  138. var newItem = newDiffItems[i];
  139. if (oldItem.data.getId(oldItem.dataIndex) !== newItem.data.getId(newItem.dataIndex)) {
  140. return false;
  141. }
  142. }
  143. return true;
  144. }
  145. function transitionBetween(oldList, newList, api) {
  146. var oldDiffItems = flattenDataDiffItems(oldList);
  147. var newDiffItems = flattenDataDiffItems(newList);
  148. function updateMorphingPathProps(from, to, rawFrom, rawTo, animationCfg) {
  149. if (rawFrom || from) {
  150. to.animateFrom({
  151. style: rawFrom && rawFrom !== from ? // dividingMethod like clone may override the style(opacity)
  152. // So extend it to raw style.
  153. extend(extend({}, rawFrom.style), from.style) : from.style
  154. }, animationCfg);
  155. }
  156. }
  157. function findKeyDim(items) {
  158. for (var i = 0; i < items.length; i++) {
  159. if (items[i].dim) {
  160. return items[i].dim;
  161. }
  162. }
  163. }
  164. var oldKeyDim = findKeyDim(oldDiffItems);
  165. var newKeyDim = findKeyDim(newDiffItems);
  166. var hasMorphAnimation = false;
  167. function createKeyGetter(isOld, onlyGetId) {
  168. return function (diffItem) {
  169. var data = diffItem.data;
  170. var dataIndex = diffItem.dataIndex; // TODO if specified dim
  171. if (onlyGetId) {
  172. return data.getId(dataIndex);
  173. } // Use group id as transition key by default.
  174. // So we can achieve multiple to multiple animation like drilldown / up naturally.
  175. // If group id not exits. Use id instead. If so, only one to one transition will be applied.
  176. var dataGroupId = data.hostModel && data.hostModel.get('dataGroupId'); // If specified key dimension(itemGroupId by default). Use this same dimension from other data.
  177. // PENDING: If only use key dimension of newData.
  178. var keyDim = isOld ? oldKeyDim || newKeyDim : newKeyDim || oldKeyDim;
  179. var dimInfo = keyDim && data.getDimensionInfo(keyDim);
  180. var dimOrdinalMeta = dimInfo && dimInfo.ordinalMeta;
  181. if (dimInfo) {
  182. // Get from encode.itemGroupId.
  183. var key = data.get(dimInfo.name, dataIndex);
  184. if (dimOrdinalMeta) {
  185. return dimOrdinalMeta.categories[key] || key + '';
  186. }
  187. return key + '';
  188. } // Get groupId from raw item. { groupId: '' }
  189. var itemVal = data.getRawDataItem(dataIndex);
  190. if (itemVal && itemVal.groupId) {
  191. return itemVal.groupId + '';
  192. }
  193. return dataGroupId || data.getId(dataIndex);
  194. };
  195. } // Use id if it's very likely to be an one to one animation
  196. // It's more robust than groupId
  197. // TODO Check if key dimension is specified.
  198. var useId = isAllIdSame(oldDiffItems, newDiffItems);
  199. var isElementStillInChart = {};
  200. if (!useId) {
  201. // We may have different diff strategy with basicTransition if we use other dimension as key.
  202. // If so, we can't simply check if oldEl is same with newEl. We need a map to check if oldEl is still being used in the new chart.
  203. // We can't use the elements that already being morphed. Let it keep it's original basic transition.
  204. for (var i = 0; i < newDiffItems.length; i++) {
  205. var newItem = newDiffItems[i];
  206. var el = newItem.data.getItemGraphicEl(newItem.dataIndex);
  207. if (el) {
  208. isElementStillInChart[el.id] = true;
  209. }
  210. }
  211. }
  212. function updateOneToOne(newIndex, oldIndex) {
  213. var oldItem = oldDiffItems[oldIndex];
  214. var newItem = newDiffItems[newIndex];
  215. var newSeries = newItem.data.hostModel; // TODO Mark this elements is morphed and don't morph them anymore
  216. var oldEl = oldItem.data.getItemGraphicEl(oldItem.dataIndex);
  217. var newEl = newItem.data.getItemGraphicEl(newItem.dataIndex); // Can't handle same elements.
  218. if (oldEl === newEl) {
  219. newEl && animateElementStyles(newEl, newItem.dataIndex, newSeries);
  220. return;
  221. }
  222. if ( // We can't use the elements that already being morphed
  223. oldEl && isElementStillInChart[oldEl.id]) {
  224. return;
  225. }
  226. if (newEl) {
  227. // TODO: If keep animating the group in case
  228. // some of the elements don't want to be morphed.
  229. // TODO Label?
  230. stopAnimation(newEl);
  231. if (oldEl) {
  232. stopAnimation(oldEl); // If old element is doing leaving animation. stop it and remove it immediately.
  233. removeEl(oldEl);
  234. hasMorphAnimation = true;
  235. applyMorphAnimation(getPathList(oldEl), getPathList(newEl), newItem.divide, newSeries, newIndex, updateMorphingPathProps);
  236. } else {
  237. fadeInElement(newEl, newSeries, newIndex);
  238. }
  239. } // else keep oldEl leaving animation.
  240. }
  241. new DataDiffer(oldDiffItems, newDiffItems, createKeyGetter(true, useId), createKeyGetter(false, useId), null, 'multiple').update(updateOneToOne).updateManyToOne(function (newIndex, oldIndices) {
  242. var newItem = newDiffItems[newIndex];
  243. var newData = newItem.data;
  244. var newSeries = newData.hostModel;
  245. var newEl = newData.getItemGraphicEl(newItem.dataIndex);
  246. var oldElsList = filter(map(oldIndices, function (idx) {
  247. return oldDiffItems[idx].data.getItemGraphicEl(oldDiffItems[idx].dataIndex);
  248. }), function (oldEl) {
  249. return oldEl && oldEl !== newEl && !isElementStillInChart[oldEl.id];
  250. });
  251. if (newEl) {
  252. stopAnimation(newEl);
  253. if (oldElsList.length) {
  254. // If old element is doing leaving animation. stop it and remove it immediately.
  255. each(oldElsList, function (oldEl) {
  256. stopAnimation(oldEl);
  257. removeEl(oldEl);
  258. });
  259. hasMorphAnimation = true;
  260. applyMorphAnimation(getPathList(oldElsList), getPathList(newEl), newItem.divide, newSeries, newIndex, updateMorphingPathProps);
  261. } else {
  262. fadeInElement(newEl, newSeries, newItem.dataIndex);
  263. }
  264. } // else keep oldEl leaving animation.
  265. }).updateOneToMany(function (newIndices, oldIndex) {
  266. var oldItem = oldDiffItems[oldIndex];
  267. var oldEl = oldItem.data.getItemGraphicEl(oldItem.dataIndex); // We can't use the elements that already being morphed
  268. if (oldEl && isElementStillInChart[oldEl.id]) {
  269. return;
  270. }
  271. var newElsList = filter(map(newIndices, function (idx) {
  272. return newDiffItems[idx].data.getItemGraphicEl(newDiffItems[idx].dataIndex);
  273. }), function (el) {
  274. return el && el !== oldEl;
  275. });
  276. var newSeris = newDiffItems[newIndices[0]].data.hostModel;
  277. if (newElsList.length) {
  278. each(newElsList, function (newEl) {
  279. return stopAnimation(newEl);
  280. });
  281. if (oldEl) {
  282. stopAnimation(oldEl); // If old element is doing leaving animation. stop it and remove it immediately.
  283. removeEl(oldEl);
  284. hasMorphAnimation = true;
  285. applyMorphAnimation(getPathList(oldEl), getPathList(newElsList), oldItem.divide, // Use divide on old.
  286. newSeris, newIndices[0], updateMorphingPathProps);
  287. } else {
  288. each(newElsList, function (newEl) {
  289. return fadeInElement(newEl, newSeris, newIndices[0]);
  290. });
  291. }
  292. } // else keep oldEl leaving animation.
  293. }).updateManyToMany(function (newIndices, oldIndices) {
  294. // If two data are same and both have groupId.
  295. // Normally they should be diff by id.
  296. new DataDiffer(oldIndices, newIndices, function (rawIdx) {
  297. return oldDiffItems[rawIdx].data.getId(oldDiffItems[rawIdx].dataIndex);
  298. }, function (rawIdx) {
  299. return newDiffItems[rawIdx].data.getId(newDiffItems[rawIdx].dataIndex);
  300. }).update(function (newIndex, oldIndex) {
  301. // Use the original index
  302. updateOneToOne(newIndices[newIndex], oldIndices[oldIndex]);
  303. }).execute();
  304. }).execute();
  305. if (hasMorphAnimation) {
  306. each(newList, function (_a) {
  307. var data = _a.data;
  308. var seriesModel = data.hostModel;
  309. var view = seriesModel && api.getViewOfSeriesModel(seriesModel);
  310. var animationCfg = getAnimationConfig('update', seriesModel, 0); // use 0 index.
  311. if (view && seriesModel.isAnimationEnabled() && animationCfg && animationCfg.duration > 0) {
  312. view.group.traverse(function (el) {
  313. if (el instanceof Path && !el.animators.length) {
  314. // We can't accept there still exists element that has no animation
  315. // if universalTransition is enabled
  316. el.animateFrom({
  317. style: {
  318. opacity: 0
  319. }
  320. }, animationCfg);
  321. }
  322. });
  323. }
  324. });
  325. }
  326. }
  327. function getSeriesTransitionKey(series) {
  328. var seriesKey = series.getModel('universalTransition').get('seriesKey');
  329. if (!seriesKey) {
  330. // Use series id by default.
  331. return series.id;
  332. }
  333. return seriesKey;
  334. }
  335. function convertArraySeriesKeyToString(seriesKey) {
  336. if (isArray(seriesKey)) {
  337. // Order independent.
  338. return seriesKey.sort().join(',');
  339. }
  340. return seriesKey;
  341. }
  342. function getDivideShapeFromData(data) {
  343. if (data.hostModel) {
  344. return data.hostModel.getModel('universalTransition').get('divideShape');
  345. }
  346. }
  347. function findTransitionSeriesBatches(globalStore, params) {
  348. var updateBatches = createHashMap();
  349. var oldDataMap = createHashMap(); // Map that only store key in array seriesKey.
  350. // Which is used to query the old data when transition from one to multiple series.
  351. var oldDataMapForSplit = createHashMap();
  352. each(globalStore.oldSeries, function (series, idx) {
  353. var oldData = globalStore.oldData[idx];
  354. var transitionKey = getSeriesTransitionKey(series);
  355. var transitionKeyStr = convertArraySeriesKeyToString(transitionKey);
  356. oldDataMap.set(transitionKeyStr, oldData);
  357. if (isArray(transitionKey)) {
  358. // Same key can't in different array seriesKey.
  359. each(transitionKey, function (key) {
  360. oldDataMapForSplit.set(key, {
  361. data: oldData,
  362. key: transitionKeyStr
  363. });
  364. });
  365. }
  366. });
  367. function checkTransitionSeriesKeyDuplicated(transitionKeyStr) {
  368. if (updateBatches.get(transitionKeyStr)) {
  369. warn("Duplicated seriesKey in universalTransition " + transitionKeyStr);
  370. }
  371. }
  372. each(params.updatedSeries, function (series) {
  373. if (series.isUniversalTransitionEnabled() && series.isAnimationEnabled()) {
  374. var newData = series.getData();
  375. var transitionKey = getSeriesTransitionKey(series);
  376. var transitionKeyStr = convertArraySeriesKeyToString(transitionKey); // Only transition between series with same id.
  377. var oldData = oldDataMap.get(transitionKeyStr); // string transition key is the best match.
  378. if (oldData) {
  379. if (process.env.NODE_ENV !== 'production') {
  380. checkTransitionSeriesKeyDuplicated(transitionKeyStr);
  381. } // TODO check if data is same?
  382. updateBatches.set(transitionKeyStr, {
  383. oldSeries: [{
  384. divide: getDivideShapeFromData(oldData),
  385. data: oldData
  386. }],
  387. newSeries: [{
  388. divide: getDivideShapeFromData(newData),
  389. data: newData
  390. }]
  391. });
  392. } else {
  393. // Transition from multiple series.
  394. if (isArray(transitionKey)) {
  395. if (process.env.NODE_ENV !== 'production') {
  396. checkTransitionSeriesKeyDuplicated(transitionKeyStr);
  397. }
  398. var oldSeries_1 = [];
  399. each(transitionKey, function (key) {
  400. var oldData = oldDataMap.get(key);
  401. if (oldData) {
  402. oldSeries_1.push({
  403. divide: getDivideShapeFromData(oldData),
  404. data: oldData
  405. });
  406. }
  407. });
  408. if (oldSeries_1.length) {
  409. updateBatches.set(transitionKeyStr, {
  410. oldSeries: oldSeries_1,
  411. newSeries: [{
  412. data: newData,
  413. divide: getDivideShapeFromData(newData)
  414. }]
  415. });
  416. }
  417. } else {
  418. // Try transition to multiple series.
  419. var oldData_1 = oldDataMapForSplit.get(transitionKey);
  420. if (oldData_1) {
  421. var batch = updateBatches.get(oldData_1.key);
  422. if (!batch) {
  423. batch = {
  424. oldSeries: [{
  425. data: oldData_1.data,
  426. divide: getDivideShapeFromData(oldData_1.data)
  427. }],
  428. newSeries: []
  429. };
  430. updateBatches.set(oldData_1.key, batch);
  431. }
  432. batch.newSeries.push({
  433. data: newData,
  434. divide: getDivideShapeFromData(newData)
  435. });
  436. }
  437. }
  438. }
  439. }
  440. });
  441. return updateBatches;
  442. }
  443. function querySeries(series, finder) {
  444. for (var i = 0; i < series.length; i++) {
  445. var found = finder.seriesIndex != null && finder.seriesIndex === series[i].seriesIndex || finder.seriesId != null && finder.seriesId === series[i].id;
  446. if (found) {
  447. return i;
  448. }
  449. }
  450. }
  451. function transitionSeriesFromOpt(transitionOpt, globalStore, params, api) {
  452. var from = [];
  453. var to = [];
  454. each(normalizeToArray(transitionOpt.from), function (finder) {
  455. var idx = querySeries(globalStore.oldSeries, finder);
  456. if (idx >= 0) {
  457. from.push({
  458. data: globalStore.oldData[idx],
  459. // TODO can specify divideShape in transition.
  460. divide: getDivideShapeFromData(globalStore.oldData[idx]),
  461. dim: finder.dimension
  462. });
  463. }
  464. });
  465. each(normalizeToArray(transitionOpt.to), function (finder) {
  466. var idx = querySeries(params.updatedSeries, finder);
  467. if (idx >= 0) {
  468. var data = params.updatedSeries[idx].getData();
  469. to.push({
  470. data: data,
  471. divide: getDivideShapeFromData(data),
  472. dim: finder.dimension
  473. });
  474. }
  475. });
  476. if (from.length > 0 && to.length > 0) {
  477. transitionBetween(from, to, api);
  478. }
  479. }
  480. export function installUniversalTransition(registers) {
  481. registers.registerUpdateLifecycle('series:beforeupdate', function (ecMOdel, api, params) {
  482. each(normalizeToArray(params.seriesTransition), function (transOpt) {
  483. each(normalizeToArray(transOpt.to), function (finder) {
  484. var series = params.updatedSeries;
  485. for (var i = 0; i < series.length; i++) {
  486. if (finder.seriesIndex != null && finder.seriesIndex === series[i].seriesIndex || finder.seriesId != null && finder.seriesId === series[i].id) {
  487. series[i][SERIES_UNIVERSAL_TRANSITION_PROP] = true;
  488. }
  489. }
  490. });
  491. });
  492. });
  493. registers.registerUpdateLifecycle('series:transition', function (ecModel, api, params) {
  494. // TODO api provide an namespace that can save stuff per instance
  495. var globalStore = getUniversalTransitionGlobalStore(api); // TODO multiple to multiple series.
  496. if (globalStore.oldSeries && params.updatedSeries && params.optionChanged) {
  497. // Use give transition config if its' give;
  498. var transitionOpt = params.seriesTransition;
  499. if (transitionOpt) {
  500. each(normalizeToArray(transitionOpt), function (opt) {
  501. transitionSeriesFromOpt(opt, globalStore, params, api);
  502. });
  503. } else {
  504. // Else guess from series based on transition series key.
  505. var updateBatches_1 = findTransitionSeriesBatches(globalStore, params);
  506. each(updateBatches_1.keys(), function (key) {
  507. var batch = updateBatches_1.get(key);
  508. transitionBetween(batch.oldSeries, batch.newSeries, api);
  509. });
  510. } // Reset
  511. each(params.updatedSeries, function (series) {
  512. // Reset;
  513. if (series[SERIES_UNIVERSAL_TRANSITION_PROP]) {
  514. series[SERIES_UNIVERSAL_TRANSITION_PROP] = false;
  515. }
  516. });
  517. } // Save all series of current update. Not only the updated one.
  518. var allSeries = ecModel.getSeries();
  519. var savedSeries = globalStore.oldSeries = [];
  520. var savedData = globalStore.oldData = [];
  521. for (var i = 0; i < allSeries.length; i++) {
  522. var data = allSeries[i].getData(); // Only save the data that can have transition.
  523. // Avoid large data costing too much extra memory
  524. if (data.count() < DATA_COUNT_THRESHOLD) {
  525. savedSeries.push(allSeries[i]);
  526. savedData.push(data);
  527. }
  528. }
  529. });
  530. }