In my ASP.NET MVC application, there is a line chart in which I want to show record counts and the month on X-axis and Y-axis.
So I’m trying to call an ajax to the controller and the model has a record created date and the value.
I can’t figure out how to return it by grouping it like the month of April has 15 records. The month of May has 12 records… likewise.
In my model structured like this,
public class CustomerFeedBack {
[Key]
public int Id {
get;
set;
}
public DateTime CreatedDate {
get;
set;
} = DateTime.Now;
public int TotalSatisfaction {
get;
set;
}
}
This is the javascript of the chart, here in the data I want to return the count of records and for labels months. Currently, it has dummy data.
var bouData = {
// Generate the days labels on the X axis.
labels: Array.from(new Array(30), function (_, i) {
return i === 0 ? 1 : i;
}),
datasets: [{
label: 'Satisfied',
fill: 'start',
data: [1500, 800, 320, 180, 240, 320, 230, 650, 590, 1200, 750, 940, 1420, 1200, 960, 1450, 1820, 2800, 2102, 1920, 3920, 3202, 3140, 2800, 3200, 3200, 3400, 2910, 3100, 4250],
backgroundColor: 'rgba(0,123,255,0.1)',
borderColor: 'rgba(0,123,255,1)',
pointBackgroundColor: '#ffffff',
pointHoverBackgroundColor: 'rgb(0,123,255)',
borderWidth: 1.5,
pointRadius: 0,
pointHoverRadius: 3
},
So in the controller, I want to return JsonResult and bind to labels as month and data as count.
This is my incomplete controller code.
var satisfied = (from a in db.tbl_Main where a.TotalSatisfaction >= 12
select new {
// Here I want to check with the create date group it as Month and sum the value of a.TotalSatisfaction count.
}).ToList();
I’m not an experienced developer regarding this, Still learning and would appreciate if you can suggest if there is another way of doing this easily or how to complete this.
Thanks.