136 lines
3.8 KiB
C#
136 lines
3.8 KiB
C#
namespace Lux.UI.Components.Compo.Charts
|
|
{
|
|
public partial class PieChart : IDisposable
|
|
{
|
|
#region Public Properties
|
|
|
|
[Parameter]
|
|
public double Alpha { get; set; } = 0.5;
|
|
|
|
[Parameter]
|
|
public bool Animate { get; set; } = false;
|
|
|
|
[Parameter]
|
|
public string CanvasId { get; set; } = "pieChart";
|
|
|
|
/// <summary>
|
|
/// Lista colori in formato RGBA per BORDO
|
|
/// es: rgba(0,0,0,0.3)
|
|
/// </summary>
|
|
[Parameter]
|
|
public Dictionary<string, string> DictColorBorder { get; set; } = new();
|
|
|
|
/// <summary>
|
|
/// Lista colori in formato RGBA per FILL
|
|
/// es: rgba(0,0,0,0.3)
|
|
/// </summary>
|
|
[Parameter]
|
|
public Dictionary<string, string> DictColorFill { get; set; } = new();
|
|
|
|
[Parameter]
|
|
public string Label { get; set; } = "#";
|
|
|
|
[Parameter]
|
|
public Dictionary<string, double> PieDict { get; set; } = new();
|
|
|
|
[Parameter]
|
|
public bool Responsive { get; set; } = true;
|
|
|
|
#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 Methods
|
|
|
|
/// <summary>
|
|
/// Setup dati per plotting
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
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<string> bgColors = new();
|
|
|
|
private List<string> bordColors = new();
|
|
|
|
private List<string> pieLabels = new();
|
|
|
|
private List<double> pieValues = new();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
[Inject]
|
|
private IJSRuntime JSRuntime { get; set; } = null!;
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Conversione/trasformazione dati + chiamata js x rendering
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
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
|
|
}
|
|
} |