From a168028bb841cedfaf2158fa07e11f0f2a29c377 Mon Sep 17 00:00:00 2001 From: Andrei Gherghescu <8067229+andrei-ng@users.noreply.github.com> Date: Wed, 18 Jun 2025 21:44:15 +0100 Subject: [PATCH] fix sankey trace x/y node fields Closes #308 Signed-off-by: Andrei Gherghescu <8067229+andrei-ng@users.noreply.github.com> --- .../recipes/basic_charts/sankey_diagrams.md | 116 ++---------------- examples/basic_charts/src/main.rs | 39 ++++++ plotly/src/traces/sankey.rs | 20 +-- 3 files changed, 60 insertions(+), 115 deletions(-) diff --git a/docs/book/src/recipes/basic_charts/sankey_diagrams.md b/docs/book/src/recipes/basic_charts/sankey_diagrams.md index 60955d33..26c39355 100644 --- a/docs/book/src/recipes/basic_charts/sankey_diagrams.md +++ b/docs/book/src/recipes/basic_charts/sankey_diagrams.md @@ -15,114 +15,16 @@ use rand_distr::{Distribution, Normal, Uniform}; The `to_inline_html` method is used to produce the html plot displayed in this page. ## Constructing a basic Sankey diagram -```rust -let trace = Sankey::new() - .orientation(Orientation::Horizontal) - .node( - Node::new() - .pad(15) - .thickness(30) - .line(SankeyLine::new().color(NamedColor::Black).width(0.5)) - .label(vec!["A1", "A2", "B1", "B2", "C1", "C2"]) - .color_array(vec![ - NamedColor::Blue, - NamedColor::Blue, - NamedColor::Blue, - NamedColor::Blue, - NamedColor::Blue, - NamedColor::Blue, - ]), - ) - .link( - Link::new() - .value(vec![8, 4, 2, 8, 4, 2]) - .source(vec![0, 1, 0, 2, 3, 3]) - .target(vec![2, 3, 3, 4, 4, 5]), - ); +```rust,no_run +{{#include ../../../../../examples/basic_charts/src/main.rs:basic_sankey_diagram}} +``` - let layout = Layout::new() - .title("Basic Sankey".into()) - .font(Font::new().size(10)); +{{#include ../../../../../examples/basic_charts/output/inline_basic_sankey_diagram.html}} - let mut plot = Plot::new(); - plot.add_trace(trace); - plot.set_layout(layout); - if show { - plot.show(); - } -} +## Skankey diagram with defined node position +```rust,no_run +{{#include ../../../../../examples/basic_charts/src/main.rs:custom_node_sankey_diagram}} ``` -
- \ No newline at end of file + +{{#include ../../../../../examples/basic_charts/output/inline_custom_node_sankey_diagram.html}} diff --git a/examples/basic_charts/src/main.rs b/examples/basic_charts/src/main.rs index c36dcd70..16df3f72 100644 --- a/examples/basic_charts/src/main.rs +++ b/examples/basic_charts/src/main.rs @@ -810,6 +810,44 @@ fn basic_sankey_diagram(show: bool, file_name: &str) { } // ANCHOR_END: basic_sankey_diagram +// ANCHOR: custom_node_sankey_diagram +fn custom_node_sankey_diagram(show: bool, file_name: &str) { + // https://plotly.com/javascript/sankey-diagram/#basic-sankey-diagram + let trace = Sankey::new() + .orientation(Orientation::Horizontal) + .arrangement(plotly::sankey::Arrangement::Snap) + .node( + Node::new() + .pad(15) + .thickness(30) + .line(SankeyLine::new().color(NamedColor::Black).width(0.5)) + .label(vec!["A", "B", "C", "D", "E", "F"]) + .x(vec![0.2, 0.1, 0.5, 0.7, 0.3, 0.5]) + .y(vec![0.2, 0.1, 0.5, 0.7, 0.3, 0.5]) + .pad(20), + ) + .link( + Link::new() + .source(vec![0, 0, 1, 2, 5, 4, 3, 5]) + .target(vec![5, 3, 4, 3, 0, 2, 2, 3]) + .value(vec![1, 2, 1, 1, 1, 1, 1, 2]), + ); + + let layout = Layout::new() + .title("Define Node Position") + .font(Font::new().size(10)); + + let mut plot = Plot::new(); + plot.add_trace(trace); + plot.set_layout(layout); + + let path = write_example_to_html(&plot, file_name); + if show { + plot.show_html(path); + } +} +// ANCHOR_END: custom_node_sankey_diagram + // ANCHOR: table_chart fn table_chart(show: bool, file_name: &str) { let trace = Table::new( @@ -1006,6 +1044,7 @@ fn main() { // Sankey Diagrams basic_sankey_diagram(false, "basic_sankey_diagram"); + custom_node_sankey_diagram(false, "custom_node_sankey_diagram"); // Pie Charts basic_pie_chart(false, "basic_pie_chart"); diff --git a/plotly/src/traces/sankey.rs b/plotly/src/traces/sankey.rs index 87453c3f..73486f98 100644 --- a/plotly/src/traces/sankey.rs +++ b/plotly/src/traces/sankey.rs @@ -59,10 +59,14 @@ pub struct Node { hover_template: Option