ASP.NET MVC:
public ActionResult chart()
{
Random y = new Random();
List<Data> value = new List<Data>();
//Data created for series
for (int i = 0; i < 12; i++)
{
value.Add(new Data(new DateTime(2016, i+1, 01), y.Next(1000, 2000)));
}
List<JsonData> json = new List<JsonData>();
//Binding dataSource with ID
for (int j = 0; j < 10; j++)
{
json.Add(new JsonData(j, value));
}
return Json(json);
}
public class JsonData
{
public double Id { get; set; }
public List<Data> data {get; set;}
public JsonData(double Id, List<Data> data)
{
this.Id = Id;
this.data = data;
}
}
//Class for x and y values
public class Data
{
public Data(DateTime xval, int yval)
{
this.xvalue = xval;
this.yvalue = yval;
}
public DateTime xvalue { get; set; }
public int yvalue { get; set; }
}
//Binding data to chart in load event using AJAX
function getJsonData(sender) {
$.ajax({
type: "POST",
url: '@Url.Action("chart","Home")',
async: false,
success: function (jsondata) {
for (var i = 0; i < jsondata.length; i++) {
if (i == jsondata [i].Id) {
sender.model.series[i].dataSource = jsondata [i].data;
sender.model.series[i].xName = "xvalue";
sender.model.series[i].yName = "yvalue";
}
}
},
});
} |