D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
ConorAspell
Full window
Github gist
HillaryBlock
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <form> <input type="button" class="button" value="Betweeness" ng-click="graph.change('Betweeness')" > <input type="button" class="button" value="Degree" ng-click="graph.change('Degree')"> <input type="button" class="button" value="Eigenvector" ng-click="graph.change('Eigenvector')"> <input type="button" class="button" value="Closeness" ng-click="graph.change('Closeness')"> <input type="button" class="button" value="Harmonic" ng-click="graph.change('Harmonic')"> <input type="button" class="button" value="Communicability" ng-click="graph.change('Communicability')"> </form> <style> .links line { stroke: #999; stroke-opacity: 0.6; } .nodes circle { stroke: #fff; stroke-width: 1.5px; } </style> <svg width="960" height="600"></svg> <script src="Scripts/angular.js"></script> <script src="Scripts/angular-animate.js"></script> <script src="Scripts/angular-aria.min.js"></script> <script src="Scripts/angular-ui-router.js"></script> <script src="Scripts/angular-css.min.js"></script> <script src="Scripts/angular-cookies.js"></script> <script src="Scripts/angular-resource.js"></script> <script src="Scripts/angular-messages.min.js"></script> <script src="Scripts/angular-material.min.js"></script> <script src="https://d3js.org/d3.v4.min.js"></script> <script> var graph = this; graph.centrality = "Degree"; console.log(graph.centrality); var charge = -5000; var size = 100; graph.groups = []; graph.temp = []; graph.sel = [] var svg = d3.select("svg"), width = +svg.attr("width"), height = +svg.attr("height"); var color = d3.scaleOrdinal(d3.schemeCategory20); var simulation = d3.forceSimulation() .force("link", d3.forceLink().id(function(d) { return d.id; })) .force("charge", d3.forceManyBody().strength(function (d) { return d.Centrality[graph.centrality] * charge; })) .force("center", d3.forceCenter(width / 2, height / 2)); d3.json("hillaryJSON.json", function(error, graph) { if (error) throw error; console.log(graph) graph.groups = []; graph.temp = []; graph.sel = []; graph.centrality = "Degree"; for (var i = 0; i < graph.info.nodes.length; i++) { if (!graph.groups.includes(graph.info.nodes[i].question)) { graph.groups.push(graph.info.nodes[i].question); graph.sel.push(graph.info.nodes.question); graph.temp.push(graph.info.nodes.question); } } console.log(graph.groups); graph.changes = function (item) { if (graph.sel.includes(item)) { var index = graph.sel.indexOf(item); graph.sel.splice(index, 1); } else { graph.sel.push(item); } } graph.type = function (d) { return graph.sel.includes(d) ? true : false; } // graph.linkfilter = function (d) { //var result = graph.question.filter(function (obj) { return obj.id == d.target })[0]; //var result2 = graph.question.filter(function (obj) { return obj.id == d.source })[0]; //return !graph.sel.includes(result.question) || !graph.sel.includes(result2.question) ? false : true; //} //graph.nodefilter = function (d) { // return graph.sel.includes(d.question) ? true : false; //} //graph.linkfilterUpdate = function (d) { // var result = info.question.filter(function (obj) { return obj.id == d.target.id })[0]; // var result2 = info.question.filter(function (obj) { return obj.id == d.source.id })[0]; // return !graph.sel.includes(result.question) || !graph.sel.includes(result2.question) ? false : true; //} var link = svg.append("g") .attr("class", "links") .selectAll("line") .data(graph.info.links) .enter().append("line") .attr("stroke-width", function(d) { return Math.sqrt(d.value); }); var node = svg.append("g") .attr("class", "nodes") .selectAll("circle") .data(graph.info.nodes) .enter().append("circle") .attr("r", function (d) { return d.Centrality[graph.centrality]*size; }) .attr("fill", function(d) { return color(d.group); }) .attr("type", function (d) { return d.question }) .call(d3.drag() .on("start", dragstarted) .on("drag", dragged) .on("end", dragended)); var texts = svg.selectAll("text.label") .data(graph.info.nodes) .enter() .append("text") .attr("class", "label") .text(function (d) { return d.id; }); node.append("title") .text(function(d) { return d.id; }); node.append("span") .text(function (d) { return d.id }); node.append("text") .attr("dx", 12) .attr("dy", ".35em") .text(function (d) { return d.id }); simulation .nodes(graph.info.nodes) .on("tick", ticked); simulation.force("link") .links(graph.info.links); graph.change = function (value) { graph.centrality = value; simulation.force("charge", d3.forceManyBody().strength(function (d) { return d.Centrality[graph.centrality] * charge; })) node .attr("cx", function (d) { return d.x; }) .attr("cy", function (d) { return d.y; }) .attr("r", function (d) { return d.Centrality[graph.centrality] * size; }); restart(); } 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; }) .attr("r", function (d) { return d.Centrality[graph.centrality] *size; }); texts.attr("x", function (d) { return d.x; }) .attr("y", 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>
https://d3js.org/d3.v4.min.js