Published
Edited
Mar 28, 2021
Insert cell
md`# India's Lower Judiciary 2013-2016

[Story](https://pratapvardhan.com/blog/india-lower-judiciary/) & [Twitter](https://twitter.com/PratapVardhan/status/1374960007695900679)
`
Insert cell
columns = ({
time: 'year',
id: 'state_2code',
x: 'cases_per_1L',
y: '%within_2years',
c: 'regions',
r: 'total_cases',
a1: 'regions',
a2: 'state_name',
xlabel: 'cases filed per 1L population →',
ylabel: '↑ % of cases decided within 2 years'
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

svg.append("g").call(xAxis);
svg.append("g").call(yAxis);
svg.append("g").call(grid);
svg.append("g").call(credit);

const data_init = dataAt(config.start_year);
const line_init = linedataAt(config.start_year);

const path = svg
.append("g")
.attrs({
fill: "none",
"stroke-width": 2,
"stroke-linejoin": "round",
"stroke-linecap": "round"
})
.selectAll("path")
.data(line_init, d => d[0])
.join("path")
.style("mix-blend-mode", "multiply")
.attr("stroke", d => color(d[1][0][columns.c]))
.attr("stroke-opacity", d => (config.line_ids.includes(d[0]) ? 0.5 : 0))
.attr("d", d => line(d[1]));

const circle = svg
.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 0.5)
.selectAll("circle")
.data(data_init, d => d[columns.id])
.join("circle")
.sort((a, b) => d3.descending(a[columns.r], b[columns.r]))
.attr("cx", d => x(d[columns.x]))
.attr("cy", d => y(d[columns.y]))
.attr("r", d => radius(d[columns.r]))
.attr("fill", d => color(d[columns.c]))
.attr("fill-opacity", d =>
config.highlightcircles ? (isHighlight(d) ? 1 : 0.65) : 1
)
.call(circle =>
circle
.append("title")
.text(d => [d[columns.a1], d[columns.a2]].join("\n"))
);

const labels = svg
.append("g")
.selectAll("text")
.data(data_init, d => d[columns.id])
.join("text")
.sort((a, b) => d3.descending(a[columns.r], b[columns.r]))
.attr("x", d => x(d[columns.x]))
.attr("y", d => y(d[columns.y]))
.text(d => (isHighlight(d) ? d[columns.a2] : d[columns.id]))
.attr("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("dy", "0.3em")
.attr("pointer-events", "none")
.attr("font-weight", d => (isHighlight(d) ? 'bold' : 'normal'))
.attr("font-size", d => config.label_size + (isHighlight(d) ? 2 : 0));

const year_text = svg
.append("g")
.attr(
"transform",
`translate(${width - margin.right}, ${height - margin.bottom})`
)
.attr("text-anchor", "end")
.append("text")
.attr("dy", -35)
.attr("dx", -10)
.attr("font-size", 88)
.attr("fill", "lightgray")
.attr("font-weight", 500)
.attr("font-family", "sans-serif");

return Object.assign(svg.node(), {
update(data, linedata) {
circle
.data(data, d => d[columns.id])
.sort((a, b) => d3.descending(a[columns.r], b[columns.r]))
.attr("cx", d => x(d[columns.x]))
.attr("cy", d => y(d[columns.y]))
.attr("r", d => radius(d[columns.r]));

labels
.data(data, d => d[columns.id])
.sort((a, b) => d3.descending(a[columns.r], b[columns.r]))
.attr("x", d => x(d[columns.x]))
.attr("y", d => y(d[columns.y]))
.text(d => (isHighlight(d) ? d[columns.a2] : d[columns.id]));

path.data(linedata, d => d[0]).attr("d", d => line(d[1]));

year_text.text(`${Math.floor(data[0].year)}`);
}
});
}
Insert cell
function isHighlight(d) {
return config.line_ids.includes(d[columns.id]);
}
Insert cell
linedataAt(config.start_year)
Insert cell
update = chart.update(currentData, currentLineData)
Insert cell
currentData = dataAt(year)
Insert cell
currentLineData = linedataAt(year)
Insert cell
line = d3
.line()
.curve(d3.curveCatmullRom)
.x(d => x(d[columns.x]))
.y(d => y(d[columns.y]))
Insert cell
function linedataAt(year) {
const y = Math.floor(year);
let tmp = data.filter(d => d[columns.time] <= y);
if (y != year) {
tmp = tmp.concat(dataAt(year));
}
return d3.groups(tmp, d => d[columns.id]);
}
Insert cell
function dataAt(year) {
return dataGroupedSorted.map(d => ({
[columns.id]: d[0],
[columns.a1]: d[1][0][columns.a1],
[columns.a2]: d[1][0][columns.a2],
[columns.x]: valueAt(year, d[1], obj => obj[columns.x]),
[columns.y]: valueAt(year, d[1], obj => obj[columns.y]),
[columns.r]: valueAt(year, d[1], obj => obj[columns.r]),
year: year
}));
}
Insert cell
bisectYear = d3.bisector(d => d[columns.time]).left
Insert cell
function valueAt(year, values, accessor) {
const i = bisectYear(values, year, 0, values.length - 1);
const a = accessor(values[i]);
if (i > 0) {
const b = accessor(values[i - 1]);
const w = values[i][columns.time] - year;
const v = a * (1 - w) + b * w;
return v;
}
return a;
}
Insert cell
radius = config.scale_radius(d3.extent(data, d => d[columns.r]), config.radius_range)
Insert cell
color = d3.scaleOrdinal().range(d3.schemeTableau10).domain(config.color_domain)
Insert cell
x = d3
.scaleLinear()
.range([margin.left, width - margin.right])
.domain(d3.extent(data, d => d[columns.x]))
Insert cell
y = d3
.scaleLinear()
.range([height - margin.bottom, margin.top])
.domain(d3.extent(data, d => d[columns.y]))
Insert cell
xAxis = g =>
g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80))
.call(g => g.select(".domain").remove())
.call(g =>
g
.append("text")
.attr("x", width - margin.right)
.attr("y", margin.bottom - 14)
.attr("font-size", config.axis_label_size)
.attr("fill", "currentColor")
.attr("font-weight", "bold")
.attr("text-anchor", "end")
.text(columns.xlabel)
)
Insert cell
yAxis = g =>
g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).tickFormat(d => d + '%'))
.call(g => g.select(".domain").remove())
.call(g =>
g
.append("text")
.attr("x", -margin.left)
.attr("y", 14)
.attr("font-size", config.axis_label_size)
.attr("fill", "currentColor")
.attr("font-weight", "bold")
.attr("text-anchor", "start")
.text(columns.ylabel)
)
Insert cell
credit = g =>
g
.attr("transform", `translate(${margin.left},${height - margin.bottom})`)
.append("text")
.attr("y", margin.bottom - 14)
.attr("text-anchor", "start")
.attr("font-size", config.axis_label_size)
.text(config.credit)
Insert cell
grid = g =>
g
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.call(g =>
g
.append("g")
.selectAll("line")
.data(x.ticks())
.join("line")
.attr("x1", d => 0.5 + x(d))
.attr("x2", d => 0.5 + x(d))
.attr("y1", margin.top)
.attr("y2", height - margin.bottom)
)
.call(g =>
g
.append("g")
.selectAll("line")
.data(y.ticks())
.join("line")
.attr("y1", d => 0.5 + y(d))
.attr("y2", d => 0.5 + y(d))
.attr("x1", margin.left)
.attr("x2", width - margin.right)
)
Insert cell
margin = ({ left: 50, right: 150, top: 25, bottom: 50 })
Insert cell
height = 500
Insert cell
dataGroupedSorted = {
const tmp = d3.groups(data, d => d[columns.id]);
tmp.forEach(d => {
d[1].sort((a, b) => a[columns.time] - b[columns.time]);
});
return tmp;
}
Insert cell
data = await FileAttachment("states-summary@2.json")
.json()
.then(arr => arr.filter(d => d.year >= 2013))
Insert cell
import { Scrubber } from "@mbostock/scrubber"
Insert cell
// d3 = require('d3', 'd3-selection-multi')
d3 = require.alias({
'd3-selection': 'd3',
d3: 'd3'
})('d3', 'd3-selection-multi')
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