Published
Edited
Oct 13, 2019
2 forks
1 star
Insert cell
Insert cell
chart = {
const scale = 3
const textOffset = 10
var data = data_gen(csv,["TREE", "MOUNTAIN"]);
const links = data.links.map(d => Object.create(d));
const nodes = data.nodes.map(d => Object.create(d));
var i;
for (i=0; i < nodes.length; i++){
if(nodes[i].__proto__.id == "BARN" || nodes[i].__proto__.id == "BRIDGE" || nodes[i].__proto__.id == "BUILDING" || nodes[i].__proto__.id == "CABIN" || nodes[i].__proto__.id == "DOCK" || nodes[i].__proto__.id == "FARM" || nodes[i].__proto__.id == "LIGHTHOUSE" || nodes[i].__proto__.id == "MILL" || nodes[i].__proto__.id == "STRUCTURE" || nodes[i].__proto__.id == "WINDMILL" || nodes[i].__proto__.id == "FENCE" || nodes[i].__proto__.id == "PATH" || nodes[i].__proto__.id == "FIRE") {
nodes[i].__proto__.group = "1";}
if(nodes[i].__proto__.id == "PERSON" || nodes[i].__proto__.id == "PORTRAIT"){
nodes[i].__proto__.group = "2";}
if(nodes[i].__proto__.id == "CLOUDS" || nodes[i].__proto__.id == "FOG" || nodes[i].__proto__.id == "SNOW"){
nodes[i].__proto__.group = "3";}
if(nodes[i].__proto__.id == "BUSHES" || nodes[i].__proto__.id == "CACTUS" || nodes[i].__proto__.id == "FLOWERS" || nodes[i].__proto__.id == "GRASS" || nodes[i].__proto__.id == "TREE"){
nodes[i].__proto__.group = "4";}
if(nodes[i].__proto__.id == "BEACH" || nodes[i].__proto__.id == "LAKE" || nodes[i].__proto__.id == "OCEAN" || nodes[i].__proto__.id == "WATERFALL" || nodes[i].__proto__.id == "WAVES" || nodes[i].__proto__.id == "BOAT" || nodes[i].__proto__.id == "RIVER"){
nodes[i].__proto__.group = "5";}
if(nodes[i].__proto__.id == "NIGHT" || nodes[i].__proto__.id == "MOON" || nodes[i].__proto__.id == "SUN" || nodes[i].__proto__.id == "AURORA_BOREALIS"){
nodes[i].__proto__.group = "6";}
if(nodes[i].__proto__.id == "HILLS" || nodes[i].__proto__.id == "MOUNTAIN" || nodes[i].__proto__.id == "RIVER ROCKS" || nodes[i].__proto__.id == "CLIFF" || nodes[i].__proto__.id == "ROCKS"){
nodes[i].__proto__.group = "7";}

}

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

const svg = d3.create("svg")
.attr("viewBox", [0, -30, width, height]);
/* .force("x", d3.forceX())
.force("y", d3.forceY());

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

*/

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

const node = svg.append("g")
.selectAll(".node")
.data(nodes)
.join("g")
.attr("class", "node")
.attr("fill", color)
.call(drag(simulation));

/*
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", 7)
.attr("fill", color)
.call(drag(simulation));
*/
const circle = node.append("circle")
.attr("r", 5)
.attr("fill", "#000000")
.call(drag(simulation));
const text = node.append("text")
.text(d => d.id)
.attr("font-family", "Arial")
.attr("font-size", "12px")
.attr("stroke", "#00000");

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);

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

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

//highlights paths
node.on("mouseover", function(d) {
var thisNode = d.id

d3.selectAll(".circleNode").attr("r", 6);
d3.select(this).attr("r", 12);

link.attr("opacity", function(d) {
return (d.source.id == thisNode || d.target.id == thisNode) ? 1 : 0.1
});

});
return svg.node();
}
Insert cell
csv = d3.csv("https://raw.githubusercontent.com/pdeshlab/Bob-Ross-Data-Viz/master/Bob%20Ross%20Data.csv");

Insert cell
function data_gen(csv, list)
{
var node = {};
var links = [];
var link = {};
var nodes = [];
var csv2 = csv.slice();
for (var j = 0; j < list.length; j++) {
for (var i = 0; i < csv2.length; i++) {
var row = csv2[i];
if (row[list[j]] == "0") {
csv2.splice(i,1);
i--;
}
}
}
for(var i = 0; i < csv2.length; i++) {
var row = csv2[i];
for (var key in row) {
if (row[key] == "1") {
node[key] = 1;
for (var k = 0; k < Math.max(nodes.length, 1); k++) {
if (nodes.length > 0 && nodes[k].id == key) {
break;
}
else if (k == Math.max(nodes.length, 1) - 1) {
nodes.push({id:key, group:1});
}
}
}
}
var j1 = 0;
for (var key1 in node) {
var j2 = 0;
for (var key2 in node) {
link = {};
if (j1 < j2) {
for (var k = 0; k < Math.max(links.length, 1); k++) {
if (links.length > 0 && links[k].source == key1 && links[k].target == key2) {
links[k].value += .1;
break;
}
else if (k == Math.max(links.length, 1) - 1) {
link.source = key1;
link.target = key2;
link.value = .1;
links.push(link);
}
}
}
j2++;
}
j1++;
}
node = {};
}
var data = {nodes: nodes, links: links};
return data;
}

Insert cell
height = 600
Insert cell
color = {
const scale = d3.scaleOrdinal(d3.schemeCategory10);
return d => scale(d.__proto__.group);
}
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
d3 = require("d3@5")
Insert cell
/*
TODO - investigate ways of making the variable relationships clearer: could we do the 3D version of this anne laure found? Should we implement the feature that when you hover over a node it darkens all of that node's connections? Should we implement labels of the nodes? Color coding nodes?

Thoughts - I think it might be a good idea to add in the extraneous columns we deleted (palm trees for example), because I don't think adding more nodes will hurt the visualization. Also, I think the portrait column should be deleted because it has no connections to any other nodes.


link highlight:
https://stackoverflow.com/questions/41244508/js-d3-js-steps-to-highlighting-of-adjacent-links



*/
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