Files
mapo-core/MP.Stats/Components/ChartEnergy.razor.cs
T
2025-02-18 15:05:50 +01:00

209 lines
6.3 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;
namespace MP.Stats.Components
{
public partial class ChartEnergy
{
#region Public Properties
[Parameter]
public bool DynMode { get; set; } = false;
[Parameter]
public List<MP.Data.DatabaseModels.OdlEnergyModel> RawData
{
get => _rawData;
set
{
// salvo valori
_rawData = value;
if (value != null)
{
// ricalcolo charting data
RecalcData();
}
}
}
#endregion Public Properties
#region Protected Fields
protected const string EsitoKO = "Esito: Non Passato";
protected const string EsitoOK = "Esito: OK";
#endregion Protected Fields
#region Protected Properties
protected SelectData _currFilter { get; set; } = new SelectData();
protected List<MP.Data.DatabaseModels.OdlEnergyModel> _rawData { get; set; } = new List<MP.Data.DatabaseModels.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
/// 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>();
int numStep = 5;
// ciclo x numStep-1
for (int j = 0; j < numStep; j++)
{
for (int i = 0; i < numRecords / numStep; i++)
{
answ.Add($"rgba({54 + (180 - 54) * j / numStep}, {254 + (180 - 254) * j / numStep}, {86 + (35 - 86) * j / numStep}, {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 string lineTitle = "Consumo/UM";
private string pieTitle = "Macchina / Articolo";
#endregion Private Fields
#region Private Properties
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 List<ChartKV> ParetoData { get; set; } = new List<ChartKV>();
private List<chartJsData.chartJsTSerie> TSData { get; set; } = new List<chartJsData.chartJsTSerie>();
#endregion Private Properties
#region Private Methods
private List<string> GetLineChartLabels()
{
var answ = TSData.Select(x => x.x.ToString("ddd dd.MM")).ToList();
return answ;
}
private void RecalcData()
{
if (RawData != null)
{
lineTitle = $"{TradService.Traduci("MP-STATS_TotEn01")}/{StatService.FluxGetUM("TotCount01")}";
ParetoData = RawData
.GroupBy(p => new { p.IdxMacchina, p.CodArticolo })
.Select(y => new ChartKV() { label = $"{y.First().IdxMacchina} {y.First().CodArticolo}", value = y.Count() })
.OrderByDescending(x => x.value)
.ToList();
if (DynMode)
{
TSData = RawData
.Select(r => new chartJsData.chartJsTSerie() { x = r.DataInizio, y = (double)r.AvgTotEn01 })
.OrderBy(o => o.x)
.ToList();
//TSData = RawData
// .GroupBy(x => x.DataInizio.Date)
// .Select(r => new chartJsData.chartJsTSerie() { x = r.First().DataInizio.Date, y = r.Average(l => (double)l.AvgTotEn01) })
// .OrderBy(o => o.x)
// .ToList();
}
else
{
TSData = RawData
.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();
}
}
}
#endregion Private Methods
}
}