d3blocks / d3blocks

The Python library to create stand-alone and interactive d3 charts.
GNU General Public License v3.0
562 stars 52 forks source link

Sankey: order of entries #46

Open davide-aloi opened 9 months ago

davide-aloi commented 9 months ago

Hi there,

Again, thanks for the amazing work.

I have a question related to the sankey graph. It seems that, when drawing the graph, the order of some elements changes, so that they get swapped. Here is an example:

Screenshot 2023-12-06 at 18 08 44

As you can see, within the second level of the relationships, allergies_reaction_2 is followed by conditions_patient, and then by allergy_code. However, in the dataframe that I am using as input allergies_reaction_2 is followed by allergy_code.

I was wondering if there is a way of forcing the order?

Thanks a lot! Davide

erdogant commented 3 months ago

To generate the Sankey graphs, a quite complicated javascript is used. The variable options are shown below. Changing the order may not be so straightforward.


  function SankeyChart({
                           nodes, // an iterable of node objects (typically [{id}, …]); implied by links if missing
                           links // an iterable of link objects (typically [{source, target}, …])
                       }, {
                           format: format$1 = ",", // a function or format specifier for values in titles
                           align = "justify", // convenience shorthand for nodeAlign
                           nodeId = d => d.id, // given d in nodes, returns a unique identifier (string)
                           nodeGroup, // given d in nodes, returns an (ordinal) value for color
                           nodeGroups, // an array of ordinal values representing the node groups
                           nodeLabel, // given d in (computed) nodes, text to label the associated rect
                           nodeTitle = d => `${d.id}\n${format$1(d.value)}`, // given d in (computed) nodes, hover text
                           nodeAlign = align, // Sankey node alignment strategy: left, right, justify, center
                           nodeWidth = 15, // width of node rects
                           nodePadding = 10, // vertical separation between adjacent nodes
                           nodeLabelPadding = 6, // horizontal separation between node and label
                           nodeStroke = "currentColor", // stroke around node rects
                           nodeStrokeWidth, // width of stroke around node rects, in pixels
                           nodeStrokeOpacity, // opacity of stroke around node rects
                           nodeStrokeLinejoin, // line join for stroke around node rects
                           linkSource = ({source}) => source, // given d in links, returns a node identifier string
                           linkTarget = ({target}) => target, // given d in links, returns a node identifier string
                           linkValue = ({value}) => value, // given d in links, returns the quantitative value
                           linkPath = sankeyLinkHorizontal(), // given d in (computed) links, returns the SVG path
                           linkTitle = d => `${d.source.id} → ${d.target.id}\n${format$1(d.value)}`, // given d in (computed) links
                           linkColor = "source-target", // source, target, source-target, or static color
                           linkStrokeOpacity = 0.5, // link stroke opacity
                           linkMixBlendMode = "multiply", // link blending mode
                           colors = schemeTableau10, // array of colors
                           width = 640, // outer width, in pixels
                           height = 400, // outer height, in pixels
                           marginTop = 5, // top margin, in pixels
                           marginRight = 1, // right margin, in pixels
                           marginBottom = 5, // bottom margin, in pixels
                           marginLeft = 1, // left margin, in pixels
                       } = {}) {