Example #1
0
 public static List <Band> GetKpiBands(
     SizeUpContext context, long industryId, long locationId, string granularityName,
     Expression <Func <IndustryData, bool> > filter,
     Expression <Func <IndustryData, LabeledValue> > selector,
     string formatString, int numBands
     )
 {
     return(Core.DataLayer.IndustryData.Get(context)
            .Where(i => i.GeographicLocation.GeographicLocations.Any(gl => gl.Id == locationId))
            .Where(i => i.IndustryId == industryId)
            .Where(i => i.GeographicLocation.Granularity.Name == granularityName)
            .Where(filter)
            .Select(selector)
            .ToList()
            .NTileDescending(i => i.Value, numBands)
            .Select(i => new Kpi.Band
     {
         Min = string.Format(formatString, Kpi.Format(i.Min(v => v.Value.Value))),
         Max = string.Format(formatString, Kpi.Format(i.Max(v => v.Value.Value))),
         Items = i.Select(v => v.Label).ToList()
     })
            .ToList());
 }
        public ActionResult Bands(
            int variableId, long boundingGeographicLocationId, int bands,
            Core.DataLayer.Granularity granularity, string contentType = "*/*")
        {
            using (var context = ContextFactory.SizeUpContext)
            {
                if ("text/html".Equals(contentType))
                {
                    var variable = context.ConsumerExpenditureVariables
                                   .Where(i => i.Id == variableId)
                                   .FirstOrDefault();
                    var gran = Enum.GetName(typeof(Core.DataLayer.Granularity), granularity);

                    int granularityId = 0;
                    if (granularity == Core.DataLayer.Granularity.ZipCode)
                    {
                        granularityId = 1;
                    }
                    else if (granularity == Core.DataLayer.Granularity.City)
                    {
                        granularityId = 2;
                    }
                    else if (granularity == Core.DataLayer.Granularity.County)
                    {
                        granularityId = 3;
                    }
                    else if (granularity == Core.DataLayer.Granularity.Place)
                    {
                        granularityId = 4;
                    }
                    else if (granularity == Core.DataLayer.Granularity.Metro)
                    {
                        granularityId = 5;
                    }
                    else if (granularity == Core.DataLayer.Granularity.State)
                    {
                        granularityId = 6;
                    }
                    else if (granularity == Core.DataLayer.Granularity.Nation)
                    {
                        granularityId = 7;
                    }

                    var data = context.ConsumerExpenditures
                               .Where(i => i.Year == CommonFilters.TimeSlice.ConsumerExpenditures.Year && i.Quarter == CommonFilters.TimeSlice.ConsumerExpenditures.Quarter)
                               .Where(i => i.GeographicLocation.Granularity.Name == gran)
                               .Where(i => i.GeographicLocation.GeographicLocations.Any(g => g.Id == boundingGeographicLocationId));

                    int count = data.ToList().Count();

                    var output = GetPayloads(variable.Variable, granularityId, boundingGeographicLocationId)
                                 .NTileDescending(i => i.Value, bands)
                                 .Select(i => new Kpi.Band
                    {
                        Min   = string.Format("${0}", Kpi.Format(i.Min(v => v.Value))),
                        Max   = string.Format("${0}", Kpi.Format(i.Max(v => v.Value))),
                        Items = i.Select(v => v.Name).ToList()
                    })
                                 .ToList();

                    ViewBag.Area = context.GeographicLocations
                                   .Where(i => i.Id == boundingGeographicLocationId)
                                   .Select(i => i.LongName)
                                   .FirstOrDefault();
                    ViewBag.Bands         = Kpi.FormatBands(output);
                    ViewBag.Expenditure   = variable;
                    ViewBag.LevelOfDetail = Kpi.TranslateGranularity(granularity);
                    ViewBag.Q             = bands.ToString();
                    ViewBag.ThemeUrl      = System.Configuration.ConfigurationManager.AppSettings["Theme.Url"];
                    ViewBag.Query         = string.Format(
                        "Rank {0} in {1}, by {2} Consumer Spending on {3}, in {4} quantiles",
                        ViewBag.LevelOfDetail, ViewBag.Area,
                        variable.Variable.StartsWith("X") ? "Average" : "Total",
                        variable.Description, ViewBag.Q
                        );
                    return(View("Heatmap"));
                }
                else
                {
                    var output = Core.DataLayer.ConsumerExpenditures.Bands(context, variableId, boundingGeographicLocationId, bands, granularity);
                    return(Json(output, JsonRequestBehavior.AllowGet));
                }
            }
        }