Published
Edited
Jul 26, 2022
Importers
7 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
class PointLayer {
constructor(opts) {
// required opts
this.geodata = opts.geodata
this.features = opts.geodata.features
// optional opts
this.featureStyle = opts.featureStyle || {}
this.labelStyle = opts.labelStyle
this.customMarker = opts.customMarker || null
this.hasTooltip = opts.hasTooltip || false
// calculated
this.style = {
radius: d => 2,
opacity: d => 1,
fill: d => '#222',
fillOpacity: d => 1,
stroke: d => null,
strokeWidth: d => 1,
strokeOpacity: d => 1,
...this.featureStyle
}
this.label = {
text: d => null,
fill: d => '#000',
fontSize: d=> '12px',
dx: d => '0.5em',
dy: d => '0.5em',
textAnchor: d => 'start',
...this.labelStyle
}
}
plot(parent, projection, tooltip){
const path = d3.geoPath().projection(projection);
const style = this.style
const label = this.label
// const shapes = parent.append('g').attr('class','feature-layer')
const customMarker = this.customMarker
const markerGroups = parent.append('g').attr('class','feature-layer')
.selectAll('.feature')
.data(this.features).enter()
.append('g')
.attr('transform', d => {
const point = path.centroid(d)
return `translate(${point[0]},${point[1]})`
})
if (customMarker) {
markerGroups.each(customMarker, label)
} else {
markerGroups.append('circle')
.attr('r', (f => style.radius(f.properties)))
.attr('cx', 0)
.attr('cy', 0)
.attr('class', 'feature')
.attr('opacity', (f => style.opacity(f.properties)))
.attr('fill', (f => style.fill(f.properties)))
.attr('fill-opacity', (f => style.fillOpacity(f.properties)))
.attr('stroke', (f => style.stroke(f.properties)))
.attr('stroke-width', (f => style.strokeWidth(f.properties)))
.attr('stroke-opacity', (f => style.strokeOpacity(f.properties)))
}
if (label) {
markerGroups.append('text')
.attr('pointer-events', 'none')
.attr('x',0)
.attr('y',0)
.attr('font-size', f => label.fontSize(f.properties))
.attr('text-anchor', f => label.textAnchor(f.properties))
.attr('dy', f => label.dy(f.properties))
.attr('dx', f => label.dx(f.properties))
.attr('fill', f => label.fill(f.properties))
.text(f => label.text(f.properties))
}
if (this.hasTooltip) {
markerGroups
.on("mouseover", event => tooltip.style("visibility", "visible"))
.on("mousemove", event => updateTooltip(tooltip, event))
.on("mouseout", event => tooltip.style("visibility", "hidden"))
}
}
}
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more