Files
mapo-core/MP.Stats/Components/ChartOEE.razor.cs
T

207 lines
6.1 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 ChartOEE
{
#region Protected Fields
protected List<double> NumGuasti = new List<double>();
protected List<double> ParetoGuasti = new List<double>();
#endregion Protected Fields
#region Protected Properties
protected SelectData _currFilter { get; set; } = new SelectData();
protected List<MP.Data.DatabaseModels.TurniOee> _rawData { get; set; } = new List<MP.Data.DatabaseModels.TurniOee>();
[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.TurniOee> 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 BarChartDataset<double> GetBarChartDataset()
{
var answ = new BarChartDataset<double>
{
Label = "Pareto OEE Macchine",
Data = ParetoData.Select(x => x.value).ToList(),
BackgroundColor = backgroundColors(ParetoData.Count, 0.4f),
BorderColor = backgroundColors(ParetoData.Count, 1f),
HoverBorderWidth = 5
};
return answ;
}
#endif
private List<string> GetBarChartLabels()
{
var answ = ParetoData.Select(x => x.label).ToList();
return answ;
}
#if false
private LineChartDataset<double> GetLineChartDataset()
{
var answ = new LineChartDataset<double>
{
Label = "TRS/OEE Periodo",
Data = TSData.Select(x => x.Value).ToList(),
BorderColor = backgroundColors(1, 1f),
Fill = true,
PointRadius = 2,
LineTension = 0,
BorderDash = new List<int> { }
};
return answ;
}
#endif
private List<double> DatiGuasti
{
get => ParetoData.Select(x => x.value).ToList();
}
private List<string> LabelGuasti
{
get => ParetoData.Select(x => x.label).ToList();
}
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(x => x.IdxMacchina)
.Select(y => new ChartKV() { label = y.First().CodMacchina, value = Math.Round(y.Average(c => c.OEE) * 100, 2) })
.OrderByDescending(x => x.value)
.ToList();
TSData = RawData
.GroupBy(x => x.DataRif.Date)
.Select(y => new ChartTS() { TLabel = y.First().DataRif.Date, Value = Math.Round(y.Average(c => c.OEE) * 100, 2) })
.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> calcColors(int numRecords, double alpha)
{
List<string> answ = new List<string>();
// verde...
for (int i = 0; i < numRecords / 3; i++)
{
answ.Add($"rgba(54, 235, 82, {alpha.ToString("N2").Replace(",", ".")})");
}
// arancione
for (int i = 0; i < numRecords / 3; i++)
{
answ.Add($"rgba(255, 206, 86, {alpha.ToString("N2").Replace(",", ".")})");
}
while (answ.Count < numRecords)
{
answ.Add($"rgba(255, 99, 132, {alpha.ToString("N2").Replace(",",".")}");
}
return answ;
}
/// <summary>
/// Genera colori sfondo 33% rosso / arancione / giallo
/// </summary>
/// <param name="numRecords"></param>
/// <returns></returns>
protected List<string> lineColors
{
get => calcColors(ParetoData.Count, 1);
}
/// <summary>
/// Genera colori sfondo 33% rosso / arancione / giallo
/// </summary>
/// <param name="numRecords"></param>
/// <returns></returns>
protected List<string> bgColors
{
get => calcColors(ParetoData.Count, 0.3);
}
protected async Task HandleRedraw()
{
if (ParetoGuasti != null)
{
#if false
await ParetoGuasti.Clear();
await ParetoGuasti.AddLabelsDatasetsAndUpdate(GetBarChartLabels(), GetBarChartDataset());
#endif
}
if (NumGuasti != null)
{
#if false
await NumGuasti.Clear();
await NumGuasti.AddLabelsDatasetsAndUpdate(GetLineChartLabels(), GetLineChartDataset());
#endif
}
}
#endregion Protected Methods
}
}