Created
November 7, 2016 21:59
-
-
Save daanraman/e1afebf80de9159528db8d2dc5a8aa2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<head> | |
<link rel="stylesheet" type="text/css" href="style.css"> | |
</head> | |
<div class="searchcontainer"> | |
<input id="search"> | |
<button type="button" id="searchbutton">Search</button> | |
</div> | |
<div id="container" class="svg-container"> | |
<svg width="1680" height="700"></svg> | |
</div> | |
<script type='text/javascript' src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.js"></script> | |
<script type='text/javascript' src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> | |
<script type='text/javascript' src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script> | |
<script type='text/javascript' src="https://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.7.1/d3-tip.min.js"></script> | |
<script> | |
// Data processing | |
// =============== | |
d3.json("network.json", function (error, graph) { | |
if (error) throw error; | |
// Define color palet to use for nodes | |
var color = d3.scaleOrdinal(d3.schemeCategory10); | |
// Tooltips | |
var tool_tip = d3 | |
.tip() | |
.attr("class", "d3-tip") | |
.offset([0, 0]) | |
.html(function (d) { | |
return "IP: " + d.id; | |
}); | |
// The SVG element already exists, get a reference to it | |
var svg = d3 | |
.select("svg") | |
.classed("svg-content", true) | |
g_links = svg.append("g").attr("class", "links") | |
g_nodes = svg.append("g").attr("class", "nodes") | |
svg.call(tool_tip); | |
var links = g_links | |
.selectAll("line") | |
.data(graph.links, function (d) { | |
return d; | |
}).enter() | |
.append("line") | |
.attr("stroke-width", function (d) { | |
return 0.3 | |
}) | |
var nodes = g_nodes | |
.selectAll("circle") | |
.data(graph.nodes, function (d) { | |
return d; | |
}) | |
.enter() | |
.append("circle") | |
.attr("r", function (d) { | |
return 2 + Math.log(d.value); | |
}) | |
.attr("fill", function (d) { | |
return color(d.group); | |
}) | |
.call(d3.drag() | |
.on("start", dragstarted) | |
.on("drag", dragged) | |
.on("end", dragended)) | |
.on('mouseover', tool_tip.show) //Added | |
.on('mouseout', tool_tip.hide); //Added | |
links.append("title") | |
.attr("dx", 12) | |
.attr("dy", ".35em") | |
.text(function (d) { | |
return d.has_ip_layer | |
}); | |
// Describe zoom behavior | |
var zoom = d3.zoom() | |
.scaleExtent([1 / 10, 4]) | |
.on("zoom", zoomed); | |
function zoomed() { | |
var transform = d3.event.transform; | |
nodes.attr("transform", d3.event.transform); | |
links.attr("transform", d3.event.transform); | |
} | |
// Describe forces to be used for the simulation | |
var simulation = d3.forceSimulation() | |
.force("link", d3.forceLink().id(function (d) { | |
return d.id; | |
})) | |
.force("center", d3.forceCenter(svg.attr("width") / 2, svg.attr("height") / 2)) | |
.force("charge", d3.forceManyBody().strength(-15)); | |
simulation | |
.nodes(graph.nodes) | |
.on("tick", ticked) | |
.force("link") | |
.links(graph.links); | |
svg | |
.style("fill", "none") | |
.style("pointer-events", "all") | |
.call(zoom); | |
function ticked() { | |
console.log("Tick called"); | |
links | |
.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; | |
}); | |
nodes | |
.attr("cx", function (d) { | |
return d.x; | |
}) | |
.attr("cy", function (d) { | |
return d.y; | |
}); | |
} | |
function dragsubject() { | |
searchRadius = 40; | |
return simulation.find(d3.event.x - svg.attr("height"), d3.event.y - svg.attr("height") / 2, searchRadius); | |
} | |
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; | |
} | |
// Search | |
var optArray = []; | |
for (var i = 0; i < svg.nodes.length - 1; i++) { | |
optArray.push(svg.nodes[i].id); | |
} | |
optArray = optArray.sort(); | |
$(function () { | |
$("#search").autocomplete({ | |
source: optArray | |
}); | |
}); | |
function searchNode() { | |
console.log("search clicked") | |
} | |
// Bind onclick | |
$('#searchbutton').click(function () { | |
searchNode(); | |
}); | |
}); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"links": [ | |
{ | |
"dst_ip": "192.168.20.183", | |
"dst_port": 35716, | |
"has_ip_layer": true, | |
"has_tcp_layer": true, | |
"source": "192.168.1.15", | |
"src_ip": "192.168.1.15", | |
"src_port": 80, | |
"target": "192.168.20.183", | |
"value": 1 | |
} | |
], | |
"nodes": [ | |
{ | |
"group": 1, | |
"id": "192.168.1.15", | |
"value": 3017 | |
}, | |
{ | |
"group": 1, | |
"id": "192.168.20.183", | |
"value": 3017 | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment