Published
Edited
May 2, 2019
Insert cell
md`# Det politiska landskapet`
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const links = data.links.map(d => Object.create(d));
let nodes = data.nodes.map(d => Object.create(d));
// nodes = nodes.map(function(x) {
// // if (x.id === "v") {
// // x.fx = width/8
// // x.fy = height/2
// // }
// return x
// })
console.log(links)
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id).distance(d => dist/d.value))
.force("charge", d3.forceManyBody())
// .force("x", d3.forceX(width/2).strength(0.10))
// .force("y", d3.forceY(height/2).strength(0.10))
// .alpha(0.4)
.velocityDecay(.5)
// .alphaDecay(0.05)
.force("center", d3.forceCenter(width / 2, height / 2))
.on("tick", ticked);
const svg = d3.select(DOM.svg(width, height));
const party = {
"-": "#d2dcdc",
"v": "#911414",
"s": "#e03141",
"mp": "#82c782",
"c": "#31a431",
"fp": "#1e69aa",
"m": "#7cbde0",
"kd": "#00006d",
"sd": "#ffc346",
"fi": "#b2367e",
"ovr": "#8c8c8c",
}
const link = svg.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.enter().append("line")
.attr("stroke-width", d => d.value*10);
const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("r", radius)
.attr("fill", d => party[d.id])
.call(drag(simulation));
const text = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("text")
.data(nodes)
.enter().append("text")
.text(d => d.id)
.attr("text-anchor", "middle")
.attr("alignment-baseline", "central")
.attr("dominant-baseline", "central ")
.attr("fill", d => party[d.id])
.call(drag(simulation));

node.append("title")
.text(d => d.id);
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);
// .attr("cx", d => d.id !== "v" ? d.x : width/2)
// .attr("cy", d => d.id !== "v" ? d.y : height/2);
text
// .attr("x", d => d.id !== "v" ? d.x : width/2)
// .attr("y", d => d.id !== "v" ? d.y : height/2);
.attr("x", d => d.x)
.attr("y", d => d.y)
}
return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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