Published
Edited
Apr 11, 2022
Insert cell
Insert cell
Insert cell
Insert cell
graduatedSymbols = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
svg.append("path")
.datum(topojson.feature(basepolygons, basepolygons.objects.india))
.attr("fill", "#ccc")
.attr("d", path_basemap);
svg.append("path")
.datum(topojson.mesh(basepolygons, basepolygons.objects.india, (a, b) => a !== b))
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("stroke-width", 0.5)
.attr("d", path_basemap);

// Below parts create graduated symbols //
svg.append("g")
.selectAll("circle")
.data(points.features
.map(d => (d.value = Math.sqrt(d.properties[attributeName]), d))
.sort((a, b) => b.value - a.value))
.join("circle")
.attr("transform", d => `translate(${path_points.centroid(d)})`)
.attr("r", d => radius(d.value))
.attr("fill", d => colors(d.value))
.attr("fill-opacity", 1)
.attr("stroke", "white")
.attr("stroke-width", 0.5)
.append("title")
.text(d => `${d.properties[idName]}: ${d.properties[attributeName]}`);
return svg.node();
}
Insert cell
Insert cell
Insert cell
height = 350
Insert cell
width = 600
Insert cell
path_points = d3.geoPath().projection(projection)
Insert cell
path_basemap = d3.geoPath().projection(projection)
Insert cell
projection = d3.geoMercator().fitSize([width, height], unit)
Insert cell
format = d3.format(".2s")
Insert cell
//proportional symbols
//radius = d3.scaleSqrt([0, d3.max(attribute)], [0, 30])
Insert cell
sizeArrayForLegend = Array.from(radiusArray, d=>Math.PI*Math.pow(d, 2.4))
Insert cell
radius = d3.scaleThreshold()
.domain(naturalbreaks)
.range(radiusArray)
Insert cell
radiusArray = [2.96, 2.96, 4.24, 5.92, 7.76]
// The only one "observation" falling into the first class is a missing value, as the missing value is coded as 0. Therefore, I set the radius for the first class equal to the second class's.
Insert cell
circleLabels = ["No Data", "<61%", "61%-73%", "73%-86%", ">86%"]
Insert cell
naturalbreaks
Insert cell
colors = d3.scaleThreshold()
.domain(naturalbreaks)
.range(RdPu)
Insert cell
// 5-class color scheme
RdPu = ["#000000", "#fbb4b9", "#f768a1", "#c51b8a", "#7a0177"]
// The first color is black, which is used for missing value
Insert cell
// using natural break classification method; 5 classes
naturalbreaks = simple.ckmeans(attribute.filter(d => d != 0), 5).map(v => v.pop())
Insert cell
d3.max(attribute)
Insert cell
d3.min(attribute)
Insert cell
attribute = Array.from(points.features, d=>Math.sqrt(d.properties[attributeName]))
Insert cell
attributeName = "india_data_main_language_share"
Insert cell
idName = "shapeName"
Insert cell
unit = topojson.feature(basepolygons, basepolygons.objects.india)
Insert cell
//import the point data (geojson)
points = FileAttachment("india_centroids.geojson").json()
Insert cell
//import the polygon base map data
basepolygons = FileAttachment("india.json").json()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more