Files
mapo-core/MP.Stats/Components/ChartScarti.razor.cs
T
2021-05-22 17:37:18 +02:00

238 lines
7.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Blazorise.Charts;
using Microsoft.AspNetCore.Components;
using MP.Stats.Data;
namespace MP.Stats.Components
{
public partial class ChartScarti
{
[Inject]
protected MessageService MessageService { get; set; }
[Inject]
protected MpStatsService StatService { get; set; }
protected SelectData _currFilter { get; set; } = new SelectData();
protected List<MP.Data.DatabaseModels.ResScarti> _listScarti { get; set; } = new List<MP.Data.DatabaseModels.ResScarti>();
[Parameter]
public List<MP.Data.DatabaseModels.ResScarti> ListScarti
{
get => _listScarti;
set
{
// salvo valori
_listScarti = value;
// ricalcolo charting data
recalcData();
var dataReload = Task.Run(async () =>
{
await HandleRedraw();
});
}
}
protected BarChart<double> ParetoGuasti;
protected LineChart<double> NumGuasti;
protected object barChartOptions = new
{
//Title = new
//{
// Display = true,
// Text = "Causali Scarto"
//},
Scales = new
{
XAxes = new object[]
{
new {
Display = false
}
},
YAxes = new object[]
{
new {
Display = true,
ticks= new {
suggestedMin = 0
}
}
}
},
Tooltips = new
{
Mode = "nearest",
Intersect = false
},
Hover = new
{
Mode = "nearest",
Intersect = false
},
//Legend = new
//{
// Display = true,
// FullWidth = true
//},
Animation = false,
AspectRatio = 2.5
};
protected object lineChartOptions = new
{
//Title = new
//{
// Display = true,
// Text = "Causali Scarto"
//},
Scales = new
{
XAxes = new object[]
{
new {
Display = true,
//type = "time"
}
},
YAxes = new object[]
{
new {
Display = true,
ticks= new {
suggestedMin = 0
}
}
}
},
Tooltips = new
{
Mode = "nearest",
Intersect = false
},
Hover = new
{
Mode = "nearest",
Intersect = false
},
//Legend = new
//{
// Display = true,
// FullWidth = true
//},
Animation = false,
AspectRatio = 2.5
};
protected List<ChartKV> ParetoData { get; set; } = new List<ChartKV>();
protected List<ChartTS> TSData { get; set; } = new List<ChartTS>();
//protected override async Task OnAfterRenderAsync(bool firstRender)
//{
// if (firstRender)
// {
// await HandleRedraw();
// }
//}
protected async Task HandleRedraw()
{
if (ParetoGuasti != null)
{
await ParetoGuasti.Clear();
await ParetoGuasti.AddLabelsDatasetsAndUpdate(GetBarChartLabels(), GetBarChartDataset());
}
if (NumGuasti != null)
{
await NumGuasti.Clear();
await NumGuasti.AddLabelsDatasetsAndUpdate(GetLineChartLabels(), GetLineChartDataset());
}
}
List<string> GetBarChartLabels()
{
var answ = ParetoData.Select(x => x.label).ToList();
return answ;
}
List<string> GetLineChartLabels()
{
var answ = TSData.Select(x => x.TLabel.ToString("ddd dd.MM")).ToList();
return answ;
}
BarChartDataset<double> GetBarChartDataset()
{
var answ = new BarChartDataset<double>
{
Label = "Pareto Causali Scarto",
Data = ParetoData.Select(x => x.value).ToList(),
BackgroundColor = backgroundColors(ParetoData.Count),
//BorderColor = borderColors,
HoverBorderWidth = 5
};
return answ;
}
LineChartDataset<double> GetLineChartDataset()
{
var answ = new LineChartDataset<double>
{
Label = "Numero Scarti Periodo",
Data = TSData.Select(x => x.Value).ToList(),
BackgroundColor = backgroundColors(1),
Fill = true,
PointRadius = 2,
SteppedLine = true,
BorderDash = new List<int> { }
};
return answ;
}
/// <summary>
/// Genera colori sfondo 33% rosso / arancione / giallo
/// </summary>
/// <param name="numRecords"></param>
/// <returns></returns>
protected List<string> backgroundColors(int numRecords)
{
List<string> answ = new List<string>();
// rosso...
for (int i = 0; i < numRecords / 3; i++)
{
answ.Add(ChartColor.FromRgba(255, 99, 132, 0.4f));
}
// arancione
for (int i = 0; i < numRecords / 3; i++)
{
answ.Add(ChartColor.FromRgba(255, 206, 86, 0.4f));
}
while (answ.Count < numRecords)
{
answ.Add(ChartColor.FromRgba(54, 82, 235, 0.4f));
}
return answ;
}
private void recalcData()
{
if (ListScarti != null)
{
ParetoData = ListScarti
.GroupBy(x => x.Causale)
.Select(y => new ChartKV() { label = y.First().Descrizione, value = y.Sum(c => c.Qta) })
.OrderByDescending(x => x.value)
.ToList();
TSData = ListScarti
.GroupBy(x => x.DataOraRif.Date)
.Select(y => new ChartTS() { TLabel = y.First().DataOraRif.Date, Value = y.Sum(c => c.Qta) })
.OrderBy(x => x.TLabel)
.ToList();
}
}
}
}