Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

fix zoom resets on resize (#67420)

Aleksandr Lesnenko07e256059641
diff --git a/e2e/test/scenarios/visualizations-charts/maps.cy.spec.js b/e2e/test/scenarios/visualizations-charts/maps.cy.spec.js
index 26933313570..454703d235d 100644
--- a/e2e/test/scenarios/visualizations-charts/maps.cy.spec.js
+++ b/e2e/test/scenarios/visualizations-charts/maps.cy.spec.js
@@ -134,6 +134,59 @@ describe("scenarios > visualizations > maps", () => {
H.popover().findByText("Blastoise").should("be.visible");
});
+ it("should preserve zoom and pan after resize (metabase#11211)", () => {
+ cy.viewport(800, 600);
+
+ H.visitQuestionAdhoc({
+ dataset_query: {
+ type: "query",
+ database: SAMPLE_DB_ID,
+ query: {
+ "source-table": PEOPLE_ID,
+ limit: 999,
+ },
+ },
+ display: "map",
+ visualization_settings: {
+ "map.type": "pin",
+ "map.latitude_column": "LATITUDE",
+ "map.longitude_column": "LONGITUDE",
+ "map.center_latitude": 40,
+ "map.center_longitude": -100,
+ "map.zoom": 4,
+ },
+ });
+
+ zoomIn(4);
+
+ cy.get(".leaflet-marker-icon")
+ .first()
+ .then(($marker) => {
+ const posAfterZoom = $marker[0].getBoundingClientRect();
+
+ // 1px resize should not reset zoom
+ cy.viewport(801, 600);
+ cy.wait(300);
+
+ cy.get(".leaflet-marker-icon")
+ .first()
+ .then(($markerAfterResize) => {
+ const posAfterResize =
+ $markerAfterResize[0].getBoundingClientRect();
+ // Position should be nearly identical (within 5px tolerance)
+ const tolerance = 5;
+ expect(posAfterResize.left).to.be.closeTo(
+ posAfterZoom.left,
+ tolerance,
+ );
+ expect(posAfterResize.top).to.be.closeTo(
+ posAfterZoom.top,
+ tolerance,
+ );
+ });
+ });
+ });
+
it("should not assign the full name of the state as the filter value on a drill-through (metabase#14650)", () => {
cy.intercept("/app/assets/geojson/**").as("geojson");
H.visitQuestionAdhoc({
@@ -465,3 +518,10 @@ function toggleFieldSelectElement(field) {
cy.findByTestId("chart-setting-select").click();
});
}
+
+function zoomIn(times) {
+ for (let i = 0; i < times; i++) {
+ cy.get(".leaflet-control-zoom-in").click();
+ cy.wait(200);
+ }
+}
diff --git a/frontend/src/metabase/visualizations/components/LeafletMap.jsx b/frontend/src/metabase/visualizations/components/LeafletMap.jsx
index 5a293b0ccd5..6ac59b5bc73 100644
--- a/frontend/src/metabase/visualizations/components/LeafletMap.jsx
+++ b/frontend/src/metabase/visualizations/components/LeafletMap.jsx
@@ -84,54 +84,76 @@ export default class LeafletMap extends Component {
}
componentDidUpdate(prevProps) {
- const { bounds, settings, zoomControl } = this.props;
+ const { bounds, settings, zoomControl, zoom, lat, lng } = this.props;
- if (zoomControl === false) {
- this.map.removeControl(this.map.zoomControl);
- } else {
- this.map.addControl(this.map.zoomControl);
+ if (prevProps.zoomControl !== zoomControl) {
+ if (zoomControl === false) {
+ this.map.zoomControl?.remove();
+ } else if (this.map.zoomControl) {
+ this.map.zoomControl.addTo(this.map);
+ }
+ }
+
+ const isInitialUpdate = !prevProps;
+ const pointsChanged = prevProps && prevProps.points !== this.props.points;
+ const dimensionsChanged =
+ prevProps &&
+ (prevProps.width !== this.props.width ||
+ prevProps.height !== this.props.height);
+
+ if (!isInitialUpdate && !pointsChanged && !dimensionsChanged) {
+ return;
}
- if (
- !prevProps ||
- prevProps.points !== this.props.points ||
- prevProps.width !== this.props.width ||
- prevProps.height !== this.props.height
- ) {
- this.map.invalidateSize();
-
- if (
- settings["map.center_latitude"] != null ||
- settings["map.center_longitude"] != null ||
- settings["map.zoom"] != null
- ) {
+ this.map.invalidateSize();
+
+ const hasSavedView =
+ settings["map.center_latitude"] != null ||
+ settings["map.center_longitude"] != null ||
+ settings["map.zoom"] != null;
+
+ // Pure resize (no data change): preserve user's current view
+ if (!isInitialUpdate && !pointsChanged && dimensionsChanged) {
+ if (zoom != null) {
+ const currentCenter = this.map.getCenter();
this.map.setView(
- [settings["map.center_latitude"], settings["map.center_longitude"]],
- settings["map.zoom"],
+ [lat ?? currentCenter.lat, lng ?? currentCenter.lng],
+ zoom,
);
- return;
}
+ // Don't reset to saved settings or recalculate on pure resize
+ return;
+ }
- if (shouldRecalculateZoom(prevProps?.points, this.props.points)) {
- // compute ideal lat and lon zoom separately and use the lesser zoom to ensure the bounds are visible
- const latZoom = this.map.getBoundsZoom(
- L.latLngBounds([
- [bounds.getSouth(), 0],
- [bounds.getNorth(), 0],
- ]),
- );
- const lonZoom = this.map.getBoundsZoom(
- L.latLngBounds([
- [0, bounds.getWest()],
- [0, bounds.getEast()],
- ]),
- );
- const zoom = Math.min(latZoom, lonZoom);
- // NOTE: unclear why calling `fitBounds` twice is sometimes required to get it to work
- this.map.fitBounds(bounds);
- this.map.setZoom(zoom);
- this.map.fitBounds(bounds);
- }
+ // Initial update or data changed: apply saved settings if available
+ if (hasSavedView) {
+ this.map.setView(
+ [settings["map.center_latitude"], settings["map.center_longitude"]],
+ settings["map.zoom"],
+ );
+ return;
+ }
+
+ // No saved view: fit to data bounds
+ if (shouldRecalculateZoom(prevProps?.points, this.props.points)) {
+ // compute ideal lat and lon zoom separately and use the lesser zoom to ensure the bounds are visible
+ const latZoom = this.map.getBoundsZoom(
+ L.latLngBounds([
+ [bounds.getSouth(), 0],
+ [bounds.getNorth(), 0],
+ ]),
+ );
+ const lonZoom = this.map.getBoundsZoom(
+ L.latLngBounds([
+ [0, bounds.getWest()],
+ [0, bounds.getEast()],
+ ]),
+ );
+ const zoom = Math.min(latZoom, lonZoom);
+ // NOTE: unclear why calling `fitBounds` twice is sometimes required to get it to work
+ this.map.fitBounds(bounds);
+ this.map.setZoom(zoom);
+ this.map.fitBounds(bounds);
}
}
diff --git a/frontend/src/metabase/visualizations/components/PinMap.jsx b/frontend/src/metabase/visualizations/components/PinMap.jsx
index 25781b9b12f..d9a7255beb4 100644
--- a/frontend/src/metabase/visualizations/components/PinMap.jsx
+++ b/frontend/src/metabase/visualizations/components/PinMap.jsx
@@ -233,7 +233,7 @@ export default class PinMap extends Component {
binWidth={binWidth}
binHeight={binHeight}
onFiltering={(filtering) => this.setState({ filtering })}
- zoomControl={!isEditing}
+ zoomControl={!(isDashboard && isEditing)}
onHoverChange={
isDashboard && isEditing ? null : mapProps.onHoverChange
}