history.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 zrUtil = require("zrender/lib/core/util");
  20. /*
  21. * Licensed to the Apache Software Foundation (ASF) under one
  22. * or more contributor license agreements. See the NOTICE file
  23. * distributed with this work for additional information
  24. * regarding copyright ownership. The ASF licenses this file
  25. * to you under the Apache License, Version 2.0 (the
  26. * "License"); you may not use this file except in compliance
  27. * with the License. You may obtain a copy of the License at
  28. *
  29. * http://www.apache.org/licenses/LICENSE-2.0
  30. *
  31. * Unless required by applicable law or agreed to in writing,
  32. * software distributed under the License is distributed on an
  33. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  34. * KIND, either express or implied. See the License for the
  35. * specific language governing permissions and limitations
  36. * under the License.
  37. */
  38. var each = zrUtil.each;
  39. var ATTR = '\0_ec_hist_store';
  40. /**
  41. * @param {module:echarts/model/Global} ecModel
  42. * @param {Object} newSnapshot {dataZoomId, batch: [payloadInfo, ...]}
  43. */
  44. function push(ecModel, newSnapshot) {
  45. var store = giveStore(ecModel); // If previous dataZoom can not be found,
  46. // complete an range with current range.
  47. each(newSnapshot, function (batchItem, dataZoomId) {
  48. var i = store.length - 1;
  49. for (; i >= 0; i--) {
  50. var snapshot = store[i];
  51. if (snapshot[dataZoomId]) {
  52. break;
  53. }
  54. }
  55. if (i < 0) {
  56. // No origin range set, create one by current range.
  57. var dataZoomModel = ecModel.queryComponents({
  58. mainType: 'dataZoom',
  59. subType: 'select',
  60. id: dataZoomId
  61. })[0];
  62. if (dataZoomModel) {
  63. var percentRange = dataZoomModel.getPercentRange();
  64. store[0][dataZoomId] = {
  65. dataZoomId: dataZoomId,
  66. start: percentRange[0],
  67. end: percentRange[1]
  68. };
  69. }
  70. }
  71. });
  72. store.push(newSnapshot);
  73. }
  74. /**
  75. * @param {module:echarts/model/Global} ecModel
  76. * @return {Object} snapshot
  77. */
  78. function pop(ecModel) {
  79. var store = giveStore(ecModel);
  80. var head = store[store.length - 1];
  81. store.length > 1 && store.pop(); // Find top for all dataZoom.
  82. var snapshot = {};
  83. each(head, function (batchItem, dataZoomId) {
  84. for (var i = store.length - 1; i >= 0; i--) {
  85. var batchItem = store[i][dataZoomId];
  86. if (batchItem) {
  87. snapshot[dataZoomId] = batchItem;
  88. break;
  89. }
  90. }
  91. });
  92. return snapshot;
  93. }
  94. /**
  95. * @param {module:echarts/model/Global} ecModel
  96. */
  97. function clear(ecModel) {
  98. ecModel[ATTR] = null;
  99. }
  100. /**
  101. * @param {module:echarts/model/Global} ecModel
  102. * @return {number} records. always >= 1.
  103. */
  104. function count(ecModel) {
  105. return giveStore(ecModel).length;
  106. }
  107. /**
  108. * [{key: dataZoomId, value: {dataZoomId, range}}, ...]
  109. * History length of each dataZoom may be different.
  110. * this._history[0] is used to store origin range.
  111. * @type {Array.<Object>}
  112. */
  113. function giveStore(ecModel) {
  114. var store = ecModel[ATTR];
  115. if (!store) {
  116. store = ecModel[ATTR] = [{}];
  117. }
  118. return store;
  119. }
  120. exports.push = push;
  121. exports.pop = pop;
  122. exports.clear = clear;
  123. exports.count = count;