Files
2023-07-25 15:18:38 +02:00

153 lines
4.6 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace EgwCoreLib.Razor
{
public partial class ChartHist
{
#region Public Properties
[Parameter]
public bool AddSlash { get; set; } = false;
[Parameter]
public int AnimationTime { get; set; } = 0;
[Parameter]
public string BackColor { get; set; } = "";
[Parameter]
public string ChartLabel { get; set; } = "";
[Parameter]
public string[]? Data { get; set; }
[Parameter]
public string GridColor { get; set; } = "";
[Parameter]
public string Id { get; set; } = "000";
[Parameter]
public string[]? Labels { get; set; }
[Parameter]
public string LineColor { get; set; } = "";
[Parameter]
public string TextColor { get; set; } = "";
#endregion Public Properties
#region Protected Methods
protected override async Task OnAfterRenderAsync(bool firstRender)
{
string jsPath = "./_content/EgwCoreLib.Razor/ChartHist.razor.js";
if (AddSlash)
{
jsPath = "/." + jsPath;
}
module = await JSRuntime.InvokeAsync<IJSObjectReference>("import", jsPath);
await Task.Delay(50);
await renderChart();
}
/// <summary>
/// Inizializzazione rendering componente
///
/// partendo da qui: https://www.williamleme.com/posts/2020/003-chartjs-blazor/
/// https://www.puresourcecode.com/dotnet/blazor/using-chart-js-with-blazor/ https://www.tutorialsteacher.com/csharp/csharp-anonymous-type
/// </summary>
/// <param name="firstRender"></param>
/// <returns></returns>
protected async Task renderChart()
{
// creazione di un oggetto anonymous type con tutte le opzioni da passare a chart.js
var config = new
{
type = "bar",
options = new
{
Responsive = true,
MaintainAspectRatio = false,
scales = new
{
yAxes = new
{
suggestedMin = 0,
display = true,
grid = new
{
color = GridColor,
display = true,
},
ticks = new
{
beginAtZero = true,
color = TextColor,
maxTicksLimit = 10,
}
},
xAxes = new
{
grid = new
{
color = GridColor,
display = true,
},
ticks = new
{
color = TextColor,
}
},
},
plugins = new
{
legend = new
{
display = true,
labels = new
{
color = TextColor,
},
},
},
Animation = new
{
Duration = AnimationTime
},
},
data = new
{
datasets = new[]
{
new
{
data = Data,
borderColor = LineColor,
backgroundColor = BackColor,
borderWidth = 1,
color = TextColor,
label = ChartLabel
}
},
labels = Labels
}
};
await JSRuntime.InvokeVoidAsync("setup", Id, config);
}
#endregion Protected Methods
#region Private Properties
[Inject]
private IJSRuntime JSRuntime { get; set; } = null!;
private IJSObjectReference module { get; set; } = null!;
#endregion Private Properties
}
}