Published
Edited
Jul 3, 2019
2 forks
6 stars
Insert cell
md`# Network with canvas`
Insert cell
Insert cell
visual2 = {
var canvas = DOM.canvas(1000,400),
context = canvas.getContext("2d"),
width = canvas.width,
height = canvas.height;
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.id; }))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));

function ticked() {
context.clearRect(0, 0, width, height);

context.beginPath();
graph.links.forEach(drawLink);
context.strokeStyle = "#aaa";
context.stroke();

context.beginPath();
graph.nodes.forEach(drawNode);
context.fill();
context.strokeStyle = "#fff";
context.stroke();
}

function dragsubject() {
return simulation.find(d3.event.x, d3.event.y);
}

function dragstarted() {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d3.event.subject.fx = d3.event.subject.x;
d3.event.subject.fy = d3.event.subject.y;
}

function dragged() {
d3.event.subject.fx = d3.event.x;
d3.event.subject.fy = d3.event.y;
}

function dragended() {
if (!d3.event.active) simulation.alphaTarget(0);
d3.event.subject.fx = null;
d3.event.subject.fy = null;
}

function drawLink(d) {
context.moveTo(d.source.x, d.source.y);
context.lineTo(d.target.x, d.target.y);
}

function drawNode(d) {
context.moveTo(d.x + 3, d.y);
context.arc(d.x, d.y, 3, 0, 2 * Math.PI);
}

simulation
.nodes(graph.nodes)
.on("tick", ticked);

simulation.force("link")
.links(graph.links);

d3.select(canvas)
.call(d3.drag()
.container(canvas)
.subject(dragsubject)
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));

console.log(graph);
return canvas;
}
Insert cell
md`## Play ground`
Insert cell
play01 = {
var canvas = DOM.canvas(1000,400),
context = canvas.getContext("2d"),
width = canvas.width,
height = canvas.height;
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.id; }))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));

function ticked() {
context.clearRect(0, 0, width, height);

context.beginPath();
graph.links.forEach(drawLink);
context.strokeStyle = "#aaa";
context.stroke();

context.beginPath();
graph.nodes.forEach(drawNode);
context.fill();
context.strokeStyle = "#fff";
context.stroke();
}

function dragsubject() {
return simulation.find(d3.event.x, d3.event.y);
}

function dragstarted() {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d3.event.subject.fx = d3.event.subject.x;
d3.event.subject.fy = d3.event.subject.y;
}

function dragged() {
d3.event.subject.fx = d3.event.x;
d3.event.subject.fy = d3.event.y;
}

function dragended() {
if (!d3.event.active) simulation.alphaTarget(0);
d3.event.subject.fx = null;
d3.event.subject.fy = null;
}

function drawLink(d) {
context.moveTo(d.source.x, d.source.y);
context.lineTo(d.target.x, d.target.y);
}

function drawNode(d) {
context.moveTo(d.x + 3, d.y);
context.arc(d.x, d.y, 3, 0, 2 * Math.PI);
}

simulation
.nodes(graph.nodes)
.on("tick", ticked);

simulation.force("link")
.links(graph.links);

d3.select(canvas)
.call(d3.drag()
.container(canvas)
.subject(dragsubject)
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));

console.log(graph);
return canvas;
}
Insert cell
md`## Static with worker
not working, cannot execute worker
https://bl.ocks.org/mbostock/01ab2e85e8727d6529d20391c0fd9a16`
Insert cell
worker2 = {

var nodes = d3.range(1000).map(function(i) {
return {
index: i
};
});

var links = d3.range(nodes.length - 1).map(function(i) {
return {
source: Math.floor(Math.sqrt(i)),
target: i + 1
};
});

var meter = document.querySelector("#progress"),
canvas = document.querySelector("canvas"),
context = canvas.getContext("2d"),
width = canvas.width,
height = canvas.height;

var worker = new Worker("worker.js");

worker.postMessage({
nodes: nodes,
links: links
});

worker.onmessage = function(event) {
switch (event.data.type) {
case "tick": return ticked(event.data);
case "end": return ended(event.data);
}
};

function ticked(data) {
var progress = data.progress;

meter.style.width = 100 * progress + "%";
}

function ended(data) {
var nodes = data.nodes,
links = data.links;

meter.style.display = "none";

context.clearRect(0, 0, width, height);
context.save();
context.translate(width / 2, height / 2);

context.beginPath();
links.forEach(drawLink);
context.strokeStyle = "#aaa";
context.stroke();

context.beginPath();
nodes.forEach(drawNode);
context.fill();
context.strokeStyle = "#fff";
context.stroke();

context.restore();
}

function drawLink(d) {
context.moveTo(d.source.x, d.source.y);
context.lineTo(d.target.x, d.target.y);
}

function drawNode(d) {
context.moveTo(d.x + 3, d.y);
context.arc(d.x, d.y, 3, 0, 2 * Math.PI);
}

}
Insert cell
md`# test zoom / not working`
Insert cell
visual3 = {
var w = 1000,
h = 400;
var canvas = DOM.canvas(w,h),
context = canvas.getContext("2d"),
width = canvas.width,
height = canvas.height;

var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.id; }))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));

function ticked() {
context.clearRect(0, 0, width, height);

context.beginPath();
graph.links.forEach(drawLink);
context.strokeStyle = "#aaa";
context.stroke();

context.beginPath();
graph.nodes.forEach(drawNode);
context.fill();
context.strokeStyle = "#fff";
context.stroke();
}

function dragsubject() {
return simulation.find(d3.event.x, d3.event.y);
}

function dragstarted() {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d3.event.subject.fx = d3.event.subject.x;
d3.event.subject.fy = d3.event.subject.y;
}

function dragged() {
d3.event.subject.fx = d3.event.x;
d3.event.subject.fy = d3.event.y;
}

function dragended() {
if (!d3.event.active) simulation.alphaTarget(0);
d3.event.subject.fx = null;
d3.event.subject.fy = null;
}

function drawLink(d) {
context.moveTo(d.source.x, d.source.y);
context.lineTo(d.target.x, d.target.y);
}

function drawNode(d) {
context.moveTo(d.x + 3, d.y);
context.arc(d.x, d.y, 3, 0, 2 * Math.PI);
}

d3.select(canvas).call(zoom);
simulation
.nodes(graph.nodes)
.on("tick", ticked);

simulation.force("link")
.links(graph.links);

d3.select(canvas)
.call(d3.drag()
.container(canvas)
.subject(dragsubject)
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));

return canvas;
}
Insert cell
w = 1000;
Insert cell
h = 400;

Insert cell

function zoom(sel) {

const context = sel.node().getContext('2d');
const zoomBehaviour = d3.zoom().on('zoom', zoomed);
sel.call(zoomBehaviour);
function zoomed() {
const t = d3.event.transform;

context.save();
context.clearRect(0,0,w,h)
context.translate(t.x, t.y);
context.scale(t.k, t.k);
context.beginPath(); // Comment this out for art's sake.
context.arc(w/2, h/2, h/4, 0, 2 * Math.PI);
context.stroke();

context.restore()
}
}
Insert cell
chart = {
const canvas = DOM.canvas(w, h);
const context = canvas.getContext('2d');

context.beginPath();
context.arc(w/2, h/2, h/4, 0, 2 * Math.PI);
context.stroke();
return canvas;
}
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