Skip to content

Commit 3f52e42

Browse files
committed
Add Reload method to RadzenSankeyDiagram.
1 parent 224032d commit 3f52e42

File tree

1 file changed

+23
-64
lines changed

1 file changed

+23
-64
lines changed

Radzen.Blazor/RadzenSankeyDiagram.razor.cs

Lines changed: 23 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Linq;
44
using System.Threading.Tasks;
55
using Microsoft.AspNetCore.Components;
6-
using Microsoft.AspNetCore.Components.Rendering;
76
using Microsoft.AspNetCore.Components.Web;
87
using Microsoft.JSInterop;
98
using Radzen.Blazor.Rendering;
@@ -75,34 +74,16 @@ public partial class RadzenSankeyDiagram<TItem> : RadzenComponent
7574
[Parameter]
7675
public ColorScheme ColorScheme { get; set; } = ColorScheme.Pastel;
7776

78-
/// <summary>
79-
/// Gets the actual width of the chart.
80-
/// </summary>
8177
private double? Width { get; set; }
8278

83-
/// <summary>
84-
/// Gets the actual height of the chart.
85-
/// </summary>
8679
private double? Height { get; set; }
8780

88-
/// <summary>
89-
/// Gets or sets the left margin.
90-
/// </summary>
9181
private double MarginLeft { get; set; } = 80;
9282

93-
/// <summary>
94-
/// Gets or sets the top margin.
95-
/// </summary>
9683
private double MarginTop { get; set; } = 10;
9784

98-
/// <summary>
99-
/// Gets or sets the right margin.
100-
/// </summary>
10185
private double MarginRight { get; set; } = 80;
10286

103-
/// <summary>
104-
/// Gets or sets the bottom margin.
105-
/// </summary>
10687
private double MarginBottom { get; set; } = 10;
10788

10889
/// <summary>
@@ -193,15 +174,6 @@ protected override string GetComponentCssClass()
193174
return $"rz-sankey-diagram rz-scheme-{colorScheme}";
194175
}
195176

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-
205177
/// <inheritdoc />
206178
public override async Task SetParametersAsync(ParameterView parameters)
207179
{
@@ -396,6 +368,14 @@ public async Task Resize(double width, double height)
396368
}
397369
}
398370

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+
399379
private void ComputeLayout()
400380
{
401381
if (Data == null || !Width.HasValue || !Height.HasValue || Width <= 0 || Height <= 0)
@@ -425,37 +405,37 @@ private void ComputeLayout()
425405
// Extract nodes and links from data
426406
var nodeMap = new Dictionary<string, SankeyNode>();
427407
var sankeyLinks = new List<SankeyLink>();
428-
408+
429409
foreach (var item in Data)
430410
{
431411
var source = sourceGetter(item);
432412
var target = targetGetter(item);
433413
var value = valueGetter(item);
434-
414+
435415
// Get labels if label getters are available
436416
var sourceLabel = sourceLabelGetter != null ? sourceLabelGetter(item) : source;
437417
var targetLabel = targetLabelGetter != null ? targetLabelGetter(item) : target;
438-
418+
439419
// Create or update source node
440420
if (!nodeMap.ContainsKey(source))
441421
{
442-
nodeMap[source] = new SankeyNode
443-
{
422+
nodeMap[source] = new SankeyNode
423+
{
444424
Id = source,
445425
Label = sourceLabel
446426
};
447427
}
448-
428+
449429
// Create or update target node
450430
if (!nodeMap.ContainsKey(target))
451431
{
452-
nodeMap[target] = new SankeyNode
453-
{
432+
nodeMap[target] = new SankeyNode
433+
{
454434
Id = target,
455435
Label = targetLabel
456436
};
457437
}
458-
438+
459439
// Create link
460440
sankeyLinks.Add(new SankeyLink
461441
{
@@ -464,16 +444,16 @@ private void ComputeLayout()
464444
Value = value
465445
});
466446
}
467-
447+
468448
var sankeyNodes = nodeMap.Values.ToList();
469449

470450
var layoutWidth = Width.Value - MarginLeft - MarginRight;
471451
var layoutHeight = Height.Value - MarginTop - MarginBottom;
472-
452+
473453
// Ensure positive dimensions
474454
layoutWidth = Math.Max(100, layoutWidth);
475455
layoutHeight = Math.Max(100, layoutHeight);
476-
456+
477457
var layout = new SankeyLayout
478458
{
479459
Width = layoutWidth,
@@ -486,14 +466,14 @@ private void ComputeLayout()
486466
};
487467

488468
(ComputedNodes, ComputedLinks) = layout.Compute(sankeyNodes, sankeyLinks);
489-
469+
490470
// Assign colors to nodes
491471
if (ComputedNodes != null)
492472
{
493473
for (int i = 0; i < ComputedNodes.Count; i++)
494474
{
495475
var node = ComputedNodes[i];
496-
476+
497477
// Use explicit color if provided, otherwise use color scheme
498478
if (NodeFills != null && i < NodeFills.Count)
499479
{
@@ -513,9 +493,6 @@ private void ComputeLayout()
513493
}
514494
}
515495

516-
/// <summary>
517-
/// Gets the fill color for a node.
518-
/// </summary>
519496
internal string GetNodeFill(ComputedSankeyNode node)
520497
{
521498
if (NodeFills != null)
@@ -529,9 +506,6 @@ internal string GetNodeFill(ComputedSankeyNode node)
529506
return null;
530507
}
531508

532-
/// <summary>
533-
/// Gets the fill color for a link.
534-
/// </summary>
535509
internal string GetLinkFill(ComputedSankeyLink link)
536510
{
537511
if (LinkFills != null)
@@ -545,9 +519,6 @@ internal string GetLinkFill(ComputedSankeyLink link)
545519
return null;
546520
}
547521

548-
/// <summary>
549-
/// Shows tooltip for a node.
550-
/// </summary>
551522
private void ShowNodeTooltip(MouseEventArgs args, ComputedSankeyNode node)
552523
{
553524
if (TooltipService == null) return;
@@ -621,9 +592,6 @@ private void ShowNodeTooltip(MouseEventArgs args, ComputedSankeyNode node)
621592
TooltipService.OpenChartTooltip(Element, args.OffsetX + 15, args.OffsetY - 5, _ => tooltip, new ChartTooltipOptions());
622593
}
623594

624-
/// <summary>
625-
/// Shows tooltip for a link.
626-
/// </summary>
627595
private void ShowLinkTooltip(MouseEventArgs args, ComputedSankeyLink link)
628596
{
629597
if (TooltipService == null) return;
@@ -638,7 +606,7 @@ private void ShowLinkTooltip(MouseEventArgs args, ComputedSankeyLink link)
638606

639607
var tooltip = new RenderFragment(builder =>
640608
{
641-
builder.OpenComponent<Rendering.ChartTooltip>(0);
609+
builder.OpenComponent<ChartTooltip>(0);
642610
builder.AddAttribute(1, "Title", $"{sourceLabel}{targetLabel}");
643611
builder.AddAttribute(2, "Label", FlowText);
644612
builder.AddAttribute(3, "Value", valueStr);
@@ -653,9 +621,6 @@ private void ShowLinkTooltip(MouseEventArgs args, ComputedSankeyLink link)
653621
TooltipService.OpenChartTooltip(Element, args.OffsetX + 15, args.OffsetY - 5, _ => tooltip, new ChartTooltipOptions());
654622
}
655623

656-
/// <summary>
657-
/// Hides the tooltip.
658-
/// </summary>
659624
private void HideTooltip()
660625
{
661626
currentTooltipNode = null;
@@ -684,11 +649,5 @@ public override void Dispose()
684649

685650
base.Dispose();
686651
}
687-
688-
class Rect
689-
{
690-
public double Width { get; set; }
691-
public double Height { get; set; }
692-
}
693652
}
694653
}

0 commit comments

Comments
 (0)