I'm generating a table to use on jQuery visualize plugin and generate a chart. The code is pretty good, but maybe there is a way to improve it. Maybe do more work on the LINQ query and less stuff on the rest of the code.
The idea is get every content and the launch date of them for each region. Separate all dates and the the amount of content launched that day. But maybe there is a better way. Anyone?
Here is the method i use:
public KeyValuePair<DateTime[], List<KeyValuePair<string, int[]>>> TimeGrowth(int days, params string[] locales)
{
DateTime startdate = DateTime.Today.AddDays(days * (-1));
var qry = from r in GlobalVariables.Regions
where locales.Contains(r.ID)
select new
{
Region = r,
Launches = (from d in r.RegionalContents
where d.RegionId == r.ID && d.PublishDate != null
group d by d.PublishDate into groupeddates
select new
{
Date = groupeddates.Key,
Count = groupeddates.Count()
}).OrderBy(x => x.Date)
};
var regions = qry.ToArray();
var dates = new List<DateTime>();
var datesbeforestart = new List<DateTime>();
var countries = new List<KeyValuePair<string, int[]>>();
for (int i = 0; i < regions.Length; i++)
{
for (int j = 0; j < regions[i].Launches.Count(); j++)
{
var data = regions[i].Launches.ElementAt(j).Date.Value;
if (data > startdate)
dates.Add(data);
else
datesbeforestart.Add(data);
}
}
dates = dates.Distinct().OrderBy(x => x.Date).ToList();
for (int i = 0; i < regions.Length; i++)
{
var values = new int[dates.Count];
values[0] = CountDateInterval(datesbeforestart.Min(), startdate, regions[i].Region.ID);
int acum = values[0];
for (int j = 1; j < dates.Count; j++)
{
int valor = 0;
if (regions[i].Launches.Any(x => x.Date == dates[j]))
valor = regions[i].Launches.Single(x => x.Date == dates[j]).Count;
acum += valor;
values[j] = acum;
}
var country = new KeyValuePair<string, int[]>(regions[i].Region.CountryEnglish, values);
countries.Add(country);
}
countries = countries.OrderByDescending(x => x.Value.Max()).ToList();
var finalresult = new KeyValuePair<DateTime[], List<KeyValuePair<string, int[]>>>(dates.ToArray(), countries);
return finalresult;
}
You can see the actual running code here on the last chart: http://programad.net/xbltoolsv2beta/Stats