Files
mapo-core/MP.Stats/Components/ChartControlli.razor.cs
T
Samuele Locatelli f0fa776829 Fix grafici controlli
2022-02-26 14:18:47 +01:00

239 lines
6.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using MP.Data;
using MP.Stats.Data;
namespace MP.Stats.Components
{
public partial class ChartControlli
{
#region Protected Fields
protected const string EsitoKO = "Esito: Non Passato";
protected const string EsitoOK = "Esito: OK";
protected object lineChartOptions = new
{
Scales = new
{
XAxes = new object[]
{
new {
Display = true
}
},
YAxes = new object[]
{
new {
Display = true,
ticks= new {
suggestedMin = 0
}
}
}
},
Tooltips = new
{
Mode = "nearest",
Intersect = false
},
Hover = new
{
Mode = "nearest",
Intersect = false
},
Animation = false,
AspectRatio = 4.9
};
protected object pieChartOptions = new
{
Scales = new
{
XAxes = new object[]
{
new {
Display = false
}
},
YAxes = new object[]
{
new {
Display = false
}
}
},
Legend = new
{
Display = false
},
Tooltips = new
{
Mode = "nearest",
Intersect = false
},
Hover = new
{
Mode = "nearest",
Intersect = false
},
Animation = false,
AspectRatio = 1
};
#if false
protected PieChart<double> PieVC = new PieChart<double>();
protected LineChart<double> TimeSerieVC = new LineChart<double>();
#endif
#endregion Protected Fields
#region Protected Properties
protected SelectData _currFilter { get; set; } = new SelectData();
protected List<MP.Data.DatabaseModels.ResControlli> _rawData { get; set; } = new List<MP.Data.DatabaseModels.ResControlli>();
[Inject]
protected MessageService MessageService { get; set; }
protected List<ChartKV> ParetoData { get; set; } = new List<ChartKV>();
[Inject]
protected MpStatsService StatService { get; set; }
protected List<chartJsData.chartJsTSerie> TSData { get; set; } = new List<chartJsData.chartJsTSerie>();
#endregion Protected Properties
#region Public Properties
[Parameter]
public List<MP.Data.DatabaseModels.ResControlli> RawData
{
get => _rawData;
set
{
// salvo valori
_rawData = value;
if (value != null)
{
// ricalcolo charting data
recalcData();
}
}
}
#endregion Public Properties
#region Private Methods
private List<double> DatiPareto
{
get => ParetoData.Select(x => x.value).ToList();
}
private List<chartJsData.chartJsTSerie> DatiPlot
{
get => TSData;
}
private List<string> LabelPareto
{
get => ParetoData.Select(x => x.label).ToList();
}
private List<string> LabelPlot
{
get => TSData.Select(r => $"{r.x:yyyy-MM-dd}").ToList();
}
private void recalcData()
{
if (RawData != null)
{
ParetoData = RawData
.GroupBy(p => p.EsitoOk)
.Select(y => new ChartKV() { label = y.First().EsitoOk ? EsitoOK : EsitoKO, value = y.Count() })
.OrderByDescending(x => x.value)
.ToList();
TSData = RawData
.GroupBy(x => x.DataOra.Date)
.Select(r => new chartJsData.chartJsTSerie() { x = r.First().DataOra.Date, y = r.Count() })
.OrderBy(o => o.x)
.ToList();
}
}
#endregion Private Methods
#region Protected Methods
/// <summary>
/// Genera colori sfondo 33% rosso / arancione / giallo
/// </summary>
/// <param name="numRecords"></param>
/// <returns></returns>
protected List<string> bgColors
{
get => semaphColors(ParetoData.Count, "0.3");
}
/// <summary>
/// Genera colori sfondo 33% rosso / arancione / giallo
/// </summary>
/// <param name="numRecords"></param>
/// <returns></returns>
protected List<string> lineColor
{
get => solidColors("1");
}
/// <summary>
/// Genera colori sfondo 33% rosso / arancione / giallo
/// </summary>
/// <param name="numRecords"></param>
/// <returns></returns>
protected List<string> lineColors
{
get => semaphColors(ParetoData.Count, "1");
}
/// Genera colori sfondo 33% rosso / arancione / giallo
/// </summary>
/// <param name="numRecords"></param>
/// <returns></returns>
protected List<string> semaphColors(int numRecords, string alpha)
{
List<string> answ = new List<string>();
// aggiungo verde/rosso
answ.Add($"rgba(54, 254, 82, {alpha})");
answ.Add($"rgba(254, 82, 65, {alpha})");
return answ;
}
/// <summary>
/// Genera colori sfondo 33% rosso / arancione / giallo
/// </summary>
/// <param name="numRecords"></param>
/// <returns></returns>
protected List<string> solidColors(string alpha)
{
List<string> answ = new List<string>();
answ.Add($"rgba(54, 162, 235, {alpha})");
return answ;
}
#endregion Protected Methods
}
}