-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.js
113 lines (99 loc) · 2.64 KB
/
render.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
import fs from 'fs'
import jsdom from 'jsdom'
import d3 from 'd3'
import chart from './chart.js'
function loadTables() {
return new Promise((resolve) => {
let data_tsv = fs.readFileSync('data.tsv', {encoding: 'utf8'})
let data = d3.tsv.parse(data_tsv)
let judete_years = []
let buffer = null
let judet = null
data.forEach(function(row) {
if(row.judet) {
if(buffer) flush()
buffer = {}
judet = row.judet
}
delete row.judet
buffer[row.an] = row
delete row.an
})
flush()
function flush() {
judete_years.push({
name: judet,
years: buffer,
})
buffer = []
}
let indicators = {}
d3.tsv.parse(fs.readFileSync('indicators.tsv', {encoding: 'utf8'})).forEach((row) => {
indicators[row.n] = row
})
let judete = d3.range(42).map((i) => {
let ind = indicators[i+1]
let slug = ind.slug
delete ind.slug
delete ind.jud
delete ind.n
return {
name: judete_years[i].name,
slug: slug,
years: judete_years[i].years,
indicators: ind,
}
})
resolve(judete)
})
}
function withJsdom(func) {
return new Promise((resolve, reject) => {
jsdom.env(`<html><body></body></html>`,
[ 'http://d3js.org/d3.v3.min.js' ],
function (err, window) {
if(err) return reject(err)
let rv = func(window)
resolve(rv)
})
})
}
const alignMap = {
'PIB-text': 'end',
'mun-text': 'end',
'comune-text': 'end',
'jud-text': 'start',
'pop-text': 'start',
'orase-text': 'start',
}
async function renderPage(master, judet) {
return await withJsdom((window) => {
let d3 = window.d3
let zebox = d3.select('body').append('div')
let headerOffset = master.indexOf('<svg')
let header = master.slice(0, headerOffset)
let svgSrc = master.slice(headerOffset)
zebox.html(svgSrc)
let plot = zebox.select('svg').append('g')
.attr('transform', 'translate(4780,17710)')
chart(d3, plot, judet, {width: 560, height: 343})
for(let key of Object.keys(judet.indicators)) {
let value = judet.indicators[key]
let align = alignMap[key] || 'middle'
zebox.select(`#${key}`)
.attr('text-anchor', align)
.html(value)
}
return header + zebox.html()
})
}
async function main() {
let judete = await loadTables()
let master = fs.readFileSync('master.svg', {encoding: 'utf8'})
for(let judet of judete) {
console.log(judet.slug)
let out = await renderPage(master, judet)
fs.writeFileSync(`out/${judet.slug}.svg`, out)
}
}
main().catch((e) => { console.error(e.stack || e) })