Skip to content

Instantly share code, notes, and snippets.

@micahstubbs
Last active July 8, 2018 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save micahstubbs/e5d0c64e487a8920e6b775f1244f8486 to your computer and use it in GitHub Desktop.
Save micahstubbs/e5d0c64e487a8920e6b775f1244f8486 to your computer and use it in GitHub Desktop.
graph neighbors on mouseover
license: MIT
border: no
<!DOCTYPE html>
<meta charset='utf-8'>
<style>
.node circle {
stroke: white;
stroke-width: 1.5px;
opacity: 1.0;
}
line {
stroke: black;
stroke-width: 1.5px;
stroke-opacity: 1.0;
}
</style>
<body>
<script src='http://d3js.org/d3.v4.js'></script>
<script src='vis.js'></script>
</body>
0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'run', 'lint' ]
2 info using npm@3.10.9
3 info using node@v7.1.0
4 verbose stack Error: missing script: lint
4 verbose stack at run (/usr/local/lib/node_modules/npm/lib/run-script.js:151:19)
4 verbose stack at /usr/local/lib/node_modules/npm/lib/run-script.js:61:5
4 verbose stack at /usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:356:5
4 verbose stack at checkBinReferences_ (/usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:320:45)
4 verbose stack at final (/usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:354:3)
4 verbose stack at then (/usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:124:5)
4 verbose stack at /usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:243:12
4 verbose stack at /usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:78:16
4 verbose stack at tryToString (fs.js:425:3)
4 verbose stack at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:412:12)
5 verbose cwd /Users/m/workspace/graph-neighbors/02
6 error Darwin 15.6.0
7 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "lint"
8 error node v7.1.0
9 error npm v3.10.9
10 error missing script: lint
11 error If you need help, you may report this error at:
11 error <https://github.com/npm/npm/issues>
12 verbose exit [ 1, true ]
/* global d3 */
const graph = {
nodes: [
{ id: 0, size: 10 },
{ id: 1, size: 5 },
{ id: 2, size: 2 },
{ id: 3, size: 3 },
{ id: 4, size: 30 },
{ id: 5, size: 40 },
],
links: [
{ source: 0, target: 1 },
{ source: 0, target: 2 },
{ source: 1, target: 0 },
{ source: 3, target: 0 },
{ source: 4, target: 1 },
],
};
const width = 960;
const height = 500;
const mouseOverFunction = function (d) {
const circle = d3.select(this);
node
.transition(500)
.style('opacity', o => {
const isConnectedValue = isConnected(o, d);
if (isConnectedValue) {
return 1.0;
}
return 0.2
})
.style('fill', (o) => {
let fillColor;
if (isConnectedAsTarget(o, d) && isConnectedAsSource(o, d)) {
fillColor = 'green';
} else if (isConnectedAsSource(o, d)) {
fillColor = 'red';
} else if (isConnectedAsTarget(o, d)) {
fillColor = 'blue';
} else if (isEqual(o, d)) {
fillColor = 'hotpink';
} else {
fillColor = '#000';
}
return fillColor;
});
link
.transition(500)
.style('stroke-opacity', o => (o.source === d || o.target === d ? 1 : 0.2))
.transition(500)
.attr('marker-end', o => (o.source === d || o.target === d ? 'url(#arrowhead)' : 'url()'));
circle
.transition(500)
.attr('r', () => 1.4 * nodeRadius(d));
};
const mouseOutFunction = function () {
const circle = d3.select(this);
node
.transition(500);
link
.transition(500);
circle
.transition(500)
.attr('r', nodeRadius);
};
const nodes = graph.nodes;
const links = graph.links;
const simulation = d3.forceSimulation()
.force('link', d3.forceLink().id(d => d.id))
.force('charge', d3.forceManyBody())
.force('center', d3.forceCenter(width / 2, height / 2));
// const simulation = d3.forceSimulation()
// .nodes(nodes)
// .links(links)
// .charge(-3000)
// .friction(0.6)
// .gravity(0.6)
// .size([width, height])
// .start();
const svg = d3.select('body').append('svg')
.attr('width', width)
.attr('height', height);
let link = svg.selectAll('line')
.data(graph.links)
.enter().append('line');
let node = svg.selectAll('.node')
.data(graph.nodes)
.enter().append("g")
.attr('class', 'node');
node
.append('circle')
.attr("r", nodeRadius)
.on('mouseover', mouseOverFunction)
.on('mouseout', mouseOutFunction)
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
svg
.append('marker')
.attr('id', 'arrowhead')
.attr('refX', 6 + 7) // Controls the shift of the arrow head along the path
.attr('refY', 2)
.attr('markerWidth', 6)
.attr('markerHeight', 4)
.attr('orient', 'auto')
.append('path')
.attr('d', 'M 0,0 V 4 L6,2 Z');
link
.attr('marker-end', 'url()');
simulation
.nodes(graph.nodes)
.on('tick', ticked);
simulation.force('link')
.links(graph.links);
let linkedByIndex = {};
links.forEach((d) => {
linkedByIndex[`${d.source.index},${d.target.index}`] = true;
});
function isConnected(a, b) {
return isConnectedAsTarget(a, b) || isConnectedAsSource(a, b) || a.index === b.index;
}
function isConnectedAsSource(a, b) {
return linkedByIndex[`${a.index},${b.index}`];
}
function isConnectedAsTarget(a, b) {
return linkedByIndex[`${b.index},${a.index}`];
}
function isEqual(a, b) {
return a.index === b.index;
}
function ticked() {
link
.attr('x1', d => d.source.x)
.attr('y1', d => d.source.y)
.attr('x2', d => d.target.x)
.attr('y2', d => d.target.y);
node
.attr('transform', d => `translate(${d.x},${d.y})`);
}
function nodeRadius(d) { return Math.pow(40.0 * d.size, 1 / 3); }
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment