-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathscalebar.js
40 lines (36 loc) · 963 Bytes
/
scalebar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import * as geoScaleBar from "d3-geo-scale-bar";
const d3 = Object.assign({}, geoScaleBar);
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;
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("class", options.id)
.attr("data-layer", JSON.stringify({ _type: "scalebar" }))
.attr("transform", `translate(${x}, ${y})`)
.append("g")
.call(scaleBar);
}
}