Skip to content

Instantly share code, notes, and snippets.

@jfsiii jfsiii/.block

Created Nov 7, 2016
Embed
What would you like to do?
license: mit
<!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 linksUpdate = g_links
.selectAll("line")
.data(graph.links, function (d) {
return d;
})
var linksEnter = linksUpdate.enter()
.append("line")
.attr("stroke-width", function (d) {
return 0.3
});
var linksMerge = linksUpdate.merge(linksEnter);
var nodesUpdate = g_nodes
.selectAll("circle")
.data(graph.nodes, function (d) {
return d;
})
var nodesEnter = nodesUpdate.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
var nodesMerge = nodesUpdate.merge(nodesEnter);
nodesMerge.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");
linksMerge
.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;
});
nodesMerge
.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>
{
"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
You can’t perform that action at this time.