|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <title>D3 Force Demo</title> |
| 5 | + <script src="/scripts/netc-lib.js"></script> |
| 6 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.min.js"></script> |
| 7 | + <script src="/scripts/netc-app.js"></script> |
| 8 | + <link rel="stylesheet" href="/styles/netc-app.css" type="text/css"> |
| 9 | + |
| 10 | + </head> |
| 11 | + <style> |
| 12 | + |
| 13 | + .links line { |
| 14 | + stroke: #999; |
| 15 | + stroke-opacity: 0.6; |
| 16 | + } |
| 17 | + |
| 18 | + .nodes circle { |
| 19 | + stroke: #fff; |
| 20 | + stroke-width: 1.5px; |
| 21 | + } |
| 22 | + |
| 23 | + .noselect { |
| 24 | + -webkit-touch-callout: none; |
| 25 | + -webkit-user-select: none; |
| 26 | + -khtml-user-select: none; |
| 27 | + -moz-user-select: none; |
| 28 | + -ms-user-select: none; |
| 29 | + user-select: none; |
| 30 | + pointer-events: none; |
| 31 | + } |
| 32 | + |
| 33 | + </style> |
| 34 | + <body> |
| 35 | + <h2>D3 Force Demo</h2> |
| 36 | + <svg width="1280" height="1280"></svg> |
| 37 | + <script> |
| 38 | + |
| 39 | + var svg = d3.select("svg"), |
| 40 | + width = +svg.attr("width"), |
| 41 | + height = +svg.attr("height"); |
| 42 | + |
| 43 | + var color = d3.scaleOrdinal(d3.schemeCategory20); |
| 44 | + |
| 45 | + var simulation = d3.forceSimulation() |
| 46 | + .force("link", d3.forceLink().id(function(d) { return d.id; }).distance(10).strength(2)) |
| 47 | + .force("collide", d3.forceCollide().radius(function(d) { return d.size/4; })) |
| 48 | + .force("charge", d3.forceManyBody().strength(function(d) { return -200; })) |
| 49 | + .force("x", d3.forceX()) |
| 50 | + .force("y", d3.forceY()) |
| 51 | + .force("center", d3.forceCenter(width / 2, height / 2)); |
| 52 | + |
| 53 | + d3.json("data.json", function(error, graph) { |
| 54 | + if (error) throw error; |
| 55 | + |
| 56 | + var link = svg.append("g") |
| 57 | + .attr("class", "links") |
| 58 | + .selectAll("line") |
| 59 | + .data(graph.edges) |
| 60 | + .enter().append("line") |
| 61 | + .attr("stroke-width", function(d) { return Math.sqrt(d.value); }); |
| 62 | + |
| 63 | + var node = svg.append("g") |
| 64 | + .classed("nodes", true) |
| 65 | + .selectAll("circle") |
| 66 | + .data(graph.nodes) |
| 67 | + .enter().append("g") |
| 68 | + .classed("nodes", true) |
| 69 | + .call( d3.drag() |
| 70 | + .on("start", dragstarted) |
| 71 | + .on("drag", dragged) |
| 72 | + .on("end", dragended)); |
| 73 | + |
| 74 | + node.append("circle") |
| 75 | + .attr("r", function(d) { return d.size/10; }) |
| 76 | + .attr("fill", function(d) { return d.color; }) |
| 77 | + |
| 78 | + node.append("text") |
| 79 | + // .classed('noselect', true) |
| 80 | + .attr("font-size", 10) |
| 81 | + .attr("dx", 8) |
| 82 | + .attr("dy", ".15em") |
| 83 | + .text(function(d) { return d.label }); |
| 84 | + |
| 85 | + simulation |
| 86 | + .nodes(graph.nodes) |
| 87 | + .on("tick", ticked); |
| 88 | + |
| 89 | + simulation.force("link") |
| 90 | + .links(graph.edges); |
| 91 | + |
| 92 | + function ticked() { |
| 93 | + link |
| 94 | + .attr("x1", function(d) { return d.source.x; }) |
| 95 | + .attr("y1", function(d) { return d.source.y; }) |
| 96 | + .attr("x2", function(d) { return d.target.x; }) |
| 97 | + .attr("y2", function(d) { return d.target.y; }); |
| 98 | + |
| 99 | + node.attr("transform", function(d) { return "translate("+d.x+","+d.y+")"; }); |
| 100 | + // node |
| 101 | + // .attr("cx", function(d) { return d.x; }) |
| 102 | + // .attr("cy", function(d) { return d.y; }); |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + function dragstarted(d) { |
| 107 | + console.log('drag started on',d); |
| 108 | + if (!d3.event.active) simulation.alphaTarget(0.3).restart(); |
| 109 | + d.fx = d.x; |
| 110 | + d.fy = d.y; |
| 111 | + } |
| 112 | + |
| 113 | + function dragged(d) { |
| 114 | + console.log('dragged',d); |
| 115 | + d.fx = d3.event.x; |
| 116 | + d.fy = d3.event.y; |
| 117 | + } |
| 118 | + |
| 119 | + function dragended(d) { |
| 120 | + console.log('drag ended on',d); |
| 121 | + if (!d3.event.active) simulation.alphaTarget(0); |
| 122 | + d.fx = null; |
| 123 | + d.fy = null; |
| 124 | + } |
| 125 | + |
| 126 | + </script> |
| 127 | + <script> |
| 128 | + // demonstrate that NetCreate system libraries are available |
| 129 | + const STORE = require('system/datastore'); |
| 130 | + STORE.Initialize(); |
| 131 | + STORE.Increment(); |
| 132 | + const $ = require('jquery'); |
| 133 | + $('body').append('<p>JQUERY ADDED THIS. Now D3 will turn me red.</p>'); |
| 134 | + // d3 is loaded |
| 135 | + // d3.select("body").style("background-color", "red"); |
| 136 | + </script> |
| 137 | + </body> |
| 138 | +</html> |
0 commit comments