Code coverage report for app/views/sou/souChartView.js

Statements: 100% (33 / 33)      Branches: 100% (16 / 16)      Functions: 100% (3 / 3)      Lines: 100% (33 / 33)      Ignored: none     

All files » app/views/sou/ » souChartView.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                  1                                         1                   9 7                     7 16 16 7   16 7   16     7 7   7 7         7 5   2     7 3 3       3 1   2   4 1 1 1 1   7 7 7   2      
/*global require, define, console, $ */
/*jslint nomen: true, debug: true */
 
/**
 * @module souChartView
 * @extends module:chartView
 */
 
 
define([
    "views/chartView",
    "underscore",
    "utils"
],
    function (chartView, _, utils) {
        "use strict";
 
        /**
         * @name module:souChartView
         * @description Sou dynamics chart
         * @requires module:chartView
         * @class Backbone.View
         * @requires module:chartView
         * @requires underscore
         * @requires module:utils
         * @see module:utils
         * @see module:module:chartView
         * @constructor
         * @returns {Function} Backbone.View constructor
         */
        return chartView.extend({
            /**
             * @name module:souChartView#update
             * @description Updates chart state by new data
             * @function
             * @param [data] {array} New data
             * @param [type] {string} Subject
             * @returns {object|undefined} chart.series or undefined
             */
            update: function (data, type) {
                if (this.chart && data) {
                    var range,
                        min = Infinity,
                        i,
                        avg = [],
                        subject = [],
                        data0,
                        data1,
                        series = this.chart.series,
                        s0 = series[0],
                        s1 = series[1];
 
                    _.each(data, function (item) {
                        i = item.indicator.quality;
                        if (item.year < min) {
                            min = item.year;
                        }
                        if (i.raw) {
                            subject[item.year] = i.raw[0].average;
                        }
                        avg[item.year] = i.average;
 
                    });
                    avg = utils.replace(avg.slice(min, avg.length), undefined, null);
                    range = _.range(min, min + avg.length);
 
                    this.chart.xAxis[0].setCategories(range);
                    data0 = {
                        data: avg,
                        name: "sou".toLocaleString()
                    };
 
                    if (s0) {
                        s0.update(data0, false);
                    } else {
                        this.chart.addSeries(data0, false);
                    }
                    
                    if (subject.length) {
                        subject = utils.replace(subject.slice(min, subject.length), undefined, null);
                        data1 = {
                            data: subject,
                            name: "sou".toLocaleString() + "-" + String(type).toLocaleString().toLocaleLowerCase()
                        };
                        if (s1) {
                            s1.update(data1, false);
                        } else {
                            this.chart.addSeries(data1, false);
                        }
                    } else if (s1) {
                        s1.options.showInLegend = false;
                        s1.legendItem = null;
                        this.chart.legend.destroyItem(s1);
                        s1.remove(false);
                    }
                    this.chart.legend.render();
                    this.redraw();
                    return series;
                }
                return;
            }
        });
    });