public async Task <ChartJS> AddDataset(ChartJSdataset dataset) { var col = GetRandomChartColor(); dataset.backgroundColor.Add("rgba(0, 0, 0, 0)"); dataset.borderColor = col.borderColor; dataset.pointBackgroundColor = col.pointBackgroundColor; mychart.data.datasets.Add(dataset); await ChartJSInterop.AddDataset(_jsRuntime, JsonSerializer.Serialize(dataset), new object()); return(mychart); }
public async Task DrawTimeline(DSoptions _options, DataResult dresult, object lockobject) { ChartJSdataset dataset = new ChartJSdataset(); dataset.label = _options.Interest + "_line"; dataset.borderWidth = 3; dataset.pointRadius = 1; ChartService.SetColor(dataset, _options.Chart.type, _options.Interest); dataset.backgroundColor.Clear(); _options.Chart.data.datasets.Add(dataset); ChartJSInterop.AddDataset(_jsRuntime, JsonConvert.SerializeObject(dataset, Formatting.Indented), lockobject).GetAwaiter(); for (int i = 0; i < _options.Chart.data.labels.Count; i++) { ChartJSInterop.AddData(_jsRuntime, _options.Chart.data.labels[i], dresult.fTimeline(i), dresult.Dataset.backgroundColor.Last(), null, lockobject ).GetAwaiter(); } }
public static DataResult GetDurations(DSoptions _options, DSReplayContext _context, IJSRuntime _jsRuntime, object lockobject) { if (String.IsNullOrEmpty(_options.Interest)) { return(new DataResult()); } var replays = DBReplayFilter.Filter(_options, _context, false); var result = (_options.Player, _options.Dataset.Any()) switch { (true, false) => from r in replays from p in r.DSPlayer where p.RACE == _options.Interest && p.NAME.Length == 64 select new { r.ID, r.DURATION, p.WIN }, (true, true) => from r in replays from p in r.DSPlayer where p.RACE == _options.Interest && _options.Dataset.Contains(p.NAME) select new { r.ID, r.DURATION, p.WIN }, _ => from r in replays from p in r.DSPlayer where p.RACE == _options.Interest select new { r.ID, r.DURATION, p.WIN } }; var lresult = result.ToList(); if (!lresult.Any()) { return(new DataResult()); } CultureInfo provider = CultureInfo.InvariantCulture; DataResult dresult = new DataResult(); List <double> data = new List <double>(); List <double> xdata = new List <double>(); List <double> ydata = new List <double>(); List <string> labels = new List <string>(); Func <double, double> f = null; int count = lresult.Count; dresult.CmdrInfo.Wins = lresult.Where(x => x.WIN == true).Count(); dresult.CmdrInfo.Games = count; dresult.CmdrInfo.AWinrate = Math.Round((double)dresult.CmdrInfo.Wins * 100 / dresult.CmdrInfo.Games, 2); dresult.CmdrInfo.ADuration = TimeSpan.FromDays(3); dresult.CmdrInfo.GameIDs = lresult.Select(s => s.ID).ToHashSet(); for (int i = 0; i < DSdata.s_durations.Length; i++) { double games = 1; double wins = 0; int startd = 0; if (i > 0) { Match m = d_rx.Match(DSdata.s_durations[i]); if (m.Success) { startd = int.Parse(m.Value); } } int endd = startd + 3; if (i == 0) { endd = 8; } if (i == DSdata.s_durations.Length - 1) { endd = 200; } var ilresult = lresult.Where(x => x.DURATION > startd * 60 && x.DURATION < endd * 60); games = ilresult.Count(); wins = ilresult.Where(x => x.WIN == true).Count(); data.Add(Math.Round(wins * 100 / games, 2)); labels.Add(DSdata.s_durations[i] + " min (" + games + ")"); xdata.Add(i); ydata.Add(data.Last()); } if (xdata.Any() && xdata.Count > 1) { int order = 3; if (xdata.Count <= 3) { if (xdata.Count < 3) { order = 1; } else { order = xdata.Count - 2; } } f = Fit.PolynomialFunc(xdata.ToArray(), ydata.ToArray(), order); } dresult.Labels = labels; dresult.Dataset.data = data; dresult.fTimeline = f; dresult.fStartTime = DateTime.MinValue; ChartService.SetColor(dresult.Dataset, _options.Chart.type, _options.Interest); dresult.Dataset.fill = false; dresult.Dataset.pointRadius = 2; dresult.Dataset.pointHoverRadius = 10; dresult.Dataset.showLine = false; for (int i = 0; i < dresult.Labels.Count; i++) { ChartJSInterop.AddData(_jsRuntime, dresult.Labels[i], dresult.Dataset.data[i], dresult.Dataset.backgroundColor.Last(), null, lockobject ).GetAwaiter(); } if (dresult.fTimeline != null) { ChartJSdataset dataset = new ChartJSdataset(); dataset.label = _options.Interest + "_line"; dataset.borderWidth = 3; dataset.pointRadius = 1; ChartService.SetColor(dataset, _options.Chart.type, _options.Interest); dataset.backgroundColor.Clear(); _options.Chart.data.datasets.Add(dataset); ChartJSInterop.AddDataset(_jsRuntime, JsonConvert.SerializeObject(dataset, Formatting.Indented), lockobject).GetAwaiter(); for (int i = 0; i < dresult.Labels.Count; i++) { double fdata = dresult.fTimeline(i); ChartJSInterop.AddData(_jsRuntime, dresult.Labels[i], fdata, dresult.Dataset.backgroundColor.Last(), null, lockobject ).GetAwaiter(); } } return(dresult); }
public async Task AddDataset(DSoptions _options, object lockobject) { ChartJSdataset dataset = new ChartJSdataset(); if (String.IsNullOrEmpty(_options.Interest)) { dataset.label = "global"; } else { dataset.label = _options.Interest; } if (_options.Mode == "Timeline" || _options.Mode == "Duration") { dataset.fill = false; dataset.pointRadius = 2; dataset.pointHoverRadius = 10; dataset.showLine = false; } SetColor(dataset, _options.Chart.type, _options.Interest); dataset.backgroundColor.Clear(); _options.Chart.data.datasets.Add(dataset); await ChartJSInterop.AddDataset(_jsRuntime, JsonConvert.SerializeObject(dataset, Formatting.Indented), lockobject); DataResult dresult = await DataService.GetData(_options, _context, _jsRuntime, _db.lockobject); if (dresult != null) { foreach (string l in _options.Chart.data.labels) { double wr = 0; string bcolor = "rgba(0, 0, 0, 0)"; int ssend = 4; if (_options.Mode == "Timeline") { ssend = 10; } string label = dresult.Labels.FirstOrDefault(s => s.Substring(0, ssend) == l.Substring(0, ssend)); if (!String.IsNullOrEmpty(label)) { int pos = dresult.Labels.FindIndex(i => i == label); _options.Chart.data.datasets.Last().data.Add(dresult.Dataset.data[pos]); if (_options.Chart.type == "line") { _options.Chart.data.datasets.Last().backgroundColor.Add(dresult.Dataset.backgroundColor.Last()); } else { _options.Chart.data.datasets.Last().backgroundColor.Add(dresult.Dataset.backgroundColor[pos]); } wr = dresult.Dataset.data[pos]; if (_options.Chart.type == "line") { bcolor = dresult.Dataset.backgroundColor.Last(); } else { bcolor = dresult.Dataset.backgroundColor[pos]; } } else { _options.Chart.data.datasets.Last().data.Add(0); _options.Chart.data.datasets.Last().backgroundColor.Add("rgba(0, 0, 0, 0)"); } ChartJSInterop.AddData(_jsRuntime, "", wr, bcolor, "", lockobject ); } _options.Cmdrinfo = dresult.CmdrInfo; if (dresult.fTimeline != null) { DrawTimeline(_options, dresult, lockobject); } } }
public static DataResult GetTimeLine(DSoptions _options, DSReplayContext _context, IJSRuntime _jsRuntime, object lockobject) { if (String.IsNullOrEmpty(_options.Interest)) { return(new DataResult()); } var replays = DBReplayFilter.Filter(_options, _context, false); var result = (_options.Player, _options.Dataset.Any()) switch { (true, false) => from r in replays from p in r.DSPlayer where p.RACE == _options.Interest && p.NAME.Length == 64 select new { r.ID, r.GAMETIME, p.WIN }, (true, true) => from r in replays from p in r.DSPlayer where p.RACE == _options.Interest && _options.Dataset.Contains(p.NAME) select new { r.ID, r.GAMETIME, p.WIN }, _ => from r in replays from p in r.DSPlayer where p.RACE == _options.Interest select new { r.ID, r.GAMETIME, p.WIN } }; var lresult = result.OrderBy(o => o.GAMETIME).ToList(); if (!lresult.Any()) { return(new DataResult()); } CultureInfo provider = CultureInfo.InvariantCulture; DataResult dresult = new DataResult(); List <double> data = new List <double>(); List <double> xdata = new List <double>(); List <double> ydata = new List <double>(); List <string> labels = new List <string>(); Func <double, double> f = null; int count = lresult.Count; int step = 50; int cstep = count / step; while (cstep < 10) { step -= 1; cstep = count / step; if (step <= 1) { break; } } dresult.CmdrInfo.Wins = lresult.Where(x => x.WIN == true).Count(); dresult.CmdrInfo.Games = count; dresult.CmdrInfo.AWinrate = Math.Round((double)dresult.CmdrInfo.Wins * 100 / dresult.CmdrInfo.Games, 2); dresult.CmdrInfo.ADuration = TimeSpan.FromDays(cstep); dresult.CmdrInfo.GameIDs = lresult.Select(s => s.ID).ToHashSet(); for (int i = 0; i < step; i++) { var sreps = lresult.Skip(i * cstep).Take(cstep); if (!sreps.Any()) { continue; } data.Add(Math.Round((double)sreps.Where(x => x.WIN == true).Count() * 100 / (double)sreps.Count(), 2)); labels.Add(sreps.Last().GAMETIME.ToString("yyyy-MM-dd")); xdata.Add(i); ydata.Add(data.Last()); } if (xdata.Any() && xdata.Count > 1) { int order = 6; if (xdata.Count <= 6) { if (xdata.Count < 3) { order = 1; } else { order = xdata.Count - 2; } } f = Fit.PolynomialFunc(xdata.ToArray(), ydata.ToArray(), order); } dresult.Labels = labels; dresult.Dataset.data = data; dresult.fTimeline = f; dresult.fStartTime = lresult.First().GAMETIME; ChartService.SetColor(dresult.Dataset, _options.Chart.type, _options.Interest); dresult.Dataset.fill = false; dresult.Dataset.pointRadius = 2; dresult.Dataset.pointHoverRadius = 10; dresult.Dataset.showLine = false; for (int i = 0; i < dresult.Labels.Count; i++) { ChartJSInterop.AddData(_jsRuntime, dresult.Labels[i], dresult.Dataset.data[i], dresult.Dataset.backgroundColor.Last(), null, lockobject ).GetAwaiter(); } if (dresult.fTimeline != null) { string fstart = dresult.Labels.First().Substring(0, 10); DateTime ffstart = DateTime.ParseExact(fstart, "yyyy-MM-dd", provider); ChartJSdataset dataset = new ChartJSdataset(); dataset.label = _options.Interest + "_line"; dataset.borderWidth = 3; dataset.pointRadius = 1; ChartService.SetColor(dataset, _options.Chart.type, _options.Interest); dataset.backgroundColor.Clear(); _options.Chart.data.datasets.Add(dataset); ChartJSInterop.AddDataset(_jsRuntime, JsonConvert.SerializeObject(dataset, Formatting.Indented), lockobject).GetAwaiter(); for (int i = 0; i < dresult.Labels.Count; i++) { double fdata = dresult.fTimeline(i); ChartJSInterop.AddData(_jsRuntime, dresult.Labels[i], fdata, dresult.Dataset.backgroundColor.Last(), null, lockobject ).GetAwaiter(); } } return(dresult); }