using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Blazorise.Charts; using Microsoft.AspNetCore.Components; using MP.Stats.Data; namespace MP.Stats.Components { public partial class ChartOEE { #region Protected Fields protected object barChartOptions = new { Scales = new { XAxes = new object[] { new { Display = false } }, YAxes = new object[] { new { Display = true, ticks= new { suggestedMin = 0 } } } }, Tooltips = new { Mode = "nearest", Intersect = false }, Hover = new { Mode = "nearest", Intersect = false }, Animation = false, AspectRatio = 3.5 }; protected object lineChartOptions = new { Scales = new { XAxes = new object[] { new { Display = true, //type = "time" } }, YAxes = new object[] { new { Display = true, ticks= new { suggestedMin = 0 } } } }, Tooltips = new { Mode = "nearest", Intersect = false }, Hover = new { Mode = "nearest", Intersect = false }, Animation = false, AspectRatio = 3.5 }; protected LineChart NumGuasti = new LineChart(); protected BarChart ParetoGuasti = new BarChart(); #endregion Protected Fields #region Protected Properties protected SelectData _currFilter { get; set; } = new SelectData(); protected List _rawData { get; set; } = new List(); [Inject] protected MessageService MessageService { get; set; } protected List ParetoData { get; set; } = new List(); [Inject] protected MpStatsService StatService { get; set; } protected List TSData { get; set; } = new List(); #endregion Protected Properties #region Public Properties [Parameter] public List RawData { get => _rawData; set { // salvo valori _rawData = value; if (value != null) { // ricalcolo charting data recalcData(); var dataReload = Task.Run(async () => { await HandleRedraw(); }); } } } #endregion Public Properties #region Private Methods private BarChartDataset GetBarChartDataset() { var answ = new BarChartDataset { Label = "Pareto OEE Macchine", Data = ParetoData.Select(x => x.value).ToList(), BackgroundColor = backgroundColors(ParetoData.Count, 0.4f), BorderColor = backgroundColors(ParetoData.Count, 1f), HoverBorderWidth = 5 }; return answ; } private List GetBarChartLabels() { var answ = ParetoData.Select(x => x.label).ToList(); return answ; } private LineChartDataset GetLineChartDataset() { var answ = new LineChartDataset { Label = "TRS/OEE Periodo", Data = TSData.Select(x => x.Value).ToList(), BorderColor = backgroundColors(1, 1f), Fill = true, PointRadius = 2, LineTension = 0, BorderDash = new List { } }; return answ; } private List GetLineChartLabels() { var answ = TSData.Select(x => x.TLabel.ToString("ddd dd.MM")).ToList(); return answ; } private void recalcData() { if (RawData != null) { ParetoData = RawData .GroupBy(x => x.IdxMacchina) .Select(y => new ChartKV() { label = y.First().IdxMacchina, value = y.Average(c => c.OEE) }) .OrderByDescending(x => x.value) .ToList(); TSData = RawData .GroupBy(x => x.DataRif.Date) .Select(y => new ChartTS() { TLabel = y.First().DataRif.Date, Value = y.Average(c => c.OEE) }) .OrderBy(x => x.TLabel) .ToList(); } } #endregion Private Methods #region Protected Methods /// /// Genera colori sfondo 33% rosso / arancione / giallo /// /// /// protected List backgroundColors(int numRecords, float alpha) { List answ = new List(); // verde... for (int i = 0; i < numRecords / 3; i++) { answ.Add(ChartColor.FromRgba(54, 235, 82, alpha)); } // arancione for (int i = 0; i < numRecords / 3; i++) { answ.Add(ChartColor.FromRgba(255, 206, 86, alpha)); } while (answ.Count < numRecords) { answ.Add(ChartColor.FromRgba(255, 99, 132, alpha)); } return answ; } protected async Task HandleRedraw() { if (ParetoGuasti != null) { await ParetoGuasti.Clear(); await ParetoGuasti.AddLabelsDatasetsAndUpdate(GetBarChartLabels(), GetBarChartDataset()); } if (NumGuasti != null) { await NumGuasti.Clear(); await NumGuasti.AddLabelsDatasetsAndUpdate(GetLineChartLabels(), GetLineChartDataset()); } } #endregion Protected Methods } }