|
// Copyright (c) 2018, Moritz E. Beber |
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); |
|
// you may not use this file except in compliance with the License. |
|
// You may obtain a copy of the License at |
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0 |
|
|
|
// Unless required by applicable law or agreed to in writing, software |
|
// distributed under the License is distributed on an "AS IS" BASIS, |
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
// See the License for the specific language governing permissions and |
|
// limitations under the License. |
|
|
|
// Define the dimensions. |
|
var width = 300; |
|
var height = 300; |
|
|
|
// Create the SVG element. |
|
var svg = d3.select('body') |
|
.append('svg') |
|
.attr('width', width) |
|
.attr('height', height); |
|
|
|
// Shift every element. |
|
var g = svg.append('g'); |
|
// .attr('transform', 'translate(0, 0)'); |
|
|
|
var simulation = d3.forceSimulation() |
|
.force("link", d3.forceLink().id(function(d) { return d.name; })) |
|
.force("charge", d3.forceManyBody()) |
|
.force("center", d3.forceCenter(width / 2, height / 2)); |
|
|
|
// Read the data and create the visualization. |
|
d3.json('./network.json', function(error, graph) { |
|
if (error) throw error; |
|
|
|
console.log(graph); |
|
|
|
var link = g |
|
.selectAll('.link') |
|
.data(graph.links) |
|
.enter().append('line') |
|
.attr('class', 'link'); |
|
|
|
var node = g |
|
.selectAll('.node') |
|
.data(graph.nodes) |
|
.enter().append('g') |
|
.attr('class', 'node'); |
|
|
|
node.append('circle') |
|
.attr('r', '5'); |
|
|
|
node.append('text') |
|
.attr('dx', '12') |
|
.attr('dy', '.35em') |
|
.attr('text-anchor', 'start') |
|
.text(function(d) { return d.name }); |
|
|
|
simulation |
|
.nodes(graph.nodes) |
|
.on("tick", ticked); |
|
|
|
simulation.force("link") |
|
.links(graph.links); |
|
|
|
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('transform', function(d) { return 'translate(' + d.x + ',' + d.y + ')'; }); |
|
} |
|
|
|
simulation.on('end', function() { |
|
}); |
|
|
|
}); |