Published
Edited
Jul 29, 2020
1 fork
Importers
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

// The bars themselves
svg
.append("g")
.selectAll("g")
.data(series)
.join("g")
// Each indivdual bar
.attr("fill", (d, i) => barColours[i % 3])
.classed("bar-group",true)

.selectAll("rect")
.data(d => d)
.join("rect")
.attr("x", d => x(d[0]))
.attr("y", (d, i) => y(d.data["Police force"]))
.attr("width", d => x(d[1]) - x(d[0]))
.attr("height", y.bandwidth())
// National average line
svg
.append("line")
.attr("x1", x(medianTotalCed))
.attr("y1", graph.margin.top + 2)
.attr("x2", x(medianTotalCed))
.attr("y2", height - graph.margin.bottom)
.style("stroke-width", 1)
.style("stroke-dasharray", ("1,2"))
.style("stroke", colours.secondary50)
.style("fill", "none")
// "Average" text
svg
.append("text")
.attr("x", x(medianTotalCed) + 5)
.attr("y", height - graph.margin.bottom - 5)
.text("Average")
.attr("text-anchor", "start")
.attr("font-family", graph.font)
.attr("font-size", graph.fontSize)
.style("fill", colours.secondary)
// Text - discharged
svg
.append("g")
.attr("fill", colours.primary)
.attr("text-anchor", "start")
.attr("font-family", graph.font)
.attr("font-size", graph.fontSize)
.selectAll("text")
.data(series[1])
.join("text")
.attr("x", d => x(d[0]))
.attr("y", (d, i) => y(d.data['Police force']) + y.bandwidth() / 2)
.attr("dy", "0.35em")
.attr("dx", 5)
.text(d => d3.format(".1f")(d[0]))
// Text - unknown
svg
.append("g")
.attr("fill", colours.primary)
.attr("text-anchor", "start")
.attr("font-family", "sans-serif")
.attr("font-size", graph.fontSize)
.selectAll("text")
.data(series[1])
.join("text")
.attr("x", d => x(d[0]))
.attr("y", (d, i) => y(d.data['Police force']) + y.bandwidth() / 2)
.attr("dy", "0.35em")
.attr("dx", 5)
.text(d => d3.format(".1f")(d[0]))
// Text - total
svg
.append("g")
.attr("fill", colours.primary)
.attr("text-anchor", "start")
.attr("font-family", graph.font)
.attr("font-size", graph.fontSize)
.selectAll("text")
.data(ced_per_100_by_force.sort((a,b) => b.Total - a.Total))
.join("text")
.attr("x", d => x(d.Total))
.attr("y", (d, i) => y(d['Police force']) + y.bandwidth() / 2)
.attr("dy", "0.35em")
.attr("dx", 5)
.text(d => d3.format(".1f")(d.Total))
.call(text => text.filter(d => d.Total > 40)
.text(d => d3.format(".1f")(d.Total))
.attr("x", width - 10)
.attr("fill", "white")
.attr("text-anchor", "end"));

// Draw axes
svg.append("g").call(xAxis);
svg.append("g").call(yAxis);

return svg.node()
}
Insert cell
getCedPer100ByForce = force => {
let forcePop = getPopByForce(force);
let forceStats = getForceStats(force);
return forceStats["Total"] / forcePop;
}
Insert cell
getCedPer100ByForce("Greater Manchester")
Insert cell
getCedLeaderboardPosition = (name) => cedLeaderBoard.findIndex((f) => f.force === name) + 1
Insert cell
getCedLeaderboardPosition("Greater Manchester")
Insert cell
Insert cell
Insert cell
cedLeaderBoard = forces.map((force) => {
if(getForceStats(force)) {
return { force: force, per100: getCedPer100ByForce(force) }
} else {
return { force: force, per100: 0 }
}
}).sort((a, b) => {
return b.per100 - a.per100
})

Insert cell
getForceStats = (force) => ced_by_region_recent.find(f => f["Police force"] === force)
Insert cell
getPopByForce = force => {
let pop = officerCounts.find(p => p["Force/Region"] === force);
if (pop == undefined) {
return NaN;
} else {
// Using March as closer to the reporting period for the taser stats
return pop["March 2019"] / 100;
}
}
Insert cell
Insert cell
Insert cell
// This object provides the stacked bars
series = d3
.stack()
.keys(['Total discharge', 'Not stated', 'Total non-discharge'])(ced_per_100_by_force)
.map(d => (d.forEach(v => (v.key = d.key)), d))
Insert cell
colour = d3.scaleOrdinal(colours).domain(ced_per_100_by_force.columns)
Insert cell
format = x.tickFormat(20, series.format)
Insert cell
// Transition function to shuffle bars around https://observablehq.com/@d3/bar-chart-race-explained
// Needs to take a transition and returns an ordered transition between the current data sort and the previous.
bar = f=>f
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
height = Math.ceil((ced_per_100_by_force.length + 0.1) * graph.barHeight) + graph.margin.top + graph.margin.bottom
Insert cell
// Creates the national average stat
function add_average(e) {
let cols = e.columns;
let avg = e.reduce((t, v) => {
cols.forEach(col => {
t[col] += v[col] / e.length;
});
return t;
});
avg["Police force"] = "National Average";
e.push(avg);
return e;
}
Insert cell
averageTotalCed = ced_per_100_by_force.reduce((acc, val) => {
return acc + val['Total']
}, 0) / ced_per_100_by_force.length
Insert cell
medianTotalCed = d3.median(ced_per_100_by_force.map((f) => f.Total))
Insert cell
barColours = [colours.primary, colours.primary50, colours.primary00]
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import { swatches } from "@d3/color-legend"
Insert cell
import {
getStats,
colours,
graph,
d3
} from '@resistancelab/resistance-lab-defaults'
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