221 lines
7.4 KiB
C#
221 lines
7.4 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();
|
|
|
|
}
|
|
}
|
|
|
|
#if false
|
|
PieChart<double> pieChart;
|
|
LineChart<double> lineChart;
|
|
HorizontalBarChart<double> barChartHoriz;
|
|
BarChart<double> barChart;
|
|
#endif
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
{
|
|
if (firstRender)
|
|
{
|
|
await HandleRedraw();
|
|
}
|
|
}
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
await HandleRedraw();
|
|
}
|
|
|
|
|
|
object horizontalLineChartOptions = new
|
|
{
|
|
//Title = new
|
|
//{
|
|
// Display = true,
|
|
// Text = "Line chart sample"
|
|
//},
|
|
//Scales = new
|
|
//{
|
|
// XAxes = new object[]
|
|
// {
|
|
// new {
|
|
// //ScaleLabel = new {
|
|
// //Display = true, LabelString = "value" },
|
|
// Stacked = true
|
|
// }
|
|
// }
|
|
//},
|
|
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()
|
|
{
|
|
#if false
|
|
await pieChart.Clear();
|
|
await pieChart.AddLabelsDatasetsAndUpdate(Labels, GetPieChartDataset());
|
|
await lineChart.Clear();
|
|
await lineChart.AddLabelsDatasetsAndUpdate(Labels, GetLineChartDataset());
|
|
await barChart.Clear();
|
|
await barChart.AddLabelsDatasetsAndUpdate(Labels, GetBarChartDataset());
|
|
await barChartHoriz.Clear();
|
|
await barChartHoriz.AddLabelsDatasetsAndUpdate(Labels, GetHorizBarChartDataset());
|
|
#endif
|
|
// calcolo hist frequenza con EFCore: https://entityframeworkcore.com/knowledge-base/60871048/group-by-and-to-dictionary-in-ef-core-3-1
|
|
var rawData = RandomizeData();
|
|
var histDict = rawData.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();
|
|
|
|
|
|
dataList = TSData(DateTime.Today.AddHours(-rawData.Count), rawData).Select(r => new chartJsData.chartJsTSerie()
|
|
{ x = r.Key, y = r.Value }).ToList();
|
|
}
|
|
|
|
protected string[]? histData { get; set; } = null;
|
|
|
|
protected string[]? histLabel { get; set; } = null;
|
|
protected List<chartJsData.chartJsTSerie>? dataList { get; set; } = null;
|
|
#if false
|
|
|
|
PieChartDataset<double> GetPieChartDataset()
|
|
{
|
|
return new PieChartDataset<double> { Label = "# of randoms", Data = RandomizeData(), BackgroundColor = backgroundColors, BorderColor = borderColors };
|
|
}
|
|
|
|
LineChartDataset<double> GetLineChartDataset()
|
|
{
|
|
return new LineChartDataset<double> { Label = "# of randoms", Data = RandomizeData(), BackgroundColor = backgroundColors, BorderColor = borderColors, Fill = true, PointRadius = 2, BorderDash = new List<int> { } };
|
|
}
|
|
|
|
BarChartDataset<double> GetBarChartDataset()
|
|
{
|
|
return new BarChartDataset<double> { Label = "# of randoms", Data = RandomizeData(), BackgroundColor = backgroundColors, BorderColor = borderColors, HoverBorderWidth = 5 };
|
|
}
|
|
|
|
BarChartDataset<double> GetHorizBarChartDataset()
|
|
{
|
|
return new BarChartDataset<double> { Label = "# of randoms", Data = RandomizeData(), BackgroundColor = backgroundColors, BorderColor = borderColors, HoverBorderWidth = 5 };
|
|
}
|
|
#endif
|
|
|
|
string[] Labels = { "Red", "Blue", "Yellow", "Green", "Purple", "Orange" };
|
|
#if false
|
|
List<string> backgroundColors = new List<string> { ChartColor.FromRgba(255, 99, 132, 0.2f), ChartColor.FromRgba(54, 162, 235, 0.2f), ChartColor.FromRgba(255, 206, 86, 0.2f), ChartColor.FromRgba(75, 192, 192, 0.2f), ChartColor.FromRgba(153, 102, 255, 0.2f), ChartColor.FromRgba(255, 159, 64, 0.2f) };
|
|
List<string> borderColors = new List<string> { ChartColor.FromRgba(255, 99, 132, 1f), ChartColor.FromRgba(54, 162, 235, 1f), ChartColor.FromRgba(255, 206, 86, 1f), ChartColor.FromRgba(75, 192, 192, 1f), ChartColor.FromRgba(153, 102, 255, 1f), ChartColor.FromRgba(255, 159, 64, 1f) };
|
|
#endif
|
|
|
|
private List<int> RandomizeData()
|
|
{
|
|
var r = new Random(DateTime.Now.Millisecond);
|
|
|
|
|
|
List<int> answ = new List<int>();
|
|
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((int)(randNormal * 10));
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
private Dictionary<DateTime, decimal> TSData(DateTime inizio, List<int> yData)
|
|
{
|
|
Dictionary<DateTime, decimal> answ = new Dictionary<DateTime, decimal>();
|
|
// 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;
|
|
}
|
|
}
|
|
} |