Published
Edited
Nov 5, 2020
2 forks
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// add scales for Read Status
//statusColor = d3.scaleOrdinal(d3.schemeCategory10);
// statusColor = d3.scaleOrdinal(["#1f77b4","#bcbd22","#d62728"]);
statusColor = d3.scaleSequential([d3.min(dnodes, d => d.relative), d3.max(dnodes, d => d.relative)], d3.interpolateTurbo)
// .domain()
Insert cell
dnodes = Array.from(new Set(items)).map(function(item) {return {name: item};});
//nodes = Array.from(new Set(items)).map(function(item) {return item;});
Insert cell
legend({
color: statusColor,
title: "Relative popularity of genre in city"
})
Insert cell
drawChart()
Insert cell
function drawChart() {
const links = dlinks.map(d => Object.create(d));
const nodes = dnodes.map(d => Object.create(d));
console.log(nodes);
// const radius = d3.scaleSqrt()
// .domain([0, d3.max(nodes, node => node.count)])
// .range([1, width / 50]);

//var bodyForce = d3.forceManyBody
const radius = 7;
var tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.name))
.force("charge", d3.forceManyBody().strength(-175))
.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", "#aaa")
.attr("stroke-opacity", 0.3)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", d => Math.sqrt(d.value) / 2);
const node = svg.append("g")
.attr("stroke", "#000")
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", d => d.radius)
.attr("fill", d => statusColor(d.relative))
.call(drag(simulation))
.on('mouseover.fade', fade(0.1))
.on('mouseout.fade', fade(1));
const textElems = svg.append('g')
.selectAll('text')
.data(nodes)
.join('text')
.text(d => d.name)
.attr('font-size',10)
.attr('font-size',10)
.call(drag(simulation))
.on('mouseover.fade', fade(0.1))
.on('mouseout.fade', fade(1));
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", function(d) { return d.x = Math.max((d.radius+1), Math.min(width - (d.radius+1), d.x)); })
.attr("cy", function(d) { return d.y = Math.max((d.radius+1), Math.min(height - (d.radius+1), d.y)); });
textElems
.attr("x", d => d.x + 10)
.attr("y", d => d.y);
// .attr("visibility", "hidden");
});
function fade(opacity) {
return d => {
console.log("d", d)
node.style('opacity', function (o) { return isConnected(d, o) ? 1 : opacity });
textElems.style('visibility', function (o) { return isConnected(d, o) ? "visible" : "hidden" });
link.style('stroke-opacity', o => (o.source === d || o.target === d ? 1 : opacity));
if(opacity === 1){
node.style('opacity', 1)
textElems.style('visibility', 'visible')
link.style('stroke-opacity', 0.3)
}
};
}
const linkedByIndex = {};
links.forEach(d => {
linkedByIndex[`${d.source.index},${d.target.index}`] = 1;
});

function isConnected(a, b) {
return linkedByIndex[`${a.index},${b.index}`] || linkedByIndex[`${b.index},${a.index}`] || a.index === b.index;
}
invalidation.then(() => simulation.stop());
return svg.node()
}
Insert cell
height = 1000
Insert cell
drag = simulation => {
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.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
Insert cell
metadataCsv = FileAttachment("4_test__Adelaide_nodes@1.csv").text()
Insert cell
nodeMetadata = d3.csvParse(metadataCsv, d3.autoType);
Insert cell
dnodes.forEach(function(node) {
const meta = nodeMetadata.find(metadatum => metadatum["genre"] === node.name);

const radius = d3.scaleSqrt()
.domain([0, d3.max(data, node => node.count)])
.range([1, width / 50]);
if (meta == null) {
console.log("pass")
}
else {
node.relative = meta["relative"];
node.count = meta["count"];
node.radius = radius(meta['count']);
}
});
Insert cell
// statusColor.domain = statusDomain;
Insert cell
statusDomain = new Set(
nodeMetadata.map(function(m) {return m["relative"];})
);
Insert cell
Insert cell
d3 = require("d3@5")
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