| 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 | 1 1 1 1 1 3 2 2 3 3 3 2 2 3 3 3 3 3 2 2 6 2 2 2 2 1 | /*global require, define, console, $ */
/*jslint nomen: true, debug: true, todo: true */
/**
* @module populationAvgChartView
* @extends module:chartView
*/
define([
"views/chartView",
"underscore",
"utils"
],
function (chartView, _, utils) {
"use strict";
/**
* @name module:populationAvgChartView
* @description Average population chart
* @requires module:chartView
* @class Backbone.View
* @requires module:chartView
* @requires underscore
* @requires module:utils
* @see module:utils
* @constructor
* @returns {Function} Backbone.View constructor
*/
return chartView.extend({
/**
* @name module:populationAvgChartView#type
* @description Chart type
* @type {string}
*/
type: "spline",
/**
* @name module:populationAvgChartView#options
* @description Chart config extension object
* @type {object}
*/
options: {
yAxis: {
title: {
text: "population".toLocaleString()
},
min: 0
},
plotOptions: {
series: {
point: {
events: {
click: function () {
this.series.chart.options
.getWidget().switchMode({
year: this.category
});
return this.category;
}
}
}
}
},
series: [
{
name: "male".toLocaleString()
},
{
name: "female".toLocaleString()
},
{
name: "total".toLocaleString()
}
],
tooltip: {
formatter: function () {
return "<b>" + this.series.name + "</b><br/>" + this.x + ": " + this.y.toPrecision(3);
}
}
},
/**
* @name module:populationAvgChartView#update
* @description Updates chart state by new data
* @function
* @param [data] {array} new data
* @returns {object|undefined} chart.series or undefined
*/
update: function (data) {
if (this.chart && data) {
var range,
j,
min = Infinity,
max = -Infinity,
series = {
male: [],
female: [],
total: []
};
_.each(data, function (i) {
/* istanbul ignore else */
Eif (i.year > max) {
max = i.year;
}
if (i.year < min) {
min = i.year;
}
});
_.each(data, function (i) {
j = i.year - min;
i = i.population;
series.male[j] = utils.sum(i.male);
series.female[j] = utils.sum(i.female);
series.total[j] = i.overall;
});
range = _.range(min, max + 1);
_.each(series, function (s) {
s = utils.replace(undefined, null);
});
this.updateSpline(series.male, series.female, series.total);
this.chart.xAxis[0].setCategories(range, false);
this.redraw();
return series;
}
return;
}
});
}); |