Files
lux/Lux.UI/Components/Compo/Charts/StackedBarChart.razor.cs
T
2025-12-18 16:24:12 +01:00

82 lines
2.7 KiB
C#

using EgwCoreLib.Lux.Core;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace Lux.UI.Components.Compo.Charts
{
public partial class StackedBarChart : IDisposable
{
#region Public Properties
[Parameter] public Dictionary<string, string> BarColor { get; set; } = new();
[Parameter] public string CanvasId { get; set; } = "barChart";
[Parameter] public Dictionary<string, List<decimal>> DictY { get; set; } = new();
[Parameter] public List<string> ListX { get; set; } = new();
#endregion Public Properties
#region Public Methods
// puoi passare DateTime.ToString()
public void Dispose()
{
// Chiama il metodo JS per pulire il dizionario
_ = JSRuntime.InvokeVoidAsync("chartsInterop.destroyChart", CanvasId);
}
#endregion Public Methods
#region Protected Properties
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (DictY != null && DictY.Any())
{
var datasets = DictY.Select(kvp => new
{
label = kvp.Key,
data = kvp.Value.ToArray(),
backgroundColor = ColorHelper.SetAlpha(BarColor.GetValueOrDefault(kvp.Key, "rgba(0,0,0,0.3)"), 0.3),
borderColor = ColorHelper.ToOpaqueRgba(BarColor.GetValueOrDefault(kvp.Key, "rgba(0,0,0,1)")),
borderWidth = 2
}).ToArray();
// Trasformiamo ListX in Array
var labelsX = ListX.ToArray();
#if false
await JSRuntime.InvokeVoidAsync("chartsInterop.createBarChart", CanvasId, ListX, datasets);
#endif
// Passiamo l'intera configurazione per gestire lo 'stacked'
await JSRuntime.InvokeVoidAsync("chartsInterop.createBarChart", CanvasId, new
{
type = "bar",
data = new
{
labels = labelsX,
datasets = datasets
},
options = new
{
responsive = true,
scales = new
{
x = new { stacked = true }, // Abilita lo stack su X
y = new { stacked = true } // Abilita lo stack su Y
}
}
});
}
}
#endregion Protected Methods
}
}