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 EC_IdxMaccRem { get; set; } [Parameter] public bool HideCurrent { get; set; } = false; [Parameter] public List 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 _rawData { get; set; } = new List(); /// /// Genera colori sfondo 33% rosso / arancione / giallo /// /// protected List bgColors { get => semaphColors(ParetoData.Count, "0.3"); } /// /// Genera colori sfondo 33% rosso / arancione / giallo /// /// protected List lineColor { get => solidColors("0.9"); } /// /// Genera colori sfondo 33% rosso / arancione / giallo /// /// protected List 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 /// /// /// /// protected List semaphColors(int numRecords, string alpha) { List answ = new List(); // 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; } /// /// Colore azzurro + alpha /// /// /// protected List solidColors(string alpha) { List answ = new List(); answ.Add($"rgba(54, 162, 235, {alpha})"); return answ; } #endregion Protected Methods #region Private Fields private List lineTitles = new List() { "Consumo/UM" }; private List listMachine = new List(); private string pieTitle = "Macchina"; #endregion Private Fields #region Private Properties private bool _dynMode { get; set; } = false; private List DatiPareto { get => ParetoData.Select(x => x.value).ToList(); } private List DatiPlot { get => TSData; } private List LabelPareto { get => ParetoData.Select(x => x.label).ToList(); } private List LabelPlot { get => TSData.Select(r => $"{r.x:yyyy-MM-dd}").ToList(); } private int numMachine { get => listMachine.Count; } private List ParetoData { get; set; } = new List(); private List TSData { get; set; } = new List(); private List> TSDataMulti { get; set; } = new List>(); #endregion Private Properties #region Private Methods private void RecalcData() { if (RawData != null) { lineTitles = new List(); 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>(); // 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 } }