Program is written in .Net Framework 4.
Class:
public class yearsComparison
{
public int year { get; set; }
public string material1 { get; set; }
public string material2 { get; set; }
public string material3 { get; set; }
public string material4 { get; set; }
public string material5 { get; set; }
public decimal value1 { get; set; }
public decimal value2 { get; set; }
public decimal value3 { get; set; }
public decimal value4 { get; set; }
public decimal value5 { get; set; }
public string vendor1 { get; set; }
public string vendor2 { get; set; }
public string vendor3 { get; set; }
public string vendor4 { get; set; }
public string vendor5 { get; set; }
}
Code to change data into json, it will return top five over purchase material from current year and last year:
public ActionResult OverPurchaseToYear()
{
var overPurchaseToYear = DAL.ReportHandler.GetOverPurchaseYearToYear(DateTime.Now.Year, 5);
List<yearsComparison> chartData = new List<yearsComparison>();
int index = 1;
foreach (var item in overPurchaseToYear)
{
if (chartData.Where(x => x.year == item.forecastYear).Any())
{
yearsComparison temp = chartData.Where(x => x.year == item.forecastYear).FirstOrDefault();
switch (index)
{
case 1:
temp.material1 = item.material;
temp.value1 = (decimal)item.overPurchaseQuantity;
temp.vendor1 = item.emVendor;
break;
case 2:
temp.material2 = item.material;
temp.value2 = (decimal)item.overPurchaseQuantity;
temp.vendor2 = item.emVendor;
break;
case 3:
temp.material3 = item.material;
temp.value3 = (decimal)item.overPurchaseQuantity;
temp.vendor3 = item.emVendor;
break;
case 4:
temp.material4 = item.material;
temp.value4 = (decimal)item.overPurchaseQuantity;
temp.vendor4 = item.emVendor;
break;
case 5:
temp.material5 = item.material;
temp.value5 = (decimal)item.overPurchaseQuantity;
temp.vendor5 = item.emVendor;
break;
}
}
else
{
yearsComparison temp = new yearsComparison();
temp.year = item.forecastYear;
switch (index)
{
case 1:
temp.material1 = item.material;
temp.value1 = (decimal)item.overPurchaseQuantity;
temp.vendor1 = item.emVendor;
break;
case 2:
temp.material2 = item.material;
temp.value2 = (decimal)item.overPurchaseQuantity;
temp.vendor2 = item.emVendor;
break;
case 3:
temp.material3 = item.material;
temp.value3 = (decimal)item.overPurchaseQuantity;
temp.vendor3 = item.emVendor;
break;
case 4:
temp.material4 = item.material;
temp.value4 = (decimal)item.overPurchaseQuantity;
temp.vendor4 = item.emVendor;
break;
case 5:
temp.material5 = item.material;
temp.value5 = (decimal)item.overPurchaseQuantity;
temp.vendor5 = item.emVendor;
break;
}
chartData.Add(temp);
}
index++;
}
var json = chartData.ToArray();
var jsonData = Json(json, JsonRequestBehavior.AllowGet);
return jsonData;
}
The json has to be in this format:
[
{"year":2018,"material1":"00-00012","material2":"00-00011","material3":"00-00010","material4":"00-00009","material5":"00-00008","value1":220.00,"value2":210.00,"value3":200.00,"value4":190.00,"value5":180.00,"vendor1":"12345","vendor2":"12345","vendor3":"12345","vendor4":"12345","vendor5":"12345"},
{"year":2017,"material1":"00-00012","material2":"00-00011","material3":"00-00010","material4":"00-00009","material5":"00-00008","value1":220.00,"value2":210.00,"value3":200.00,"value4":190.00,"value5":180.00,"vendor1":"12345","vendor2":"12345","vendor3":"12345","vendor4":"12345","vendor5":"12345"}
]
The generated chart will look like this
Code works but ugly, how can I improve it?