-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapFrame Constructor.js
74 lines (63 loc) · 1.75 KB
/
MapFrame Constructor.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
/*
Hash extands Map in JS to have functionalities similar to Python's Panda DataFrame
Basic Usage:
hash = new Hash ({url, metrics, dimensions, id}, initialArray)
hash.filter((val, id) => )
hash.map((val, id) => val)
hash.reduce((val, id) => )
hash.info({metrics, filter_ids, subset})
hash.unfo({filter_ids})
hash.clean()
hash.info({clean: true})
hash.reset()
Example:
var cdn_host = "http://127.0.01:8887/cdn/",
data_host = "http://127.0.01:8887/data/"
url = data_host + "latest housing valuation.csv"
id = 'Fips'
hash = new Hash({id, url})
*/
async function load_libraries () {
libraries = [
"papaparse.min.js",
//"topojson.v1.min.js",
//"crossfilter.min.js",
//"d3.v3.min.js",
//"d3.tip.v0.6.3.js",
//"jsoneditor-minimalist.min.js"
]
for(let url of libraries) {
await fetch(cdn_host+url).then(res => res.text()).then(eval)
}
}
load_libraries()
class Hash extends Map {
constructor ({url, text, array, id, metrics, dimensions}, arr) {
super(arr)
this.info({id, metrics, dimensions})
var parser = 'fromCSV'
if (url) {
fetch(url)
.then(res => res.text())
.then(text => Papa.parse(text, {dynamicTyping: true, header: true}))
.then(papa => this[parser](papa.data))
return
}
}
}
Hash.prototype.fromCSV = function (data) {
let id = this._.id
console.log(id)
data.forEach(row => {
this.set(row[id], row)
})
return this
}
Hash.prototype.info = function (obj) {
this._ = {...this._, ...obj}
return this
}
Hash.prototype.unfo = function (keys) {
keys.forEach(k => delete this._[k])
return this
}