public PartialViewResult ComparativeGraph() { string graphDataJSON = ""; List<AnonymousProperty> graphData = new List<AnonymousProperty>(); ArrayList data = new ArrayList(); try { graphData = ResponseReader.convertTo<List<AnonymousProperty>>(emAPI.getComparativeCostsForUser(int.Parse(User.Identity.Name))); ArrayList header = new ArrayList { "Postcode", "Your Properties", "Other Properties" }; data.Add(header); } catch { return PartialView("Error"); } if (graphData == null | graphData.Count == 0) { return PartialView("NotEnoughDataError"); } double totalAmount = 0; foreach (AnonymousProperty anonProperty in graphData) { if (anonProperty.AnnualCost != 0) { ArrayList point = new ArrayList(); ///if the property is the user's, add 0 to other and cost to your point.Add(anonProperty.Postcode); if (anonProperty.isUsers) { point.Add(Math.Round(anonProperty.AnnualCost,2)); point.Add(0); } else { ///not the user's, so reverse the order point.Add(0); point.Add(Math.Round(anonProperty.AnnualCost,2)); } data.Add(point); totalAmount += anonProperty.AnnualCost; } } if (totalAmount != 0) { string json = JsonConvert.SerializeObject(data).ToString(); json = json.Replace("\"", "'"); Graph graph = new Graph(); graph.Data = json; graph.Title = ""; graph.Width = 900; graph.Height = 400; graph.hAxisTitle = "Postcode"; graph.hAxisFormat = ""; graph.vAxisTitle = "Annual Cost, ₤"; graph.vAxisFormat = "#,###"; graph.Legend = "bottom"; graph.Baseline = 0; graph.chartAreaWidth = 750; return PartialView("GraphFromArray", graph); } else { return PartialView("NotEnoughDataError"); } }
//GRPAHING METHODS /// <summary> /// Shows standard coloum graph /// </summary> /// <param name="options"></param> /// <param name="dataTypeId"></param> /// <returns></returns> public PartialViewResult IntervalGraph(AnalysisOptionsModel options, int dataTypeId) { ///graphJSON holds data for display by javascript in GraphFromArray.cshtml string graphJSON = ""; try { ///get list of chunks for required period from API int propertyId = options.propertyId; string startDate = options.startDate.ToShortDateString(); string endDate = options.endDate.ToShortDateString(); int intervalId = options.periodId; graphJSON = emAPI.getDataAtProperty(propertyId, startDate, endDate, intervalId, dataTypeId); } catch { return PartialView("Error"); } ///go to error page if not enough data List<Chunk> graphData = ResponseReader.convertTo<List<Chunk>>(graphJSON); if (graphData == null) { return PartialView("NotEnoughDataError"); } ///create header of datatable ArrayList data = new ArrayList(); ArrayList header = new ArrayList { "Date", "Amount" }; data.Add(header); ///create variable used to assess whether any data has been returned. double totalAmount = 0; ///add each row to datatable foreach (Chunk chunk in graphData) { totalAmount += chunk.Amount; ArrayList point = new ArrayList(); point.Add(chunk.EndDate.ToShortDateString()); point.Add(Math.Round(chunk.Amount, 2)); data.Add(point); } string json = JsonConvert.SerializeObject(data).ToString(); json = json.Replace("\"", "'"); ///configure graph Graph graph = new Graph(); graph.Data = json; graph.Title = ""; graph.Width = 900; graph.Height = 400; graph.hAxisTitle = "Interval Ending"; graph.hAxisFormat = ""; graph.vAxisFormat = "#,###"; graph.Legend = "none"; graph.Baseline = 0; graph.chartAreaWidth = 750; ///populate graph title with switch switch (options.periodId) { case 1: graph.vAxisTitle = "Daily kWh"; break; case 2: graph.vAxisTitle = "Weekly kWh"; break; case 3: graph.vAxisTitle = "Monthly kWh"; break; case 4: graph.vAxisTitle = "Quarterly kWh"; break; case 5: graph.vAxisTitle = "Annual kWh"; break; default: break; } ///show graph if there is data if (totalAmount > 0) { return PartialView("GraphFromArray", graph); } else { ///return error if no data return PartialView("NotEnoughDataError"); } }