Files
mapo-core/MP.Stats/Components/ChartOEE.razor.cs
T
2022-02-26 18:16:21 +01:00

182 lines
5.3 KiB
C#

using Microsoft.AspNetCore.Components;
using MP.Data;
using MP.Stats.Data;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MP.Stats.Components
{
public partial class ChartOEE
{
#region Private Properties
private List<double> DatiParetoOee
{
get => ParetoData.Select(x => x.value).ToList();
}
private List<chartJsData.chartJsTSerie> DatiTrs
{
get => TSData;
}
private List<string> LabelParetoOee
{
get => ParetoData.Select(x => x.label).ToList();
}
private List<string> LabelTrs
{
get => TSData.Select(r => $"{r.x:yyyy-MM-dd}").ToList();
}
#endregion Private Properties
#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>();
/// <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");
}
[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.TurniOee> RawData
{
get => _rawData;
set
{
// salvo valori
_rawData = value;
if (value != null)
{
// ricalcolo charting data
recalcData();
}
}
}
#endregion Public Properties
#region Private Methods
private Dictionary<DateTime, double> calcTSData(DateTime inizio, List<double> yData)
{
Dictionary<DateTime, double> answ = new Dictionary<DateTime, double>();
// usando i dati ricevuti aggiunge variabile x = tempo crescente
int idx = 0;
foreach (var item in yData)
{
answ.Add(inizio.AddHours(idx), item);
idx++;
}
// restituisco!
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(r => new chartJsData.chartJsTSerie() { x = r.First().DataRif.Date, y = Math.Round(r.Average(c => c.OEE) * 100, 2) })
.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> semaphColors(int numRecords, string alpha)
{
List<string> answ = new List<string>();
// verde...
for (int i = 0; i < numRecords / 3; i++)
{
answ.Add($"rgba(54, 235, 82, {alpha})");
}
// arancione
for (int i = 0; i < numRecords / 3; i++)
{
answ.Add($"rgba(255, 206, 86, {alpha})");
}
while (answ.Count < numRecords)
{
answ.Add($"rgba(255, 99, 132, {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
}
}