Published
Edited
Sep 22, 2020
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.style("background-color", '#262625')
.style("color", "white");

svg.append("g")
.selectAll("g")
.data(series)
.join("g")
.attr("fill", ({key}) => color(key))
.call(g => g.selectAll("rect")
.data(d => d)
.join("rect")
.attr("x", d => x(d.data.year))
.attr("y", d => y(d[1]))
.attr("width", x.bandwidth() - 1)
.attr("height", d => y(d[0]) - y(d[1]))
.append("title")
.text(d => `${d.data.name}, ${d.data.year}th day
${formatRevenue(d.data.value)}`));

svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);

return svg.node();
}
Insert cell
data = Object.assign(d3.csvParse(await FileAttachment("observations_based_on_days@4.csv").text(), ({Format, Year, ["Revenue (Inflation Adjusted)"]: Revenue}) => ({name: Format, year: +Year, value: +Revenue})), {y: "Total Risky Users"})
Insert cell
colors = new Map([
["Attendance Tardiness", "#2A5784"],
["Interpersonal Skills", "#43719F"],
["Grievance", "#5B8DB8"],
["Personality Or Ego", "#7AAAD0"],
["Police Arrest Records", "#9BC7E4"],
["Dishonesty", "#BADDF1"],
["Expense Reporting", "#E1575A"],
["Policy Violation", "#EE7423"],
["Financial Health Stress", "#F59D3D"],
["Insubordination", "#FFC686"],
["Job Performance", "#9D7760"],
["Non Disciplinary", "#F1CF63"],
["Substance Abuse", "#7C4D79"],
["Emotional Health Stress", "#9B6A97"],
["Rule Breaker", "#BE89AC"],
["Harassment", "#D5A5C4"],
["Misuse Of Confidential Information", "#EFC9E6"],
["Loss of Productivity", "#BBB1AC"],
["Anger and Violence", "#24693D"],
["Involvement in Weapons", "#398949"],
["Social Media Communication Problem", "#61AA57"],
["Political Agenda", "#7DC470"],
["Unauthorized Digital Content", "#B4E0A7"]
])
Insert cell
series = d3.stack()
.keys(colors.keys())
.value((group, key) => group.get(key).value)
.order(d3.stackOrderReverse)
(d3.rollup(data, ([d]) => d, d => d.year, d => d.name).values())
.map(s => (s.forEach(d => d.data = d.data.get(s.key)), s))
Insert cell
x = d3.scaleBand()
.domain(data.map(d => d.year))
.rangeRound([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(series, d => d3.max(d, d => d[1]))]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
color = d3.scaleOrdinal()
.domain(colors.keys())
.range(colors.values())
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x)
.tickValues(d3.ticks(...d3.extent(x.domain()), width / 80))
.tickSizeOuter(0))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y)
.tickFormat(x => (x / 1e9).toFixed(0)))
.call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 3)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(data.y))
Insert cell
formatRevenue = x => (+(x / 1e9).toFixed(2) >= 1)
? `${(x / 1e9).toFixed(0)}`
: `${(x / 1e9).toFixed(0)}`
Insert cell
height = 500
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 30})
Insert cell
d3 = require("d3@6")
Insert cell
import {swatches} from "@d3/color-legend"
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