Skip to content

Commit 3f24916

Browse files
Added options for changing color of edges.
1 parent d54b378 commit 3f24916

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

samples/KristofferStrube.Blazor.GraphEditor.WasmExample/Pages/Christmas.razor

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
EdgeFromMapper="e => e.From"
1616
EdgeToMapper="e => e.To"
1717
EdgeWidthMapper="e => e.Width"
18-
EdgeSpringLengthMapper="e => e.Length" />
18+
EdgeColorMapper="e => e.Color"
19+
EdgeSpringLengthMapper="e => e.Length"
20+
EdgeShowsArrow="_ => false" />
1921
</div>
2022

2123
@code {
@@ -60,7 +62,7 @@
6062
.ToList();
6163

6264
List<Edge> edgesToAdd = [
63-
.. Enumerable.Range(0, 12).Select(i => new Edge(pointsToAdd[i], pointsToAdd[(i + 1) % 12], Width: 5)),
65+
.. Enumerable.Range(0, 12).Select(i => new Edge(pointsToAdd[i], pointsToAdd[(i + 1) % 12], Width: 5, Color: "#FF2424")),
6466
new (pointsToAdd[0], pointsToAdd[6], 250),
6567
new (pointsToAdd[0], pointsToAdd[4], 200),
6668
new (pointsToAdd[0], pointsToAdd[8], 200),
@@ -92,7 +94,7 @@
9294

9395
public record Point(string Id);
9496

95-
public record Edge(Point From, Point To, float Length = 100, float Width = 1);
97+
public record Edge(Point From, Point To, float Length = 100, float Width = 1, string Color = "#000000");
9698

9799
public void Dispose()
98100
{

src/KristofferStrube.Blazor.GraphEditor/Edge.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ public Edge(IElement element, SVGEditor.SVGEditor svg) : base(element, svg)
2323

2424
public new string StrokeWidth => GraphEditor.EdgeWidthMapper(Data).AsString();
2525

26-
public new string Stroke => "black";
26+
public new string Stroke => GraphEditor.EdgeColorMapper(Data);
27+
28+
public bool ShowArrow => GraphEditor.EdgeShowsArrow(Data);
2729

2830
public override void HandlePointerMove(PointerEventArgs eventArgs)
2931
{
@@ -86,15 +88,15 @@ public void UpdateLine()
8688
double differenceY = To.Cy - From.Cy;
8789
double distance = Math.Sqrt((differenceX * differenceX) + (differenceY * differenceY));
8890

89-
if (distance < To.R + From.R + GraphEditor.EdgeWidthMapper(Data) * 3)
91+
if (distance < To.R + From.R + (ShowArrow ? GraphEditor.EdgeWidthMapper(Data) * 3 : 0))
9092
{
9193
(X1, Y1) = (X2, Y2);
9294
}
9395
else
9496
{
9597
SetStart((To.Cx, To.Cy));
96-
X2 = To.Cx - (differenceX / distance * (To.R + GraphEditor.EdgeWidthMapper(Data) * 3));
97-
Y2 = To.Cy - (differenceY / distance * (To.R + GraphEditor.EdgeWidthMapper(Data) * 3));
98+
X2 = To.Cx - (differenceX / distance * (To.R + (ShowArrow ? GraphEditor.EdgeWidthMapper(Data) * 3 : 0)));
99+
Y2 = To.Cy - (differenceY / distance * (To.R + (ShowArrow ? GraphEditor.EdgeWidthMapper(Data) * 3 : 0)));
98100
}
99101
}
100102

src/KristofferStrube.Blazor.GraphEditor/EdgeEditor.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
stroke-dasharray="@SVGElement.StrokeDasharray"
2828
stroke-dashoffset="@SVGElement.StrokeDashoffset.AsString()"
2929
fill="@SVGElement.Fill"
30-
marker-end=@(SVGElement.X1 != SVGElement.X2 && SVGElement.Y1 != SVGElement.Y2 ? "url(#arrow)" : "")>
30+
marker-end=@(SVGElement.ShowArrow && SVGElement.X1 != SVGElement.X2 && SVGElement.Y1 != SVGElement.Y2 ? "url(#arrow)" : "")>
3131
</line>
3232
</g>
3333
</ContextMenuTrigger>

src/KristofferStrube.Blazor.GraphEditor/GraphEditor.razor.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ private string EdgeId(TEdge e)
6363
[Parameter]
6464
public Func<TEdge, double> EdgeSpringLengthMapper { get; set; } = _ => 200;
6565

66+
/// <summary>
67+
/// Defaults to <c>"#000000"</c>.
68+
/// </summary>
69+
[Parameter]
70+
public Func<TEdge, string> EdgeColorMapper { get; set; } = _ => "#000000";
71+
72+
[Parameter]
73+
/// <summary>
74+
/// Defaults to <see langword="true"/>
75+
/// </summary>
76+
public Func<TEdge, bool> EdgeShowsArrow { get; set; } = _ => true;
77+
6678
[Parameter]
6779
public Func<TNode, Task>? NodeSelectionCallback { get; set; }
6880

0 commit comments

Comments
 (0)