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.
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 = db.tbl_Main.Where(m => m.TotalSatisfaction >= 12).GroupBy(
m => new { m.CreatedDate.Year, m.CreatedDate.Month },
m => m.TotalSatisfaction
)
.Select(g => new
{
label = new DateTime(g.Key.Year, g.Key.Month, 1),
data = g.Sum()
})
.ToList();
I would appreciate it if you can suggest if there is another way of doing this easily or how to complete this.
In the end, What I want to return is,
* tbl_Main contains the record as Datetime.
* I want to get the sum of the count of the same month.
* Return example
January 10
February 30
March 18
Likewise month with record count.