Published
Edited
Dec 17, 2021
1 fork
Insert cell
# Waste Density
Insert cell
zips = FileAttachment("bangkok district.geojson").json()
Insert cell
waterbodies = FileAttachment("bangkok water bodies@1.geojson").json()
Insert cell
floodpoint = FileAttachment("bangkok flood point.geojson").json()
Insert cell
floodgate = FileAttachment("bangkok flood gate.geojson").json()
Insert cell
wasteQ = FileAttachment("Quantity Waste.geojson").json()
Insert cell
wasteC = FileAttachment("Waste Center.geojson").json()
Insert cell
housing = FileAttachment("community housing.geojson").json()
Insert cell
stream = FileAttachment("Stream Selection.geojson").json()
Insert cell
river = FileAttachment("River Outline.geojson").json()
Insert cell
river2 = FileAttachment("River Outline 2.geojson").json()
Insert cell
waterQ = FileAttachment("Water Quality.geojson").json()
Insert cell
river5 = FileAttachment("river 5.geojson").json()
Insert cell
wasteQ2 = FileAttachment("Quantity Waste 2.geojson").json()
Insert cell
wasteD = FileAttachment("waste density.geojson").json()
Insert cell
d3 = require("d3@6")
Insert cell
chart2 =
Plot.plot({
color: {
type: "sequential",
range: ["rgb(250, 219, 194)", "rgb(186, 90, 42)"],
},
marks: [
Plot.cell([2000, 6300, 10600, 14900, 19200, 23500, 27800, 32100, 36400, 40700, 45000], {x: d => d, fill: d => d})
]
})

Insert cell
chart = {
const width = 960,
height = 900;
const svg = d3.create("svg")
.attr("viewBox", [-60, 0, width, height]);

// Use Mercator projection qtotal
var projection = d3
.geoMercator()
.fitSize([width - 60, height - 60], zips);

var path = d3.geoPath().projection(projection);
var path2 = d3.geoPath().projection(projection);
var path3 = d3.geoPath().projection(projection);
var path4 = d3.geoPath().projection(projection);



var g = svg.append("g").attr("id", "paths");
g.selectAll("path") //d3 geopath
//svg
//.selectAll('path')
.data(zips.features) //get data to define path
.enter() //there are more data than elements, this selects them
.append("path") //appends path to data
.attr('class','outlines')
.attr("d", path) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "white")
.style('fill-opacity','.25')
.style("stroke-width", "1")
.style("stroke", "#ccc")


g.selectAll("path4") //d3 geopath
//svg
//.selectAll('path')
.data(wasteD.features) //get data to define path
.enter() //there are more data than elements, this selects them
.append("path") //appends path to data
.attr('class','outlines')
.attr("d", path) //The d attribute defines a path to be drawn, only applies to appended elements
.style("stroke-width", "1")
.style("stroke", "#ccc")
.attr("fill", d => color(d.properties.waste_dens))
.style('fill-opacity','0.5')
.on('mouseover', addfill)
.on('mouseleave', removefill)
//.on('mouseover', addBox2)
//.on('mouseleave', removeBox2)

var tooltip = svg.append("g");

//add waste tool tip
function addBox2(d) {
var centroid = path.centroid(d);
tooltip.call(
callout,
`${d.properties.qtotal}
${d.properties.waste_dens}`
);
tooltip.attr("transform", "translate(" + centroid + ")");
}


//add stream tool tip
function addBox(event, d) {
var centroid = path.centroid(d);
tooltip.call(
callout,
`${d.properties.STREAM_}
${d.properties.LENGTH}, ${d.properties.LENGTH}`
);
tooltip.attr("transform", "translate(" + centroid + ")");
}


function removeBox(event, d) {
tooltip.call(callout,null)
}
function addfill(event,d) {
d3.select(this).style("fill", "rgb(235,120,19)")
d3.select(this).style('fill-opacity', '0.75')

svg
.append('text')
.attr("class","labels")
.attr('x','100')
.attr('y','100')
.attr('font-size','.85em')
.text(d.properties.dname_e)

addBox2(d)
}
function removefill(event,d) {
d3.selectAll('text.labels')
d3.select(this).style("fill", d => color(d.properties.waste_dens))
d3.select(this).style('fill-opacity', '0.5')
d3.select(this).attr("fill", d => color(d.properties.waste_dens))

removeBox2(d)
}

function removeBox2(event, d) {
tooltip.call(callout,null)
}


g.selectAll("path3") //d3 geopath
//svg
//.selectAll('path')
.data(river5.features) //get data to define path
.enter() //there are more data than elements, this selects them
.append("path") //appends path to data
.attr('class','outlines')
.attr("d", path) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "rgb(20, 180, 225)")
.style('fill-opacity','.7')
.style("stroke-width", "1")
.style("stroke", "black")

//insert waste centers into map
g.selectAll("circle") //d3 geopath
//svg
//.selectAll('path')
.data(wasteC.features) //get data to define path
.enter() //there are more data than elements, this selects them
.append("circle") //appends path to data
.attr('cx',function(d) {return path.centroid(d)[0]})
.attr("cy", function(d) {return path.centroid(d)[1]})
.attr('r',5)
.attr("fill", "rgb(88, 53, 11)")
.style('fill-opacity','0.75')

//insert water quality control plants into map
g.selectAll("circle") //d3 geopath
//svg
//.selectAll('path')
.data(waterQ.features) //get data to define path
.enter() //there are more data than elements, this selects them
.append("circle") //appends path to data
.attr('cx',function(d) {return path.centroid(d)[0]})
.attr("cy", function(d) {return path.centroid(d)[1]})
.attr('r',5)
.attr("fill", "rgb(25,117,164)")
.style('fill-opacity','0.75')



return svg.node();
}
Insert cell
//yourData.map((element, index) => {
//})
Insert cell
color = d3.scaleQuantize([2000, 45000], d3.schemeOranges[9])
Insert cell
callout = (g, value) => {
if (!value) return g.style("display", "none");

g
.style("display", null)
.style("pointer-events", "none")
.style("font", "10px sans-serif");

const path = g.selectAll("path")
.data([null])
.join("path")
.attr("fill", "white")
.attr("stroke", "black");

const text = g.selectAll("text")
.data([null])
.join("text")
.call(text => text
.selectAll("tspan")
.data((value + "").split(/\n/))
.join("tspan")
.attr("x", 0)
.attr("y", (d, i) => `${i * 1.1}em`)
.style("font-weight", (_, i) => i ? null : "bold")
.text(d => d));

const {x, y, width: w, height: h} = text.node().getBBox();

text.attr("transform", `translate(${-w / 2},${15 - y})`);
path.attr("d", `M${-w / 2 - 10},5H-5l5,-5l5,5H${w / 2 + 10}v${h + 20}h-${w + 20}z`);
}
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