Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mayblue9/893df4ecc5680690cd6f18dffc84105e to your computer and use it in GitHub Desktop.
Save mayblue9/893df4ecc5680690cd6f18dffc84105e to your computer and use it in GitHub Desktop.
var Network = module.exports = View.extend({
// ...
/**
* Initialize the edge index.
*/
_initEdges: function() {
this.edgeIndex = new rbush();
this.edgeIndex.load(this.data.edgeIndex);
// Where `edgeIndex` is an array of:
// [x1, y1, x2, y2, {metadata}]
},
/**
* Clear the current background edges and render a new set of edges that
* fall within the current viewport. Called after the scene is panned.
*/
filterEdgesByExtent: function() {
// Query for visible edges.
var edges = this.edgeIndex.search(
this._getBoundingBox()
);
// Sort by edge weight.
var edges = _.sortBy(edges, function(e) {
return 1-e[4].weight
});
// Take the X heaviest edges.
var edges = _.first(edges, this.options.edgeCount);
// Clear current edges.
this.edgeGroup
.selectAll('line.background')
.remove();
_.each(edges, _.bind(function(e) {
// Render the new edges.
this.edgeGroup.append('line')
.classed({ background: true })
.datum({
x1: e[0],
y1: e[1],
x2: e[2],
y2: e[3]
});
}, this));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment