Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add createEdgeLine option (setter and getter) #366

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions lib/create-edge-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ var intersectNode = require("./intersect/intersect-node");
var util = require("./util");
var d3 = require("./d3");
module.exports = createEdgePaths;
module.exports.createLine = createLine;

function createEdgePaths(selection, g, arrows) {
function createEdgePaths(selection, g, arrows, options) {
var previousPaths = selection.selectAll("g.edgePath")
.data(g.edges(), function(e) { return util.edgeToId(e); })
.classed("update", true);

var newPaths = enter(previousPaths, g);
var newPaths = enter(previousPaths, g, options);
exit(previousPaths, g);

var svgPaths = previousPaths.merge !== undefined ? previousPaths.merge(newPaths) : previousPaths;
Expand Down Expand Up @@ -44,7 +45,7 @@ function createEdgePaths(selection, g, arrows) {
.style("fill", "none");

util.applyTransition(domEdge, g)
.attr("d", function(e) { return calcPoints(g, e); });
.attr("d", function(e) { return calcPoints(g, e, options); });

util.applyStyle(domEdge, edge.style);
});
Expand All @@ -65,22 +66,22 @@ function makeFragmentRef(url, fragmentId) {
return baseUrl + "#" + fragmentId;
}

function calcPoints(g, e) {
function calcPoints(g, e, options) {
var edge = g.edge(e);
var tail = g.node(e.v);
var head = g.node(e.w);
var points = edge.points.slice(1, edge.points.length - 1);
points.unshift(intersectNode(tail, points[0]));
points.push(intersectNode(head, points[points.length - 1]));

return createLine(edge, points);
return options.createLine(edge, points);
}

function createLine(edge, points) {
var line = (d3.line || d3.svg.line)()
.x(function(d) { return d.x; })
.y(function(d) { return d.y; });

(line.curve || line.interpolate)(edge.curve);

return line(points);
Expand All @@ -95,7 +96,7 @@ function getCoords(elem) {
return { x: matrix.e, y: matrix.f };
}

function enter(svgPaths, g) {
function enter(svgPaths, g, options) {
var svgPathsEnter = svgPaths.enter().append("g")
.attr("class", "edgePath")
.style("opacity", 0);
Expand All @@ -105,7 +106,7 @@ function enter(svgPaths, g) {
var edge = g.edge(e);
var sourceElem = g.node(e.v).elem;
var points = _.range(edge.points.length).map(function() { return getCoords(sourceElem); });
return createLine(edge, points);
return options.createLine(edge, points);
});
svgPathsEnter.append("defs");
return svgPathsEnter;
Expand Down
11 changes: 10 additions & 1 deletion lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function render() {
var createClusters = require("./create-clusters");
var createEdgeLabels = require("./create-edge-labels");
var createEdgePaths = require("./create-edge-paths");
var createEdgeLine = createEdgePaths.createLine;
var positionNodes = require("./position-nodes");
var positionEdgeLabels = require("./position-edge-labels");
var positionClusters = require("./position-clusters");
Expand All @@ -29,7 +30,9 @@ function render() {

positionNodes(nodes, g);
positionEdgeLabels(edgeLabels, g);
createEdgePaths(edgePathsGroup, g, arrows);
createEdgePaths(edgePathsGroup, g, arrows, {
createLine: createEdgeLine
});

var clusters = createClusters(clustersGroup, g);
positionClusters(clusters, g);
Expand Down Expand Up @@ -61,6 +64,12 @@ function render() {
return fn;
};

fn.createEdgeLine = function(value) {
if (!arguments.length) return createEdgeLine;
createEdgeLine = value;
return fn;
};

fn.shapes = function(value) {
if (!arguments.length) return shapes;
shapes = value;
Expand Down