1

Most libraries provide limited customization of the Sankey chart (like node color, width, padding).

Default Sankey chart (d3):

enter image description here

But what if I need to change node heights and center the nodes vertically? How I can do that?

My requirements:

enter image description here

Can anyone help me?

2 Answers2

0

Slightly hacky, but you could override the CSS rules to style the elements however you wish.

For example, if I have a d3-generated Sankey chart within a div named "sankey", I can add these CSS rules to change the rect elements to red, and the path elements that form the links to blue.

From some very brief testing, the rect elements seem to require the "!important" CSS modifier, hence the comment about it seeming a little hacky:

#sankey > svg > g > g > g > rect {
    fill: red !important;
}

#sankey > svg > g > g > path {
    stroke: blue;
    stroke-opacity: 0.7;
}

To make the rects larger, you could possibly set their stroke color to be the same as the fill, and set the stroke width to something large, and then play with the "nodePadding" parameter in the d3 API.

I've attached a screenshot of what I got to work by playing with these:

enter image description here

phil76
  • 134
  • 7
0

Without watching your code - it is a guessing game. So, I assume, you've used examples of sankey D3.js in the internet that are pretty much sharing same code. At some point you could see something like this:

var sankey = d3.sankey()
    .nodeWidth(12)
    .nodePadding(12)
    .size([width, height]);

Please, try to fiddle around with nodeWidth (enlarge the value) to get what you're after.

Roman T.
  • 333
  • 3
  • 10