How to use the D3 diagonal function to draw curved lines?D3 Sankey chart using circle node instead of rectangle nodeDraw curve between two points using diagonal function in d3 jsIs there an “exists” function for jQuery?How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?var functionName = function() vs function functionName() How do I redirect to another webpage?How do I include a JavaScript file in another JavaScript file?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?How do I return the response from an asynchronous call?
Do Legal Documents Require Signing In Standard Pen Colors?
MAXDOP Settings for SQL Server 2014
How do I implement a file system driver driver in Linux?
Bob has never been a M before
Can we have a perfect cadence in a minor key?
How do I repair my stair bannister?
Query about absorption line spectra
Did US corporations pay demonstrators in the German demonstrations against article 13?
Difference between -| and |- in TikZ
Some numbers are more equivalent than others
Did arcade monitors have same pixel aspect ratio as TV sets?
Why does Async/Await work properly when the loop is inside the async function and not the other way around?
What is the grammatical term for “‑ed” words like these?
My friend sent me a screenshot of a transaction hash, but when I search for it I find divergent data. What happened?
Is XSS in canonical link possible?
Find last 3 digits of this monster number
Visiting the UK as unmarried couple
Does the Mind Blank spell prevent the target from being frightened?
Why did the EU agree to delay the Brexit deadline?
Indicating multiple different modes of speech (fantasy language or telepathy)
Engineer refusing to file/disclose patents
Should I install hardwood flooring or cabinets first?
When quoting, must I also copy hyphens used to divide words that continue on the next line?
Is there a conventional notation or name for the slip angle?
How to use the D3 diagonal function to draw curved lines?
D3 Sankey chart using circle node instead of rectangle nodeDraw curve between two points using diagonal function in d3 jsIs there an “exists” function for jQuery?How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?var functionName = function() vs function functionName() How do I redirect to another webpage?How do I include a JavaScript file in another JavaScript file?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?How do I return the response from an asynchronous call?
I looked at sample http://bl.ocks.org/mbostock/raw/4063570/:

It produces nice merged lines from source target from left to right.
In my case I need to layout nodes manually and put x, y coordinates. In this case the lines are not merged at source nodes. Here is the test code that reproduce this problem:
var data = [ name: "p1", children: [name: "c1", name: "c2", name: "c3", name: "c4"]];
var width = 400, height = 200, radius = 10, gap = 50;
// test layout
var nodes = [];
var links = [];
data.forEach(function(d, i)
d.x = width/4;
d.y = height/2;
nodes.push(d);
d.children.forEach(function(c, i)
c.x = 3*width/4;
c.y = gap * (i +1) -2*radius;
nodes.push(c);
links.push(source: d, target: c);
)
)
var color = d3.scale.category20();
var svg = d3.select("#chart").append("svg")
.attr("width", width)
.attr("height", height)
.append("g");
var diagonal = d3.svg.diagonal()
.projection(function(d) return [d.x, d.y]; );
var link = svg.selectAll(".link")
.data(links)
.enter().append("path")
.attr("class", "link")
.attr("d", diagonal);
var circle = svg.selectAll(".circle")
.data(nodes)
.enter()
.append("g")
.attr("class", "circle");
var el = circle.append("circle")
.attr("cx", function(d) return d.x)
.attr("cy", function(d) return d.y)
.attr("r", radius)
.style("fill", function(d) return color(d.name))
.append("title").text(function(d) return d.name);
There is sample of this at http://jsfiddle.net/zmagdum/qsEbd/:

However, it looks like the behavior of curves close to nodes are opposite of desired. I would like them to start straight horizontally at the nodes and make a curve in the middle. Is there a trick to do this?
javascript svg d3.js curve
add a comment |
I looked at sample http://bl.ocks.org/mbostock/raw/4063570/:

It produces nice merged lines from source target from left to right.
In my case I need to layout nodes manually and put x, y coordinates. In this case the lines are not merged at source nodes. Here is the test code that reproduce this problem:
var data = [ name: "p1", children: [name: "c1", name: "c2", name: "c3", name: "c4"]];
var width = 400, height = 200, radius = 10, gap = 50;
// test layout
var nodes = [];
var links = [];
data.forEach(function(d, i)
d.x = width/4;
d.y = height/2;
nodes.push(d);
d.children.forEach(function(c, i)
c.x = 3*width/4;
c.y = gap * (i +1) -2*radius;
nodes.push(c);
links.push(source: d, target: c);
)
)
var color = d3.scale.category20();
var svg = d3.select("#chart").append("svg")
.attr("width", width)
.attr("height", height)
.append("g");
var diagonal = d3.svg.diagonal()
.projection(function(d) return [d.x, d.y]; );
var link = svg.selectAll(".link")
.data(links)
.enter().append("path")
.attr("class", "link")
.attr("d", diagonal);
var circle = svg.selectAll(".circle")
.data(nodes)
.enter()
.append("g")
.attr("class", "circle");
var el = circle.append("circle")
.attr("cx", function(d) return d.x)
.attr("cy", function(d) return d.y)
.attr("r", radius)
.style("fill", function(d) return color(d.name))
.append("title").text(function(d) return d.name);
There is sample of this at http://jsfiddle.net/zmagdum/qsEbd/:

However, it looks like the behavior of curves close to nodes are opposite of desired. I would like them to start straight horizontally at the nodes and make a curve in the middle. Is there a trick to do this?
javascript svg d3.js curve
add a comment |
I looked at sample http://bl.ocks.org/mbostock/raw/4063570/:

It produces nice merged lines from source target from left to right.
In my case I need to layout nodes manually and put x, y coordinates. In this case the lines are not merged at source nodes. Here is the test code that reproduce this problem:
var data = [ name: "p1", children: [name: "c1", name: "c2", name: "c3", name: "c4"]];
var width = 400, height = 200, radius = 10, gap = 50;
// test layout
var nodes = [];
var links = [];
data.forEach(function(d, i)
d.x = width/4;
d.y = height/2;
nodes.push(d);
d.children.forEach(function(c, i)
c.x = 3*width/4;
c.y = gap * (i +1) -2*radius;
nodes.push(c);
links.push(source: d, target: c);
)
)
var color = d3.scale.category20();
var svg = d3.select("#chart").append("svg")
.attr("width", width)
.attr("height", height)
.append("g");
var diagonal = d3.svg.diagonal()
.projection(function(d) return [d.x, d.y]; );
var link = svg.selectAll(".link")
.data(links)
.enter().append("path")
.attr("class", "link")
.attr("d", diagonal);
var circle = svg.selectAll(".circle")
.data(nodes)
.enter()
.append("g")
.attr("class", "circle");
var el = circle.append("circle")
.attr("cx", function(d) return d.x)
.attr("cy", function(d) return d.y)
.attr("r", radius)
.style("fill", function(d) return color(d.name))
.append("title").text(function(d) return d.name);
There is sample of this at http://jsfiddle.net/zmagdum/qsEbd/:

However, it looks like the behavior of curves close to nodes are opposite of desired. I would like them to start straight horizontally at the nodes and make a curve in the middle. Is there a trick to do this?
javascript svg d3.js curve
I looked at sample http://bl.ocks.org/mbostock/raw/4063570/:

It produces nice merged lines from source target from left to right.
In my case I need to layout nodes manually and put x, y coordinates. In this case the lines are not merged at source nodes. Here is the test code that reproduce this problem:
var data = [ name: "p1", children: [name: "c1", name: "c2", name: "c3", name: "c4"]];
var width = 400, height = 200, radius = 10, gap = 50;
// test layout
var nodes = [];
var links = [];
data.forEach(function(d, i)
d.x = width/4;
d.y = height/2;
nodes.push(d);
d.children.forEach(function(c, i)
c.x = 3*width/4;
c.y = gap * (i +1) -2*radius;
nodes.push(c);
links.push(source: d, target: c);
)
)
var color = d3.scale.category20();
var svg = d3.select("#chart").append("svg")
.attr("width", width)
.attr("height", height)
.append("g");
var diagonal = d3.svg.diagonal()
.projection(function(d) return [d.x, d.y]; );
var link = svg.selectAll(".link")
.data(links)
.enter().append("path")
.attr("class", "link")
.attr("d", diagonal);
var circle = svg.selectAll(".circle")
.data(nodes)
.enter()
.append("g")
.attr("class", "circle");
var el = circle.append("circle")
.attr("cx", function(d) return d.x)
.attr("cy", function(d) return d.y)
.attr("r", radius)
.style("fill", function(d) return color(d.name))
.append("title").text(function(d) return d.name);
There is sample of this at http://jsfiddle.net/zmagdum/qsEbd/:

However, it looks like the behavior of curves close to nodes are opposite of desired. I would like them to start straight horizontally at the nodes and make a curve in the middle. Is there a trick to do this?
javascript svg d3.js curve
javascript svg d3.js curve
edited May 17 '15 at 17:11
VividD
8,50565097
8,50565097
asked Feb 21 '13 at 16:53
John SmithJohn Smith
121126
121126
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
This solution is based on excellent @bmdhacks solution, however, I believe mine is slightly better, since it doesn't require swapping x and y within data itself.
The idea is that you can use diagonal.source() and diagonal.target() to swap x and y:
var diagonal = d3.svg.diagonal()
.source(function(d) return "x":d.source.y, "y":d.source.x; )
.target(function(d) return "x":d.target.y, "y":d.target.x; )
.projection(function(d) return [d.y, d.x]; );
All x y swapping is now encapsulated within the code above.
The result:

Here is also jsfiddle.
Thanks for giving this example showing that the expected return values forsourceandprojectionare different.
– David J.
Sep 10 '14 at 14:35
What if there are multiple links between two nodes? How can I show it without overlapping?
– Jerry
May 17 '17 at 11:06
add a comment |
Note that in the blocks example, the x and y values are swapped in the links. This would normally draw the links in the wrong place, but he's also supplied a projection function that swaps them back.
var diagonal = d3.svg.diagonal()
.projection(function(d) return [d.y, d.x]; );
Here's your jsfiddle with this technique applied:
http://jsfiddle.net/bmdhacks/qsEbd/5/
What to do if i want to make link between two children? Like first child linked with second child in first row after root?
– Sohail Ahmad
Jul 24 '13 at 10:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15007877%2fhow-to-use-the-d3-diagonal-function-to-draw-curved-lines%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This solution is based on excellent @bmdhacks solution, however, I believe mine is slightly better, since it doesn't require swapping x and y within data itself.
The idea is that you can use diagonal.source() and diagonal.target() to swap x and y:
var diagonal = d3.svg.diagonal()
.source(function(d) return "x":d.source.y, "y":d.source.x; )
.target(function(d) return "x":d.target.y, "y":d.target.x; )
.projection(function(d) return [d.y, d.x]; );
All x y swapping is now encapsulated within the code above.
The result:

Here is also jsfiddle.
Thanks for giving this example showing that the expected return values forsourceandprojectionare different.
– David J.
Sep 10 '14 at 14:35
What if there are multiple links between two nodes? How can I show it without overlapping?
– Jerry
May 17 '17 at 11:06
add a comment |
This solution is based on excellent @bmdhacks solution, however, I believe mine is slightly better, since it doesn't require swapping x and y within data itself.
The idea is that you can use diagonal.source() and diagonal.target() to swap x and y:
var diagonal = d3.svg.diagonal()
.source(function(d) return "x":d.source.y, "y":d.source.x; )
.target(function(d) return "x":d.target.y, "y":d.target.x; )
.projection(function(d) return [d.y, d.x]; );
All x y swapping is now encapsulated within the code above.
The result:

Here is also jsfiddle.
Thanks for giving this example showing that the expected return values forsourceandprojectionare different.
– David J.
Sep 10 '14 at 14:35
What if there are multiple links between two nodes? How can I show it without overlapping?
– Jerry
May 17 '17 at 11:06
add a comment |
This solution is based on excellent @bmdhacks solution, however, I believe mine is slightly better, since it doesn't require swapping x and y within data itself.
The idea is that you can use diagonal.source() and diagonal.target() to swap x and y:
var diagonal = d3.svg.diagonal()
.source(function(d) return "x":d.source.y, "y":d.source.x; )
.target(function(d) return "x":d.target.y, "y":d.target.x; )
.projection(function(d) return [d.y, d.x]; );
All x y swapping is now encapsulated within the code above.
The result:

Here is also jsfiddle.
This solution is based on excellent @bmdhacks solution, however, I believe mine is slightly better, since it doesn't require swapping x and y within data itself.
The idea is that you can use diagonal.source() and diagonal.target() to swap x and y:
var diagonal = d3.svg.diagonal()
.source(function(d) return "x":d.source.y, "y":d.source.x; )
.target(function(d) return "x":d.target.y, "y":d.target.x; )
.projection(function(d) return [d.y, d.x]; );
All x y swapping is now encapsulated within the code above.
The result:

Here is also jsfiddle.
edited Oct 5 '17 at 17:52
answered May 3 '14 at 15:14
VividDVividD
8,50565097
8,50565097
Thanks for giving this example showing that the expected return values forsourceandprojectionare different.
– David J.
Sep 10 '14 at 14:35
What if there are multiple links between two nodes? How can I show it without overlapping?
– Jerry
May 17 '17 at 11:06
add a comment |
Thanks for giving this example showing that the expected return values forsourceandprojectionare different.
– David J.
Sep 10 '14 at 14:35
What if there are multiple links between two nodes? How can I show it without overlapping?
– Jerry
May 17 '17 at 11:06
Thanks for giving this example showing that the expected return values for
source and projection are different.– David J.
Sep 10 '14 at 14:35
Thanks for giving this example showing that the expected return values for
source and projection are different.– David J.
Sep 10 '14 at 14:35
What if there are multiple links between two nodes? How can I show it without overlapping?
– Jerry
May 17 '17 at 11:06
What if there are multiple links between two nodes? How can I show it without overlapping?
– Jerry
May 17 '17 at 11:06
add a comment |
Note that in the blocks example, the x and y values are swapped in the links. This would normally draw the links in the wrong place, but he's also supplied a projection function that swaps them back.
var diagonal = d3.svg.diagonal()
.projection(function(d) return [d.y, d.x]; );
Here's your jsfiddle with this technique applied:
http://jsfiddle.net/bmdhacks/qsEbd/5/
What to do if i want to make link between two children? Like first child linked with second child in first row after root?
– Sohail Ahmad
Jul 24 '13 at 10:52
add a comment |
Note that in the blocks example, the x and y values are swapped in the links. This would normally draw the links in the wrong place, but he's also supplied a projection function that swaps them back.
var diagonal = d3.svg.diagonal()
.projection(function(d) return [d.y, d.x]; );
Here's your jsfiddle with this technique applied:
http://jsfiddle.net/bmdhacks/qsEbd/5/
What to do if i want to make link between two children? Like first child linked with second child in first row after root?
– Sohail Ahmad
Jul 24 '13 at 10:52
add a comment |
Note that in the blocks example, the x and y values are swapped in the links. This would normally draw the links in the wrong place, but he's also supplied a projection function that swaps them back.
var diagonal = d3.svg.diagonal()
.projection(function(d) return [d.y, d.x]; );
Here's your jsfiddle with this technique applied:
http://jsfiddle.net/bmdhacks/qsEbd/5/
Note that in the blocks example, the x and y values are swapped in the links. This would normally draw the links in the wrong place, but he's also supplied a projection function that swaps them back.
var diagonal = d3.svg.diagonal()
.projection(function(d) return [d.y, d.x]; );
Here's your jsfiddle with this technique applied:
http://jsfiddle.net/bmdhacks/qsEbd/5/
answered May 15 '13 at 17:55
bmdhacksbmdhacks
13.4k62852
13.4k62852
What to do if i want to make link between two children? Like first child linked with second child in first row after root?
– Sohail Ahmad
Jul 24 '13 at 10:52
add a comment |
What to do if i want to make link between two children? Like first child linked with second child in first row after root?
– Sohail Ahmad
Jul 24 '13 at 10:52
What to do if i want to make link between two children? Like first child linked with second child in first row after root?
– Sohail Ahmad
Jul 24 '13 at 10:52
What to do if i want to make link between two children? Like first child linked with second child in first row after root?
– Sohail Ahmad
Jul 24 '13 at 10:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15007877%2fhow-to-use-the-d3-diagonal-function-to-draw-curved-lines%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown