roamHelper.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. * Licensed to the Apache Software Foundation (ASF) under one
  21. * or more contributor license agreements. See the NOTICE file
  22. * distributed with this work for additional information
  23. * regarding copyright ownership. The ASF licenses this file
  24. * to you under the Apache License, Version 2.0 (the
  25. * "License"); you may not use this file except in compliance
  26. * with the License. You may obtain a copy of the License at
  27. *
  28. * http://www.apache.org/licenses/LICENSE-2.0
  29. *
  30. * Unless required by applicable law or agreed to in writing,
  31. * software distributed under the License is distributed on an
  32. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  33. * KIND, either express or implied. See the License for the
  34. * specific language governing permissions and limitations
  35. * under the License.
  36. */
  37. /**
  38. * @param {module:echarts/coord/View} view
  39. * @param {Object} payload
  40. * @param {Object} [zoomLimit]
  41. */
  42. function updateCenterAndZoom(view, payload, zoomLimit) {
  43. var previousZoom = view.getZoom();
  44. var center = view.getCenter();
  45. var zoom = payload.zoom;
  46. var point = view.dataToPoint(center);
  47. if (payload.dx != null && payload.dy != null) {
  48. point[0] -= payload.dx;
  49. point[1] -= payload.dy;
  50. var center = view.pointToData(point);
  51. view.setCenter(center);
  52. }
  53. if (zoom != null) {
  54. if (zoomLimit) {
  55. var zoomMin = zoomLimit.min || 0;
  56. var zoomMax = zoomLimit.max || Infinity;
  57. zoom = Math.max(Math.min(previousZoom * zoom, zoomMax), zoomMin) / previousZoom;
  58. } // Zoom on given point(originX, originY)
  59. view.scale[0] *= zoom;
  60. view.scale[1] *= zoom;
  61. var position = view.position;
  62. var fixX = (payload.originX - position[0]) * (zoom - 1);
  63. var fixY = (payload.originY - position[1]) * (zoom - 1);
  64. position[0] -= fixX;
  65. position[1] -= fixY;
  66. view.updateTransform(); // Get the new center
  67. var center = view.pointToData(point);
  68. view.setCenter(center);
  69. view.setZoom(zoom * previousZoom);
  70. }
  71. return {
  72. center: view.getCenter(),
  73. zoom: view.getZoom()
  74. };
  75. }
  76. exports.updateCenterAndZoom = updateCenterAndZoom;