D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
mostaphaRoudsari
Full window
Github gist
Ladybug Analysis Tools Graph
<!DOCTYPE html> <meta charset="utf-8"> <title>Ladybug Analysis Tools</title> <style> body { overflow:hidden; margin:0; } line { stroke: black } text { font-family: "Helvetica", "sans", "sans-serif"; font-size: 10px; pointer-events: none; } .node { pointer-events: all; } .nodes circle { stroke: #fff; stroke-width: 1.5px; } circle { fill: } </style> <head> <script src="./d3.v4.1.1.min.js"></script> </head> <body> <script> var width = 960, height = 960, iconSize = 45, textPadding = 25; var graph = { "nodes": [ {"id": "Ladybug", "group": 1, "img": "ladybug.png", "size": 60}, {"id": "Honeybee", "group": 2, "img": "honeybee.png", "size": 60}, {"id": "Butterfly", "group": 3, "img": "butterfly.png", "size": 60}, {"id": "Weather_Data", "group": 1, "img": "weather_data.png", "size": 90}, {"id": "Psychrometric", "group": 1, "img": "Psychrometric.png", "size": 90}, {"id": "Renewables", "group": 1, "img": "renewables.png", "size": 90}, {"id": "Analysis", "group": 1, "img": "analysis.png", "size": 90}, {"id": "Energy", "group": 2, "img": "energy.png", "size": 90}, {"id": "Daylight", "group": 2, "img": "daylight.png", "size": 90}, {"id": "Therm", "group": 2, "img": "therm.png", "size": 90}, {"id": "Comfort", "group": 2, "img": "comfort.png", "size": 90}, {"id": "Airflow", "group": 3, "img": "airflow.png", "size": 90}, ], "links": [ {"source": "Ladybug", "target": "Honeybee", "value": 4}, {"source": "Honeybee", "target": "Butterfly", "value": 4}, {"source": "Butterfly", "target": "Ladybug", "value": 4}, {"source": "Ladybug", "target": "Weather_Data", "value": 4}, {"source": "Ladybug", "target": "Psychrometric", "value": 4}, {"source": "Ladybug", "target": "Renewables", "value": 4}, {"source": "Ladybug", "target": "Analysis", "value": 4}, {"source": "Honeybee", "target": "Energy", "value": 4}, {"source": "Honeybee", "target": "Daylight", "value": 4}, {"source": "Honeybee", "target": "Therm", "value": 4}, {"source": "Honeybee", "target": "Comfort", "value": 4}, {"source": "Butterfly", "target": "Airflow", "value": 4}, ] } var transform = d3.zoomIdentity; var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height) .call(d3.zoom().scaleExtent([1 / 2, 8]).on("zoom", zoomed)) .append("g") .attr("transform", "translate(0,0)"); function zoomed() { svg.attr("transform", d3.event.transform); } var simulation = d3.forceSimulation() .force("link", d3.forceLink().id(function(d) { return d.id; })) .force("charge", d3.forceManyBody().strength(-2500)) .force("center", d3.forceCenter(width / 2, height / 2)) .force("collide", d3.forceCollide().radius(100).iterations(8)) .force("y", d3.forceY(0)) .force("x", d3.forceX(0)) .on("tick", ticked); simulation.nodes(graph.nodes); simulation .force("link") .links(graph.links); var link = svg.selectAll(".link") .data(graph.links) .enter().append("line") .attr("class", "link"); var node = svg.selectAll(".node") .data(graph.nodes) .enter().append("g") .attr("class", "node"); node.append("text") .attr("dx", function(d) { return d.size / 2 + 4; }) .attr("dy", ".35em") .text(function(d) { return d.id; }); simulation.on("tick", function() { link.attr("x1", function(d) { return d.source.x; }) .attr("y1", function(d) { return d.source.y; }) .attr("x2", function(d) { return d.target.x; }) .attr("y2", function(d) { return d.target.y; }) node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }) }); node.append("image") .attr("xlink:href", function(d) { return d.img; }) .attr("x", function(d) { return -d.size / 2; }) .attr("y", function(d) { return -d.size / 2; }) .attr("width", function(d) { return d.size; }) .attr("height", function(d) { return d.size; }) .call(d3.drag() .on("start", dragstarted) .on("drag", dragged) .on("end", dragended)); // add titles d3.selectAll("image") .append("title") .text(function(d) { return d.id; }) function ticked() { link .attr("x1", function(d) { return d.source.x; }) .attr("y1", function(d) { return d.source.y; }) .attr("x2", function(d) { return d.target.x; }) .attr("y2", function(d) { return d.target.y; }); node .attr("cx", function(d) { return d.x; }) .attr("cy", function(d) { return d.y; }); } 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; } </script> </body>