Using d3.js graphs, I successfully added the graph to the View cshtml page. Now, I want to dynamically fetch values for the graph from my database. To achieve this, I implemented the following function in the Controller:
protected int CalculateReadinessAvg()
{
var avgReadiness = 0;
var countItems = 0;
foreach (var item in db.Reviews)
{
avgReadiness = avgReadiness + item.LecturerReadine;
countItems++;
}
avgReadiness = avgReadiness / countItems;
return avgReadiness;
}
The above function works perfectly and returns the relevant value. Now, I need to incorporate this value into the JS code that renders the graph. Here is my attempt:
var freqData = [
{ State: '2013', freq: { LecturerReadine: '<%=CalculateReadinessAvg()%>', LecturerTransferRate: 412, LecturerAttitude: 674, LecturerKnowledge: 2001 } }
, { State: '2014', freq: { LecturerReadine: 932, LecturerTransferRate: 2149, LecturerAttitude: 418, LecturerKnowledge: 4726 } }
, { State: '2015', freq: { LecturerReadine: 832, LecturerTransferRate: 1152, LecturerAttitude: 1862, LecturerKnowledge: 2135 } }
];
However, the call to the function LecturerReadine: '<%=CalculateReadinessAvg()%>' doesn't seem to be working as expected. Any suggestions on how to resolve this?