InsideZoomView.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. var DataZoomView = require("./DataZoomView");
  21. var sliderMove = require("../helper/sliderMove");
  22. var roams = require("./roams");
  23. /*
  24. * Licensed to the Apache Software Foundation (ASF) under one
  25. * or more contributor license agreements. See the NOTICE file
  26. * distributed with this work for additional information
  27. * regarding copyright ownership. The ASF licenses this file
  28. * to you under the Apache License, Version 2.0 (the
  29. * "License"); you may not use this file except in compliance
  30. * with the License. You may obtain a copy of the License at
  31. *
  32. * http://www.apache.org/licenses/LICENSE-2.0
  33. *
  34. * Unless required by applicable law or agreed to in writing,
  35. * software distributed under the License is distributed on an
  36. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  37. * KIND, either express or implied. See the License for the
  38. * specific language governing permissions and limitations
  39. * under the License.
  40. */
  41. var bind = zrUtil.bind;
  42. var InsideZoomView = DataZoomView.extend({
  43. type: 'dataZoom.inside',
  44. /**
  45. * @override
  46. */
  47. init: function (ecModel, api) {
  48. /**
  49. * 'throttle' is used in this.dispatchAction, so we save range
  50. * to avoid missing some 'pan' info.
  51. * @private
  52. * @type {Array.<number>}
  53. */
  54. this._range;
  55. },
  56. /**
  57. * @override
  58. */
  59. render: function (dataZoomModel, ecModel, api, payload) {
  60. InsideZoomView.superApply(this, 'render', arguments); // Hence the `throttle` util ensures to preserve command order,
  61. // here simply updating range all the time will not cause missing
  62. // any of the the roam change.
  63. this._range = dataZoomModel.getPercentRange(); // Reset controllers.
  64. zrUtil.each(this.getTargetCoordInfo(), function (coordInfoList, coordSysName) {
  65. var allCoordIds = zrUtil.map(coordInfoList, function (coordInfo) {
  66. return roams.generateCoordId(coordInfo.model);
  67. });
  68. zrUtil.each(coordInfoList, function (coordInfo) {
  69. var coordModel = coordInfo.model;
  70. var getRange = {};
  71. zrUtil.each(['pan', 'zoom', 'scrollMove'], function (eventName) {
  72. getRange[eventName] = bind(roamHandlers[eventName], this, coordInfo, coordSysName);
  73. }, this);
  74. roams.register(api, {
  75. coordId: roams.generateCoordId(coordModel),
  76. allCoordIds: allCoordIds,
  77. containsPoint: function (e, x, y) {
  78. return coordModel.coordinateSystem.containPoint([x, y]);
  79. },
  80. dataZoomId: dataZoomModel.id,
  81. dataZoomModel: dataZoomModel,
  82. getRange: getRange
  83. });
  84. }, this);
  85. }, this);
  86. },
  87. /**
  88. * @override
  89. */
  90. dispose: function () {
  91. roams.unregister(this.api, this.dataZoomModel.id);
  92. InsideZoomView.superApply(this, 'dispose', arguments);
  93. this._range = null;
  94. }
  95. });
  96. var roamHandlers = {
  97. /**
  98. * @this {module:echarts/component/dataZoom/InsideZoomView}
  99. */
  100. zoom: function (coordInfo, coordSysName, controller, e) {
  101. var lastRange = this._range;
  102. var range = lastRange.slice(); // Calculate transform by the first axis.
  103. var axisModel = coordInfo.axisModels[0];
  104. if (!axisModel) {
  105. return;
  106. }
  107. var directionInfo = getDirectionInfo[coordSysName](null, [e.originX, e.originY], axisModel, controller, coordInfo);
  108. var percentPoint = (directionInfo.signal > 0 ? directionInfo.pixelStart + directionInfo.pixelLength - directionInfo.pixel : directionInfo.pixel - directionInfo.pixelStart) / directionInfo.pixelLength * (range[1] - range[0]) + range[0];
  109. var scale = Math.max(1 / e.scale, 0);
  110. range[0] = (range[0] - percentPoint) * scale + percentPoint;
  111. range[1] = (range[1] - percentPoint) * scale + percentPoint; // Restrict range.
  112. var minMaxSpan = this.dataZoomModel.findRepresentativeAxisProxy().getMinMaxSpan();
  113. sliderMove(0, range, [0, 100], 0, minMaxSpan.minSpan, minMaxSpan.maxSpan);
  114. this._range = range;
  115. if (lastRange[0] !== range[0] || lastRange[1] !== range[1]) {
  116. return range;
  117. }
  118. },
  119. /**
  120. * @this {module:echarts/component/dataZoom/InsideZoomView}
  121. */
  122. pan: makeMover(function (range, axisModel, coordInfo, coordSysName, controller, e) {
  123. var directionInfo = getDirectionInfo[coordSysName]([e.oldX, e.oldY], [e.newX, e.newY], axisModel, controller, coordInfo);
  124. return directionInfo.signal * (range[1] - range[0]) * directionInfo.pixel / directionInfo.pixelLength;
  125. }),
  126. /**
  127. * @this {module:echarts/component/dataZoom/InsideZoomView}
  128. */
  129. scrollMove: makeMover(function (range, axisModel, coordInfo, coordSysName, controller, e) {
  130. var directionInfo = getDirectionInfo[coordSysName]([0, 0], [e.scrollDelta, e.scrollDelta], axisModel, controller, coordInfo);
  131. return directionInfo.signal * (range[1] - range[0]) * e.scrollDelta;
  132. })
  133. };
  134. function makeMover(getPercentDelta) {
  135. return function (coordInfo, coordSysName, controller, e) {
  136. var lastRange = this._range;
  137. var range = lastRange.slice(); // Calculate transform by the first axis.
  138. var axisModel = coordInfo.axisModels[0];
  139. if (!axisModel) {
  140. return;
  141. }
  142. var percentDelta = getPercentDelta(range, axisModel, coordInfo, coordSysName, controller, e);
  143. sliderMove(percentDelta, range, [0, 100], 'all');
  144. this._range = range;
  145. if (lastRange[0] !== range[0] || lastRange[1] !== range[1]) {
  146. return range;
  147. }
  148. };
  149. }
  150. var getDirectionInfo = {
  151. grid: function (oldPoint, newPoint, axisModel, controller, coordInfo) {
  152. var axis = axisModel.axis;
  153. var ret = {};
  154. var rect = coordInfo.model.coordinateSystem.getRect();
  155. oldPoint = oldPoint || [0, 0];
  156. if (axis.dim === 'x') {
  157. ret.pixel = newPoint[0] - oldPoint[0];
  158. ret.pixelLength = rect.width;
  159. ret.pixelStart = rect.x;
  160. ret.signal = axis.inverse ? 1 : -1;
  161. } else {
  162. // axis.dim === 'y'
  163. ret.pixel = newPoint[1] - oldPoint[1];
  164. ret.pixelLength = rect.height;
  165. ret.pixelStart = rect.y;
  166. ret.signal = axis.inverse ? -1 : 1;
  167. }
  168. return ret;
  169. },
  170. polar: function (oldPoint, newPoint, axisModel, controller, coordInfo) {
  171. var axis = axisModel.axis;
  172. var ret = {};
  173. var polar = coordInfo.model.coordinateSystem;
  174. var radiusExtent = polar.getRadiusAxis().getExtent();
  175. var angleExtent = polar.getAngleAxis().getExtent();
  176. oldPoint = oldPoint ? polar.pointToCoord(oldPoint) : [0, 0];
  177. newPoint = polar.pointToCoord(newPoint);
  178. if (axisModel.mainType === 'radiusAxis') {
  179. ret.pixel = newPoint[0] - oldPoint[0]; // ret.pixelLength = Math.abs(radiusExtent[1] - radiusExtent[0]);
  180. // ret.pixelStart = Math.min(radiusExtent[0], radiusExtent[1]);
  181. ret.pixelLength = radiusExtent[1] - radiusExtent[0];
  182. ret.pixelStart = radiusExtent[0];
  183. ret.signal = axis.inverse ? 1 : -1;
  184. } else {
  185. // 'angleAxis'
  186. ret.pixel = newPoint[1] - oldPoint[1]; // ret.pixelLength = Math.abs(angleExtent[1] - angleExtent[0]);
  187. // ret.pixelStart = Math.min(angleExtent[0], angleExtent[1]);
  188. ret.pixelLength = angleExtent[1] - angleExtent[0];
  189. ret.pixelStart = angleExtent[0];
  190. ret.signal = axis.inverse ? -1 : 1;
  191. }
  192. return ret;
  193. },
  194. singleAxis: function (oldPoint, newPoint, axisModel, controller, coordInfo) {
  195. var axis = axisModel.axis;
  196. var rect = coordInfo.model.coordinateSystem.getRect();
  197. var ret = {};
  198. oldPoint = oldPoint || [0, 0];
  199. if (axis.orient === 'horizontal') {
  200. ret.pixel = newPoint[0] - oldPoint[0];
  201. ret.pixelLength = rect.width;
  202. ret.pixelStart = rect.x;
  203. ret.signal = axis.inverse ? 1 : -1;
  204. } else {
  205. // 'vertical'
  206. ret.pixel = newPoint[1] - oldPoint[1];
  207. ret.pixelLength = rect.height;
  208. ret.pixelStart = rect.y;
  209. ret.signal = axis.inverse ? -1 : 1;
  210. }
  211. return ret;
  212. }
  213. };
  214. var _default = InsideZoomView;
  215. module.exports = _default;