Skip to content

Commit 2c2d04e

Browse files
author
Tryggvi Bjorgvinsson
committed
Moved visualisations into their own directory and created a utils directory with common utils. Split current utils into two files, one for amounts, one for colors (as their own properties in the OpenSpending object). Renamed openspending.choropleth.js to jquery.choropleth.js to conform with jquery plugin convention
1 parent 9256d31 commit 2c2d04e

File tree

5 files changed

+281
-4
lines changed

5 files changed

+281
-4
lines changed

Gruntfile.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ module.exports = function(grunt) {
1212
'lib/vendor/raphael-min.js',
1313
'lib/vendor/chroma.js',
1414
'lib/vendor/kartograph/kartograph.min.js',
15-
// Source files for openspending
16-
'src/*.js'
15+
'lib/vendor/accounting.js',
16+
// Source files for openspendingjs' utilisation library
17+
'src/utils/*.js',
18+
// Source files for openspendingjs' jquery widgets
19+
'src/visualisations/*.js'
1720
],
1821
dest: 'dist/<%= pkg.name %>-<%= pkg.version %>.js'
1922
}

src/utils/openspending.amounts.js

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
/*! openspending.amounts.js - Amount formatting for OpenSpending
2+
* ------------------------------------------------------------------------
3+
*
4+
* Copyright 2013 Open Knowledge Foundation
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// Define OpenSpending object (if used as a separate module)
20+
var OpenSpending = OpenSpending || {};
21+
// Define Amounts property
22+
OpenSpending.Amounts = OpenSpending.Amounts || {};
23+
24+
// Give a short hand version of the amount (with bn, m, k abbreviations)
25+
OpenSpending.Amounts.shorthand = function (amount) {
26+
// Define supported amounts
27+
var billion = 1000000000;
28+
var million = 1000000;
29+
var thousand = 1000;
30+
31+
// Get the absolute value (since negative numbers should work as well
32+
var absolute_amount = Math.abs(amount);
33+
34+
// Billions get 'bn'
35+
if (absolute_amount > billion) {
36+
return OpenSpending.Amounts.format(amount / billion) + 'bn';
37+
}
38+
// Millions get 'm'
39+
else if (absolute_amount > million) {
40+
return OpenSpending.Amounts.format(amount / million) + 'm';
41+
}
42+
// Thousands get 'k'
43+
else if (absolute_amount > thousand) {
44+
return OpenSpending.Amounts.format(amount / thousand) + 'k';
45+
}
46+
// Anything less just returns the amount with two decimal places
47+
else {
48+
return OpenSpending.Amounts.format(amount, 2);
49+
}
50+
};
51+
52+
// Format amount using accounting
53+
OpenSpending.Amounts.format = function (amount, precision, currency) {
54+
// Get the currency symbol
55+
currency = OpenSpending.Amounts.currencySymbol(currency);
56+
// Use accounting.js to format the amount (default precision is 0)
57+
return accounting.formatMoney(amount, currency, precision||0,
58+
OpenSpending.localeGroupSeparator,
59+
OpenSpending.localeDecimalSeparator);
60+
};
61+
62+
// Get the currency symbol
63+
OpenSpending.Amounts.currencySymbol = function (currency) {
64+
// If currency is defined return currency symbol or currency
65+
if(currency) {
66+
return OpenSpending.Amounts.currencySymbols[currency] || currency;
67+
} else { // If not we just return an empty string
68+
return '';
69+
}
70+
};
71+
72+
// All known currency symbols in openspendingjs
73+
OpenSpending.Amounts.currencySymbols = {
74+
"AED": "د.إ",
75+
"AFN": "؋",
76+
"ALL": "L",
77+
"AMD": "դր.",
78+
"ANG": "ƒ",
79+
"AOA": "Kz",
80+
"ARS": "$",
81+
"AUD": "$",
82+
"AWG": "ƒ",
83+
"AZN": "m",
84+
"BAM": "KM",
85+
"BBD": "$",
86+
"BDT": "৳",
87+
"BGN": "лв",
88+
"BHD": "ب.د",
89+
"BIF": "Fr",
90+
"BMD": "$",
91+
"BND": "$",
92+
"BOB": "Bs.",
93+
"BRL": "R$",
94+
"BSD": "$",
95+
"BTN": "Nu",
96+
"BWP": "P",
97+
"BYR": "Br",
98+
"BZD": "$",
99+
"CAD": "$",
100+
"CDF": "Fr",
101+
"CHF": "Fr",
102+
"CLP": "$",
103+
"CNY": "¥",
104+
"COP": "$",
105+
"CRC": "₡",
106+
"CUP": "$",
107+
"CVE": "$, Esc",
108+
"CZK": "Kč",
109+
"DJF": "Fr",
110+
"DKK": "kr",
111+
"DOP": "$",
112+
"DZD": "د.ج",
113+
"EEK": "KR",
114+
"EGP": "£,ج.م",
115+
"ERN": "Nfk",
116+
"ETB": "Br",
117+
"EUR": "€",
118+
"FJD": "$",
119+
"FKP": "£",
120+
"GBP": "£",
121+
"GEL": "ლ",
122+
"GHS": "₵",
123+
"GIP": "£",
124+
"GMD": "D",
125+
"GNF": "Fr",
126+
"GTQ": "Q",
127+
"GYD": "$",
128+
"HKD": "$",
129+
"HNL": "L",
130+
"HRK": "kn",
131+
"HTG": "G",
132+
"HUF": "Ft",
133+
"IDR": "Rp",
134+
"ILS": "₪",
135+
"INR": "₨",
136+
"IQD": "ع.د",
137+
"IRR": "﷼",
138+
"ISK": "kr",
139+
"JMD": "$",
140+
"JOD": "د.ا",
141+
"JPY": "¥",
142+
"KES": "KSh",
143+
"KGS": "лв",
144+
"KHR": "៛",
145+
"KMF": "Fr",
146+
"KPW": "₩",
147+
"KRW": "₩",
148+
"KWD": "د.ك",
149+
"KYD": "$",
150+
"KZT": "Т",
151+
"LAK": "₭",
152+
"LBP": "ل.ل",
153+
"LKR": "ரூ",
154+
"LRD": "$",
155+
"LSL": "L",
156+
"LTL": "Lt",
157+
"LVL": "Ls",
158+
"LYD": "ل.د",
159+
"MAD": "د.م.",
160+
"MDL": "MDL",
161+
"MGA": "Ar",
162+
"MKD": "ден",
163+
"MMK": "K",
164+
"MNT": "₮",
165+
"MOP": "P",
166+
"MRO": "UM",
167+
"MUR": "₨",
168+
"MVR": "ރ.",
169+
"MWK": "MK",
170+
"MXN": "$",
171+
"MYR": "RM",
172+
"MZN": "MT",
173+
"NAD": "$",
174+
"NGN": "₦",
175+
"NIO": "C$",
176+
"NOK": "kr",
177+
"NPR": "₨",
178+
"NZD": "$",
179+
"OMR": "ر.ع.",
180+
"PAB": "B/.",
181+
"PEN": "S/.",
182+
"PGK": "K",
183+
"PHP": "₱",
184+
"PKR": "₨",
185+
"PLN": "zł",
186+
"PYG": "₲",
187+
"QAR": "ر.ق",
188+
"RON": "RON",
189+
"RSD": "RSD",
190+
"RUB": "р.",
191+
"RWF": "Fr",
192+
"SAR": "ر.س",
193+
"SBD": "$",
194+
"SCR": "₨",
195+
"SDG": "S$",
196+
"SEK": "kr",
197+
"SGD": "$",
198+
"SHP": "£",
199+
"SLL": "Le",
200+
"SOS": "Sh",
201+
"SRD": "$",
202+
"STD": "Db",
203+
"SYP": "£, ل.س",
204+
"SZL": "L",
205+
"THB": "฿",
206+
"TJS": "ЅМ",
207+
"TMT": "m",
208+
"TND": "د.ت",
209+
"TOP": "T$",
210+
"TRY": "₤",
211+
"TTD": "$",
212+
"TWD": "$",
213+
"TZS": "Sh",
214+
"UAH": "₴",
215+
"UGX": "Sh",
216+
"USD": "$",
217+
"UYU": "$",
218+
"UZS": "лв",
219+
"VEF": "Bs",
220+
"VND": "₫",
221+
"VUV": "Vt",
222+
"WST": "T",
223+
"XAF": "Fr",
224+
"XCD": "$",
225+
"XOF": "Fr",
226+
"XPF": "Fr",
227+
"YER": "﷼",
228+
"ZAR": "R",
229+
"ZMK": "ZK",
230+
"ZWL": "$"
231+
};
232+

src/utils/openspending.colors.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*! openspending.colors.js - Color palettes for OpenSpending
2+
* ------------------------------------------------------------------------
3+
*
4+
* Copyright 2013 Open Knowledge Foundation
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// Define OpenSpending object (if used as a separate module)
20+
var OpenSpending = OpenSpending || {};
21+
// Define Colors property
22+
OpenSpending.Colors = OpenSpending.Colors || {};
23+
24+
// Default color palette for openspendingjs
25+
OpenSpending.Colors.Palette = ["#CA221D", "#C22769", "#3F93E1",
26+
"#481B79", "#6AAC32", "#42928F",
27+
"#D32645", "#CD531C", "#EDC92D",
28+
"#A5B425", "#211D79", "#449256",
29+
"#7A2077", "#CA221D", "#E29826",
30+
"#44913D", "#2458A3", "#2458A3",
31+
"#14388C"];
32+
33+
OpenSpending.Colors.getColorPalette = function (palette_length) {
34+
// Initialize an empty color array
35+
var colors = [];
36+
// Loop over palette length and add a color to the array
37+
for (var i = 0; i < palette_length; i++) {
38+
// We add colors in order (using modulo to loop around)
39+
colors.push(OpenSpending.Colors.Palette[i % (OpenSpending.Colors.Palette.length-1)]);
40+
}
41+
// Return the color palette
42+
return colors;
43+
}

src/README.md renamed to src/visualisations/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Choropleth map creator. It draws up an svg map of regions and paints the regions
88

99
### Default application
1010

11-
The choropleth is automatically called on any dom element that have a *choropleth* class defined.
11+
The choropleth is automatically called on any dom elements that have a *choropleth* class defined.
1212

1313
### Options
1414

@@ -37,4 +37,3 @@ Defaults of choropleth map can be overwritten in two different ways:
3737
Hierarchy is that data attributes overwrite default and javascript call options overwrite data-attributes:
3838

3939
defaults < data attributes < options
40-

0 commit comments

Comments
 (0)