public ActionResult DateCapsule(DateGrouping grouping, string capsuleCode) { DateTime inicio = DateTime.Now; var reproduccionesDB = Database.SpawnsCapsulesItems.Where(p => p.SpawnsCapsules.Spawns.UserId == Username && (string.IsNullOrEmpty(capsuleCode) || p.SpawnsCapsules.CapsuleCode == capsuleCode)) .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 filas = reproduccionesDB .GroupBy(r => new { Code = r.Code, Date = r.Date.GetResolvedDate(grouping) }) .Select(s => new FechaTotalViewModel { Code = s.Key.Code, Date = s.Key.Date, TotalQuantity = s.Sum(p => p.Quantity), Items = s.GroupBy(u => u.ItemId).Select(v => new ItemCapsuleViewModel { Quantity = v.Sum(w => w.Quantity), CurrentItem = ItemBase.Create(ItemsXml, v.First().ItemId) }) }).ToList(); var totales = reproduccionesDB .GroupBy(r => r.Code) .Select(s => new FechaTotalViewModel { Code = s.Key, Date = DateTime.MinValue, TotalQuantity = s.Sum(p => p.Quantity), Items = s.GroupBy(u => u.ItemId).Select(v => new ItemCapsuleViewModel { Quantity = v.Sum(w => w.Quantity), CurrentItem = ItemBase.Create(ItemsXml, v.First().ItemId) }) }).ToList(); var model = new ByDateViewModel(); model.Grouping = grouping; model.Capsules = reproduccionesDB.Select(p => p.Code).Distinct().ToList(); model.DateInfo = reproduccionesDB.GroupBy(p => p.Date.GetResolvedDate(grouping)).Select(q => new DateInfoModel { Date = q.Key, TotalCapsules = q.Select(z => z.Code).Distinct().Count(), TotalItems = q.Sum(r => r.Quantity), RealDays = (int)(q.Max(h => h.Date) - q.Min(i => i.Date)).TotalDays + 1 }).OrderByDescending(b => b.Date); model.Rows = filas; model.Totals = totales; TimeSpan tiempo = DateTime.Now.Subtract(inicio); return(View(model)); }
public ActionResult InterestsChart(DateGrouping grouping, string itemId, bool accumulative) { DateTime inicio = DateTime.Now; var reproduccionesDB = Database.SpawnsCapsules.Where(p => p.Spawns.UserId == Username) .Select(s => new { Code = s.CapsuleCode, Date = s.Spawns.Date, Items = s.SpawnsCapsulesItems.Select(y => new { ItemId = y.ItemId, Quantity = y.Quantity }), }).ToList(); var model = new ByDateViewModel(); model.Grouping = grouping; model.DateInfo = reproduccionesDB .GroupBy(r => r.Date.GetResolvedDate(grouping)) .Select(s => new DateInfoModel { Date = s.Key, TotalCapsules = s.Where(n => n.Items.Any(q => string.IsNullOrEmpty(itemId) || q.ItemId == itemId)).Select(z => z.Code).Distinct().Count(), TotalItems = s.SelectMany(n => n.Items).Where(b => string.IsNullOrEmpty(itemId) || b.ItemId == itemId).DefaultIfEmpty().Sum(p => p == null ? 0 : p.Quantity) }).OrderBy(i => i.Date).ToList(); if (accumulative) { int actualItemQty = 0; int actualCapsQty = 0; foreach (DateInfoModel info in model.DateInfo) { info.TotalItems += actualItemQty; actualItemQty = info.TotalItems; info.TotalCapsules += actualCapsQty; actualCapsQty = info.TotalCapsules; } } TimeSpan tiempo = DateTime.Now.Subtract(inicio); string dateFormat = Resources.General.ResourceManager.GetString(string.Format("InterestsChart_DateFormat_{0}", model.Grouping.ToString())); 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; chart.Palette = ChartColorPalette.None; chart.PaletteCustomColors = new[] { Color.Orange, Color.LightGray }; 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(model.DateInfo.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); Series itemsSerie = new Series("Items"); itemsSerie.Font = fuente; itemsSerie.ChartType = SeriesChartType.Spline; itemsSerie.Points.DataBindXY(model.DateInfo.Select(p => p.Date.ToString(dateFormat)).ToArray(), model.DateInfo.Select(p => p.TotalItems).ToArray()); itemsSerie.IsValueShownAsLabel = grouping != DateGrouping.Day; itemsSerie.LabelForeColor = Color.Orange; chart.Series.Add(itemsSerie); Series capsulesSerie = new Series("Capsules"); capsulesSerie.Font = fuente; capsulesSerie.ChartType = SeriesChartType.Column; capsulesSerie.Points.DataBindXY(model.DateInfo.Select(p => p.Date.ToString(dateFormat)).ToArray(), model.DateInfo.Select(p => p.TotalCapsules).ToArray()); capsulesSerie.IsValueShownAsLabel = grouping != DateGrouping.Day; capsulesSerie.LabelForeColor = Color.LightGray; chart.Series.Add(capsulesSerie); MemoryStream ms = new MemoryStream(); chart.SaveImage(ms, ChartImageFormat.Png); return(File(ms.ToArray(), "image/png")); } } }