Files
mapo-core/MP.Stats/Components/ChartControlli.razor.cs
T
2022-02-22 16:24:32 +01:00

264 lines
7.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
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<ChartTS> TSData { get; set; } = new List<ChartTS>();
#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();
var dataReload = Task.Run(async () =>
{
await HandleRedraw();
});
}
}
}
#endregion Public Properties
#region Private Methods
#if false
private PieChartDataset<double> GetBarChartDataset()
{
var answ = new PieChartDataset<double>
{
Label = "Numero Controlli",
Data = ParetoData.Select(x => x.value).ToList(),
BackgroundColor = getPieColors(0.4f),
BorderColor = getPieColors(1f),
HoverBorderWidth = 3
};
return answ;
}
#endif
private List<string> GetBarChartLabels()
{
var answ = ParetoData.Select(x => x.label).ToList();
return answ;
}
#if false
/// <summary>
/// Elenco 2 linee x controli KO /KO
/// </summary>
/// <returns></returns>
private LineChartDataset<double> GetLineChartDataset()
{
var answ = new LineChartDataset<double>
{
Label = "Numero controlli",
Data = TSData.Select(x => x.Value).ToList(),
BorderColor = getLineColors(1f),
Fill = true,
PointRadius = 2,
LineTension = 0,
BorderDash = new List<int> { }
};
return answ;
}
#endif
private List<string> GetLineChartLabels()
{
var answ = TSData.Select(x => x.TLabel.ToString("ddd dd.MM")).ToList();
return answ;
}
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(y => new ChartTS() { TLabel = y.First().DataOra.Date, Value = y.Count() })
.OrderBy(x => x.TLabel)
.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> getLineColors(float alpha)
{
List<string> answ = new List<string>();
#if false
answ.Add(ChartColor.FromRgba(54, 82, 254, alpha));
#endif
return answ;
}
/// <summary>
/// Genera colori sfondo 33% rosso / arancione / giallo
/// </summary>
/// <param name="numRecords"></param>
/// <returns></returns>
protected List<string> getPieColors(float alpha)
{
List<string> answ = new List<string>();
#if false
foreach (var item in ParetoData)
{
if (item.label == EsitoOK)
{
answ.Add(ChartColor.FromRgba(54, 254, 82, alpha));
}
else
{
answ.Add(ChartColor.FromRgba(254, 82, 65, alpha));
}
}
#endif
return answ;
}
protected async Task HandleRedraw()
{
#if false
if (PieVC != null)
{
await PieVC.Clear();
await PieVC.AddLabelsDatasetsAndUpdate(GetBarChartLabels(), GetBarChartDataset());
}
if (TimeSerieVC != null)
{
await TimeSerieVC.Clear();
await TimeSerieVC.AddLabelsDatasetsAndUpdate(GetLineChartLabels(), GetLineChartDataset());
}
#endif
}
#endregion Protected Methods
}
}