Public
Edited
Apr 26, 2023
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Plot.plot({
width: 1152,
height: 800,
insetLeft: 10,
insetRight: 60,
r: {range: [1, 30]},
style: "overflow: visible;",
marks: [
Plot.dot(
sortedData.filter(d => d['Disaster Type'] === 'Flood'),
Plot.dodgeY({
anchor: "middle",
padding: 2,
x: "date",
r: "damages",
// title: (d) => `${d["Bank Name"]}\n${(d["Assets"] / 1000).toFixed(1)}B`,
fill: d => multiColorScale(d.casualties, d['Disaster Type']),
fillOpacity: .6,
strokeWidth: 1,
stroke: d => multiColorScale(d.casualties, d['Disaster Type']),
// fillOpacity: d => d.casualties >= d3.quantile(sortedData, .75, d => d.casualties) ? 1 : .2 ,
})
),
]
})
Insert cell
d3.quantile(sortedData, .75, d => d.casualties)
Insert cell
Insert cell
Insert cell
Insert cell
colorScale = {
const colors = ["#94d2bd","#005f73","#0a9396","#ee9b00","#ca6702","#bb3e03", "#e9d8a6", "tomato"]
const disasters = new d3.InternSet(allDisasters.map(d => d['Disaster Type'] ) )
return d3.scaleOrdinal().domain(disasters).range(colors)
}
Insert cell
function multiColorScale(value, category) {
const color = colorScale(category)
const quantScale = d3.scaleLog().domain(d3.extent(allDisasters, d => d.casualties)).range(['#FFF', color])
return quantScale(value + 1)
}
Insert cell
d3.extent(allDisasters, d => d.casualties)
Insert cell
multiColorScale(2000, 'Flood')
Insert cell
Insert cell
allDisasters = await FileAttachment("all_top_disasters@4.json").json()
Insert cell
decades = await FileAttachment("disaster_decades.json").json()
Insert cell
allDisasters.forEach(d => {
//d.date = d3.timeParse("%Y-%m-%d")(d.date)
if (['Earthquake','Wildfire', 'Drought', 'Landslide', 'Extreme temperature'].includes(d['Disaster Type'])) {
d.damages1 = -(d.damages)
} else {
d.damages1 = d.damages
}
})
Insert cell
height = 1000
Insert cell
Insert cell
d3.extent(allDisasters, d => d.casualties)
Insert cell
function cartesianToPolar(x, y) {
const r = Math.sqrt(x*x + y*y); // calculate radius
const theta = Math.atan2(y, x); // calculate angle in radians
return {r, theta};
}

Insert cell
coords = cartesianToPolar(200, 250)
Insert cell
function polarToSVG(polarCoords, dims) {
const {r, theta} = polarCoords
const x = (r * Math.cos(theta)) + (dims[0]/2)
const i = r * Math.sin(theta)
const y = (i - (i * 2)) + (dims[1]/2)

return [x, y]
}
Insert cell
polarToSVG(coords, [800, 500])
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