-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e9d4921
commit fb752b7
Showing
2 changed files
with
98 additions
and
110 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
samples/KristofferStrube.Blazor.GraphEditor.WasmExample/Pages/Christmas.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
@page "/Christmas" | ||
@implements IDisposable | ||
@using KristofferStrube.Blazor.GraphEditor | ||
|
||
<PageTitle>Blazor.GraphEditor - Christmas</PageTitle> | ||
|
||
<h1>Christmas</h1> | ||
<div style="height:70vh;"> | ||
<GraphEditor @ref=GraphEditor | ||
TNode="Point" | ||
TEdge="Edge" | ||
NodeIdMapper="n => n.Id" | ||
NodeColorMapper=@(_ => "#FF4444") | ||
NodeRadiusMapper="_ => 20" | ||
EdgeFromMapper="e => e.From" | ||
EdgeToMapper="e => e.To" | ||
EdgeWidthMapper="e => e.Width" | ||
EdgeSpringLengthMapper="e => e.Length" /> | ||
</div> | ||
|
||
@code { | ||
private List<Point> points = new(); | ||
private List<Edge> edges = new(); | ||
|
||
private GraphEditor.GraphEditor<Point, Edge> GraphEditor = default!; | ||
private bool running = true; | ||
|
||
protected override async Task OnAfterRenderAsync(bool firstRender) | ||
{ | ||
if (!firstRender) return; | ||
while (!GraphEditor.IsReadyToLoad) | ||
{ | ||
await Task.Delay(50); | ||
} | ||
|
||
await GraphEditor.LoadGraph(points, edges); | ||
|
||
await Task.WhenAll( | ||
Layout(), | ||
AddEdges() | ||
); | ||
} | ||
|
||
private async Task Layout() | ||
{ | ||
while (running) | ||
{ | ||
await GraphEditor.ForceDirectedLayout(); | ||
if (edges.Count > 0) | ||
GraphEditor.SVGEditor.FitViewportToAllShapes(delta: 0.1); | ||
await Task.Delay(10); | ||
} | ||
} | ||
|
||
private async Task AddEdges() | ||
{ | ||
List<Point> pointsToAdd = Enumerable.Range(0, 12) | ||
.Select(i => new Point(i.ToString())) | ||
.ToList(); | ||
|
||
List<Edge> edgesToAdd = [ | ||
.. Enumerable.Range(0, 12).Select(i => new Edge(pointsToAdd[i], pointsToAdd[(i + 1) % 12], Width: 5)), | ||
new (pointsToAdd[0], pointsToAdd[6], 250), | ||
new (pointsToAdd[0], pointsToAdd[4], 200), | ||
new (pointsToAdd[0], pointsToAdd[8], 200), | ||
new (pointsToAdd[0], pointsToAdd[3], 200), | ||
new (pointsToAdd[0], pointsToAdd[9], 200), | ||
new (pointsToAdd[0], pointsToAdd[5], 200), | ||
new (pointsToAdd[0], pointsToAdd[7], 200), | ||
new (pointsToAdd[0], pointsToAdd[2], 170), | ||
new (pointsToAdd[0], pointsToAdd[10], 170), | ||
]; | ||
|
||
for (int i = 0; i < edgesToAdd.Count && running; i++) | ||
{ | ||
Edge edge = edgesToAdd[i]; | ||
|
||
if (!points.Contains(edge.From)) | ||
points.Add(edge.From); | ||
if (!points.Contains(edge.To)) | ||
points.Add(edge.To); | ||
|
||
edges.Add(edgesToAdd[i]); | ||
|
||
await GraphEditor.UpdateGraph(points, edges); | ||
await Task.Delay(200); | ||
} | ||
} | ||
|
||
public record Point(string Id); | ||
|
||
public record Edge(Point From, Point To, float Length = 100, float Width = 1); | ||
|
||
public void Dispose() | ||
{ | ||
running = false; | ||
} | ||
} |
110 changes: 0 additions & 110 deletions
110
samples/KristofferStrube.Blazor.GraphEditor.WasmExample/Pages/Dotnet8.razor
This file was deleted.
Oops, something went wrong.