Public
Edited
Apr 12, 2023
1 fork
Insert cell
Insert cell
function forceSimulation(nodes, links) {
return d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id).distance(250))
.force("charge", d3.forceManyBody().strength(-400).distanceMin(80).distanceMax(180))
.force("center", d3.forceCenter());
}
Insert cell
Insert cell
chart = {
const links = data.links.map(d => Object.create(d));
const nodes = data.nodes.map((d, index) => Object.create(d, { id: { value: index } }));
console.log(links,
nodes);
const simulation = forceSimulation(nodes, links).on("tick", ticked);

const svg = d3.select(DOM.svg(width, height))
.attr("viewBox", [-width / 2, -height / 2, width, height]);

const link = svg.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.enter().append("line")
.attr("stroke-width", d => Math.sqrt(d.value));

const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("r", d => myKWScale(d.nb_kw)) //taille dépend du nombre de mots clefs
.attr("fill", d => color(d.nb_epi)) // couleur dépend du nombre d'épigrammes
.attr("stroke", "grey")
.call(drag(simulation));
var texts = svg.selectAll(".texts")
.data(nodes)
.enter()
.append("text")
.attr("font-family", "sans-serif")
.attr("dx", 12)
.attr("dy", "0.35em")
.text( d => d.name );

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

function ticked() {
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)
texts
.attr("x", d => d.x)
.attr("y", d => d.y);
}

return svg.node();
}
Insert cell
import {Legend, Swatches} from "@d3/color-legend"

Insert cell
Insert cell
data = d3.json("https://raw.githubusercontent.com/EcrituresNumeriques/2022-Hackathon-Navigations/Team4/Team4/data.json")
Insert cell
color = d3.scaleSequential(d3.interpolateRdPu).domain([-10, 1000])
Insert cell
height = 800
Insert cell
Insert cell
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