Public
Edited
Dec 5, 2023
Insert cell
Insert cell
Insert cell
md`### Examing the gender ratio`
Insert cell
gender = d3.csvParse(await FileAttachment("genders.csv").text())
Insert cell
genders = Array.from(gender.map(d => d.gender))
Insert cell
setgender = ["Male", "Female"]
Insert cell
Object.defineProperties(Array.prototype, {
count: {
value: function(value) {
return this.filter(x => x==value).length;
}
}
});
Insert cell
genderfrequencies = [genders.count("http://www.wikidata.org/entity/Q6581097"), genders.count("http://www.wikidata.org/entity/Q6581072")]
Insert cell
Plot.barY(genders, {x: setgender, y: genderfrequencies}).plot()

Insert cell
Insert cell
FileAttachment("genders.txt").text()
Insert cell
Insert cell
country = d3.csvParse(await FileAttachment("countries.csv").text())
Insert cell
countries = Array.from(country.map(d => d.countryLabel))
Insert cell
countryfrequencies = countries.reduce(function (acc, curr) {
return acc[curr] ? ++acc[curr] : acc[curr] = 1, acc
}, {});
Insert cell
function filterItems(arr) {
return Object.keys(arr).filter(function(el) {
return arr[el] > 5;
})
}
Insert cell
setcountries = filterItems(countryfrequencies)
Insert cell
actualcountryfrequencies = [countryfrequencies["United States of America"], countryfrequencies["France"], countryfrequencies["Soviet Union"], countryfrequencies["Russia"], countryfrequencies["United Kingdom"], countryfrequencies["Austria"], countryfrequencies["Germany"], countryfrequencies["Kingdom of Italy"], countryfrequencies["Italy"], countryfrequencies["Poland"]]
// I had to do this manually because for loops kept throwing errors
Insert cell
Plot.barY(countries, {x: setcountries, y: actualcountryfrequencies, sort: {x: "-y"}}).plot()

Insert cell
Insert cell
FileAttachment("countries.txt").text()
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const links = data1.links1.map(d => Object.create(d));
const nodes = data1.nodes.map(d => Object.create(d));


const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody().strength(-400))
.force("x", d3.forceX())
.force("y", d3.forceY());

const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height])
.style("font", "12px sans-serif");
// Create a container to enable mousewheel zoom
var g = svg.append("g")
.attr("class", "everything");
// Per-type markers, as they don't inherit styles.
svg.append("defs").selectAll("marker")
.data(types1)
.join("marker")
.attr("id", d => `arrow-${d}`)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -0.5)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("path")
.attr("fill", color1)
.attr("d", "M0,-5L10,0L0,5");

const link = g.append("g")
.attr("fill", "none")
.attr("stroke-width", 1.5)
.selectAll("path")
.data(links)
.join("path")
.attr("stroke", d => color1(d.type))
.attr("marker-end", d => `url(${new URL(`#arrow-${d.type}`, location)})`);

link.append("title")
.text(d => d.id);

const node = g.append("g")
.attr("fill", "currentColor")
.attr("stroke-linecap", "round")
.attr("stroke-linejoin", "round")
.selectAll("g")
.data(nodes)
.join("g")
.call(drag(simulation));

node.append("circle")
.attr("stroke", "white")
.attr("stroke-width", 1.5)
.attr("r", 4);

node.append("title")
.text(d => d.id);


const textElems = g.append('g')
.selectAll('text')
.data(nodes)
.join('text')
.text(d => d.id)
.attr('font-size',12)
.attr('font-size',12)
.call(drag(simulation))

textElems.append("title")
.text(d => d.id);

simulation.on("tick", () => {
link.attr("d", linkArc);
node.attr("transform", d => `translate(${d.x},${d.y})`);
textElems
.attr("x", d => d.x + 10)
.attr("y", d => d.y);
});


invalidation.then(() => simulation.stop());

// Add mousewheel zoom functionality
svg.call(d3.zoom()
.extent([[0, 0], [width, height]])
.scaleExtent([0, 8])
.on("zoom", zoomed));

function zoomed({transform}) {
g.attr("transform", transform);
}
return svg.node();
}


Insert cell
links1 = d3.csvParse(await FileAttachment("1997@6.csv").text())
Insert cell
types1 = Array.from(new Set(links1.map(d => d.type)))
Insert cell
data1 = ({nodes: Array.from(new Set(links1.flatMap(l => [l.source, l.target])), id => ({id})), links1})
Insert cell
Insert cell
color1 = d3.scaleOrdinal(types1, d3.schemeCategory10)
Insert cell
Insert cell
drag = simulation => {
function dragstarted(event, d) {
if (!event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(event, d) {
d.fx = event.x;
d.fy = event.y;
}
function dragended(event, d) {
if (!event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
Insert cell
d3 = require("d3@6")
Insert cell
import {swatches} from "@d3/color-legend"
Insert cell
Insert cell
Select a data source…
Type Chart, then Shift-Enter. Ctrl-space for more options.

Insert cell
FileAttachment("1997@2.txt").text()
Insert cell
Insert cell
Insert cell
chart2 = {
const links = data2.links2.map(d => Object.create(d));
const nodes = data2.nodes.map(d => Object.create(d));


const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody().strength(-400))
.force("x", d3.forceX())
.force("y", d3.forceY());

const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height])
.style("font", "12px sans-serif");
// Create a container to enable mousewheel zoom
var g = svg.append("g")
.attr("class", "everything");
// Per-type markers, as they don't inherit styles.
svg.append("defs").selectAll("marker")
.data(types2)
.join("marker")
.attr("id", d => `arrow-${d}`)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -0.5)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("path")
.attr("fill", color1)
.attr("d", "M0,-5L10,0L0,5");

const link = g.append("g")
.attr("fill", "none")
.attr("stroke-width", 1.5)
.selectAll("path")
.data(links)
.join("path")
.attr("stroke", d => color1(d.type))
.attr("marker-end", d => `url(${new URL(`#arrow-${d.type}`, location)})`);

link.append("title")
.text(d => d.id);

const node = g.append("g")
.attr("fill", "currentColor")
.attr("stroke-linecap", "round")
.attr("stroke-linejoin", "round")
.selectAll("g")
.data(nodes)
.join("g")
.call(drag(simulation));

node.append("circle")
.attr("stroke", "white")
.attr("stroke-width", 1.5)
.attr("r", 4);

node.append("title")
.text(d => d.id);


const textElems = g.append('g')
.selectAll('text')
.data(nodes)
.join('text')
.text(d => d.id)
.attr('font-size',12)
.attr('font-size',12)
.call(drag(simulation))

textElems.append("title")
.text(d => d.id);

simulation.on("tick", () => {
link.attr("d", linkArc);
node.attr("transform", d => `translate(${d.x},${d.y})`);
textElems
.attr("x", d => d.x + 10)
.attr("y", d => d.y);
});


invalidation.then(() => simulation.stop());

// Add mousewheel zoom functionality
svg.call(d3.zoom()
.extent([[0, 0], [width, height]])
.scaleExtent([0, 8])
.on("zoom", zoomed));

function zoomed({transform}) {
g.attr("transform", transform);
}
return svg.node();
}


Insert cell
links2 = d3.csvParse(await FileAttachment("2001.csv").text())
Insert cell
types2 = Array.from(new Set(links2.map(d => d.type)))
Insert cell
data2 = ({nodes: Array.from(new Set(links2.flatMap(l => [l.source, l.target])), id => ({id})), links2})
Insert cell
FileAttachment("2001.txt").text()
Insert cell
Insert cell
md`## 2008-2012

Some of the notable deaths that occurred during this time were Steve Jobs, Amy Winehouse, and Michael Jackson.
`
Insert cell
chart3 = {
const links = data3.links3.map(d => Object.create(d));
const nodes = data3.nodes.map(d => Object.create(d));


const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody().strength(-400))
.force("x", d3.forceX())
.force("y", d3.forceY());

const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height])
.style("font", "12px sans-serif");
// Create a container to enable mousewheel zoom
var g = svg.append("g")
.attr("class", "everything");
// Per-type markers, as they don't inherit styles.
svg.append("defs").selectAll("marker")
.data(types3)
.join("marker")
.attr("id", d => `arrow-${d}`)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -0.5)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("path")
.attr("fill", color1)
.attr("d", "M0,-5L10,0L0,5");

const link = g.append("g")
.attr("fill", "none")
.attr("stroke-width", 1.5)
.selectAll("path")
.data(links)
.join("path")
.attr("stroke", d => color1(d.type))
.attr("marker-end", d => `url(${new URL(`#arrow-${d.type}`, location)})`);

link.append("title")
.text(d => d.id);

const node = g.append("g")
.attr("fill", "currentColor")
.attr("stroke-linecap", "round")
.attr("stroke-linejoin", "round")
.selectAll("g")
.data(nodes)
.join("g")
.call(drag(simulation));

node.append("circle")
.attr("stroke", "white")
.attr("stroke-width", 1.5)
.attr("r", 4);

node.append("title")
.text(d => d.id);


const textElems = g.append('g')
.selectAll('text')
.data(nodes)
.join('text')
.text(d => d.id)
.attr('font-size',12)
.attr('font-size',12)
.call(drag(simulation))

textElems.append("title")
.text(d => d.id);

simulation.on("tick", () => {
link.attr("d", linkArc);
node.attr("transform", d => `translate(${d.x},${d.y})`);
textElems
.attr("x", d => d.x + 10)
.attr("y", d => d.y);
});


invalidation.then(() => simulation.stop());

// Add mousewheel zoom functionality
svg.call(d3.zoom()
.extent([[0, 0], [width, height]])
.scaleExtent([0, 8])
.on("zoom", zoomed));

function zoomed({transform}) {
g.attr("transform", transform);
}
return svg.node();
}

Insert cell
links3 = d3.csvParse(await FileAttachment("2008.csv").text())
Insert cell
types3 = Array.from(new Set(links3.map(d => d.type)))
Insert cell
data3 = ({nodes: Array.from(new Set(links3.flatMap(l => [l.source, l.target])), id => ({id})), links3})
Insert cell
FileAttachment("2008.txt").text()
Insert cell
Insert cell
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