public static string createMultipleChartScript(InstructorGraph data, List <string> labels) { string script = ""; script += @"var ctx = document.getElementById('instructorChart'); if($(window).width() < 400) ctx.height = 700; else if($(window).width() < 450) ctx.height = 650; else if($(window).width() < 600) ctx.height = 450; else if($(window).width() < 767) ctx.height = 350; var instructorChart = new Chart(ctx, { type: 'line', data: { labels: " + data.labels + @", datasets: ["; int i = 0; foreach (var element in data.data) { script += @"{ label: '" + labels[i] + @"', data: " + element + @", borderWidth: 1, borderColor: '" + StaticData.graphColors[i] + @"', hidden: " + (i == data.data.Count() - 1 || (data.data.Count() > 1 && i == 0) ? "false" : "true") + @" },"; i++; } //Delete last comma if (script[script.Length - 1] == ',') { script = script.Remove(script.Length - 1); } script += @"] }, options: { scales: { yAxes: [{ ticks: { suggestedMin: 1 } }] }, responsive: true } });"; return(script); }
public void createMultipleChartScriptTest() { //Arrange InstructorGraph testGraph = new InstructorGraph(); testGraph.data = new List <string>(); List <string> testLabels = new List <string>(); //Act string result = GlobalFunctions.createMultipleChartScript(testGraph, testLabels); //Assert Assert.IsNotNull(result); }
public ViewResult Detail() { InstructorDetailViewModel viewModel = new InstructorDetailViewModel(); if (Request.QueryString["instructor"] != null && StaticData.instructorList.Where(p => GlobalFunctions.escapeQuerystringElement(p.firstName + p.lastName) == GlobalFunctions.escapeQuerystringElement(Request.QueryString["instructor"].ToString())).Count() == 1) { Instructor instructor = StaticData.instructorList .Where(p => GlobalFunctions.escapeQuerystringElement(p.firstName + p.lastName) == GlobalFunctions.escapeQuerystringElement(Request.QueryString["instructor"].ToString())) .FirstOrDefault(); //Get semesters to display List <Semester> semesters = StaticData.semesters.Where(p => new SemesterComparer().Compare(p.semester, GlobalVariables.CurrentSemesterLow) >= 0 && new SemesterComparer().Compare(p.semester, GlobalVariables.CurrentSemesterHigh) <= 0).ToList(); List <CourseRating> courseRatingsOverall = CourseRatingRepositorySQL.Instance.listByInstructor(instructor.instructorID); //Get all course ratings for the instructor, and adjust to the semester range List <CourseRating> courseRatings = courseRatingsOverall .Where(p => semesters.Select(y => y.semester).Contains(p.semester)).ToList(); //Calculate number of responses and number of students var responsesOverall = courseRatingsOverall.Select(y => y.responses).Sum(z => z); var studentsOverall = courseRatingsOverall.Select(y => y.classSize).Sum(z => z); var responses = courseRatings.Select(y => y.responses).Sum(z => z); var students = courseRatings.Select(y => y.classSize).Sum(z => z); List <string> departments = new List <string>(); List <OverallRating> overallRatings = StaticData.categoryList.Where(p => p.name != "NULL").Select(p => { return(new OverallRating { category = p.name, rating = courseRatings.Sum(z => ((double)z.responses / (double)responses) * z.ratings.Where(a => a.categoryID == p.categoryID).FirstOrDefault().averageRating).ToString("#.##") }); }).ToList(); int totalResponses = 0; int totalStudents = 0; double averageRating = 0.0; List <CourseDomain> courses = StaticData.courseList.Where(p => courseRatings.Select(a => a.courseID).Distinct().Contains(p.courseID)).Select(p => { var courseRatingsList = courseRatings.Where(x => x.courseID == p.courseID); var courseResponses = courseRatingsList.Select(y => y.responses).Sum(z => z); var courseStudents = courseRatingsList.Select(y => y.classSize).Sum(z => z); var instructors = courseRatingsList.Select(y => y.instructorID).Distinct().Count(); var deptName = StaticData.departmentList.Where(u => u.departmentID == p.departmentID).FirstOrDefault().name; if (!departments.Contains(deptName)) { departments.Add(deptName); } totalResponses += courseResponses; totalStudents += courseStudents; averageRating += courseRatingsList .Sum(z => ((double)z.responses * z.ratings.Where(a => a.categoryID == Convert.ToInt32(GlobalVariables.CurrentCategory)).FirstOrDefault().averageRating)); return(new CourseDomain { code = p.code, title = p.title, departmentID = p.departmentID, departmentName = deptName, rating = courseRatingsList .Sum(z => ((double)z.responses / (double)courseResponses) * z.ratings.Where(a => a.categoryID == Convert.ToInt32(GlobalVariables.CurrentCategory)).FirstOrDefault().averageRating).ToString("#.##"), responses = courseResponses.ToString(), students = courseStudents.ToString(), responseRate = ((double)courseResponses / (double)courseStudents).ToString("p1") }); }).Where(t => Convert.ToInt32(t.students) > 0).ToList(); //Populate graph InstructorGraph instructorGraph = new InstructorGraph(); //Create a list of IEnumerables, in which each element will hold a pair, first for the semester, to sort by, second for the rating List <IEnumerable <Pair <string, string> > > graphData = new List <IEnumerable <Pair <string, string> > >(); //List of labels to display on the graph, in this case the category names List <string> labels = new List <string>(); //Loop through all categories and calculate the instructor rating for each semester for said category foreach (var category in StaticData.categoryList.Where(p => p.name != "NULL")) { graphData.Add(courseRatings.Select(p => p.semester).Distinct().Select(u => { var graphRatingsList = courseRatings.Where(x => x.semester == u); var graphResponses = graphRatingsList.Select(y => y.responses).Sum(z => z); return(new Pair <string, string>(u, graphRatingsList .Sum(z => ((double)z.responses / (double)graphResponses) * z.ratings.Where(e => e.categoryID == category.categoryID).FirstOrDefault().averageRating).ToString("#.##") )); }).OrderBy(p => p.First, new SemesterComparer()) ); labels.Add(category.name); } //Join all semesters once for the labels to be in JSON instructorGraph.labels = "['" + String.Join("', '", graphData[0].Select(p => p.First).ToArray()) + "']"; instructorGraph.data = new List <string>(); //Join the data for each linegraph to be in JSON foreach (var line in graphData) { instructorGraph.data.Add("[" + String.Join(", ", line.Select(p => p.Second).ToArray()) + "]"); } viewModel.instructor = instructor; viewModel.overallRatings = overallRatings; viewModel.courseRatingsAll = courseRatings.Select(p => { Course thisCourse = StaticData.courseList.Where(y => y.courseID == p.courseID).FirstOrDefault(); return(new CourseRatingDomain { classSize = p.classSize, courseID = p.courseID, courseRatingID = p.courseRatingID, instructorID = p.instructorID, ratings = p.ratings, responses = p.responses, section = p.section, semester = p.semester, term = p.term, courseCode = thisCourse.code }); }).ToList(); viewModel.firstTerm = courseRatingsOverall.Select(v => v.semester).Distinct() .OrderBy(t => t, new SemesterComparer()).FirstOrDefault().ToString(); viewModel.responsesAll = responsesOverall.ToString(); viewModel.studentsAll = studentsOverall.ToString(); viewModel.departments = departments; viewModel.responseRateOverall = ((double)responsesOverall / (double)studentsOverall).ToString("p1"); viewModel.courses = courses; viewModel.currentCategory = StaticData.categoryList.Where(p => p.categoryID == Convert.ToInt32(GlobalVariables.CurrentCategory)) .FirstOrDefault().name; ViewBag.loadChart = GlobalFunctions.createMultipleChartScript(instructorGraph, labels); viewModel.totalResponses = totalResponses.ToString("N0"); viewModel.totalStudents = totalStudents.ToString("N0"); viewModel.averageResponseRate = ((double)totalResponses / (double)totalStudents).ToString("p1"); viewModel.averageRating = (averageRating / (double)totalResponses).ToString("#.##"); viewModel.currentSemesterLow = GlobalVariables.CurrentSemesterLow; viewModel.currentSemesterLow = GlobalVariables.CurrentSemesterHigh; } else { throw new HttpException(404, "Instructor not found!"); } return(View(viewModel)); }