forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.Models.ChartModel.js
171 lines (132 loc) · 4.32 KB
/
App.Models.ChartModel.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
163
164
165
166
167
168
169
170
171
;( function() {
"use strict";
App.Models.ChartModel = Backbone.Model.extend( {
//urlRoot: Global.rootUrl + '/charts/',
//urlRoot: Global.rootUrl + '/data/config/',
url: function() {
if( $("#form-view").length ) {
if( this.id ) {
//editing existing
return Global.rootUrl + "/charts/" + this.id;
} else {
//saving new
return Global.rootUrl + "/charts";
}
} else {
return Global.rootUrl + "/data/config/" + this.id;
}
},
defaults: {
"cache": true,
"selected-countries": [],
"tabs": [ "chart", "data", "sources" ],
"line-type": "2",
"chart-description": "",
"chart-dimensions": [],
"variables": [],
"y-axis": {},
"x-axis": {},
"margins": { top: 10, left: 60, bottom: 10, right: 10 },
"units": "",
"iframe-width": "100%",
"iframe-height": "660px",
"hide-legend": false,
"group-by-variables": false,
"add-country-mode": "add-country",
"x-axis-scale-selector": false,
"y-axis-scale-selector": false
},
initialize: function() {
this.on( "sync", this.onSync, this );
},
onSync: function() {
if( this.get( "chart-type" ) == 2 ) {
//make sure for scatter plot, we have color set as continents
var chartDimensions = $.parseJSON( this.get( "chart-dimensions" ) );
if( !_.findWhere( chartDimensions, { "property": "color" } ) ) {
//this is where we add color property
var colorPropObj = { "variableId":"123","property":"color","unit":"","name":"Color","period":"single","mode":"specific","targetYear":"2000","tolerance":"5","maximumAge":"5"};
chartDimensions.push( colorPropObj );
var charDimensionsString = JSON.stringify( chartDimensions );
this.set( "chart-dimensions", charDimensionsString );
}
}
},
addSelectedCountry: function( country ) {
//make sure we're using object, not associative array
/*if( $.isArray( this.get( "selected-countries" ) ) ) {
//we got empty array from db, convert to object
this.set( "selected-countries", {} );
}*/
var selectedCountries = this.get( "selected-countries" );
//make sure the selected contry is not there
if( !_.findWhere( selectedCountries, { id: country.id } ) ) {
selectedCountries.push( country );
//selectedCountries[ country.id ] = country;
this.trigger( "change:selected-countries" );
this.trigger( "change" );
}
},
updateSelectedCountry: function( countryId, color ) {
var country = this.findCountryById( countryId );
if( country ) {
country.color = color;
this.trigger( "change:selected-countries" );
this.trigger( "change" );
}
},
removeSelectedCountry: function( countryId ) {
var country = this.findCountryById( countryId );
if( country ) {
var selectedCountries = this.get( "selected-countries" ),
countryIndex = _.indexOf( selectedCountries, country );
selectedCountries.splice( countryIndex, 1 );
this.trigger( "change:selected-countries" );
this.trigger( "change" );
}
},
replaceSelectedCountry: function( country ) {
if( country ) {
this.set( "selected-countries", [ country ] );
}
},
findCountryById: function( countryId ) {
var selectedCountries = this.get( "selected-countries" ),
country = _.findWhere( selectedCountries, { id: countryId.toString() } );
return country;
},
setAxisConfig: function( axisName, prop, value ) {
if( $.isArray( this.get( "y-axis" ) ) ) {
//we got empty array from db, convert to object
this.set( "y-axis", {} );
}
if( $.isArray( this.get( "x-axis" ) ) ) {
//we got empty array from db, convert to object
this.set( "x-axis", {} );
}
var axis = this.get( axisName );
if( axis ) {
axis[ prop ] = value;
}
this.trigger( "change" );
},
updateVariables: function( newVar ) {
//copy array
var variables = this.get( "variables" ).slice(),
varInArr = _.find( variables, function( v ){ return v.id == newVar.id; } );
if( !varInArr ) {
variables.push( newVar );
this.set( "variables", variables );
}
},
removeVariable: function( varIdToRemove ) {
//copy array
var variables = this.get( "variables" ).slice(),
varInArr = _.find( variables, function( v ){ return v.id == newVar.id; } );
if( !varInArr ) {
variables.push( newVar );
this.set( "variables", variables );
}
}
} );
})();