I am developing a system at ASP.NET MVC4. I have a code written at javascript, and which executes when the page starts, to display a graph about the performance of some students.
The code of the graph is the following
<script>
window.onload = function () {
var r = Raphael("holder"),
txtattr = { font: "20px sans-serif" };
var lines = r.linechart(30, 30, 600, 440,<%= Json.Encode(ViewBag.dates) %>,<%= Json.Encode(ViewBag.Grades)%>, {axisxstep : 6,nostroke: false, axis: "0 0 1 1", symbol: "circle", smooth: false }).hoverColumn(function () {
this.tags = r.set();
for (var i = 0, ii = this.y.length; i < ii; i++) {
this.tags.push(r.tag(this.x, this.y[i], Number(this.values[i]).toFixed(5), 160, 10).insertBefore(this).attr([{ fill: "#fff" }, { fill: this.symbols[i].attr("fill") }]));
}
});
</script>
The data of the graph, come from the function of the controller DisplayGraph. The code of the controller is shown below.
public ActionResult DisplayGraph(String InputStudent = "IP11")
{
var query = (from b in db.gradesPerStudent
where b.Student.Equals(InputStudent)
orderby b.Date
select b);
ViewBag.ChartEmpty = "No";
if (query.ToList().Count == 0)
{
ViewBag.ChartEmpty = "Yes";
ViewBag.Title = "No results for Student " + InputStudent;
ViewBag.dates = null;
ViewBag.grades = null;
DateTime aminDate = new DateTime();
ViewBag.minDate = aminDate.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
return View();
}
DateTime minDate = query.Min(y => y.Date);
DateTime maxDate = query.Max(y => y.Date);
var dates = query.Select(x => EntityFunctions.DiffDays(query.Min(y => y.Date), x.Date));
var grades = query.Select(x => x.grades);
var datestable = dates.ToArray();
var gradestable = grades.ToArray();
List<int?> dateslist = new List<int?>();
List<double> gradeslist = new List<double>();
double result = dates.Count() * 1.0 / 70.0;
int pointStep = (int)Math.Ceiling(result);
for (int i = 0; i < dates.Count(); i = i + pointStep)
{
dateslist.Add(datestable.ElementAt(i));
gradeslist.Add(gradestable.ElementAt(i));
}
ViewBag.dates = dateslist;
ViewBag.grades = gradeslist;
ViewBag.minDate = minDate.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
return View(query.ToList());
}
Now, at when the student is new at a school, I do not want to display a graph, but a message that the student is new, and there are no statistics yet for that student.
So my question is. How do I say from my controller when a javascript should be executed and when not?
Thanks a lot