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 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 _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(); 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; } /// /// 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 string lineTitle = "Consumo/UM"; private string pieTitle = "Macchina / Articolo"; #endregion Private Fields #region Private Properties 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 List ParetoData { get; set; } = new List(); private List TSData { get; set; } = new List(); #endregion Private Properties #region Private Methods private List 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 } }