d3-geo icon indicating copy to clipboard operation
d3-geo copied to clipboard

projection.naturalHeight?

Open mbostock opened this issue 6 years ago • 0 comments

After you call projection.fitWidth, it’d be nice to have a method which computes the “natural height” of the projection. As it stands, I currently tend to do something like this:

projection = d3.geoMercator()
height = {
  const [[, y0], [, y1]] = d3.geoPath(projection.fitWidth(width, object)).bounds(object);
  return Math.ceil(y1 - y0);
}

Adapted from https://observablehq.com/@d3/world-map.

The problem with the above approach is that it mutates the projection. It’d be cleaner to do it like this:

projection = d3.geoMercator().fitWidth(width, object)
height = projection.naturalHeight

I guess this assumes that projection.naturalHeight is populated by the previous call to projection.fitWidth, so we’d have to clear the natural dimensions if you change any of the projection properties. Alternatively, we could maybe recompute them again as projection.naturalHeight(object).

mbostock avatar Mar 26 '19 17:03 mbostock