Difference between revisions of "MediaWiki:Network-graph.js"

From SUDEP Wiki
Jump to navigation Jump to search
 
(36 intermediate revisions by the same user not shown)
Line 1: Line 1:
var Ctrl = {
+
/*
baseUrl: '/site-links', // Where to find external files related to the network graph
 
  
canvas: {
+
Name: MediaWiki:Network-graph.js
width: 500, // pixels
+
Purpose: Build the network graph for the page being displayed
height: 500, // pixels
+
Parameters: title = the internal title of the reference (e.g., Cerebral_hemispheric_lateralization_in_cardiac_autonomic_control)
depth: 7, // levels to show (1 - 9)
+
depth = the number of levels to include in the graph (1 - 9)
},
+
Author: Alan O'Neill
 +
Release Date: September 23, 2019
  
style: {
+
*/
strokeWidth: 2,
+
 
strokeColor: [
+
var Json; // populated once when the page loads
'red', // level 0
+
var Ctrl; // rebuilt each time the graph is rendered on a given page (e.g., when resizing)
'orange', // level 1 ...
+
 
'yellow',
+
function init() {
'green',
+
Ctrl = {
'blue',
+
json: {},
'purple',
+
 
'#880000',
+
canvas: {
'#008800',
+
width: $('#network-graph').width(),
'#000088',
+
height: $('#network-graph').width(),
'#888800', // level 9
+
borderColor: 'black',
 +
borderWidth: '1px'
 +
},
 +
 
 +
color: [
 +
'#e6194B', // red
 +
'#f58231', // orange
 +
'#ffe119', // yellow
 +
'#3cb44b', // green
 +
'#42d4f4', // cyan
 +
'#4363d8', // blue
 +
'#000075', // navy
 +
'#f032e6', // magenta
 +
'#800000', // maroon
 +
'#9a6324', // brown
 
],
 
],
fillColor: [
 
'purple', // level 0
 
'blue', // level 1 ...
 
'green',
 
'yellow',
 
'orange',
 
'red',
 
'#FFFF00',
 
'#FF00FF',
 
'#0000FF',
 
'#00FF00', // level 9
 
],
 
},
 
  
physical: {
+
tocColor: '#48a5d1',
charge: -10,
+
 
linkDistance: 10,
+
circle: {
distance: 120,
+
radius: 14,
gravity: .01,
+
textXOffset: -4,
},
+
textYOffset: 4,
 +
fontSize: '12px',
 +
fontColor: 'white',
 +
fontWeight: 'bold',
 +
},
 +
 
 +
arrow: {
 +
size: 6,
 +
strokeWidth: 1, // 0 = no arrow
 +
color: 'grey',
 +
},
 +
 
 +
line: {
 +
strokeWidth: 2,
 +
color: 'grey',
 +
},
  
circle: {
+
physical: {
radius: 6,
+
charge: -10,
strokeWidth: 2, // 0 = no stroke
+
linkDistance: 10,
},
+
distance: 120,
 +
gravity: .02,
 +
},
  
arrow: {
+
force: null,
size: 6,
+
svg: null,
strokeWidth: 1, // 0 = no arrow
+
}
},
 
  
svg: null,
+
$.extend(Ctrl.json, Json);
nodes: null,
 
links: null,
 
force: null,
 
 
}
 
}
  
Line 66: Line 80:
 
}
 
}
 
);
 
);
 +
}
 +
 +
function inBounds(n) {
 +
// Adjust a coordinate so it is within the bounds of the canvas (remember, it's always square)
 +
// n - the number to adjust
 +
// Return the adjusted value
 +
 +
var min = Ctrl.circle.radius * 3;
 +
var max = Ctrl.canvas.width - min;
 +
return Math.min(Math.max(n, min), max);
 
}
 
}
  
 
function getQuery(name) {
 
function getQuery(name) {
// e.g., https://devapps.pathology.jhu.edu/sudepwiki/index.php/Excess_mortality_and_sudden_unexpected_death_in_epilepsy
+
// Return the value of a parameter in the query string
 +
// name - the name in the name/value pair
 +
 
 +
var value = '';
 +
var query = window.location.search.substring(1).split('&');
 +
for (var i = 0; i < query.length; i++) {
 +
var nv = query[i].split('=');
 +
if (nv[0] == name) {
 +
value = nv[1];
 +
break;
 +
}
 +
}
 +
 
 +
return value;
 +
}
 +
 
 +
function getBaseUrl() {
 +
// Return the base URL for the site (e.g., 'https://sudepwiki.pathology.jhmi.edu')
 +
 
 +
var baseUrl = location.protocol + '//' + location.host + location.pathname;
 +
var i = baseUrl.indexOf('/index.php');
 +
if (i < 0) { i = baseUrl.indexOf('/site-links'); }
 +
baseUrl = baseUrl.substr(0, i);
 +
 
 +
return baseUrl;
 +
}
 +
 
 +
function getTitle() {
 +
// Return the title of the article from the URL
 +
 
 +
var title = getQuery('title');
 +
if (title.length == 0) {
 +
var delim = '/index.php/';
 +
title = location.pathname.substr(location.pathname.indexOf(delim) + delim.length); // e.g., Cerebral_hemispheric_lateralization_in_cardiac_autonomic_control
 +
}
 +
title = decodeURIComponent(title);
  
var url = $(location).attr('href').split('/');
 
var title = url[url.length - 1];
 
 
return title;
 
return title;
 
}
 
}
  
function tickHandler() {
+
function getDepth() {
Ctrl.links .attr('x1', function(link) { return link.source.x; })
+
// Return the depth to use for the graph (?)
.attr('y1', function(link) { return link.source.y; })
+
 
.attr('x2', function(link) { return link.target.x; })
+
var depth = 9;
.attr('y2', function(link) { return link.target.y; });
 
  
Ctrl.nodes.attr('transform', function(node) { return 'translate(' + node.x + ',' + node.y + ')'; });
+
if (depth < 1) { depth = 1; }
 +
else if (depth > 9) { depth = 9; }
 +
 
 +
return depth;
 
}
 
}
  
function centerNode(node, index) {
+
function gotoPage(node, index) {
// Translate the graph to center around the selected node.
+
// Go to the page associated with a node
// node - the clicked node
+
// node - the node that was double clicked (object)
// index - the index of the clicked node in Ctrl.nodes[]
+
// index - the index of the node that was double clicked
  
for(var i = 0; i < Ctrl.nodes[0].length; i++) {
+
// When node.level is zero, the node refers to the page being displayed
if (i != index) {
+
if (node.level > 0) {
Ctrl.nodes[0][i].fixed = false;
+
window.location.href = getBaseUrl() + '/index.php/' + node.name;
Ctrl.nodes[0][i].cx = 0;
+
} else {
Ctrl.nodes[0][i].cy = 0;
+
alert("You're already viewing this page.");
} else {
 
Ctrl.nodes[0][i].fixed = true;
 
Ctrl.nodes[0][i].cx = Math.floor(Ctrl.canvas.width / 2);
 
Ctrl.nodes[0][i].cy = Math.floor(Ctrl.canvas.height / 2);
 
}
 
 
}
 
}
 +
};
 +
 +
function loadData(onSuccess) {
 +
$('#network-graph').html('Retrieving data for the network graph...');
 +
 +
$.ajax({
 +
method: 'get',
 +
url: getBaseUrl() + '/site-links/network-graph.php',
 +
data: { depth: getDepth(), title: getTitle() },
 +
dataType: 'json',
 +
success: function(data, textStatus, jqXHR) {
 +
if (data == 0) {
 +
$('#network-graph').html('No data is available for this graph.');
 +
} else {
 +
Json = data;
 +
onSuccess();
 +
}
 +
},
 +
error: function(jqXHR, textStatus, errorThrown) {
 +
$('#network-graph').html('An error occurred while retrieving data for this graph.');
 +
console.log('loadData: jqXHR =', jqXHR, '; textStatus =', textStatus, '; errorThrown =', errorThrown);
 +
},
 +
});
 +
}
 +
 +
function renderGraph() {
 +
if (typeof Json !== 'undefined') {
 +
$('#network-graph').html('Hover over a node to see the title of the article. Double-click on a node to view the article.<br />');
  
tickHandler();
+
init();
};
 
  
function main() {
+
// Adjust the canvas size based on the number of nodes
var title = getQuery('title');
+
var nodes = Json.nodes.length;
 +
if (nodes > 200) { Ctrl.circle.radius *= 2 / 3; }
 +
var max = Ctrl.canvas.width;
 +
var min = Math.sqrt(nodes) * Ctrl.circle.radius * 10;
 +
if (min < max) { Ctrl.canvas.width = Ctrl.canvas.height = min; }
  
// Create the canvas
+
// Create the canvas
Ctrl.svg = d3.select('#network-graph')
+
Ctrl.svg = d3.select('#network-graph')
.append('svg')
+
.append('svg')
.attr('width', Ctrl.canvas.width)
+
.attr('width', Ctrl.canvas.width)
.attr('height', Ctrl.canvas.height);
+
.attr('height', Ctrl.canvas.height);
  
// Define the arrow head
+
// Define the arrow head
for (var level = 0; level < 10; level++ ) {
+
var depth = getDepth();
let fullSize = Ctrl.arrow.size;
+
for (var level = 0; level < depth; level++ ) {
let halfSize = Math.floor(fullSize / 2);
+
let fullSize = Ctrl.arrow.size;
Ctrl.svg.append('defs').selectAll('marker')
+
let halfSize = Math.floor(fullSize / 2);
.data(['arrow' + level])
+
Ctrl.svg
.enter()
+
.append('defs')
.append('marker')
+
.selectAll('marker')
.attr('id', function(node, index) { return node; })
+
.data(['arrow' + level])
.attr('viewBox', '0 -' + halfSize + ' ' + fullSize + ' ' + fullSize)
+
.enter()
.attr('refX', Ctrl.circle.radius / 2 + Ctrl.arrow.size + Ctrl.circle.strokeWidth / 4)
+
.append('marker')
.attr('refY', 0)
+
.attr('id', function(node, index) { return node; }) // e.g., 'arrow0'
.attr('markerWidth', fullSize)
+
.attr('viewBox', '0 -' + halfSize + ' ' + fullSize + ' ' + fullSize)
.attr('markerHeight', fullSize)
+
.attr('refX', function(node, index) { return Math.floor(Ctrl.circle.radius / 2) + Ctrl.arrow.size; })
.attr('orient', 'auto')
+
.attr('refY', 0)
.append('path')
+
.attr('markerWidth', fullSize)
.attr('d', 'M0,-' + halfSize + 'L' + fullSize + ',0L0,' + halfSize + 'L' + fullSize + ',0L0,-' + halfSize)
+
.attr('markerHeight', fullSize)
.style('stroke', Ctrl.style.strokeColor[level])
+
.attr('orient', 'auto')
.style('stroke-width', Ctrl.arrow.strokeWidth)
+
.append('path')
.style('opacity', '0.6');
+
.attr('d', function(node, index) { return 'M0,-' + halfSize + 'L' + fullSize + ',0L0,' + halfSize + 'L' + fullSize + ',0L0,-' + halfSize; })
}
+
.style('stroke', Ctrl.arrow.color)
 +
.style('stroke-width', Ctrl.arrow.strokeWidth)
 +
}
  
// Define the characteristics of the force simulation
+
// Define the characteristics of the force simulation
Ctrl.force = d3.layout.force()
+
Ctrl.force = d3.layout
.size([Ctrl.canvas.width, Ctrl.canvas.height])
+
.force()
.charge(Ctrl.physical.charge)
+
.size([Ctrl.canvas.width, Ctrl.canvas.height])
.linkDistance(Ctrl.physical.linkDistance)
+
.charge(Ctrl.physical.charge)
.distance(Ctrl.physical.distance)
+
.linkDistance(Ctrl.physical.linkDistance)
.gravity(Ctrl.physical.gravity);
+
.distance(Ctrl.physical.distance)
 +
.gravity(Ctrl.physical.gravity);
  
// Read the data and use it to build the graph
 
var url = Ctrl.baseUrl + '/network-graph.php?depth=' + Ctrl.canvas.depth + '&title=' + title;
 
d3.json(url, function(json) {
 
 
// Introduce the data to the force simulation
 
// Introduce the data to the force simulation
 
Ctrl.force
 
Ctrl.force
.nodes(json.nodes)
+
.nodes(Ctrl.json.nodes)
.links(json.links);
+
.links(Ctrl.json.links);
  
 
// Add the links (lines and arrows)
 
// Add the links (lines and arrows)
Ctrl.links = Ctrl.svg
+
Ctrl.svg
 
.selectAll('.link')
 
.selectAll('.link')
.data(json.links)
+
.data(Ctrl.json.links)
 
.enter()
 
.enter()
 
.append('line')
 
.append('line')
 
.attr('class', 'link')
 
.attr('class', 'link')
.style('stroke', function(node, index) { return Ctrl.style.strokeColor[node.level]; })
+
.style('stroke', Ctrl.line.color)
.style('stroke-width', Ctrl.style.strokeWidth)
+
.style('stroke-width', Ctrl.line.strokeWidth)
.style('marker-end', function(node, index) { return 'url(#arrow' + node.level + ')'; });
+
.style('marker-end', function(node, index) { return 'url(#arrow0)'; });
  
// Add the nodes
+
// Add the nodes (circles)
Ctrl.nodes = Ctrl.svg
+
Ctrl.svg
 
.selectAll('.node')
 
.selectAll('.node')
.data(json.nodes)
+
.data(Ctrl.json.nodes)
 
.enter()
 
.enter()
 
.append('g')
 
.append('g')
 
.attr('class', 'node')
 
.attr('class', 'node')
.on('dblclick', centerNode)
+
.on('dblclick', gotoPage)
 
.append('circle')
 
.append('circle')
 
.attr('r', Ctrl.circle.radius)
 
.attr('r', Ctrl.circle.radius)
.style('stroke', function(node, index) { return Ctrl.style.strokeColor[node.level]; })
+
.style('fill', function(node, index) { return (node.level < 100) ? Ctrl.color[node.level] : Ctrl.tocColor; })
.style('stroke-width', Ctrl.circle.strokeWidth)
 
.style('fill', function(node, index) { return Ctrl.style.fillColor[node.level]; })
 
 
.call(Ctrl.force.drag);
 
.call(Ctrl.force.drag);
 
// Add text to each node
 
// Ctrl.nodes
 
// .append('text')
 
// .attr('dx', 12)
 
// .attr('dy', '.35em')
 
// .text(function(node, index) { return index; }); // was node.name, which is the title of the article
 
  
 
// Add a title to each node
 
// Add a title to each node
Ctrl.nodes
+
Ctrl.svg
 +
.selectAll('circle')
 
.append('title')
 
.append('title')
.data(json.nodes)
+
.text(function(node, index) {
.text(function(node, index) { return node.name.replace(/_/g, ' ').toTitleCase(); });
+
var title = node.name.replace(/_/g, ' ').toTitleCase();
 +
if (node.level == 0) { title += ' (this page)'; }
 +
return title;
 +
});
  
Ctrl.svg.append('polyline')
+
// Add the level number to each node
.style('stroke', 'black')
+
Ctrl.svg
 +
.selectAll('.node')
 +
.append('text')
 +
.attr('dx', Ctrl.circle.textXOffset)
 +
.attr('dy', Ctrl.circle.textYOffset)
 +
.text(function(node, index) { return (node.level < 100) ? node.level : ''; })
 +
.style('font-size', Ctrl.circle.fontSize)
 +
.style('fill', Ctrl.circle.fontColor)
 +
.style('font-weight', Ctrl.circle.fontWeight)
 +
.append('title')
 +
.text(function(node, index) {
 +
var title = node.name.replace(/_/g, ' ').toTitleCase();
 +
if (node.level == 0) { title += ' (this page)'; }
 +
return title;
 +
});
 +
 +
// Draw a border around the graph
 +
Ctrl.svg.append('rect')
 +
.attr('x', 0)
 +
.attr('y', 0)
 +
.attr('height', Ctrl.canvas.height - 1)
 +
.attr('width', Ctrl.canvas.width - 1)
 +
.style('stroke', Ctrl.canvas.borderColor)
 
.style('fill', 'none')
 
.style('fill', 'none')
.attr('points', '1,1,' +
+
.style('stroke-width', Ctrl.canvas.borderWidth);
+ Ctrl.canvas.width + ',1,'
 
+ Ctrl.canvas.width + ',' + Ctrl.canvas.height + ','
 
+ '1,' + Ctrl.canvas.height
 
);
 
  
 
// Set up the tick handler
 
// Set up the tick handler
Ctrl.force.on('tick', function() { tickHandler(); });
+
Ctrl.force.on('tick', function() {
 +
Ctrl.svg
 +
.selectAll('.link')
 +
.attr('x1', function(link) { return inBounds(link.source.x); })
 +
.attr('y1', function(link) { return inBounds(link.source.y); })
 +
.attr('x2', function(link) { return inBounds(link.target.x); })
 +
.attr('y2', function(link) { return inBounds(link.target.y); });
 +
 
 +
Ctrl.svg
 +
.selectAll('.node')
 +
.attr('transform', function(node) {
 +
return 'translate(' + inBounds(node.x) + ',' + inBounds(node.y) + ')';
 +
});
 +
});
  
 
// Start the force simulation
 
// Start the force simulation
Ctrl.force.start();
+
Ctrl.force.start();
 +
}
 +
}
 +
 
 +
function main() {
 +
$(window).resize(function() {
 +
renderGraph();
 
});
 
});
 +
 +
loadData(renderGraph);
 
}
 
}
  
 
$(document).ready(function() { main(); });
 
$(document).ready(function() { main(); });

Latest revision as of 14:06, 23 September 2019

/*

Name:		MediaWiki:Network-graph.js
Purpose:	Build the network graph for the page being displayed
Parameters:	title = the internal title of the reference (e.g., Cerebral_hemispheric_lateralization_in_cardiac_autonomic_control)
		depth = the number of levels to include in the graph (1 - 9)
Author:		Alan O'Neill
Release Date:	September 23, 2019

*/

var Json; // populated once when the page loads
var Ctrl; // rebuilt each time the graph is rendered on a given page (e.g., when resizing)

function init() {
	Ctrl = {
		json:	{},

		canvas: {
			width:		$('#network-graph').width(),
			height:		$('#network-graph').width(),
			borderColor:	'black',
			borderWidth:	'1px'
		},

		color: [
			'#e6194B',	// red
			'#f58231',	// orange
			'#ffe119',	// yellow
			'#3cb44b',	// green
			'#42d4f4',	// cyan
			'#4363d8',	// blue
			'#000075',	// navy
			'#f032e6',	// magenta
			'#800000',	// maroon
			'#9a6324',	// brown
		],

		tocColor:		'#48a5d1',

		circle: {
			radius:		14,
			textXOffset:	-4,
			textYOffset:	4,
			fontSize:	'12px',
			fontColor:	'white',
			fontWeight:	'bold',
		},

		arrow: {
			size:		6,
			strokeWidth:	1,		// 0 = no arrow
			color:		'grey',
		},

		line: {
			strokeWidth:	2,
			color:		'grey',
		},

		physical: {
			charge:		-10,
			linkDistance:	10,
			distance:	120,
			gravity:	.02,
		},

		force:			null,
		svg:			null,
	}

	$.extend(Ctrl.json, Json);
}

String.prototype.toTitleCase = function() {
	return this.replace(
		/\w\S*/g,
		function(txt) {
			return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
		}
	);
}

function inBounds(n) {
	// Adjust a coordinate so it is within the bounds of the canvas (remember, it's always square)
	// n - the number to adjust
	// Return the adjusted value
	
	var min = Ctrl.circle.radius * 3;
	var max = Ctrl.canvas.width - min;
	return Math.min(Math.max(n, min), max);
}

function getQuery(name) {
	// Return the value of a parameter in the query string
	// name - the name in the name/value pair

	var value = '';
	var query = window.location.search.substring(1).split('&');
	for (var i = 0; i < query.length; i++) {
		var nv = query[i].split('=');
		if (nv[0] == name) {
			value = nv[1];
			break;
		}
	}

	return value;
}

function getBaseUrl() {
	// Return the base URL for the site (e.g., 'https://sudepwiki.pathology.jhmi.edu')

	var baseUrl = location.protocol + '//' + location.host + location.pathname;
	var i = baseUrl.indexOf('/index.php');
	if (i < 0) { i = baseUrl.indexOf('/site-links'); }
	baseUrl = baseUrl.substr(0, i);

	return baseUrl;
}

function getTitle() {
	// Return the title of the article from the URL

	var title = getQuery('title');
	if (title.length == 0) {
		var delim = '/index.php/';
		title = location.pathname.substr(location.pathname.indexOf(delim) + delim.length); // e.g., Cerebral_hemispheric_lateralization_in_cardiac_autonomic_control
	}
	title = decodeURIComponent(title);

	return title;
}

function getDepth() {
	// Return the depth to use for the graph (?)

	var depth = 9;

	if (depth < 1) { depth = 1; }
	else if (depth > 9) { depth = 9; }

	return depth;
}

function gotoPage(node, index) {
	// Go to the page associated with a node
	// node - the node that was double clicked (object)
	// index - the index of the node that was double clicked

	// When node.level is zero, the node refers to the page being displayed
	if (node.level > 0) {
		window.location.href = getBaseUrl() + '/index.php/' + node.name;
	} else {
		alert("You're already viewing this page.");
	}
};

function loadData(onSuccess) {
	$('#network-graph').html('Retrieving data for the network graph...');

	$.ajax({
		method:		'get',
		url:		getBaseUrl() + '/site-links/network-graph.php',
		data:		{ depth: getDepth(), title: getTitle() },
		dataType:	'json',
		success:	function(data, textStatus, jqXHR) {
					if (data == 0) {
						$('#network-graph').html('No data is available for this graph.');
					} else {
						Json = data;
						onSuccess();
					}
				},
		error:		function(jqXHR, textStatus, errorThrown) {
					$('#network-graph').html('An error occurred while retrieving data for this graph.');
					console.log('loadData: jqXHR =', jqXHR, '; textStatus =', textStatus, '; errorThrown =', errorThrown);
				},
	});
}

function renderGraph() {
	if (typeof Json !== 'undefined') {
		$('#network-graph').html('Hover over a node to see the title of the article. Double-click on a node to view the article.<br />');

		init();

		// Adjust the canvas size based on the number of nodes
		var nodes = Json.nodes.length;
		if (nodes > 200) { Ctrl.circle.radius *= 2 / 3; }
		var max = Ctrl.canvas.width;
		var min = Math.sqrt(nodes) * Ctrl.circle.radius * 10;
		if (min < max) { Ctrl.canvas.width = Ctrl.canvas.height = min; }

		// Create the canvas
		Ctrl.svg = d3.select('#network-graph')
			.append('svg')
			.attr('width', Ctrl.canvas.width)
			.attr('height', Ctrl.canvas.height);

		// Define the arrow head
		var depth = getDepth();
		for (var level = 0; level < depth; level++ ) {
			let fullSize = Ctrl.arrow.size;
			let halfSize = Math.floor(fullSize / 2);
			Ctrl.svg
				.append('defs')
				.selectAll('marker')
				.data(['arrow' + level])
				.enter()
				.append('marker')
				.attr('id', function(node, index) { return node; }) // e.g., 'arrow0'
				.attr('viewBox', '0 -' + halfSize + ' ' + fullSize + ' ' + fullSize)
				.attr('refX', function(node, index) { return Math.floor(Ctrl.circle.radius / 2) + Ctrl.arrow.size; })
				.attr('refY', 0)
				.attr('markerWidth', fullSize)
				.attr('markerHeight', fullSize)
				.attr('orient', 'auto')
				.append('path')
				.attr('d', function(node, index) { return 'M0,-' + halfSize + 'L' + fullSize + ',0L0,' + halfSize + 'L' + fullSize + ',0L0,-' + halfSize; })
				.style('stroke', Ctrl.arrow.color)
				.style('stroke-width', Ctrl.arrow.strokeWidth)
		}

		// Define the characteristics of the force simulation
		Ctrl.force = d3.layout
			.force()
			.size([Ctrl.canvas.width, Ctrl.canvas.height])
			.charge(Ctrl.physical.charge)
			.linkDistance(Ctrl.physical.linkDistance)
			.distance(Ctrl.physical.distance)
			.gravity(Ctrl.physical.gravity);

		// Introduce the data to the force simulation
		Ctrl.force
			.nodes(Ctrl.json.nodes)
			.links(Ctrl.json.links);

		// Add the links (lines and arrows)
		Ctrl.svg
			.selectAll('.link')
			.data(Ctrl.json.links)
			.enter()
			.append('line')
			.attr('class', 'link')
			.style('stroke', Ctrl.line.color)
			.style('stroke-width', Ctrl.line.strokeWidth)
			.style('marker-end', function(node, index) { return 'url(#arrow0)'; });

		// Add the nodes (circles)
		Ctrl.svg
			.selectAll('.node')
			.data(Ctrl.json.nodes)
			.enter()
			.append('g')
			.attr('class', 'node')
			.on('dblclick', gotoPage)
			.append('circle')
			.attr('r', Ctrl.circle.radius)
			.style('fill', function(node, index) { return (node.level < 100) ? Ctrl.color[node.level] : Ctrl.tocColor; })
			.call(Ctrl.force.drag);

		// Add a title to each node
		Ctrl.svg
			.selectAll('circle')
			.append('title')
			.text(function(node, index) {
				var title = node.name.replace(/_/g, ' ').toTitleCase();
				if (node.level == 0) { title += ' (this page)'; }
				return title;
			});

		// Add the level number to each node
		Ctrl.svg
			.selectAll('.node')
			.append('text')
			.attr('dx', Ctrl.circle.textXOffset)
			.attr('dy', Ctrl.circle.textYOffset)
			.text(function(node, index) { return (node.level < 100) ? node.level : ''; })
			.style('font-size', Ctrl.circle.fontSize)
			.style('fill', Ctrl.circle.fontColor)
			.style('font-weight', Ctrl.circle.fontWeight)
			.append('title')
			.text(function(node, index) {
				var title = node.name.replace(/_/g, ' ').toTitleCase();
				if (node.level == 0) { title += ' (this page)'; }
				return title;
			});
			
		// Draw a border around the graph
		Ctrl.svg.append('rect')
			.attr('x', 0)
			.attr('y', 0)
			.attr('height', Ctrl.canvas.height - 1)
			.attr('width', Ctrl.canvas.width - 1)
			.style('stroke', Ctrl.canvas.borderColor)
			.style('fill', 'none')
			.style('stroke-width', Ctrl.canvas.borderWidth);

		// Set up the tick handler
		Ctrl.force.on('tick', function() {
			Ctrl.svg
				.selectAll('.link')
				.attr('x1', function(link) { return inBounds(link.source.x); })
				.attr('y1', function(link) { return inBounds(link.source.y); })
				.attr('x2', function(link) { return inBounds(link.target.x); })
				.attr('y2', function(link) { return inBounds(link.target.y); });

			Ctrl.svg
				.selectAll('.node')
				.attr('transform', function(node) {
					return 'translate(' + inBounds(node.x) + ',' + inBounds(node.y) + ')';
				});
		});

		// Start the force simulation
		Ctrl.force.start();	
	}
}

function main() {
	$(window).resize(function() {
		renderGraph();
	});

	loadData(renderGraph);
}

$(document).ready(function() { main(); });