Skip to content

Commit 9be54eb

Browse files
committed
fix sankey trace x/y node fields
Closes #308 Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 8ee31bc commit 9be54eb

File tree

3 files changed

+60
-115
lines changed

3 files changed

+60
-115
lines changed

docs/book/src/recipes/basic_charts/sankey_diagrams.md

Lines changed: 9 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -15,114 +15,16 @@ use rand_distr::{Distribution, Normal, Uniform};
1515
The `to_inline_html` method is used to produce the html plot displayed in this page.
1616

1717
## Constructing a basic Sankey diagram
18-
```rust
19-
let trace = Sankey::new()
20-
.orientation(Orientation::Horizontal)
21-
.node(
22-
Node::new()
23-
.pad(15)
24-
.thickness(30)
25-
.line(SankeyLine::new().color(NamedColor::Black).width(0.5))
26-
.label(vec!["A1", "A2", "B1", "B2", "C1", "C2"])
27-
.color_array(vec![
28-
NamedColor::Blue,
29-
NamedColor::Blue,
30-
NamedColor::Blue,
31-
NamedColor::Blue,
32-
NamedColor::Blue,
33-
NamedColor::Blue,
34-
]),
35-
)
36-
.link(
37-
Link::new()
38-
.value(vec![8, 4, 2, 8, 4, 2])
39-
.source(vec![0, 1, 0, 2, 3, 3])
40-
.target(vec![2, 3, 3, 4, 4, 5]),
41-
);
18+
```rust,no_run
19+
{{#include ../../../../../examples/basic_charts/src/main.rs:basic_sankey_diagram}}
20+
```
4221

43-
let layout = Layout::new()
44-
.title("Basic Sankey".into())
45-
.font(Font::new().size(10));
22+
{{#include ../../../../../examples/basic_charts/output/inline_basic_sankey_diagram.html}}
4623

47-
let mut plot = Plot::new();
48-
plot.add_trace(trace);
49-
plot.set_layout(layout);
5024

51-
if show {
52-
plot.show();
53-
}
54-
}
25+
## Skankey diagram with defined node position
26+
```rust,no_run
27+
{{#include ../../../../../examples/basic_charts/src/main.rs:custom_node_sankey_diagram}}
5528
```
56-
<div id="basic-sankey-diagram" class="plotly-graph-div" style="height:100%; width:100%;"></div>
57-
<script type="text/javascript">
58-
window.PLOTLYENV=window.PLOTLYENV || {};
59-
if (document.getElementById("basic-sankey-diagram")) {
60-
Plotly.newPlot('basic-sankey-diagram', {
61-
"data": [
62-
{
63-
"type": "sankey",
64-
"orientation": "h",
65-
"node": {
66-
"color": [
67-
"blue",
68-
"blue",
69-
"blue",
70-
"blue",
71-
"blue",
72-
"blue"
73-
],
74-
"label": [
75-
"A1",
76-
"A2",
77-
"B1",
78-
"B2",
79-
"C1",
80-
"C2"
81-
],
82-
"line": {
83-
"color": "black",
84-
"width": 0.5
85-
},
86-
"pad": 15,
87-
"thickness": 30
88-
},
89-
"link": {
90-
"source": [
91-
0,
92-
1,
93-
0,
94-
2,
95-
3,
96-
3
97-
],
98-
"target": [
99-
2,
100-
3,
101-
3,
102-
4,
103-
4,
104-
5
105-
],
106-
"value": [
107-
8,
108-
4,
109-
2,
110-
8,
111-
4,
112-
2
113-
]
114-
}
115-
}
116-
],
117-
"layout": {
118-
"title": {
119-
"text": "Basic Sankey"
120-
},
121-
"font": {
122-
"size": 10
123-
}
124-
},
125-
"config": {}
126-
});
127-
};
128-
</script>
29+
30+
{{#include ../../../../../examples/basic_charts/output/inline_custom_node_sankey_diagram.html}}

examples/basic_charts/src/main.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,44 @@ fn basic_sankey_diagram(show: bool, file_name: &str) {
810810
}
811811
// ANCHOR_END: basic_sankey_diagram
812812

813+
// ANCHOR: custom_node_sankey_diagram
814+
fn custom_node_sankey_diagram(show: bool, file_name: &str) {
815+
// https://plotly.com/javascript/sankey-diagram/#basic-sankey-diagram
816+
let trace = Sankey::new()
817+
.orientation(Orientation::Horizontal)
818+
.arrangement(plotly::sankey::Arrangement::Snap)
819+
.node(
820+
Node::new()
821+
.pad(15)
822+
.thickness(30)
823+
.line(SankeyLine::new().color(NamedColor::Black).width(0.5))
824+
.label(vec!["A", "B", "C", "D", "E", "F"])
825+
.x(vec![0.2, 0.1, 0.5, 0.7, 0.3, 0.5])
826+
.y(vec![0.2, 0.1, 0.5, 0.7, 0.3, 0.5])
827+
.pad(20),
828+
)
829+
.link(
830+
Link::new()
831+
.source(vec![0, 0, 1, 2, 5, 4, 3, 5])
832+
.target(vec![5, 3, 4, 3, 0, 2, 2, 3])
833+
.value(vec![1, 2, 1, 1, 1, 1, 1, 2]),
834+
);
835+
836+
let layout = Layout::new()
837+
.title("Define Node Position")
838+
.font(Font::new().size(10));
839+
840+
let mut plot = Plot::new();
841+
plot.add_trace(trace);
842+
plot.set_layout(layout);
843+
844+
let path = write_example_to_html(&plot, file_name);
845+
if show {
846+
plot.show_html(path);
847+
}
848+
}
849+
// ANCHOR_END: custom_node_sankey_diagram
850+
813851
// ANCHOR: table_chart
814852
fn table_chart(show: bool, file_name: &str) {
815853
let trace = Table::new(
@@ -1006,6 +1044,7 @@ fn main() {
10061044

10071045
// Sankey Diagrams
10081046
basic_sankey_diagram(false, "basic_sankey_diagram");
1047+
custom_node_sankey_diagram(false, "custom_node_sankey_diagram");
10091048

10101049
// Pie Charts
10111050
basic_pie_chart(false, "basic_pie_chart");

plotly/src/traces/sankey.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@ pub struct Node {
5959
hover_template: Option<Dim<String>>,
6060
label: Option<Vec<String>>,
6161
line: Option<Line>,
62+
/// Sets the padding (in px) between the `nodes`.
6263
pad: Option<usize>,
64+
/// Sets the thickness (in px) of the `nodes`.
6365
thickness: Option<usize>,
64-
x: Option<f64>,
65-
y: Option<f64>,
66+
/// The normalized horizontal position of the node.
67+
x: Option<Vec<f64>>,
68+
/// The normalized vertical position of the node.
69+
y: Option<Vec<f64>>,
6670
}
6771

6872
impl Node {
@@ -115,12 +119,12 @@ impl Node {
115119
self
116120
}
117121

118-
pub fn x(mut self, x: f64) -> Self {
122+
pub fn x(mut self, x: Vec<f64>) -> Self {
119123
self.x = Some(x);
120124
self
121125
}
122126

123-
pub fn y(mut self, y: f64) -> Self {
127+
pub fn y(mut self, y: Vec<f64>) -> Self {
124128
self.y = Some(y);
125129
self
126130
}
@@ -509,8 +513,8 @@ mod tests {
509513
.line(Line::new())
510514
.pad(5)
511515
.thickness(10)
512-
.x(0.5)
513-
.y(0.25);
516+
.x(vec![0.5])
517+
.y(vec![0.25]);
514518
let expected = json!({
515519
"color": ["blue"],
516520
"hoverinfo": "all",
@@ -519,8 +523,8 @@ mod tests {
519523
"line": {},
520524
"pad": 5,
521525
"thickness": 10,
522-
"x": 0.5,
523-
"y": 0.25
526+
"x": [0.5],
527+
"y": [0.25]
524528
});
525529

526530
assert_eq!(to_value(node).unwrap(), expected)

0 commit comments

Comments
 (0)