3
3
using System . Linq ;
4
4
using System . Threading . Tasks ;
5
5
using Microsoft . AspNetCore . Components ;
6
- using Microsoft . AspNetCore . Components . Rendering ;
7
6
using Microsoft . AspNetCore . Components . Web ;
8
7
using Microsoft . JSInterop ;
9
8
using Radzen . Blazor . Rendering ;
@@ -75,34 +74,16 @@ public partial class RadzenSankeyDiagram<TItem> : RadzenComponent
75
74
[ Parameter ]
76
75
public ColorScheme ColorScheme { get ; set ; } = ColorScheme . Pastel ;
77
76
78
- /// <summary>
79
- /// Gets the actual width of the chart.
80
- /// </summary>
81
77
private double ? Width { get ; set ; }
82
78
83
- /// <summary>
84
- /// Gets the actual height of the chart.
85
- /// </summary>
86
79
private double ? Height { get ; set ; }
87
80
88
- /// <summary>
89
- /// Gets or sets the left margin.
90
- /// </summary>
91
81
private double MarginLeft { get ; set ; } = 80 ;
92
82
93
- /// <summary>
94
- /// Gets or sets the top margin.
95
- /// </summary>
96
83
private double MarginTop { get ; set ; } = 10 ;
97
84
98
- /// <summary>
99
- /// Gets or sets the right margin.
100
- /// </summary>
101
85
private double MarginRight { get ; set ; } = 80 ;
102
86
103
- /// <summary>
104
- /// Gets or sets the bottom margin.
105
- /// </summary>
106
87
private double MarginBottom { get ; set ; } = 10 ;
107
88
108
89
/// <summary>
@@ -193,15 +174,6 @@ protected override string GetComponentCssClass()
193
174
return $ "rz-sankey-diagram rz-scheme-{ colorScheme } ";
194
175
}
195
176
196
- /// <inheritdoc />
197
- protected override void OnInitialized ( )
198
- {
199
- base . OnInitialized ( ) ;
200
-
201
- // Don't compute layout here - wait for JavaScript to provide dimensions
202
- }
203
-
204
-
205
177
/// <inheritdoc />
206
178
public override async Task SetParametersAsync ( ParameterView parameters )
207
179
{
@@ -396,6 +368,14 @@ public async Task Resize(double width, double height)
396
368
}
397
369
}
398
370
371
+ /// <summary>
372
+ /// Causes the component to re-render. Use it when <see cref="Data" /> has changed.
373
+ /// </summary>
374
+ public void Reload ( )
375
+ {
376
+ ComputeLayout ( ) ;
377
+ }
378
+
399
379
private void ComputeLayout ( )
400
380
{
401
381
if ( Data == null || ! Width . HasValue || ! Height . HasValue || Width <= 0 || Height <= 0 )
@@ -425,37 +405,37 @@ private void ComputeLayout()
425
405
// Extract nodes and links from data
426
406
var nodeMap = new Dictionary < string , SankeyNode > ( ) ;
427
407
var sankeyLinks = new List < SankeyLink > ( ) ;
428
-
408
+
429
409
foreach ( var item in Data )
430
410
{
431
411
var source = sourceGetter ( item ) ;
432
412
var target = targetGetter ( item ) ;
433
413
var value = valueGetter ( item ) ;
434
-
414
+
435
415
// Get labels if label getters are available
436
416
var sourceLabel = sourceLabelGetter != null ? sourceLabelGetter ( item ) : source ;
437
417
var targetLabel = targetLabelGetter != null ? targetLabelGetter ( item ) : target ;
438
-
418
+
439
419
// Create or update source node
440
420
if ( ! nodeMap . ContainsKey ( source ) )
441
421
{
442
- nodeMap [ source ] = new SankeyNode
443
- {
422
+ nodeMap [ source ] = new SankeyNode
423
+ {
444
424
Id = source ,
445
425
Label = sourceLabel
446
426
} ;
447
427
}
448
-
428
+
449
429
// Create or update target node
450
430
if ( ! nodeMap . ContainsKey ( target ) )
451
431
{
452
- nodeMap [ target ] = new SankeyNode
453
- {
432
+ nodeMap [ target ] = new SankeyNode
433
+ {
454
434
Id = target ,
455
435
Label = targetLabel
456
436
} ;
457
437
}
458
-
438
+
459
439
// Create link
460
440
sankeyLinks . Add ( new SankeyLink
461
441
{
@@ -464,16 +444,16 @@ private void ComputeLayout()
464
444
Value = value
465
445
} ) ;
466
446
}
467
-
447
+
468
448
var sankeyNodes = nodeMap . Values . ToList ( ) ;
469
449
470
450
var layoutWidth = Width . Value - MarginLeft - MarginRight ;
471
451
var layoutHeight = Height . Value - MarginTop - MarginBottom ;
472
-
452
+
473
453
// Ensure positive dimensions
474
454
layoutWidth = Math . Max ( 100 , layoutWidth ) ;
475
455
layoutHeight = Math . Max ( 100 , layoutHeight ) ;
476
-
456
+
477
457
var layout = new SankeyLayout
478
458
{
479
459
Width = layoutWidth ,
@@ -486,14 +466,14 @@ private void ComputeLayout()
486
466
} ;
487
467
488
468
( ComputedNodes , ComputedLinks ) = layout . Compute ( sankeyNodes , sankeyLinks ) ;
489
-
469
+
490
470
// Assign colors to nodes
491
471
if ( ComputedNodes != null )
492
472
{
493
473
for ( int i = 0 ; i < ComputedNodes . Count ; i ++ )
494
474
{
495
475
var node = ComputedNodes [ i ] ;
496
-
476
+
497
477
// Use explicit color if provided, otherwise use color scheme
498
478
if ( NodeFills != null && i < NodeFills . Count )
499
479
{
@@ -513,9 +493,6 @@ private void ComputeLayout()
513
493
}
514
494
}
515
495
516
- /// <summary>
517
- /// Gets the fill color for a node.
518
- /// </summary>
519
496
internal string GetNodeFill ( ComputedSankeyNode node )
520
497
{
521
498
if ( NodeFills != null )
@@ -529,9 +506,6 @@ internal string GetNodeFill(ComputedSankeyNode node)
529
506
return null ;
530
507
}
531
508
532
- /// <summary>
533
- /// Gets the fill color for a link.
534
- /// </summary>
535
509
internal string GetLinkFill ( ComputedSankeyLink link )
536
510
{
537
511
if ( LinkFills != null )
@@ -545,9 +519,6 @@ internal string GetLinkFill(ComputedSankeyLink link)
545
519
return null ;
546
520
}
547
521
548
- /// <summary>
549
- /// Shows tooltip for a node.
550
- /// </summary>
551
522
private void ShowNodeTooltip ( MouseEventArgs args , ComputedSankeyNode node )
552
523
{
553
524
if ( TooltipService == null ) return ;
@@ -621,9 +592,6 @@ private void ShowNodeTooltip(MouseEventArgs args, ComputedSankeyNode node)
621
592
TooltipService . OpenChartTooltip ( Element , args . OffsetX + 15 , args . OffsetY - 5 , _ => tooltip , new ChartTooltipOptions ( ) ) ;
622
593
}
623
594
624
- /// <summary>
625
- /// Shows tooltip for a link.
626
- /// </summary>
627
595
private void ShowLinkTooltip ( MouseEventArgs args , ComputedSankeyLink link )
628
596
{
629
597
if ( TooltipService == null ) return ;
@@ -638,7 +606,7 @@ private void ShowLinkTooltip(MouseEventArgs args, ComputedSankeyLink link)
638
606
639
607
var tooltip = new RenderFragment ( builder =>
640
608
{
641
- builder . OpenComponent < Rendering . ChartTooltip > ( 0 ) ;
609
+ builder . OpenComponent < ChartTooltip > ( 0 ) ;
642
610
builder . AddAttribute ( 1 , "Title" , $ "{ sourceLabel } → { targetLabel } ") ;
643
611
builder . AddAttribute ( 2 , "Label" , FlowText ) ;
644
612
builder . AddAttribute ( 3 , "Value" , valueStr ) ;
@@ -653,9 +621,6 @@ private void ShowLinkTooltip(MouseEventArgs args, ComputedSankeyLink link)
653
621
TooltipService . OpenChartTooltip ( Element , args . OffsetX + 15 , args . OffsetY - 5 , _ => tooltip , new ChartTooltipOptions ( ) ) ;
654
622
}
655
623
656
- /// <summary>
657
- /// Hides the tooltip.
658
- /// </summary>
659
624
private void HideTooltip ( )
660
625
{
661
626
currentTooltipNode = null ;
@@ -684,11 +649,5 @@ public override void Dispose()
684
649
685
650
base . Dispose ( ) ;
686
651
}
687
-
688
- class Rect
689
- {
690
- public double Width { get ; set ; }
691
- public double Height { get ; set ; }
692
- }
693
652
}
694
653
}
0 commit comments