[Chartjs]-Using ChartJs with MVC 5 Razor

1👍

This is just example how it should work.
Inside your controller make something similar like this.

public ActionResult Chart()
{
   var Value1 = db.MYTABLE.Where(i => i.Value1);
   var Value2 = db.MYTABLE.Where(i => i.Value2);          
   var dataForChart = new[]
   {  
      new 
      {
         label = "Your Label1", 
         value = Value1.Count()
      },
      new 
      {
         label = "Your Label2", 
         value = Value2.Count()
      }                
   };

   var result = Json(dataForChart, JsonRequestBehavior.AllowGet);
   return result;

 }

Your JavaScript should look like this:

$.get("/ControllerName/Chart", function (response) {            
  //Construct your ChartJS here.          
});

Place this in your View to call it:

<canvas id="Chart" width="400" height="400"></canvas>

Leave a comment