Published
Edited
Sep 30, 2020
Insert cell
Insert cell
chart = {
const width = 960;
const height = 600;
const path = d3.geoPath();
const radius = d3.scaleSqrt().domain([0, 1e6]).range([0, 15]);
var nodes = [
{name: "A"},
{name: "B"},
{name: "C"},
{name: "D"},
{name: "E"},
{name: "F"},
{name: "G"}
];
var links = [
{ source: "A", target: "B" },
{ source: "B", target: "C" },
{ source: "C", target: "D" },
{ source: "D", target: "E" },
{ source: "E", target: "F" },
{ source: "F", target: "G" }
];
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.name))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));

const svg = d3.select(DOM.svg(width, height))
.style("width", "100%")
.style("height", "auto");

svg.append("path")
.datum(topojson.feature(us, us.objects.nation))
.attr("fill", "#ccc")
.attr("d", path);

svg.append("path")
.datum(topojson.mesh(us, us.objects.states, (a, b) => a !== b))
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("d", path);
const link = svg.append("g")
.attr("stroke", "darkgrey")
.attr("stroke-opacity", 1)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", d => Math.sqrt(d.value));
const palette = d3.interpolateRdYlBu
const color = d3.scaleLinear().range([0,1]).domain([-1,nodes.length-1])
const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", 8)
.attr("fill", (d,i) => palette(color(i))) //Change the color here
.call(drag(simulation));
node.append("title")
.text(d => d.name);
const text = svg.append("g")
.selectAll("text")
.data(nodes)
.join("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.name });
simulation.on("tick", () => {
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);

node
.attr("cx", d => d.x)
.attr("cy", d => d.y);
text
.attr("x", d => d.x)
.attr("y", d => d.y);
});

invalidation.then(() => simulation.stop());
return svg.node();
}
Insert cell
// I only claim the changes made to this original template are mine. Please see history to validate.
// Template code; see below for mine
sixdegrees = {
//const links = data.links.map(d => Object.create(d));
//const nodes = data.nodes.map(d => Object.create(d));

var nodes = [
{name: "A"},
{name: "B"},
{name: "C"},
{name: "D"},
{name: "E"},
{name: "F"},
{name: "G"}
];

// The `links` array contains objects with a `source` and a `target`
// property. The values of those properties are the indices in
// the `nodes` array of the two endpoints of the link.

var links = [
{ source: "A", target: "B" },
{ source: "B", target: "C" },
{ source: "C", target: "D" },
{ source: "D", target: "E" },
{ source: "E", target: "F" },
{ source: "F", target: "G" }
];

const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.name))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

const link = svg.append("g")
.attr("stroke", "darkgrey")
.attr("stroke-opacity", 1)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", d => Math.sqrt(d.value));
const palette = d3.interpolateRdYlBu
const color = d3.scaleLinear().range([0,1]).domain([-1,nodes.length-1])
const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", 8)
.attr("fill", (d,i) => palette(color(i))) //Change the color here
.call(drag(simulation));
node.append("title")
.text(d => d.name);
const text = svg.append("g")
.selectAll("text")
.data(nodes)
.join("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.name });

simulation.on("tick", () => {
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);

node
.attr("cx", d => d.x)
.attr("cy", d => d.y);
text
.attr("x", d => d.x)
.attr("y", d => d.y);
});

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

return svg.node();
}
Insert cell
us = d3.json("https://unpkg.com/us-atlas@1/us/10m.json")
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require("d3@5")
Insert cell
Insert cell
width = 800
Insert cell
height = 300
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