Files
2022-07-06 10:10:50 +02:00

199 lines
6.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using System.Net.Http;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.Web.Virtualization;
using Microsoft.JSInterop;
using MP.Data;
using MP.Stats;
namespace MP.Stats.Pages
{
public partial class Test
{
private int _numRec = 100;
private double _mean = 14;
private double _stdDev = 1;
public int numRec
{
get
{
return _numRec;
}
set
{
_numRec = value;
var pUpd = Task.Run(async () => await HandleRedraw());
pUpd.Wait();
}
}
public double mean
{
get
{
return _mean;
}
set
{
_mean = value;
var pUpd = Task.Run(async () => await HandleRedraw());
pUpd.Wait();
}
}
public double stdDev
{
get
{
return _stdDev;
}
set
{
_stdDev = value;
var pUpd = Task.Run(async () => await HandleRedraw());
pUpd.Wait();
}
}
protected int lineTens { get; set; } = 0;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await HandleRedraw();
}
}
protected override async Task OnInitializedAsync()
{
await HandleRedraw();
}
object horizontalLineChartOptions = new
{
Tooltips = new
{
Mode = "nearest",
Intersect = false
}
,
Hover = new
{
Mode = "nearest",
Intersect = false
}
,
Legend = new
{
Display = true,
FullWidth = true
}
,
AspectRatio = 2.5
}
;
private async Task HandleRedraw()
{
await Task.Delay(1);
// calcolo hist frequenza con EFCore: https://entityframeworkcore.com/knowledge-base/60871048/group-by-and-to-dictionary-in-ef-core-3-1
randData = RandomizeData();
//var histDict = randData.GroupBy(r => r).Select(g => new
//{
// g.Key,
// Count = g.Count()
//}).OrderBy(d => d.Key).ToDictionary(x => x.Key, x => x.Count.ToString());
//histData = histDict.Values.ToArray();
//histLabel = histDict.Keys.Select(x => $"{x}").ToArray();
// calcolo i valori raggruppati in numGroup...
int numGroup = (int)Math.Sqrt(numRec);
double minVal = randData.Min();
double maxVal = randData.Max();
double step = (maxVal - minVal) / numGroup;
var histDictGroup = randData.GroupBy(r => ((int)Math.Round((r - minVal) / step)) * step + minVal).Select(g => new
{
g.Key,
Count = g.Count()
}).OrderBy(d => d.Key).ToDictionary(x => x.Key, x => x.Count);
histGroupData = histDictGroup.Values.Select(x => $"{x}").ToArray();
histGroupLabel = histDictGroup.Keys.Select(x => $"{x:N2}").ToArray();
histData = histDictGroup.Values.Select(x => (double)x).ToList();
histLabel = histDictGroup.Keys.Select(x => $"{x:N2}").ToList();
dataList = TSData(DateTime.Today.AddHours(-randData.Count), randData).Select(r => new chartJsData.chartJsTSerie()
{ x = r.Key, y = r.Value }).ToList();
lineColors = new List<string>() { "rgba(255, 99, 132, 1)", "rgba(255, 159, 64, 1)", "rgba(255, 205, 86, 1)", "rgba(75, 192, 192, 1)", "rgba(54, 162, 235, 1)", "rgba(153, 102, 255, 1)", "rgba(201, 203, 207, 1)" };
bgColors = new List<string>() { "rgba(255, 99, 132, 0.3)", "rgba(255, 159, 64, 0.3)", "rgba(255, 205, 86, 0.3)", "rgba(75, 192, 192, 0.3)", "rgba(54, 162, 235, 0.3)", "rgba(153, 102, 255, 0.3)", "rgba(201, 203, 207, 0.3)" };
}
protected List<double> histData { get; set; } = new List<double>();
protected List<string> histLabel { get; set; } = new List<string>();
protected string[]? histGroupData { get; set; } = null;
protected string[]? histGroupLabel { get; set; } = null;
protected List<double> randData { get; set; } = new List<double>();
protected List<string> bgColors = new List<string>();
protected List<string> lineColors = new List<string>();
protected List<chartJsData.chartJsTSerie> dataList { get; set; } = new List<chartJsData.chartJsTSerie>();
string[] Labels = { "Red", "Blue", "Yellow", "Green", "Purple", "Orange" };
private List<double> RandomizeData()
{
var r = new Random(DateTime.Now.Millisecond);
List<double> answ = new List<double>();
for (int i = 0; i < numRec; i++)
{
double u1 = 1.0 - r.NextDouble(); // uniform (0,1] random doubles
double u2 = 1.0 - r.NextDouble();
double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2);
double randNormal = mean + stdDev * randStdNormal; // random normal (mean, stdDev^2)
//answ.Add(r.Next(3, 30) * r.NextDouble());
answ.Add(randNormal);
}
return answ;
}
private Dictionary<DateTime, double> TSData(DateTime inizio, List<double> yData)
{
Dictionary<DateTime, double> answ = new Dictionary<DateTime, double>();
// usando i dati ricevuti aggiunge variabile x = tempo crescente
int idx = 0;
foreach (var item in yData)
{
answ.Add(inizio.AddHours(idx), item);
idx++;
}
// restituisco!
return answ;
}
}
}