public FileResult PercentageRarityChart(DateGrouping grouping, bool percentage) { DateTime inicio = DateTime.Now; var type = SeriesChartType.StackedColumn; if (percentage) { type = SeriesChartType.StackedColumn100; } string dateFormat = Resources.General.ResourceManager.GetString(string.Format("InterestsChart_DateFormat_{0}", grouping.ToString())); var rarityDef = new[] { new { Rarity = "R1", Name = "Very Common", Color = "#b5b2b5" }, new { Rarity = "R2", Name = "Common", Color = "#84f7b5" }, new { Rarity = "R3", Name = "Rare", Color = "#ad8eff" }, new { Rarity = "R4", Name = "Very Rare", Color = "#ff8ef7" } }; var dbData = Database.SpawnsCapsulesItems.Where(p => p.SpawnsCapsules.Spawns.UserId == Username) .GroupBy(r => new { Date = DbFunctions.TruncateTime(r.SpawnsCapsules.Spawns.Date).Value, ItemId = r.ItemId }) .Select(s => new { Date = s.Key.Date, ItemId = s.Key.ItemId, Quantity = s.Sum(m => m.Quantity), }).ToList() .GroupBy(r => new { Rarity = ItemsXml.Single(j => j.ItemId == r.ItemId).Rarity, Date = r.Date.GetResolvedDate(grouping) }) .Select(s => new { Rarity = s.Key.Rarity, Date = s.Key.Date, Quantity = s.Sum(w => w.Quantity) }); var dates = dbData.Select(p => p.Date.GetResolvedDate(grouping)).Distinct().ToArray(); var rarityies = dbData.Select(p => p.Rarity).Distinct().ToArray(); var points = from date in dates from rarity in rarityies select new { Rarity = rarity, Date = date }; var matrix = from point in points join data in dbData on new { point.Rarity, point.Date } equals new { data.Rarity, data.Date } into joineddbData from subData in joineddbData.DefaultIfEmpty() select new { Rarity = point.Rarity, Date = point.Date, Qty = subData == null ? 0 : subData.Quantity }; var itemsQtyDate = matrix .OrderByDescending(a => a.Rarity) .ThenBy(d => d.Date) .GroupBy(b => b.Rarity) .Select(c => new { Rarity = c.Key, Name = rarityDef.Single(p => p.Rarity == c.Key).Name, Color = System.Drawing.ColorTranslator.FromHtml(rarityDef.Single(p => p.Rarity == c.Key).Color), Dates = c.Select(o => o.Date.ToString(dateFormat)).ToArray(), Values = c.Select(o => o.Qty).ToArray() } ); PrivateFontCollection pfc = new PrivateFontCollection(); pfc.AddFontFile(Server.MapPath("~/Content/Coda-Regular.ttf")); using (Chart chart = new Chart()) { chart.Font.Name = pfc.Families[0].Name; chart.Font.Size = FontUnit.Point(8); chart.Width = 1000; chart.Height = 400; chart.BackColor = Color.FromArgb(255, 0x27, 0x2B, 0x30); chart.BorderlineDashStyle = ChartDashStyle.Solid; chart.BorderlineColor = Color.Gray; chart.BorderlineWidth = 1; Legend legend = new Legend(); legend.BackColor = Color.Transparent; legend.ForeColor = Color.White; chart.Legends.Add(legend); chart.Palette = ChartColorPalette.None; chart.PaletteCustomColors = itemsQtyDate.Select(p => p.Color).ToArray(); using (Font fuente = new Font(pfc.Families[0], 8, GraphicsUnit.Point)) { ChartArea area = new ChartArea(); area.BackColor = Color.Transparent; area.ShadowColor = Color.Transparent; area.BorderColor = Color.FromArgb(255, 0x88, 0x88, 0x88); area.BorderDashStyle = ChartDashStyle.Solid; var ejeX = area.AxisX; ejeX.LabelStyle.Font = fuente; ejeX.LabelStyle.ForeColor = Color.White; ejeX.LineColor = Color.FromArgb(255, 0x99, 0x99, 0x99); ejeX.IsLabelAutoFit = false; ejeX.IsMarginVisible = true; ejeX.MajorGrid.LineColor = Color.FromArgb(255, 0x99, 0x99, 0x99); ejeX.MajorTickMark.LineColor = Color.FromArgb(255, 0xAA, 0xAA, 0xAA); var interval = Math.Max(dates.Count() / 25, 1); ejeX.Interval = interval; ejeX.LabelStyle.Angle = -90; var ejeY = area.AxisY; ejeY.LabelStyle.Font = fuente; ejeY.LabelStyle.ForeColor = Color.White; ejeY.LineColor = Color.FromArgb(255, 0x99, 0x99, 0x99); ejeY.IsLabelAutoFit = false; ejeY.IsMarginVisible = true; ejeY.MajorGrid.LineColor = Color.FromArgb(255, 0x99, 0x99, 0x99); ejeY.MajorTickMark.LineColor = Color.FromArgb(255, 0xAA, 0xAA, 0xAA); chart.ChartAreas.Add(area); foreach (var item in itemsQtyDate) { Series itemsSerie = new Series(item.Name); itemsSerie.Font = fuente; itemsSerie.ChartType = type; itemsSerie.Points.DataBindXY(item.Dates, item.Values); itemsSerie.IsValueShownAsLabel = grouping != DateGrouping.Day; chart.Series.Add(itemsSerie); } MemoryStream ms = new MemoryStream(); chart.SaveImage(ms, ChartImageFormat.Png); return(File(ms.ToArray(), "image/png")); } } }
public JsonResult ByItemDate(DateGrouping grouping, int periods) { DateTime startDate = new DateTime(2000, 1, 1); // all data if (periods < 0) { periods = 4; } if (periods > 0) { switch (grouping) { case DateGrouping.Year: startDate = DateTime.Now.Date.GetResolvedDate(DateGrouping.Year).AddYears(1 - periods); break; case DateGrouping.Month: startDate = DateTime.Now.Date.GetResolvedDate(DateGrouping.Month).AddMonths(1 - periods); break; case DateGrouping.Week: startDate = DateTime.Now.Date.GetResolvedDate(DateGrouping.Week).AddDays((1 - periods) * 7); break; case DateGrouping.Day: default: startDate = DateTime.Now.Date.AddDays(1 - periods); break; } } string dateFormat = Resources.General.ResourceManager.GetString(string.Format("InterestsByDate_DateFormat_{0}", grouping.ToString())); var reproduccionesDB = Database.SpawnsCapsulesItems.Where(p => p.SpawnsCapsules.Spawns.UserId == Username && DbFunctions.TruncateTime(p.SpawnsCapsules.Spawns.Date).Value >= startDate) .GroupBy(r => new { Date = DbFunctions.TruncateTime(r.SpawnsCapsules.Spawns.Date).Value, Code = r.SpawnsCapsules.CapsuleCode, ItemId = r.ItemId }) .Select(s => new { Code = s.Key.Code, Date = s.Key.Date, ItemId = s.Key.ItemId, Quantity = s.Sum(m => m.Quantity), }).ToList(); var items = reproduccionesDB .GroupBy(r => new { Date = r.Date.GetResolvedDate(grouping) }) .Select(s => new DateInfoTotalModel { Date = s.Key.Date, FormattedDate = s.Key.Date.ToString(dateFormat), TotalCapsules = s.Select(h => h.Code).Distinct().Count(), TotalItems = s.Sum(p => p.Quantity), RealDays = (int)(s.Max(h => h.Date) - s.Min(i => i.Date)).TotalDays + 1, Items = s.GroupBy(u => u.ItemId).Select(v => new { ItemId = v.Key, Cantidad = v.Sum(w => w.Quantity) }).ToDictionary(m => m.ItemId, n => n.Cantidad) }).ToList(); var totals = reproduccionesDB .GroupBy(q => q.ItemId) .Select(i => new ItemCapsuleViewModel { CurrentItem = ItemBase.Create(ItemsXml, i.Key), Quantity = i.Sum(j => j.Quantity) }).OrderBy(k => k.CurrentItem.Order); var highests = reproduccionesDB .GroupBy(r => new { Date = r.Date.GetResolvedDate(grouping), ItemId = r.ItemId }) .Select(s => new { ItemId = s.Key.ItemId, Cantidad = s.Sum(p => p.Quantity) }) .GroupBy(h => h.ItemId) .Select(i => new { ItemId = i.Key, Maximo = i.Max(j => j.Cantidad) }) .ToDictionary(h => h.ItemId, i => i.Maximo); var model = new ByDateTotalViewModel(); model.Grouping = grouping; model.DateInfo = items.OrderByDescending(b => b.Date); model.Totals = totals; model.Highests = highests; model.TotalItems = totals.Sum(p => p.Quantity); model.Items = model.Totals.Select(p => p.CurrentItem.ItemId).ToArray(); return(Json(model, JsonRequestBehavior.AllowGet)); }