context.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. "use strict";
  2. exports.__esModule = true;
  3. exports["default"] = void 0;
  4. var _scale = require("../../scale");
  5. var _array = require("../../util/array");
  6. var _const = require("../../chart/const");
  7. var _common = require("../../util/common");
  8. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  9. // 判断新老values是否相等,这里只要判断前后是否相等即可
  10. function isValuesEqual(values, newValues) {
  11. if (values.length !== newValues.length) {
  12. return false;
  13. }
  14. var lastIndex = values.length - 1;
  15. return values[0] === newValues[0] && values[lastIndex] === newValues[lastIndex];
  16. } // 不同交互之间共享的上下文
  17. var defaultRange = [0, 1];
  18. var Context = /*#__PURE__*/function () {
  19. // 最开始的原始值
  20. // 当前显示的范围
  21. // 缩放最小的点数
  22. // 最小的缩放比例, 默认通过minCount计算
  23. // minScale = 0.01;
  24. // 交互开始时,ticks个数,主要为了每次缩放后,更新ticks个数
  25. // lastTickCount;
  26. function Context(chart) {
  27. var _this = this;
  28. _defineProperty(this, "chart", null);
  29. _defineProperty(this, "values", null);
  30. _defineProperty(this, "range", defaultRange);
  31. _defineProperty(this, "startRange", defaultRange);
  32. _defineProperty(this, "minCount", 10);
  33. _defineProperty(this, "_afterinit", function () {
  34. // 初始化value值
  35. var scale = _this.getPinchScale(); // 记录原始全量数据
  36. var values = [].concat(scale.values);
  37. _this.values = values; // 最小的缩放比例
  38. if (!_this.minScale) {
  39. _this.minScale = _this.minCount / values.length;
  40. } // 初始化的时候有设置range,则初始化成默认比例
  41. if (_this.range !== defaultRange) {
  42. _this.updateRange(_this.range);
  43. _this.updateTicks();
  44. }
  45. });
  46. _defineProperty(this, "_afterdatachange", function () {
  47. _this.updateRange(_this.range);
  48. });
  49. this.chart = chart;
  50. this._initEvent(chart);
  51. }
  52. var _proto = Context.prototype;
  53. _proto._initEvent = function _initEvent(chart) {
  54. // 在整体初始化后还需要设置一些初始状态
  55. chart.on(_const.EVENT_AFTER_INIT, this._afterinit);
  56. chart.on(_const.EVENT_AFTER_DATA_CHANGE, this._afterdatachange);
  57. } // 缩放的主轴scale
  58. ;
  59. _proto.getPinchScale = function getPinchScale() {
  60. var chart = this.chart; // 默认缩放x轴
  61. var scale = chart.getXScale();
  62. return scale;
  63. } // 跟随轴的scale
  64. ;
  65. _proto.getFollowScale = function getFollowScale() {
  66. var chart = this.chart; // 默认缩放x轴
  67. var scales = chart.getYScales() || [];
  68. return scales[0];
  69. };
  70. _proto.start = function start() {
  71. var range = this.range;
  72. var scale = this.getPinchScale();
  73. var start = range[0],
  74. end = range[1]; // 记录交互起始的范围
  75. this.startRange = [start, end]; // 记录开始时的ticks个数
  76. this.lastTickCount = scale.tickCount;
  77. };
  78. _proto.doZoom = function doZoom(leftScale, rightScale, zoom) {
  79. var range = this.startRange,
  80. minScale = this.minScale;
  81. var start = range[0],
  82. end = range[1];
  83. var zoomOffset = 1 - zoom;
  84. var rangeLen = end - start;
  85. var rangeOffset = rangeLen * zoomOffset;
  86. var leftOffset = rangeOffset * leftScale;
  87. var rightOffset = rangeOffset * rightScale;
  88. var newStart = Math.max(0, start - leftOffset);
  89. var newEnd = Math.min(1, end + rightOffset);
  90. var newRange = [newStart, newEnd]; // 如果已经到了最小比例,则不能再继续再放大
  91. if (newEnd - newStart < minScale) {
  92. return;
  93. }
  94. this.updateRange(newRange);
  95. };
  96. _proto.doMove = function doMove(ratio) {
  97. // 不管是0, 还是其他,都不用处理
  98. if (!ratio) return;
  99. var range = this.startRange;
  100. var start = range[0],
  101. end = range[1];
  102. var rangeLen = end - start;
  103. var rangeOffset = rangeLen * ratio;
  104. var newStart = start - rangeOffset;
  105. var newEnd = end - rangeOffset; // 处理边界值
  106. var newRange;
  107. if (newStart < 0) {
  108. newRange = [0, rangeLen];
  109. } else if (newEnd > 1) {
  110. newRange = [1 - rangeLen, 1];
  111. } else {
  112. newRange = [newStart, newEnd];
  113. }
  114. this.updateRange(newRange);
  115. };
  116. _proto.updateRange = function updateRange(range) {
  117. var values = this.values; // 0, 1 的范围之间
  118. var start = range[0],
  119. end = range[1]; // start 不能小于0
  120. start = Math.max(0, start); // end 不能大于1
  121. end = Math.min(1, end); // 设置当前的范围
  122. this.range = [start, end];
  123. var len = values.length;
  124. var valueStart = start * len;
  125. var valueEnd = end * len; // 从原始数据里截取需要显示的数据
  126. var newValues = values.slice(valueStart, valueEnd);
  127. this.repaint(newValues);
  128. };
  129. _proto.repaint = function repaint(newValues) {
  130. var chart = this.chart;
  131. var scale = this.getPinchScale();
  132. var currentValues = scale.values,
  133. ticks = scale.ticks; // 如果新数组和当前显示的数组相同,则不更新
  134. if (isValuesEqual(currentValues, newValues)) {
  135. return;
  136. } // 更新主轴values
  137. this.updateScale(scale, {
  138. ticks: ticks,
  139. values: newValues
  140. });
  141. this.updateFollowScale(scale, newValues);
  142. chart.repaint();
  143. };
  144. _proto.updateFollowScale = function updateFollowScale(pinchScale, pinchValues) {
  145. var chart = this.chart;
  146. var followScale = this.getFollowScale();
  147. var pinchField = pinchScale.field,
  148. pinchScaleType = pinchScale.type;
  149. var followField = followScale.field; // 根据主轴的value值,找到所有从轴的value值
  150. var values = []; // 转成map,让查找性能更高
  151. var pinchValueMap = {};
  152. pinchValues.forEach(function (item) {
  153. pinchValueMap[item] = true;
  154. });
  155. var data = chart.get('data');
  156. data.forEach(function (item) {
  157. if (pinchScaleType === 'timeCat') {
  158. var value = (0, _common.toTimeStamp)(item[pinchField]);
  159. if (pinchValueMap[value]) {
  160. values.push(item[followField]);
  161. }
  162. }
  163. });
  164. var _getRange = (0, _array.getRange)(values),
  165. min = _getRange.min,
  166. max = _getRange.max;
  167. this.updateScale(followScale, {
  168. min: min,
  169. max: max,
  170. nice: true
  171. });
  172. };
  173. _proto.updateScale = function updateScale(scale, cfg) {
  174. if (!scale) {
  175. return;
  176. }
  177. scale.change(cfg);
  178. } // 上一次的tick个数
  179. ;
  180. _proto.updateTicks = function updateTicks() {
  181. var chart = this.chart,
  182. values = this.values;
  183. var scale = this.getPinchScale();
  184. var currentValues = scale.values,
  185. tickCount = scale.tickCount; // 根据当前数据的比例,和定义的tickCount计算应该需要多少个ticks
  186. var newTickCount = Math.round(tickCount * values.length / currentValues.length);
  187. var catTicks = (0, _scale.getTickMethod)('cat');
  188. var ticks = catTicks({
  189. tickCount: newTickCount,
  190. values: values
  191. });
  192. this.updateScale(scale, {
  193. ticks: ticks,
  194. values: currentValues
  195. }); // 更新完后,需要重新绘制一次
  196. chart.repaint();
  197. };
  198. _proto.destroy = function destroy() {
  199. var chart = this.chart;
  200. chart.off(_const.EVENT_AFTER_INIT, this._afterinit);
  201. chart.off(_const.EVENT_AFTER_DATA_CHANGE, this._afterdatachange);
  202. };
  203. return Context;
  204. }();
  205. var _default = Context;
  206. exports["default"] = _default;