-
Notifications
You must be signed in to change notification settings - Fork 6
/
sankey-finance.js
229 lines (207 loc) · 7.04 KB
/
sankey-finance.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env node
// imports
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import jsdom from 'jsdom'
import { format, select } from 'd3'
import d3sankey from 'd3-sankey'
import fs from 'fs'
import xlsx from 'read-excel-file/node'
import { nodesSchema, linksSchema, metadataSchema } from './schema.js'
import svg2img from 'svg2img'
// command line arguments parsing
const argv = yargs(hideBin(process.argv)).argv
yargs(hideBin(process.argv))
.usage('$0 -i input_file -o output_file -a [left|right|center|justify] -n -l -w [image_width] -h [image_height] -p [node_padding] -d [node_width]')
.example('$0 -i Nokia_results.xlsx -o Nokia_results.svg -a right -n -l -d 80 -p 100 -w 1024 -h 768')
.option('input', {
alias: 'i',
demandOption: true,
type: 'string',
description: 'Path to input Excel spreadsheet file'
})
.option('output', {
alias: 'o',
type: 'string',
default: 'output',
description: 'Output SVG file name'
})
.option('align', {
alias: 'a',
type: 'string',
choices: ['left', 'right', 'center', 'justify'],
default: 'justify',
description: 'Node alignment'
})
.option('nodewidth', {
alias: 'd',
type: 'number',
number: true,
requiresArg: true,
default: 50,
description: 'Node width'
})
.option('nodepadding', {
alias: 'p',
type: 'number',
number: true,
requiresArg: true,
default: 80,
description: 'Node alignment'
})
.option('nodesort', {
alias: 'n',
type: 'boolean',
description: 'Node sorting'
})
.option('linksort', {
alias: 'l',
type: 'boolean',
description: 'Link sorting'
})
.option('width', {
alias: 'w',
type: 'number',
number: true,
requiresArg: true,
default: 1920,
description: 'Image width'
})
.option('height', {
alias: 'h',
type: 'number',
number: true,
requiresArg: true,
default: 1080,
description: 'Image alignment'
})
.wrap(yargs.terminalWidth)
.parse()
const input = argv.i || argv.input
const output = argv.o || argv.output || 'output'
const [filename, fileExtension] = output.split('.')
const align = argv.a || argv.align || 'justify'
const nodesort = (argv.n || argv.nodesort) ? null : undefined
const linksort = (argv.l || argv.linksort) ? null : undefined
const nodePadding = argv.p || argv.nodepadding || 80
const nodeWidth = argv.d || argv.nodewidth || 50
const imageWidth = argv.w || argv.width || 1920
const imageHeight = argv.h || argv.height || 1080
const color = {
default: '#666666',
profit: '#2ba02d',
loss: '#cc0001'
}
// helper functions
const formatValue = (value, currency, abbreviation) => {
return format(',.2~f')(value) + abbreviation + currency
}
const findMaxValue = (objects, attribute) => Math.max(...objects.map(object => object[attribute]))
async function getXlsData (input, sheet, schema) {
const { rows, errors } = await xlsx(input, { schema, sheet })
if (errors.length > 0) throw new Error('Error loading Excel data!')
return rows
}
const nodes = await getXlsData(input, 'nodes', nodesSchema)
const links = await getXlsData(input, 'links', linksSchema)
const metadata = await getXlsData(input, 'metadata', metadataSchema)
const graph = {
header: metadata[0]?.header ?? '',
currency: metadata[0]?.currency ?? '€',
abbreviation: metadata[0]?.abbreviation ?? 'B',
nodes,
links
}
// chart generation
const { JSDOM } = jsdom
const dom = new JSDOM('<!DOCTYPE html><body></body>')
const margin = ({ top: 140, right: 250, bottom: 30, left: 50 })
const width = imageWidth - margin.left - margin.right
const height = imageHeight - margin.top - margin.bottom
const body = select(dom.window.document.querySelector('body'))
const svg = body
.append('svg')
.attr('width', imageWidth)
.attr('height', imageHeight)
.attr('xmlns', 'http://www.w3.org/2000/svg')
.attr('viewBox', `0 0 ${width} ${height}`)
.style('background', '#fff')
const mySankey = d3sankey.sankey()
.nodeId(d => d.name)
.nodeWidth(nodeWidth)
.nodePadding(nodePadding)
.nodeSort(nodesort)
.linkSort(linksort)
.nodeAlign(d3sankey[`sankey${align[0].toUpperCase()}${align.slice(1)}`])
.extent([[margin.left, margin.top], [width - margin.right, height - margin.top]])
mySankey(graph)
const numLayers = findMaxValue(graph.nodes, 'layer')
// links
svg.append('g')
.attr('fill', 'none')
.attr('class', 'links')
.attr('stroke-opacity', 0.5)
.selectAll('path')
.data(graph.links)
.enter().append('path')
.attr('d', d3sankey.sankeyLinkHorizontal())
.attr('stroke-width', d => Math.max(1, d.width))
.style('stroke', d => d.color || (d.target.type === 'profit' ? color.profit : d.target.type === 'loss' ? color.loss : color.default))
.append('title')
.text(d => d.index)
.append('title')
.text(d => `${d.source.name} → ${d.target.name}\n${d.value.toLocaleString()}`)
// nodes
svg.append('g')
.attr('class', 'nodes')
.selectAll('rect')
.data(graph.nodes)
.enter().append('rect')
.attr('width', d => d.x1 - d.x0)
.attr('height', d => d.y1 - d.y0)
.attr('x', d => d.x0)
.attr('y', d => d.y0)
.style('fill', d => d.color || (d.type === 'profit' ? color.profit : d.type === 'loss' ? color.loss : color.default))
// header
svg.append('g')
.append('text')
.attr('x', d => width / 2)
.attr('y', d => 70)
.attr('font-family', 'Arial')
.attr('font-size', '5em')
.attr('text-length', '4em')
.attr('font-weight', 'bold')
.attr('text-anchor', 'middle')
.attr('fill', d => 'blue')
.text(graph.header)
// node names and values
svg.append('g')
.attr('class', 'texts')
.selectAll('text')
.data(graph.nodes)
.enter().append('text')
.attr('x', d => d.layer < numLayers ? (d.x0 + d.x1) / 2 : d.x1 + 6)
.attr('y', d => d.layer < numLayers ? d.y0 - nodePadding / 2 : (d.y1 + d.y0) / 2)
.attr('font-family', 'Arial')
.attr('font-size', nodePadding / 2.5)
.attr('font-weight', 'bold')
.attr('text-anchor', d => d.layer < numLayers ? 'middle' : 'start')
.attr('fill', d => d.color || (d.type === 'profit' ? color.profit : d.type === 'loss' ? color.loss : color.default))
.text(d => d.name)
.append('tspan')
.attr('fill-opacity', 0.7)
.attr('x', d => d.layer < numLayers ? (d.x0 + d.x1) / 2 : d.x1 + 6)
.attr('dy', '1em')
.attr('font-weight', 'normal')
.attr('font-size', '0.75em')
.text(d => d.type === 'loss' ? ` (${formatValue(d.value, graph.currency, graph.abbreviation)})` : ` ${formatValue(d.value, graph.currency, graph.abbreviation)}`)
// save the chart
if (fileExtension === 'png') {
svg2img(body.html(), function (_error, buffer) {
fs.writeFileSync(`${filename}.png`, buffer)
})
} else if (fileExtension === undefined) {
fs.writeFileSync(`${output}.svg`, body.html())
} else {
fs.writeFileSync(output, body.html())
}