Files
2025-07-08 18:37:35 +02:00

256 lines
8.5 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.IdentityModel.Tokens;
using MP.Data;
using MP.Data.Services;
using MP.Stats.Data;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MP.Stats.Components
{
public partial class ChartEnergy
{
#region Public Properties
[Parameter]
public bool DynMode
{
get => _dynMode;
set
{
// salvo valori
if (_dynMode != value)
{
_dynMode = value;
// ricalcolo charting data
RecalcData();
}
}
}
[Parameter]
public EventCallback<string> EC_IdxMaccRem { get; set; }
[Parameter]
public bool HideCurrent { get; set; } = false;
[Parameter]
public List<MP.Data.DbModels.OdlEnergyModel> RawData
{
get => _rawData;
set
{
// salvo valori
_rawData = value;
if (value != null)
{
// ricalcolo charting data
RecalcData();
}
}
}
[Parameter]
public bool ShowRem { get; set; } = false;
#endregion Public Properties
#region Protected Properties
protected SelectData _currFilter { get; set; } = new SelectData();
protected List<MP.Data.DbModels.OdlEnergyModel> _rawData { get; set; } = new List<MP.Data.DbModels.OdlEnergyModel>();
/// <summary>
/// Genera colori sfondo 33% rosso / arancione / giallo
/// </summary>
/// <returns></returns>
protected List<string> bgColors
{
get => semaphColors(ParetoData.Count, "0.3");
}
/// <summary>
/// Genera colori sfondo 33% rosso / arancione / giallo
/// </summary>
/// <returns></returns>
protected List<string> lineColor
{
get => solidColors("0.9");
}
/// <summary>
/// Genera colori sfondo 33% rosso / arancione / giallo
/// </summary>
/// <returns></returns>
protected List<string> lineColors
{
get => semaphColors(ParetoData.Count, "1");
}
[Inject]
protected Data.MessageService MessageService { get; set; }
[Inject]
protected MpStatsService StatService { get; set; }
[Inject]
protected TranslateSrv TradService { get; set; }
#endregion Protected Properties
#region Protected Methods
/// <summary>
/// Genera colori sfondo 33% rosso / arancione / giallo
/// </summary>
/// <param name="numRecords"></param>
/// <param name="alpha"></param>
/// <returns></returns>
protected List<string> semaphColors(int numRecords, string alpha)
{
List<string> answ = new List<string>();
// ciclo x numRecords-1
for (int j = 0; j < numRecords; j++)
{
answ.Add($"rgba({54 + (180 - 54) * j / numRecords}, {86 + (35 - 86) * j / numRecords}, {254 + (180 - 254) * j / numRecords}, {alpha}");
}
// chiude
while (answ.Count < numRecords)
{
answ.Add($"rgba(180, 180, 35, {alpha})");
}
return answ;
}
/// <summary>
/// Colore azzurro + alpha
/// </summary>
/// <param name="alpha"></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
#region Private Fields
private List<string> lineTitles = new List<string>() { "Consumo/UM" };
private List<string> listMachine = new List<string>();
private string pieTitle = "Macchina";
#endregion Private Fields
#region Private Properties
private bool _dynMode { get; set; } = false;
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 int numMachine
{
get => listMachine.Count;
}
private List<ChartKV> ParetoData { get; set; } = new List<ChartKV>();
private List<chartJsData.chartJsTSerie> TSData { get; set; } = new List<chartJsData.chartJsTSerie>();
private List<List<chartJsData.chartJsTSerie>> TSDataMulti { get; set; } = new List<List<chartJsData.chartJsTSerie>>();
#endregion Private Properties
#region Private Methods
private void RecalcData()
{
if (RawData != null)
{
lineTitles = new List<string>();
if (DynMode)
{
ParetoData = RawData
//.GroupBy(p => new { p.IdxMacchina, p.CodArticolo })
//.Select(y => new ChartKV() { label = $"{y.First().IdxMacchina} {y.First().CodArticolo}", value = y.Count() })
.GroupBy(p => new { p.IdxMacchina })
.Select(y => new ChartKV() { label = y.First().IdxMacchina, value = y.Count() })
.OrderByDescending(x => x.value)
.ToList();
TSData = RawData
.Select(r => new chartJsData.chartJsTSerie() { x = r.DataInizio, y = (double)r.AvgTotEn01 })
.OrderBy(o => o.x)
.ToList();
// reset lista
TSDataMulti = new List<List<chartJsData.chartJsTSerie>>();
// ciclo x ogni macchina
listMachine = RawData
.GroupBy(x => x.IdxMacchina)
.Select(y => y.First().IdxMacchina)
.ToList();
foreach (var idxMacc in listMachine)
{
lineTitles.Add($"{idxMacc} | {TradService.Traduci("MP-STATS_TotEn01")} / {StatService.FluxGetUM("TotCount01")}");
var TSDataCurr = RawData
.Where(x => x.IdxMacchina == idxMacc && (!HideCurrent || (x.TotCount01 + x.TotCount02 + x.TotCount03) > 0))
.Select(r => new chartJsData.chartJsTSerie() { x = r.DataInizio, y = (double)r.AvgTotEn01 })
.OrderBy(o => o.x)
.ToList();
TSDataMulti.Add(TSDataCurr);
}
}
else
{
lineTitles.Add($"{TradService.Traduci("MP-STATS_TotEn01")} / {StatService.FluxGetUM("TotCount01")}");
ParetoData = RawData
.GroupBy(p => new { p.IdxMacchina })
//.GroupBy(p => new { p.IdxMacchina, p.CodArticolo })
.Select(y => new ChartKV() { label = y.First().IdxMacchina, value = y.Count() })
//.Select(y => new ChartKV() { label = $"{y.First().IdxMacchina} {y.First().CodArticolo}", value = y.Count() })
.OrderByDescending(x => x.value)
.ToList();
TSData = RawData
.Where(x => (!HideCurrent || (x.TotCount01 + x.TotCount02 + x.TotCount03) > 0))
.GroupBy(x => x.DataInizio.Date)
.Select(r => new chartJsData.chartJsTSerie() { x = r.First().DataInizio.Date, y = r.Average(l => (double)l.AvgWatt) })
.OrderBy(o => o.x)
.ToList();
}
}
}
private async Task RemoveMachine(string idxMacc)
{
await EC_IdxMaccRem.InvokeAsync(idxMacc);
}
#endregion Private Methods
}
}