Skip to content

Commit

Permalink
Added options for changing color of edges.
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferStrube committed Nov 25, 2024
1 parent d54b378 commit 3f24916
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
EdgeFromMapper="e => e.From"
EdgeToMapper="e => e.To"
EdgeWidthMapper="e => e.Width"
EdgeSpringLengthMapper="e => e.Length" />
EdgeColorMapper="e => e.Color"
EdgeSpringLengthMapper="e => e.Length"
EdgeShowsArrow="_ => false" />
</div>

@code {
Expand Down Expand Up @@ -60,7 +62,7 @@
.ToList();

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

public record Point(string Id);

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

public void Dispose()
{
Expand Down
10 changes: 6 additions & 4 deletions src/KristofferStrube.Blazor.GraphEditor/Edge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public Edge(IElement element, SVGEditor.SVGEditor svg) : base(element, svg)

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

public new string Stroke => "black";
public new string Stroke => GraphEditor.EdgeColorMapper(Data);

public bool ShowArrow => GraphEditor.EdgeShowsArrow(Data);

public override void HandlePointerMove(PointerEventArgs eventArgs)
{
Expand Down Expand Up @@ -86,15 +88,15 @@ public void UpdateLine()
double differenceY = To.Cy - From.Cy;
double distance = Math.Sqrt((differenceX * differenceX) + (differenceY * differenceY));

if (distance < To.R + From.R + GraphEditor.EdgeWidthMapper(Data) * 3)
if (distance < To.R + From.R + (ShowArrow ? GraphEditor.EdgeWidthMapper(Data) * 3 : 0))
{
(X1, Y1) = (X2, Y2);
}
else
{
SetStart((To.Cx, To.Cy));
X2 = To.Cx - (differenceX / distance * (To.R + GraphEditor.EdgeWidthMapper(Data) * 3));
Y2 = To.Cy - (differenceY / distance * (To.R + GraphEditor.EdgeWidthMapper(Data) * 3));
X2 = To.Cx - (differenceX / distance * (To.R + (ShowArrow ? GraphEditor.EdgeWidthMapper(Data) * 3 : 0)));
Y2 = To.Cy - (differenceY / distance * (To.R + (ShowArrow ? GraphEditor.EdgeWidthMapper(Data) * 3 : 0)));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/KristofferStrube.Blazor.GraphEditor/EdgeEditor.razor
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
stroke-dasharray="@SVGElement.StrokeDasharray"
stroke-dashoffset="@SVGElement.StrokeDashoffset.AsString()"
fill="@SVGElement.Fill"
marker-end=@(SVGElement.X1 != SVGElement.X2 && SVGElement.Y1 != SVGElement.Y2 ? "url(#arrow)" : "")>
marker-end=@(SVGElement.ShowArrow && SVGElement.X1 != SVGElement.X2 && SVGElement.Y1 != SVGElement.Y2 ? "url(#arrow)" : "")>
</line>
</g>
</ContextMenuTrigger>
Expand Down
12 changes: 12 additions & 0 deletions src/KristofferStrube.Blazor.GraphEditor/GraphEditor.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ private string EdgeId(TEdge e)
[Parameter]
public Func<TEdge, double> EdgeSpringLengthMapper { get; set; } = _ => 200;

/// <summary>
/// Defaults to <c>"#000000"</c>.
/// </summary>
[Parameter]
public Func<TEdge, string> EdgeColorMapper { get; set; } = _ => "#000000";

[Parameter]
/// <summary>
/// Defaults to <see langword="true"/>
/// </summary>
public Func<TEdge, bool> EdgeShowsArrow { get; set; } = _ => true;

[Parameter]
public Func<TNode, Task>? NodeSelectionCallback { get; set; }

Expand Down

0 comments on commit 3f24916

Please sign in to comment.