-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Sankey: colorscales per component, linked to concentration #3501
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6f056eb
identify link per label, compute concentration and color accordingly
antoinerg 5e43023
sankey: fix attribute role and editType
antoinerg a791291
Merge branch 'master' into pr-sankey-link-concentration
antoinerg c228b37
sankey: forEach -> for loops, no FX on link hover if concentrationscale
antoinerg c976dbb
remove unused d3-array from Sankey
antoinerg ca6661c
sankey: test that eventData has appropriate flow statistics keys
antoinerg 60d0448
fix typo in mock's figure title
antoinerg f628b54
update packge-lock.json, remove newline
antoinerg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -288,6 +288,7 @@ module.exports = function plot(gd, calcData) { | |
}; | ||
|
||
render( | ||
gd, | ||
svg, | ||
calcData, | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
|
||
var c = require('./constants'); | ||
var d3 = require('d3'); | ||
var sum = require('d3-array').sum; | ||
var tinycolor = require('tinycolor2'); | ||
var Color = require('../../components/color'); | ||
var Drawing = require('../../components/drawing'); | ||
|
@@ -67,6 +68,57 @@ function sankeyModel(layout, d, traceIndex) { | |
Lib.warn('node.pad was reduced to ', sankey.nodePadding(), ' to fit within the figure.'); | ||
} | ||
|
||
function computeLinkConcentrations() { | ||
graph.nodes.forEach(function(node) { | ||
// Links connecting the same two nodes are part of a flow | ||
var flows = {}; | ||
node.targetLinks.forEach(function(link) { | ||
var flowKey = link.source.pointNumber + ':' + link.target.pointNumber; | ||
if(!flows.hasOwnProperty(flowKey)) flows[flowKey] = []; | ||
flows[flowKey].push(link); | ||
}); | ||
|
||
// Compute statistics for each flow | ||
Object.keys(flows).forEach(function(flowKey) { | ||
var flowLinks = flows[flowKey]; | ||
|
||
// Find the total size of the flow and total size per label | ||
var total = 0; | ||
var totalPerLabel = {}; | ||
flowLinks.forEach(function(link) { | ||
if(!totalPerLabel[link.label]) totalPerLabel[link.label] = 0; | ||
totalPerLabel[link.label] += link.value; | ||
total += link.value; | ||
}); | ||
|
||
// Find the ratio of the link's value and the size of the flow | ||
flowLinks.forEach(function(link) { | ||
link.flow = { | ||
value: total, | ||
labelConcentration: totalPerLabel[link.label] / total, | ||
concentration: link.value / total, | ||
links: flowLinks | ||
}; | ||
}); | ||
}); | ||
|
||
// Gather statistics of all links at current node | ||
var totalOutflow = sum(node.sourceLinks, function(n) { | ||
return n.value; | ||
}); | ||
node.sourceLinks.forEach(function(link) { | ||
link.concentrationOut = link.value / totalOutflow; | ||
}); | ||
var totalInflow = sum(node.targetLinks, function(n) { | ||
return n.value; | ||
}); | ||
node.targetLinks.forEach(function(link) { | ||
link.concenrationIn = link.value / totalInflow; | ||
}); | ||
}); | ||
} | ||
computeLinkConcentrations(); | ||
|
||
return { | ||
circular: circular, | ||
key: traceIndex, | ||
|
@@ -100,6 +152,9 @@ function sankeyModel(layout, d, traceIndex) { | |
|
||
function linkModel(d, l, i) { | ||
var tc = tinycolor(l.color); | ||
if(l.concentrationscale) { | ||
tc = tinycolor(l.concentrationscale(l.flow.labelConcentration)); | ||
} | ||
var basicKey = l.source.label + '|' + l.target.label; | ||
var key = basicKey + '__' + i; | ||
|
||
|
@@ -121,7 +176,8 @@ function linkModel(d, l, i) { | |
valueSuffix: d.valueSuffix, | ||
sankey: d.sankey, | ||
parent: d, | ||
interactionState: d.interactionState | ||
interactionState: d.interactionState, | ||
flow: l.flow | ||
}; | ||
} | ||
|
||
|
@@ -568,7 +624,7 @@ function switchToSankeyFormat(nodes) { | |
} | ||
|
||
// scene graph | ||
module.exports = function(svg, calcData, layout, callbacks) { | ||
module.exports = function(gd, svg, calcData, layout, callbacks) { | ||
|
||
var styledData = calcData | ||
.filter(function(d) {return unwrap(d).trace.visible;}) | ||
|
@@ -616,6 +672,7 @@ module.exports = function(svg, calcData, layout, callbacks) { | |
.attr('d', linkPath()) | ||
.call(attachPointerEvents, sankey, callbacks.linkEvents); | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⚡ no need to commit this newline. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in f628b54 |
||
sankeyLink | ||
.style('stroke', function(d) { | ||
return salientEnough(d) ? Color.tinyRGB(tinycolor(d.linkLineColor)) : d.tinyColorHue; | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
{ | ||
"data": [ | ||
{ | ||
"type": "sankey", | ||
"node": { | ||
"pad": 25, | ||
"line": { | ||
"color": "white", | ||
"width": 2 | ||
}, | ||
"label": ["process0", "process1", "process2", "process3", "process4"] | ||
}, | ||
"link": { | ||
"source": [ | ||
0, 0, 0, 0, | ||
1, 1, 1, 1, | ||
1, 1, 1, 1, | ||
1, 1, | ||
2 | ||
], | ||
"target": [ | ||
1, 1, 1, 1, | ||
2, 2, 2, 2, | ||
3, 3, 3, 3, | ||
4, 4, | ||
0 | ||
], | ||
"value": [ | ||
10, 20, 40, 30, | ||
10, 5, 10, 20, | ||
0, 10, 10, 10, | ||
15, 5, | ||
5 | ||
|
||
], | ||
"label": [ | ||
"elementA", "elementB", "elementC", "elementD", | ||
"elementA", "elementB", "elementC", "elementD", | ||
"elementA", "elementB", "elementC", "elementD", | ||
"elementC", "elementC", | ||
"elementA" | ||
], | ||
"line": { | ||
"color": "white", | ||
"width": 2 | ||
}, | ||
"colorscales": [ | ||
{ | ||
"label": "elementA", | ||
"colorscale": [[0, "white"], [1, "blue"]] | ||
}, | ||
{ | ||
"label": "elementB", | ||
"colorscale": [[0, "white"], [1, "red"]] | ||
}, | ||
{ | ||
"label": "elementC", | ||
"colorscale": [[0, "white"], [1, "green"]] | ||
}, | ||
{ | ||
"label": "elementD" | ||
} | ||
], | ||
|
||
"hovertemplate": "%{label}<br><b>flow.labelConcentration</b>: %{flow.labelConcentration:%0.2f}<br><b>flow.concentration</b>: %{flow.concentration:%0.2f}<br><b>flow.value</b>: %{flow.value}" | ||
} | ||
|
||
}], | ||
"layout": { | ||
"title": "Sankey with links colored based on its concentration within a flow", | ||
"width": 800, | ||
"height": 800 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha this here only works between
d3-sankey
already depends ond3-array
.We should add
d3-array
to the (plotly.js) dependencies OR use plain loops and save one line in ourpackage.json
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I will do the latter and save one line in our
package.json
.I should probably replace all the
forEach
while I'm at it right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be amazing 💪
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in c228b37