Sankey Diagram Strategy¶
sankey_strategy ¶
Sankey Strategy - Alluvial/Flow Diagram Visualizations.
This module implements the SankeyStrategy for generating Sankey (alluvial) diagrams that visualize flow relationships between multiple categorical levels.
Classes:
| Name | Description |
|---|---|
SankeyStrategy | Strategy for Sankey diagram generation using Plotly. |
Notes
- Visualizes flow/transition between categorical stages
- Shows proportional relationships across multiple levels
- Flow thickness proportional to count/aggregated value
- Supports flexible multi-level flow configuration
For supported use cases, refer to the official documentation.
Classes¶
SankeyStrategy ¶
Bases: BasePlotStrategy
Strategy for Sankey diagram flow/alluvial visualizations.
This strategy creates Sankey diagrams showing flow relationships where nodes represent unique values and links show connections between stages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config | Dict[str, Any] | Complete configuration from YAML file. | required |
Attributes:
| Name | Type | Description |
|---|---|---|
data_config | Dict[str, Any] | Data processing configuration. |
plotly_config | Dict[str, Any] | Plotly-specific configuration. |
flow_columns | List[str] | Columns defining the flow stages (in order). |
value_column | Optional[str] | Column for value aggregation (None = count occurrences). |
aggregation | str | Aggregation method: 'count', 'sum', 'nunique'. |
node_pad | int | Padding between nodes. |
node_thickness | int | Thickness of node rectangles. |
color_by_stage | bool | If True, color nodes by their stage level. |
color_by_first_level | bool | If True, color links by their source node at first level. |
Methods:
| Name | Description |
|---|---|
validate_data | Validate input data for Sankey diagram requirements |
process_data | Process data for Sankey diagram |
create_figure | Create Sankey diagram figure from processed flow data |
get_stage_statistics | Calculate statistics for each stage in the flow |
Notes
- Flow thickness proportional to count or aggregated value
- Supports multiple aggregation methods
- Customizable node and link colors
Initialize strategy with configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config | Dict[str, Any] | Complete configuration from YAML file. | required |
Source code in src/domain/plot_strategies/charts/sankey_strategy.py
Functions¶
validate_data ¶
Validate input data for Sankey diagram requirements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df | DataFrame | Input data to validate. | required |
Raises:
| Type | Description |
|---|---|
ValueError | If DataFrame is empty, flow columns missing, or fewer than 2 flow columns specified. |
Source code in src/domain/plot_strategies/charts/sankey_strategy.py
process_data ¶
Process data for Sankey diagram: aggregate flows between stages.
Cleans data, removes placeholders, and aggregates flows between adjacent stages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df | DataFrame | Input data with flow columns. | required |
Returns:
| Type | Description |
|---|---|
DataFrame | Processed data with source, target, value columns for each link. |
Source code in src/domain/plot_strategies/charts/sankey_strategy.py
create_figure ¶
Create Sankey diagram figure from processed flow data.
Builds node and link structures, applies styling, and creates Plotly Sankey visualization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
processed_df | DataFrame | Processed data with flow columns and values. | required |
Returns:
| Type | Description |
|---|---|
Figure | Configured Plotly Sankey figure. |
Source code in src/domain/plot_strategies/charts/sankey_strategy.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
get_stage_statistics ¶
Calculate statistics for each stage in the flow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df | DataFrame | Processed flow data. | required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any] | Statistics per stage including unique counts and top values. |
Source code in src/domain/plot_strategies/charts/sankey_strategy.py
apply_filters ¶
Apply filters to data.
This is a common implementation that can be overridden by subclasses if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df | DataFrame | Data to filter. | required |
filters | Optional[Dict[str, Any]] | Filter specifications. | None |
Returns:
| Type | Description |
|---|---|
DataFrame | Filtered data. |
Source code in src/domain/plot_strategies/base/base_plot_strategy.py
apply_customizations ¶
Apply custom styling to figure.
This is a hook for future customization features (FLEXIVEL and FLEXIVEL2).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig | Figure | Base figure. | required |
customizations | Optional[Any] | Customization specifications. | None |
Returns:
| Type | Description |
|---|---|
Figure | Customized figure. |
Source code in src/domain/plot_strategies/base/base_plot_strategy.py
generate_plot ¶
generate_plot(data: DataFrame, filters: Optional[Dict[str, Any]] = None, customizations: Optional[Any] = None) -> go.Figure
Generate complete plot (Template Method).
This method orchestrates the entire plot generation process: 1. Validate input data 2. Process data 3. Apply filters 4. Create figure 5. Apply customizations
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | DataFrame | Input data. | required |
filters | Optional[Dict[str, Any]] | Filters to apply. | None |
customizations | Optional[Any] | Customizations to apply. | None |
Returns:
| Type | Description |
|---|---|
Figure | Complete Plotly figure. |
Raises:
| Type | Description |
|---|---|
ValueError | If validation fails. |