function mapChart(data, {
svg,
projection,
feature,
border,
colorScale,
radiusScale,
colorBy,
radiusBy,
thresholdBigMagnitude = 8,
thresholdMidMagnitude = 7.8
} = {}) {
const path = d3.geoPath(projection)
const innerChart = svg
.append('g')
.attr('transform', `translate(0, 0)`)
.attr('class', 'map')
innerChart
.append('g')
.attr('class', 'map-features')
.selectAll('path')
.data(feature.features)
.join('path')
.attr('d', path)
.attr('fill', '#ddd')
innerChart
.append('g')
.attr('class', 'map-borders')
.selectAll('path')
.data(border.features)
.join('path')
.attr('d', path)
.attr('fill', 'none')
.attr('stroke', 'gray')
.attr('stroke-width', 0.25)
.attr('stroke-linejoin', 'round')
.attr('pointer-events', 'none')
// Add data
let circleG = innerChart
.append('g')
.attr('class', 'map-data')
.selectAll(null)
.data(data, d => d.eventId)
.join('g')
.attr('transform', d => {
return "translate(" + projection(d.geometry.coordinates) + ")"
})
// add circles
circleG
.append('circle')
.attr('stroke', 'black')
.attr('fill-opacity', 0.8)
.attr('stroke-width', d => d.magnitude >= thresholdBigMagnitude ? 2 : 0.3) //
.attr('fill', d => colorScale(d[colorBy])) //
.attr("r", d => radiusScale(d[radiusBy])) //
// add text
const textCircles = circleG
.append('text')
.attr('text-anchor', "end")
.style("font-size", "15px")
.attr("x", 0)
.attr('y', 0)
.attr('stroke-width', 0.25)
.attr('text-anchor', 'middle')
.attr('stroke', d => {
return d.magnitude >= thresholdBigMagnitude ? 'black'
: d.magnitude >= thresholdMidMagnitude ? '#f0f0f0' : null})
textCircles
.append('tspan')
.attr('y', d => - (radiusScale(d[radiusBy]) + 20))
.text(d => {
const title = `M${format(d[radiusBy])}, ${d.year}`
return d.magnitude >= thresholdMidMagnitude ? title : null
})
textCircles
.append('tspan')
//.attr('text-anchor', 'middle')
.attr('x', d => {
const title = `${d.description}, ${d.department}`
return - (title.length - 10)
})
.attr('y', d => - (radiusScale(d[radiusBy]) + 5))
.text(d => {
const title = `${d.description}, ${d.department}`
return d.magnitude >= thresholdMidMagnitude ? title : null
})
// add lines guides
/*circleG.append('line')
.attr('stroke', d => {
return d.magnitude >= thresholdBigMagnitude ? 'black'
: d.magnitude >= thresholdMidMagnitude ? 'gray' : null})
.attr('x1', 0)
.attr('y1', 0)
.attr('x2', -40)
.attr('y2', -30)*/
return svg.node()
}