lineAnimationDiff.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. var _helper = require("./helper");
  20. var prepareDataCoordInfo = _helper.prepareDataCoordInfo;
  21. var getStackedOnPoint = _helper.getStackedOnPoint;
  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. // var arrayDiff = require('zrender/src/core/arrayDiff');
  41. // 'zrender/src/core/arrayDiff' has been used before, but it did
  42. // not do well in performance when roam with fixed dataZoom window.
  43. // function convertToIntId(newIdList, oldIdList) {
  44. // // Generate int id instead of string id.
  45. // // Compare string maybe slow in score function of arrDiff
  46. // // Assume id in idList are all unique
  47. // var idIndicesMap = {};
  48. // var idx = 0;
  49. // for (var i = 0; i < newIdList.length; i++) {
  50. // idIndicesMap[newIdList[i]] = idx;
  51. // newIdList[i] = idx++;
  52. // }
  53. // for (var i = 0; i < oldIdList.length; i++) {
  54. // var oldId = oldIdList[i];
  55. // // Same with newIdList
  56. // if (idIndicesMap[oldId]) {
  57. // oldIdList[i] = idIndicesMap[oldId];
  58. // }
  59. // else {
  60. // oldIdList[i] = idx++;
  61. // }
  62. // }
  63. // }
  64. function diffData(oldData, newData) {
  65. var diffResult = [];
  66. newData.diff(oldData).add(function (idx) {
  67. diffResult.push({
  68. cmd: '+',
  69. idx: idx
  70. });
  71. }).update(function (newIdx, oldIdx) {
  72. diffResult.push({
  73. cmd: '=',
  74. idx: oldIdx,
  75. idx1: newIdx
  76. });
  77. }).remove(function (idx) {
  78. diffResult.push({
  79. cmd: '-',
  80. idx: idx
  81. });
  82. }).execute();
  83. return diffResult;
  84. }
  85. function _default(oldData, newData, oldStackedOnPoints, newStackedOnPoints, oldCoordSys, newCoordSys, oldValueOrigin, newValueOrigin) {
  86. var diff = diffData(oldData, newData); // var newIdList = newData.mapArray(newData.getId);
  87. // var oldIdList = oldData.mapArray(oldData.getId);
  88. // convertToIntId(newIdList, oldIdList);
  89. // // FIXME One data ?
  90. // diff = arrayDiff(oldIdList, newIdList);
  91. var currPoints = [];
  92. var nextPoints = []; // Points for stacking base line
  93. var currStackedPoints = [];
  94. var nextStackedPoints = [];
  95. var status = [];
  96. var sortedIndices = [];
  97. var rawIndices = [];
  98. var newDataOldCoordInfo = prepareDataCoordInfo(oldCoordSys, newData, oldValueOrigin);
  99. var oldDataNewCoordInfo = prepareDataCoordInfo(newCoordSys, oldData, newValueOrigin);
  100. for (var i = 0; i < diff.length; i++) {
  101. var diffItem = diff[i];
  102. var pointAdded = true; // FIXME, animation is not so perfect when dataZoom window moves fast
  103. // Which is in case remvoing or add more than one data in the tail or head
  104. switch (diffItem.cmd) {
  105. case '=':
  106. var currentPt = oldData.getItemLayout(diffItem.idx);
  107. var nextPt = newData.getItemLayout(diffItem.idx1); // If previous data is NaN, use next point directly
  108. if (isNaN(currentPt[0]) || isNaN(currentPt[1])) {
  109. currentPt = nextPt.slice();
  110. }
  111. currPoints.push(currentPt);
  112. nextPoints.push(nextPt);
  113. currStackedPoints.push(oldStackedOnPoints[diffItem.idx]);
  114. nextStackedPoints.push(newStackedOnPoints[diffItem.idx1]);
  115. rawIndices.push(newData.getRawIndex(diffItem.idx1));
  116. break;
  117. case '+':
  118. var idx = diffItem.idx;
  119. currPoints.push(oldCoordSys.dataToPoint([newData.get(newDataOldCoordInfo.dataDimsForPoint[0], idx), newData.get(newDataOldCoordInfo.dataDimsForPoint[1], idx)]));
  120. nextPoints.push(newData.getItemLayout(idx).slice());
  121. currStackedPoints.push(getStackedOnPoint(newDataOldCoordInfo, oldCoordSys, newData, idx));
  122. nextStackedPoints.push(newStackedOnPoints[idx]);
  123. rawIndices.push(newData.getRawIndex(idx));
  124. break;
  125. case '-':
  126. var idx = diffItem.idx;
  127. var rawIndex = oldData.getRawIndex(idx); // Data is replaced. In the case of dynamic data queue
  128. // FIXME FIXME FIXME
  129. if (rawIndex !== idx) {
  130. currPoints.push(oldData.getItemLayout(idx));
  131. nextPoints.push(newCoordSys.dataToPoint([oldData.get(oldDataNewCoordInfo.dataDimsForPoint[0], idx), oldData.get(oldDataNewCoordInfo.dataDimsForPoint[1], idx)]));
  132. currStackedPoints.push(oldStackedOnPoints[idx]);
  133. nextStackedPoints.push(getStackedOnPoint(oldDataNewCoordInfo, newCoordSys, oldData, idx));
  134. rawIndices.push(rawIndex);
  135. } else {
  136. pointAdded = false;
  137. }
  138. } // Original indices
  139. if (pointAdded) {
  140. status.push(diffItem);
  141. sortedIndices.push(sortedIndices.length);
  142. }
  143. } // Diff result may be crossed if all items are changed
  144. // Sort by data index
  145. sortedIndices.sort(function (a, b) {
  146. return rawIndices[a] - rawIndices[b];
  147. });
  148. var sortedCurrPoints = [];
  149. var sortedNextPoints = [];
  150. var sortedCurrStackedPoints = [];
  151. var sortedNextStackedPoints = [];
  152. var sortedStatus = [];
  153. for (var i = 0; i < sortedIndices.length; i++) {
  154. var idx = sortedIndices[i];
  155. sortedCurrPoints[i] = currPoints[idx];
  156. sortedNextPoints[i] = nextPoints[idx];
  157. sortedCurrStackedPoints[i] = currStackedPoints[idx];
  158. sortedNextStackedPoints[i] = nextStackedPoints[idx];
  159. sortedStatus[i] = status[idx];
  160. }
  161. return {
  162. current: sortedCurrPoints,
  163. next: sortedNextPoints,
  164. stackedOnCurrent: sortedCurrStackedPoints,
  165. stackedOnNext: sortedNextStackedPoints,
  166. status: sortedStatus
  167. };
  168. }
  169. module.exports = _default;