Files
gpw_next/GPW.CORE.Comp/ChartHist.razor.cs
Samuele Locatelli 6f7f676b01 Fix CHartJs display
2023-01-22 12:38:53 +01:00

132 lines
4.0 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace GPW.CORE.Comp
{
public partial class ChartHist
{
#region Public Properties
[Parameter]
public string BackColor { get; set; } = "";
[Parameter]
public string TextColor { get; set; } = "";
[Parameter]
public string ChartLabel { get; set; } = "";
[Parameter]
public string[]? Data { get; set; }
[Parameter]
public string Id { get; set; } = "MyHist";
[Parameter]
public string[]? Labels { get; set; }
[Parameter]
public string LineColor { get; set; } = "";
[Parameter]
public string GridColor { get; set; } = "";
[Parameter]
public int AnimationTime { get; set; } = 0;
#endregion Public Properties
#region Protected Methods
protected override async Task OnAfterRenderAsync(bool firstRender)
{
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,
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
}
}