Skip to content

Commit

Permalink
Add timeline graph, i thought htis was in here before?
Browse files Browse the repository at this point in the history
  • Loading branch information
TheArcaneBrony committed Mar 17, 2024
1 parent 409890c commit 7c6627c
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions ArcaneLibs.Blazor.Components/TimelineGraph.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
@using ArcaneLibs
<svg width="@Width" height="@Height">
@if (Data.Count > 0) {
<polyline points="@string.Join(" ", _points.Select(x => $"{x.Key},{x.Value}"))" style="fill:none;stroke:black;stroke-width:3"/>
@* Y min/max labels *@
<text>
<text x="0" y="@Height" fill="black">@(ValueFormatter.Invoke(_minValue))</text>
<text x="0" y="15" fill="black">@(ValueFormatter.Invoke(_maxValue))</text>
</text>
@* outline *@
<rect x="0" y="0" width="@Width" height="@Height" style="fill:none;stroke:black;stroke-width:1"/>
}
</svg>

@code {

[Parameter]
public Dictionary<DateTime, double> Data { get; set; }

Check warning on line 18 in ArcaneLibs.Blazor.Components/TimelineGraph.razor

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Data' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[Parameter]
public int Width { get; set; } = 100;

[Parameter]
public int Height { get; set; } = 100;

[Parameter]
public double? MinValue { get; set; }

[Parameter]
public double? MaxValue { get; set; }

//value formatter
[Parameter]
public Func<double, string> ValueFormatter { get; set; } = x => x.ToString("X2");

private double _minValue => MinValue ?? (Data.Count > 0 ? Data.Values.Min() : 0);
private double _maxValue => MaxValue ?? (Data.Count > 0 ? Data.Values.Max() : 0);

private Dictionary<double, double> _points = [];

protected override async Task OnParametersSetAsync() {
await RebuildGraph();
await base.OnParametersSetAsync();
}

private async Task RebuildGraph() {

Check warning on line 46 in ArcaneLibs.Blazor.Components/TimelineGraph.razor

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
if (Data.Count == 0) return;
_points.Clear();
var startTime = Data.Keys.Min(x => x).Ticks;
var endTime = Data.Keys.Max(x => x).Ticks;
var minValue = _minValue;
var maxValue = _maxValue;
// Console.WriteLine($"Start: {startTime}, End: {endTime}, Min: {minValue}, Max: {maxValue}");
foreach (var item in Data) {
_points.Add(Map(item.Key.Ticks, startTime, endTime, 0, Width),
Map(item.Value, minValue, maxValue, Height, 0));
}
}

private static double Map(
double value,
double originalStart,
double originalEnd,
double newStart,
double newEnd) {
double num = (newEnd - newStart) / (originalEnd - originalStart);
return newStart + (value - originalStart) * num;
}

}

0 comments on commit 7c6627c

Please sign in to comment.