public void SetCmdrPics(ChartJS chart) { if (chart.type != "bar") { return; } List <ChartJSPluginlabelsImage> images = new List <ChartJSPluginlabelsImage>(); foreach (string lcmdr in chart.data.labels) { foreach (string cmdr in DSdata.s_races) { if (lcmdr.StartsWith(cmdr)) { ChartJSPluginlabelsImage myimage = new ChartJSPluginlabelsImage(); //myimage.src = "images/btn-unit-hero-" + cmdr.ToLower() + ".png"; myimage.src = "_content/sc2dsstats.shared/images/btn-unit-hero-" + cmdr.ToLower() + ".png"; images.Add(myimage); } } } ChartJsoptionsBar opt = new ChartJsoptionsBar(); opt = chart.options as ChartJsoptionsBar; opt.plugins.labels.images = images; chart.options = opt; }
public JsonResult PieChartJS() { ChartJS chart = null; httpClient.DefaultRequestHeaders.Add("Authorization", HttpContext.Session.GetString("Token")); var responseTask = httpClient.GetAsync("Products"); responseTask.Wait(); var result = responseTask.Result; if (result.IsSuccessStatusCode) { var readTask = result.Content.ReadAsAsync <IEnumerable <Product> >(); readTask.Wait(); var list = readTask.Result; chart = new ChartJS(list.Count()); list = list.OrderByDescending(o => o.Stock); int i = 0; foreach (var report in list) { chart.labels[i] = report.Name; chart.datasets[0].data[i] = report.Stock; i++; } } return(Json(new { data = chart })); }
public void CreateMiddleChart(DSReplay replay, ChartJS _chart) { List <string> labels = new List <string>(); List <double> midTeam1 = new List <double>(); List <double> midTeam2 = new List <double>(); TimeSpan gt = TimeSpan.FromSeconds(replay.DURATION) / 100; for (int i = 0; i < 100; i++) { TimeSpan gtint = gt * i; labels.Add(gtint.ToString(@"hh\:mm\:ss")); midTeam1.Add(replay.GetMiddle((int)(gtint.TotalSeconds * 22.4), 0) / 22.4); midTeam2.Add(replay.GetMiddle((int)(gtint.TotalSeconds * 22.4), 1) / 22.4); } _chart.data.labels = labels; ChartJSdataset dsTeam1 = new ChartJSdataset(); ChartJSdataset dsTeam2 = new ChartJSdataset(); dsTeam1.label = "Team1"; dsTeam1.fill = true; dsTeam1.pointRadius = 0; dsTeam1.pointHoverRadius = 6; dsTeam1.showLine = true; dsTeam1.borderWidth = 3; dsTeam2.label = "Team2"; dsTeam2.fill = true; dsTeam2.pointRadius = 0; dsTeam2.pointHoverRadius = 6; dsTeam2.showLine = true; dsTeam2.borderWidth = 3; var col = GetChartColor(replay.WINNER == 0); dsTeam1.backgroundColor.Add("rgba(0, 0, 0, 0)"); dsTeam1.borderColor = col.borderColor; dsTeam1.pointBackgroundColor = col.pointBackgroundColor; col = GetChartColor(replay.WINNER == 1); dsTeam2.backgroundColor.Add("rgba(0, 0, 0, 0)"); dsTeam2.borderColor = col.borderColor; dsTeam2.pointBackgroundColor = col.pointBackgroundColor; dsTeam1.data = midTeam1; dsTeam2.data = midTeam2; _chart.data.datasets.Add(dsTeam1); _chart.data.datasets.Add(dsTeam2); DrawChart(_chart); }
public async Task <ChartJS> GetChartBase(bool draw = true) { mychart = new ChartJS(); mychart.type = "line"; mychart.options = GetOptions(); mychart.options.title.text = "game details"; mychart.options.title.fontColor = "#c9c9ff"; mychart.options.legend.labels.fontColor = "#c9c9ff"; if (draw == true) { await ChartJSInterop.ChartChanged(_jsRuntime, JsonSerializer.Serialize(mychart)); } mycolorPool = new List <string>(colorPool); return(mychart); }
public static void SetColor(ChartJS mychart) { int i = 0; foreach (var ent in mychart.data.datasets) { i++; var col = GetChartColorFromLabel(ent.label); if (mychart.type == "bar") { if (ent.label == "global") { foreach (var cmdr in mychart.data.labels) { Match m = Regex.Match(cmdr, @"^(\w)+"); if (m.Success) { var col_global = GetChartColorFromLabel(m.Value); ent.backgroundColor.Add(col_global.backgroundColor); } } } else { ent.backgroundColor.Add(col.backgroundColor); } ent.borderColor = col.barborderColor; ent.borderWidth = 1; } else if (mychart.type == "line") { ent.backgroundColor.Add("rgba(0, 0, 0, 0)"); ent.borderColor = col.borderColor; ent.pointBackgroundColor = col.pointBackgroundColor; } else if (mychart.type == "radar") { ent.backgroundColor.Add(col.pointBackgroundColor); ent.borderColor = col.borderColor; ent.pointBackgroundColor = col.pointBackgroundColor; } } }
//public IViewComponentResult Invoke(string chartData) //{ // // Ref: https://www.chartjs.org/docs/latest/ // ChartJS chart = JsonConvert.DeserializeObject<ChartJS>(chartData); // var chartModel = new ChartJsViewModel // { // Chart = chart, // ChartJson = JsonConvert.SerializeObject(chart, new JsonSerializerSettings // { // NullValueHandling = NullValueHandling.Ignore // }) // }; // return View(chartModel); //} public IViewComponentResult Invoke(ChartJS chart) { // Ref: https://www.chartjs.org/docs/latest/ string teste = JsonConvert.SerializeObject(chart, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); var chartModel = new ChartJsViewModel { Chart = chart, ChartJson = JsonConvert.SerializeObject(chart, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }) }; return(View(chartModel)); }
public async Task <List <string> > SortChart(ChartJS chart, bool dry = false) { if (chart.type != "bar") { return(chart.data.labels); } if (chart.data.datasets.Count == 0 || chart.data.datasets.Count > 1) { return(chart.data.labels); } Dictionary <int, ChartJSsorthelper> sortMe = new Dictionary <int, ChartJSsorthelper>(); var opt = chart.options as ChartJsoptionsBar; for (int i = 0; i < chart.data.labels.Count; i++) { sortMe[i] = new ChartJSsorthelper(chart.data.labels[i], chart.data.datasets[0].data[i], opt.plugins.labels.images[i], chart.data.datasets[0].backgroundColor[i], chart.s_races_ordered[i]); } var sortedMe = sortMe.Values.OrderBy(o => o.Winrate); chart.data.labels = sortedMe.Select(s => s.Label).ToList(); chart.data.datasets[0].data = sortedMe.Select(s => s.Winrate).ToList(); opt.plugins.labels.images = sortedMe.Select(s => s.Image).ToList(); chart.data.datasets[0].backgroundColor = sortedMe.Select(s => s.Color).ToList(); chart.s_races_ordered = sortedMe.Select(s => s.s_race).ToList(); string labels = JsonConvert.SerializeObject(chart.data.labels); string winrates = JsonConvert.SerializeObject(chart.data.datasets[0].data); string images = JsonConvert.SerializeObject(opt.plugins.labels.images); string colors = JsonConvert.SerializeObject(chart.data.datasets[0].backgroundColor); if (dry == false) { await ChartJSInterop.SortChart(_jsRuntime, labels, winrates, images, colors); } return(chart.data.labels); }
public async Task DrawChart(ChartJS chart) { mychart = chart; await ChartJSInterop.ChartChanged(_jsRuntime, JsonSerializer.Serialize(mychart)); }
public async Task <ChartJS> GetChartBase(DSoptions _options, bool doit = true) { ChartJS mychart = new ChartJS(); List <string> GameModes = _options.Gamemodes.Where(x => x.Value == true).Select(s => s.Key).ToList(); if (GameModes.Contains("GameModeStandard") || GameModes.Contains("GameModeGear") || GameModes.Contains("GameModeSabotage") || GameModes.Contains("GameModeSwitch")) { mychart.s_races_ordered = DSdata.s_races.ToList(); } else { mychart.s_races_ordered = DSdata.s_races_cmdr.ToList(); } mychart.type = "bar"; if (_options.Mode == "Synergy" || _options.Mode == "AntiSynergy") { mychart.type = "radar"; } else if (_options.Mode == "Timeline") { mychart.type = "line"; List <string> _s_races_cmdr_ordered = new List <string>(); DateTime startdate = _options.Startdate; if (startdate == DateTime.MinValue) { startdate = new DateTime(2018, 1, 1); } DateTime enddate = _options.Enddate; if (enddate == DateTime.MinValue) { enddate = DateTime.Now.AddDays(1); } DateTime breakpoint = startdate; while (DateTime.Compare(breakpoint, enddate) < 0) { breakpoint = breakpoint.AddDays(1); _s_races_cmdr_ordered.Add(breakpoint.ToString("yyyy-MM-dd")); } _s_races_cmdr_ordered.RemoveAt(_s_races_cmdr_ordered.Count() - 1); mychart.s_races_ordered = _s_races_cmdr_ordered; } else if (_options.Mode == "Duration") { mychart.type = "line"; mychart.s_races_ordered = new List <string>(DSdata.s_durations); } mychart.options = GetOptions(_options); mychart.s_races = new List <string>(mychart.s_races_ordered); if (doit) { ChartJSdataset dataset = new ChartJSdataset(); if (_options.Mode == "Timeline" || _options.Mode == "Duration") { dataset.fill = false; dataset.pointRadius = 2; dataset.pointHoverRadius = 10; dataset.showLine = false; } if (String.IsNullOrEmpty(_options.Interest)) { dataset.label = "global"; if (_options.Mode == "Synergy" || _options.Mode == "AntiSynergy" || _options.Mode == "Timeline" || _options.Mode == "Duration") { _options.Interest = "Abathur"; dataset.label = _options.Interest; _options.CmdrsChecked["Abathur"] = true; } } else { dataset.label = _options.Interest; } SetColor(dataset, mychart.type, _options.Interest); dataset.backgroundColor.Clear(); mychart.data.datasets.Add(dataset); _options.Chart = mychart; await ChartJSInterop.ChartChanged(_jsRuntime, JsonConvert.SerializeObject(_options.Chart, Formatting.Indented)); DataResult dresult = await DataService.GetData(_options, _context, _jsRuntime, _db.lockobject); if (dresult != null) { _options.Chart.data.labels = new List <string>(dresult.Labels); _options.Chart.data.datasets[0] = dresult.Dataset.DeepCopy(); if (_options.Chart.type == "bar") { var options = _options.Chart.options as ChartJsoptionsBar; options.plugins.labels.images = dresult.Images; } _options.Cmdrinfo = dresult.CmdrInfo; SetCmdrPics(_options.Chart); await ChartJSInterop.ChartChanged(_jsRuntime, JsonConvert.SerializeObject(_options.Chart, Formatting.Indented)); if (dresult.fTimeline != null) { await DrawTimeline(_options, dresult, _db.lockobject); } } await SortChart(_options.Chart); } return(mychart); }
public async Task <IActionResult> Details(string?id) { if (id == null) { return(NotFound()); } var product = await _context.Product.Include(x => x.IdCategoriaNavigation).Include(x => x.IdPrecoNavigation).Include(x => x.IdPrecoNavigation.PrecoVariacoes) .FirstOrDefaultAsync(m => m.Id == id); if (product == null) { return(NotFound()); } ChartJsViewModel chartJS = new ChartJsViewModel(); ChartJS chart = new ChartJS(); Data data = new Data(); chartJS.Product = product; // Queremos um gráfico linear int i = 2; List <Datum> dataList = new List <Datum>(); foreach (var item in product.IdPrecoNavigation.PrecoVariacoes) { // Queremos as datas assim com as mudanças de preço dataList.Add(new Datum { x = item.DataAlteracao.ToString("MM/dd/yyyy"), y = item.Preco }); i++; } // Ordenar a lista de acordo com a data //dataList = dataList.OrderBy(o => o.t).ToList(); Datum[] dataListArray = dataList.ToArray(); data.datasets = new Dataset[] { new Dataset { label = product.Nome, data = dataListArray, backgroundColor = new string[] { "rgba(255,195,160, .5)" }, borderColor = new string[] { "rgba(253,165,92, .7)" }, borderWidth = 2, } }; //chart.data.datasets.; // data.datasets = new Dataset[] { dataset //}; // { //label = product.Nome, //data = dataArray, //backgroundColor = new string[] { "rgba(105, 0, 132, .2)" }, //borderColor = new string[] { "rgba(200, 99, 132, .7)" }, //borderWidth = 2 // } //}; chart.type = "line"; chart.options = new Options { responsive = true, scales = new Scales { xAxes = new Xax[] { new Xax { type = "time", time = new Time { unit = "day", unitStepSize = 1, displayFormats = new DisplayFormats { day = "MMM DD" } } } } }, }; chart.data = data; chartJS.Chart = chart; return(View(chartJS)); }