I have spent a good few days learning linq and producing json results from my controller in mvc. I am now however stuck on an issue where I wish to rolling sum (cumulative sum) values of Y-Axis data to produce a year to date line chart.
My code at present to produce simple monthly data is as follows:
//Generic Json For Graphs
public JsonResult GetJSONYTD(int kpiID)
{
var ViewData =
(from kpidata in departmentrepo.GetGraphData(kpiID)
select new DepartmentOverviewDetailsViewModel.GraphJSONViewModel
{
XData = kpidata.Year.Year1 + "-"
+ kpidata.Month.Real_Month_Int + "-01",
YData = kpidata.Value
});
var ChartData = ViewData.Select(
x => new object[] { x.XData, x.YData }).ToArray();
return Json(ChartData, JsonRequestBehavior.AllowGet);
}
The above produces the following array:
[
["2011-10-01",0],
["2011-11-01",22],
["2011-12-01",22],
["2012-1-01",14],
["2012-2-01",14.4],
["2012-3-01",17.5],
["2012-4-01",20.3],
["2012-5-01",23.5],
["2012-6-01",24.5],
["2012-7-01",26.5]
]
I would like to output:
[
["2011-10-01",0],
["2011-11-01",22],
["2011-12-01",44],
["2012-1-01",38],
["2012-2-01",52.4],
etc
]
Any help?
Change your code to:
//Generic Json For Graphs
The above code will store in YData the sum of all data points that have a data less than or equal to the current date.