Code coverage report for app/views/exams/examsSubjectChartView.js

Statements: 100% (29 / 29)      Branches: 100% (11 / 11)      Functions: 100% (6 / 6)      Lines: 100% (29 / 29)      Ignored: 1 branch     

All files » app/views/exams/ » examsSubjectChartView.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162                  1                                     1     1                                                                                   1       1                                                                 2 1                       1 2   2 2   2 1   2     1 2 2 2 2 2 2     1   1 5         1 1   1 1   1      
/*globals define, test, equal, ok, $, describe, it, beforeEach, afterEach, expect, window*/
/*jslint nomen: true, debug: true */
 
/**
 * @module examsSubjectChartView
 * @extends module:chartView
 */
 
 
define([
    "views/chartView",
    "underscore",
    "utils"
],
    function (chartView, _, utils) {
        "use strict";
        /**
         * @name module:examsSubjectChartView
         * @description Exam subject chart view
         * @class Backbone.View
         * @requires module:chartView
         * @requires framework
         * @requires underscore
         * @requires module:utils
         * @constructor
         * @returns {Function} Backbone.View constructor
         */
 
        var AXISCOLOR = "#777",
            GRIDLINECOLOR = "#EEE";
 
        return chartView.extend({
            /**
             * @name module:examsSubjectChartView#type
             * @description Chart type identifier
             * @type {string}
             */
            type: "spline",
            /**
             * @name module:examsSubjectChartView#options
             * @description Chart options extension
             * @property
             * @type {object}
             */
            options: {
                yAxis: [
                    {
                        title: {
                            text: "score".toLocaleString() + ", %",
                            style: {
                                color: AXISCOLOR,
                                fontWeight: "normal"
                            }
                        },
                        gridLineColor: GRIDLINECOLOR
                    },
                    {
                        title: {
                            text: "choice".toLocaleString() + ", %",
                            style: {
                                color: AXISCOLOR,
                                fontWeight: "normal"
                            }
                        },
                        gridLineColor: GRIDLINECOLOR,
                        opposite: true
                    }
                ],
                plotOptions: {
                    series: {
                        point: {
                            events: {
                                click: function () {
                                    this.series.chart.options
                                        .getWidget().switchMode({
                                            year: this.category
                                        });
                                    return this.category;
                                }
                            }
                        }
                    }
                },
                series: [
                    {
                        name: "school".toLocaleString()
                    },
                    {
                        name: "district".toLocaleString()
                    },
                    {
                        name: "region".toLocaleString()
                    },
                    {
                        name: "country".toLocaleString()
                    },
                    {
                        name: "whoChose".toLocaleString(),
                        dashStyle: "shortdot",
                        yAxis: 1
                    }
                ]
            },
            /**
             * @name module:examsSubjectChartView#update
             * @description Updates chart state by new data
             * @function
             * @returns {object|undefined} series or undefined
             */
            update: function (data, type) {
                if (this.chart && data) {
                    var range,
                        view = this,
                        i,
                        min = Infinity,
                        max = -Infinity,
                        series = {
                            school:     [],
                            city:       [],
                            region:     [],
                            country:    [],
                            choice:     []
                        };
                    _.each(data, function (exam) {
                        var year = (new Date(exam.date)).getFullYear();
                        /* istanbul ignore else */
                        Eif (year > max) {
                            max = year;
                        }
                        if (year < min) {
                            min = year;
                        }
                        exam.y = year;
                    });
 
                    _.each(data, function (exam) {
                        i = exam.y - min;
                        series.school[i] =  exam.average;
                        series.city[i] =    exam.averageCity;
                        series.region[i] =  exam.averageRegion;
                        series.country[i] = exam.averageCountry;
                        series.choice[i] =  100 * exam.total / (exam.gtotal || exam.total || 1);
                    });
 
                    range = _.range(min, max + 1);
 
                    _.each(series, function (s) {
                        s = utils.replace(undefined, null);
                    });
 
 
 
                    this.updateSpline(series.school, series.city, series.region, series.country, series.choice);
                    this.chart.xAxis[0].setCategories(range, false);
 
                    this.redraw();
                    return series;
                }
                return undefined;
            }
        });
    });