Published
Edited
Feb 26, 2021
Insert cell
Insert cell
chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

svg
.append("g")
.attr("fill", "steelblue")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", x(0))
.attr("y", (d, i) => y(i))
.attr("width", d => x(d.value) - x(0))
.attr("height", y.bandwidth());

svg
.append("g")
.attr("fill", "white")
.attr("text-anchor", "end")
.attr("font-family", "sans-serif")
.attr("font-size", 16)
.selectAll("text")
.data(data)
.join("text")
.attr("x", d => x(d.value))
.attr("y", (d, i) => y(i) + y.bandwidth() / 2)
.attr("dy", "0.35em")
.attr("dx", -4)
.text(d => format(d.value))
.call(text =>
text
.filter(d => x(d.value) - x(0) < 20) // short bars
.attr("dx", +4)
.attr("fill", "black")
.attr("text-anchor", "start")
);

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

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

return svg.node();
}
Insert cell
funderTypeLabels = ({
academic: "Academic Institution",
activist: "Activist Network",
community: "Community Based Organization",
faithbased: "Faith-Based Organization",
forprofit: "For-Profit Business",
governmentowned: "Government-Owned Corporation",
individual: "Individual",
international: "International Organization",
labortrade: "Labor/Trade Union",
local: "Local Government (e.g. Village, Town, City)",
"n/a": "Not Applicable",
national: "National Government",
nongovernmental: "Non-Governmental Organization",
philanthropic: "Philanthropic Organization",
regional: "Regional Government",
social: "Social Movement"
})
Insert cell
format = x.tickFormat(20, data.format)
Insert cell
x = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)])
.range([margin.left, width - margin.right])
Insert cell
y = d3
.scaleBand()
.domain(d3.range(data.length))
.rangeRound([margin.top, height - margin.bottom])
.padding(0.1)
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${margin.top})`)
.call(d3.axisTop(x).ticks(width / 80, data.format))
.call(g => g.select(".domain").remove())
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).tickFormat(i => data[i].name).tickSizeOuter(0))
Insert cell
barHeight = 25
Insert cell
height = Math.ceil((data.length + 0.1) * barHeight) + margin.top + margin.bottom
Insert cell
data = countsByFunderType
Insert cell
countsByFunderType = {
const filteredCases = caseData
.map(c => {
if (c.funder_types !== "") {
return {
id: c.id,
funder_types: c.funder_types.split(",")
};
}
})
.filter(c => c);

const countsByFunderTypeData = {};
console.log("filteredCases", filteredCases);
filteredCases.forEach(c => {
c.funder_types.forEach(type => {
if (countsByFunderTypeData[type]) {
countsByFunderTypeData[type] = countsByFunderTypeData[type] + 1;
} else {
countsByFunderTypeData[type] = 1;
}
});
});

const dataFormatted = Object.keys(countsByFunderTypeData).map(key => {
return {
name: funderTypeLabels[key],
value: countsByFunderTypeData[key],
key: key
};
});

dataFormatted.push({
name: "No funder type entered",
value: caseData.length - filteredCases.length
});
return dataFormatted;
}
Insert cell
caseData = FileAttachment(
"participedia-data-cases for Obeservable - participedia-data-cases (2).csv"
).csv()
Insert cell
margin = ({ top: 30, right: 0, bottom: 10, left: 200 })
Insert cell
d3 = require("d3@5")
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