Skip to content

Commit

Permalink
Fit the map to the data
Browse files Browse the repository at this point in the history
Create a GeoJSON FeatureCollection from the data that we can pass to
D3's fitSize method to zoom the map to the data.

I tried using d3.extent to find the range of the longitude and latitude
in the data from which I created a FeatureCollection with just 2 points,
but the longitude range ended up being nearly [-180, 180], so it never
actually zoomed the map. Seems like maybe D3 is doing something smart to
handle collections that wrap around the globe, or possibly there's some
unaccounted for weirdness in the data.
  • Loading branch information
esheehan-gsl committed Dec 21, 2023
1 parent 65fd4b9 commit 4430c09
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/unified_graphics/static/js/component/ChartMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,26 @@ export default class ChartMap extends ChartElement {

if (!(borders || observations)) return;

this.#projection.fitSize([width, height], borders);
if (observations) {
// Build a GeoJSON object to provide D3's fitSize method so that we can always
// zoom the map to the available data.
const points = {
type: "FeatureCollection",
features: observations.map((obs) => {
return {
type: "Feature",
geometry: {
type: "Point",
coordinates: [obs.longitude, obs.latitude],
},
};
}),
};

this.#projection.fitSize([width, height], points);
} else {
this.#projection.fitSize([width, height], borders);
}

const ctx = canvas.getContext("2d");
if (!ctx) return;
Expand Down

0 comments on commit 4430c09

Please sign in to comment.