Skip to content

Commit

Permalink
fix: hide wrong projection when projected coords
Browse files Browse the repository at this point in the history
  • Loading branch information
neocarto committed Jul 22, 2022
1 parent e3a4734 commit 024e816
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ export function draw({ params = {}, layers = {} } = {}) {
// Scalebar
let s = layers.find((d) => d.type == "scalebar");
if (s) {
scalebar(svg, projection, width, height, {
scalebar(svg, projection, planar, width, height, {
x: s.x,
y: s.y,
units: s.units,
Expand Down
53 changes: 31 additions & 22 deletions src/layers/scalebar.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
import * as geoScaleBar from "d3-geo-scale-bar";
const d3 = Object.assign({}, geoScaleBar);

export function scalebar(selection, projection, width, height, options = {}) {
let x = options.x ? options.x : 20;
let y = options.y ? options.y : height - 30;
let units = options.units ? options.units : "kilometers";
export function scalebar(
selection,
projection,
planar,
width,
height,
options = {}
) {
if (!planar) {
let x = options.x ? options.x : 20;
let y = options.y ? options.y : height - 30;
let units = options.units ? options.units : "kilometers";

x = x / width;
y = y / height;
x = x / width;
y = y / height;

const scaleBar = d3
.geoScaleBar()
.projection(projection)
.size([width, height])
.left(x)
.top(y)
.label(units == "miles" ? "Miles" : "Km")
.units(units == "miles" ? d3.geoScaleMiles : d3.geoScaleKilometers)
.orient(d3.geoScaleBottom)
.tickPadding(5)
.tickSize(0);
const scaleBar = d3
.geoScaleBar()
.projection(projection)
.size([width, height])
.left(x)
.top(y)
.label(units == "miles" ? "Miles" : "Km")
.units(units == "miles" ? d3.geoScaleMiles : d3.geoScaleKilometers)
.orient(d3.geoScaleBottom)
.tickPadding(5)
.tickSize(0);

selection
.append("g")
.attr("transform", `translate(${x}, ${y})`)
.append("g")
.call(scaleBar);
selection
.append("g")
.attr("transform", `translate(${x}, ${y})`)
.append("g")
.call(scaleBar);
}
}

0 comments on commit 024e816

Please sign in to comment.