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 ChartScarti { [Inject] protected MessageService MessageService { get; set; } [Inject] protected MpStatsService StatService { get; set; } protected SelectData _currFilter { get; set; } = new SelectData(); protected List _listScarti { get; set; } = new List(); [Parameter] public List ListScarti { get => _listScarti; set { // salvo valori _listScarti = value; // ricalcolo charting data recalcData(); var dataReload = Task.Run(async () => { await HandleRedraw(); }); } } protected BarChart ParetoGuasti; protected LineChart NumGuasti; protected object barChartOptions = new { //Title = new //{ // Display = true, // Text = "Causali Scarto" //}, 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 }, //Legend = new //{ // Display = true, // FullWidth = true //}, Animation = false, AspectRatio = 2.5 }; protected object lineChartOptions = new { //Title = new //{ // Display = true, // Text = "Causali Scarto" //}, 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 }, //Legend = new //{ // Display = true, // FullWidth = true //}, Animation = false, AspectRatio = 2.5 }; protected List ParetoData { get; set; } = new List(); protected List TSData { get; set; } = new List(); //protected override async Task OnAfterRenderAsync(bool firstRender) //{ // if (firstRender) // { // await HandleRedraw(); // } //} 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()); } } List GetBarChartLabels() { var answ = ParetoData.Select(x => x.label).ToList(); return answ; } List GetLineChartLabels() { var answ = TSData.Select(x => x.TLabel.ToString()).ToList(); return answ; } BarChartDataset GetBarChartDataset() { var answ = new BarChartDataset { Label = "Pareto Causali Scarto", Data = ParetoData.Select(x => x.value).ToList(), BackgroundColor = backgroundColors(ParetoData.Count), //BorderColor = borderColors, HoverBorderWidth = 5 }; return answ; } LineChartDataset GetLineChartDataset() { var answ = new LineChartDataset { Label = "Numero Scarti Periodo", Data = TSData.Select(x => x.Value).ToList(), BackgroundColor = backgroundColors(1), Fill = true, PointRadius = 2, SteppedLine = true, BorderDash = new List { } }; return answ; } /// /// Genera colori sfondo 33% rosso / arancione / giallo /// /// /// protected List backgroundColors(int numRecords) { List answ = new List(); // rosso... for (int i = 0; i < numRecords / 3; i++) { answ.Add(ChartColor.FromRgba(255, 99, 132, 0.4f)); } // arancione for (int i = 0; i < numRecords / 3; i++) { answ.Add(ChartColor.FromRgba(255, 206, 86, 0.4f)); } while (answ.Count < numRecords) { answ.Add(ChartColor.FromRgba(54, 82, 235, 0.4f)); } return answ; } private void recalcData() { if (ListScarti != null) { ParetoData = ListScarti .GroupBy(x => x.Causale) .Select(y => new ChartKV() { label = y.First().Descrizione, value = y.Sum(c => c.Qta) }) .OrderByDescending(x => x.value) .ToList(); TSData = ListScarti .Select(y => new ChartTS() { TLabel = y.DataOraRif, Value = y.Qta }) .OrderBy(x => x.TLabel) .ToList(); } } } }