-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
191 lines (179 loc) · 4.7 KB
/
index.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
document.getElementById('contentFile').onchange = loadFile;
/**
* Parse the JSON file.
* @param {Object} event Event when loading the JSON file.
* Inspired by https://stackoverflow.com/a/56062650
*/
function loadFile(event) {
try {
const files = event.target.files;
if (!files.length) {
alert('No file selected!');
return;
}
const file = files[0];
const reader = new FileReader();
reader.onload = parseJournal;
reader.readAsText(file);
} catch (err) {
console.error(err);
}
};
/**
* Parses the loaded journal JSON.
* @param {Object} event The file reader event.
*/
function parseJournal(event) {
const json = JSON.parse(event.target.result);
journal = new Journal(json);
data = createChartData(journal);
const chart = am4core.create('chartdiv', am4charts.SankeyDiagram);
chart.data = data;
chart.dataFields.fromName = 'from';
chart.dataFields.toName = 'to';
chart.dataFields.value = 'value';
chart.paddingRight = 300;
chart.paddingTop = 80;
chart.paddingBottom = 80;
chart.nodeAlign = 'bottom';
// Amount labels.
// See: https://www.amcharts.com/docs/v4/reference/label/
chart.nodes.template.nameLabel.label.propertyFields.text = "labelText";
chart.nodes.template.nameLabel.label.propertyFields.tooltipText = "labelText";
chart.nodes.template.nameLabel.label.truncate = false;
};
/**
* The data structure of the journal JSON file.
*/
class Journal {
/**
* The representation of the journal file.
* @param {Object} json The original JSON file.
*/
constructor(json) {
this.data = json;
}
/**
* The beginning of the journal.
*/
get begin() {
return this.data.cbrDates[0][0];
}
/**
* The end of the journal.
*/
get end() {
return this.data.cbrDates[0][1];
}
/**
* Parse a subreport.
* @param {String} name The name of the subreport.
* @return {Object} The subreport.
*/
parse(name) {
for (const subreport of this.data.cbrSubreports) {
if (subreport[0] == name) {
const result = subreport[1];
result.rows = result.prRows.map((element) => new Row(element));
return result;
}
}
throw new Error(`No ${name} found. ` +
`Did you declare ${name} in your journal?`);
}
/**
* The revenues in the journal.
*/
get revenues() {
return this.parse('Revenues');
}
/**
* The expenses in the journal.
*/
get expenses() {
return this.parse('Expenses');
}
}
/**
* The datastructure of a single subreport.
*/
class Row {
/**
* The representation of a single category.
* @param {Object} json The JSON representation.
*/
constructor(json) {
this.uri = json.prrName;
const elements = json.prrName.split(':');
this.name = elements.pop();
this.ancestors = elements;
this.amount = json.prrAmounts[0][0].aquantity.floatingPoint;
this.currency = json.prrAmounts[0][0].acommodity;
this.isRoot = !this.ancestors.length;
this.label = `${this.name} [bold]${this.amount.toFixed(0)} ${this.currency}[/]`;
}
}
/**
* Convert the flat list to the tree required for representation.
* @param {Array} rows Rows of categories.
* @return {Array} Tree representation.
*/
function parseTree(rows) {
const tree = rows;
const root = rows[0];
const parents = [];
let sibling = root.name;
for (const row of tree.slice(1)) {
if (row.ancestors.includes(sibling)) {
row.source = sibling;
parents.push(sibling);
} else if (row.ancestors.includes(parents.at(-1))) {
row.source = parents.at(-1);
} else {
parents.pop();
row.source = parents.at(-1);
}
sibling = row.name;
}
return tree;
}
/**
* Convert the journal data to chart data.
* @param {Object} journal The journal data.
* @return {Object} The chart data.
*/
function createChartData(journal) {
const revenueTree = parseTree(journal.revenues.rows);
const expensesTree = parseTree(journal.expenses.rows);
const data = [];
let totalExpenses = 0;
for (const row of expensesTree) {
let source = row.source;
if (row.isRoot) {
source = 'revenues';
totalExpenses = row.amount;
}
const target = row.name;
const value = row.amount;
data.push({from: source,
to: target,
value: value,
labelText: row.label});
}
for (const row of revenueTree) {
if (!row.isRoot) {
data.push({to: row.source,
from: row.name,
value: row.amount,
labelText: row.label});
} else {
const savings = row.amount - totalExpenses;
const label = `savings [bold] ${savings.toFixed(0)} ${row.currency}`;
data.push({to: 'savings',
from: 'revenues',
value: savings,
labelText: label});
}
}
return data;
}