From 70108f8db744da336a72ab7e68422bffe89656bb Mon Sep 17 00:00:00 2001 From: futpib Date: Tue, 21 May 2019 14:12:34 +0300 Subject: [PATCH] Add `createEdgeLine` option (setter and getter) to allow overriding edge line without overriding the whole `createEdge` --- lib/create-edge-paths.js | 17 +++++++++-------- lib/render.js | 11 ++++++++++- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/create-edge-paths.js b/lib/create-edge-paths.js index 0b839ace..07a57d0b 100644 --- a/lib/create-edge-paths.js +++ b/lib/create-edge-paths.js @@ -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; @@ -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); }); @@ -65,7 +66,7 @@ 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); @@ -73,14 +74,14 @@ function calcPoints(g, e) { 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); @@ -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); @@ -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; diff --git a/lib/render.js b/lib/render.js index 36d235c7..ff09d21d 100644 --- a/lib/render.js +++ b/lib/render.js @@ -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"); @@ -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); @@ -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;