using EgwCoreLib.Lux.Core;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System.Linq;
namespace Lux.UI.Components.Compo.Charts
{
public partial class PieChart : IDisposable
{
#region Public Properties
[Parameter]
public bool Animate { get; set; } = false;
[Parameter]
public string CanvasId { get; set; } = "pieChart";
[Parameter]
public string Label { get; set; } = "#";
///
/// Lista colori in formato RGBA per FILL
/// es: rgba(0,0,0,0.3)
///
[Parameter]
public Dictionary DictColorFill { get; set; } = new();
///
/// Lista colori in formato RGBA per BORDO
/// es: rgba(0,0,0,0.3)
///
[Parameter]
public Dictionary DictColorBorder { get; set; } = new();
[Parameter]
public Dictionary PieDict { get; set; } = new();
[Parameter]
public bool Responsive { get; set; } = true;
[Parameter]
public double Alpha { get; set; } = 0.5;
#endregion Public Properties
#region Public Methods
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
///
/// Setup dati per plotting
///
///
protected override async Task OnParametersSetAsync()
{
// solo se ho tutti i dati...
if (PieDict != null && PieDict.Count > 0)
{
await DoRenderChart();
}
}
#endregion Protected Methods
#region Private Fields
private List bgColors = new();
private List bordColors = new();
private List pieLabels = new();
private List pieValues = new();
#endregion Private Fields
#region Private Methods
///
/// Conversione/trasformazione dati + chiamata js x rendering
///
///
private async Task DoRenderChart()
{
pieLabels = PieDict.Keys.ToList();
pieValues = PieDict.Values.ToList();
bgColors = DictColorFill.Select(l => l.Value).ToList();
bordColors = DictColorBorder.Select(l => l.Value).ToList();
await JSRuntime.InvokeVoidAsync("chartsInterop.createPieChart", CanvasId, new
{
type = "pie",
data = new
{
labels = pieLabels,
datasets = new[]
{
new {
label = Label,
data = pieValues,
backgroundColor= bgColors,
borderColor= bordColors,
borderWidth= 2
}
}
},
options = new
{
responsive = Responsive,
animation = Animate,
// Aggiungiamo la configurazione per i plugin
plugins = new
{
legend = new
{
// La legenda è visibile SOLO se il numero di etichette è inferiore a 5
display = pieLabels.Count() < 5
}
}
}
});
}
#endregion Private Methods
}
}