-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
368 lines (324 loc) · 12 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import {data} from './data.js';
import savingsChart from './savings-chart.js';
import spendingChart from './spending-chart.js';
import stackedAreaChart from './stacked-area-chart.js';
import timeSeriesChart from './time-series-chart.js';
import {colorScale, entries, keys, sum, values} from './util.js';
function curveCatmullRom(line) {
return line.curve(d3.curveCatmullRom);
}
function convertDates(items) {
return items.map(([date, value]) => ({date: new Date(date), value}));
}
savingsChart(document.getElementById('savings'), data.yearlySavingsGoals);
spendingChart(document.getElementById('spending'), data.spending, `
<h3>Total spending</h3>
<p>
This chart visualizes all spending this month — groceries, health
insurance, mortgage, taxes, etc.
</p>`);
spendingChart(document.getElementById('discretionary-spending'), data.discretionarySpending, `
<h3>Discretionary spending</h3>
<p>
This chart visualizes discretionary spending this month. It excludes
things like mortgage, health insurance and taxes that we can't
reasonably decide to not pay.
</p>`);
timeSeriesChart(
document.getElementById('historical-spending'),
{
markers: convertDates(data.historicalSpending.markers),
points: convertDates(data.historicalSpending.points),
},
(element, {date, value}={}, adjustments) => {
element.style.paddingRight = element.style.paddingLeft = "2rem";
element.innerHTML = '<h2>Monthly discretionary spending</h2>' + (date == null?
`
<p>Hover over the chart to see spending for the given month.</p>
<p>
Vertical markers indicate some significant expense that's been
<strong>excluded</strong> from this chart for clarity.
</p>
` :
`
<h3>
<span style="font-style: italic; opacity: 0.7; margin-right: 0.4rem">Month of</span>
${date.toISOString().split(/T/)[0].split('-').slice(0, 2).join('-')}
</h3>
<table style="padding: 2rem">
<tr>
<td>Spending</td>
<td>$${Math.floor(value).toLocaleString()}</td>
</tr>
</table>
${adjustments != null?
`<p>This ignores some significant expenses:</p>
${adjustments.value.map(adjustment =>
`<ul>
<li>${adjustment[0][0]} (${adjustment[0][1]}): $${adjustment[1].toLocaleString()}</li>
</ul>`).join('')}
` :
''}
`);
},
{
colors: {value: colorScale(4)[0]},
lines: {value: curveCatmullRom},
domains: {left: ([x0, x1]) => [0, x1]}
});
const netWorthData = data.networth.points.slice(1)
.map(([date, costBasis, value]) => ({date: new Date(date), costBasis, value}));
const growthData = netWorthData.map(({date, costBasis, value}) => ({date, value: value - costBasis}));
const networthElement = document.getElementById('networth');
let currentData = {
points: netWorthData,
markers: convertDates(data.networth.markers),
};
networthElement.addEventListener('click', event => {
if (event.target.classList.contains('toggle'))
updateNetworthData(currentData = {
points: currentData.points == netWorthData? growthData : netWorthData,
});
});
const updateNetworthData = timeSeriesChart(
networthElement,
currentData,
(element, {date, costBasis, value}={}, marker) => {
element.style.paddingRight = element.style.paddingLeft = "2rem";
element.innerHTML = `
<h2 style="flex: 1">
Net worth
<button style="font-size: 0.4em; vertical-align: top; float: right" type=button class=toggle>
Toggle growth vs total
</button>
</h2>
${date == null ?
`<p>Hover over the chart to see net worth and earnings.</p>` :
`
<h3>
<span style="font-style: italic; opacity: 0.7; margin-right: 0.4rem">Date: </span>
${date.toISOString().split(/T/)[0]}
</h3>
<table style="width: 19rem; padding: 2rem;">
<tr>
<td>Net worth <div class=color-block style="background-color: ${colorScale(4)[0]}"></div></td>
<td>$${Math.floor(value).toLocaleString()}</td>
</tr>
<tr>
<td>Cost basis <div class=color-block style="background-color: ${colorScale(16)[3]}"></div></td>
<td>$${Math.floor(costBasis).toLocaleString()}</td>
</tr>
<tr>
<td>Growth</td>
<td>$${Math.floor(value - costBasis).toLocaleString()}</td>
</tr>
</table>
${marker != null?
`<p>${marker.value}</p>` :
``}
`
}
`;
},
{colors: {costBasis: colorScale(16)[3], value: colorScale(4)[0]}});
data.yearlyFormatted = entries(data.yearly).map(([year, data]) => {
const date = new Date(Number.parseInt(year, 10), 0, 1);
const income = data.income.agi ?? 0;
const savings = sum(values(data.savings));
const taxes = sum(values(data.taxes));
const giving = data.giving ?? 0;
const spending = income - savings - taxes - giving;
return {date, income, savings, taxes, giving, spending};
}).filter(({date}) => date.getFullYear() < new Date().getFullYear());
const yearlyColors = {
income: 'white',
spending: '#8a4848',
taxes: 'rgb(164, 157, 120)',
savings: 'rgb(132 186 132)',
giving: '#3794ff',
}
timeSeriesChart(
document.getElementById('yearly'),
{
points: data.yearlyFormatted
.map(({date, income, savings, taxes, giving, spending}) => ({
date,
income,
spending,
taxes: taxes / income,
giving: giving / income,
savings: savings / income,
})),
},
(element, {date, income, savings, taxes, giving, spending}={}) => {
element.style.paddingRight = element.style.paddingLeft = "2rem";
element.innerHTML = '<h2>Yearly income vs. expenses</h2>' + (date == null?
`
<p>
This chart visualizes income and spending over the years and the
percentage we're saving, giving, and paying in taxes.
</p>
<p>
It has 2 Y-axes — the left is in dollars, the right is in
percent. Solid lines (income and expenses) are in dollars, and dotted
lines (taxes, giving, and savings) are in percent of income
(specifically AGI).
</p>
<p>
This is useful for showing not just our earnings over the years, but
also answering questions like "if we earn more, are we more generous?"
</p>
` :
`
<h3>
<span style="font-style: italic; opacity: 0.7; margin-right: 0.4rem">Year</span>
${date.getFullYear()}
</h3>
<table style="width: 18rem; padding: 2rem;">
<tr>
<td>Income <div class=color-block style="background-color: ${yearlyColors.income}"></div></td>
<td>$${Math.floor(income).toLocaleString()}</td>
</tr>
<tr>
<td>Spending <div class=color-block style="background-color: ${yearlyColors.spending}"></div></td>
<td>$${Math.floor(spending).toLocaleString()}</td>
</tr>
<tr>
<td>Savings <div class=color-block style="background-color: ${yearlyColors.savings}"></div></td>
<td>${Math.floor(savings * 100)}%</td>
</tr>
<tr>
<td>Taxes <div class=color-block style="background-color: ${yearlyColors.taxes}"></div></td>
<td>${Math.floor(taxes * 100)}%</td>
</tr>
<tr>
<td>Giving <div class=color-block style="background-color: ${yearlyColors.giving}"></div></td>
<td>${Math.floor(giving * 100)}%</td>
</tr>
</table>
`);
},
{
colors: yearlyColors,
domainSides: {income: 'left', spending: 'left', taxes: 'right', giving: 'right', savings: 'right'},
lines: {
income: curveCatmullRom,
spending: curveCatmullRom,
taxes: curveCatmullRom,
giving: curveCatmullRom,
savings: curveCatmullRom,
},
paths: {
income: path => path.attr('stroke-width', '3px'),
spending: path => path.attr('stroke-width', '3px'),
taxes: path => path.style('stroke-dasharray', '3, 3'),
giving: path => path.style('stroke-dasharray', '3, 3'),
savings: path => path.style('stroke-dasharray', '3, 3'),
},
});
stackedAreaChart(
document.getElementById('yearly-normalized'),
data.yearlyFormatted.map(({date, savings, taxes, giving, spending}) => ({
date,
savings,
giving,
taxes,
spending,
})),
(element, {date, savings, taxes, giving, spending}={}) => {
const total = savings + taxes + giving + spending;
element.style.paddingRight = element.style.paddingLeft = "2rem";
element.innerHTML = '<h2>Yearly expense proportions</h2>' + (date == null?
`
<p>
This chart visualizes our expense breakdown over the years.
</p>
<p>
This is useful for understanding the relationship between different
expense categories over time.
</p>
` :
`
<h3>
<span style="font-style: italic; opacity: 0.7; margin-right: 0.4rem">Year</span>
${date.getFullYear()}
</h3>
<table style="width: 18rem; padding: 2rem;">
<tr>
<td>Spending <div class=color-block style="background-color: ${yearlyColors.spending}"></div></td>
<td>${Math.floor(spending / total * 100)}%</td>
</tr>
<tr>
<td>Savings <div class=color-block style="background-color: ${yearlyColors.savings}"></div></td>
<td>${Math.floor(savings / total * 100)}%</td>
</tr>
<tr>
<td>Taxes <div class=color-block style="background-color: ${yearlyColors.taxes}"></div></td>
<td>${Math.floor(taxes / total * 100)}%</td>
</tr>
<tr>
<td>Giving <div class=color-block style="background-color: ${yearlyColors.giving}"></div></td>
<td>${Math.floor(giving / total * 100)}%</td>
</tr>
</table>
`);
},
{
colors: yearlyColors,
stack: stack => stack.offset(d3.stackOffsetExpand),
yAxis: y => y.tickFormat(d => `${d * 100}%`),
});
data.networthObjects = data.networth.points.slice(1)
.map(row => row.reduce((object, value, i) => {
const key = i == 0? 'date' : data.networth.points[0][i];
if (key != 'Cost basis' && key != 'Value')
object[key] = (i == 0 ? new Date(value) : value) ?? 0;
return object;
}, {}));
stackedAreaChart(
document.getElementById('networth-stacked'),
data.networthObjects,
(element, object, highlighted) => {
element.innerHTML = object != null?
`
<h3>
<span style="opacity: 0.6">Assets on ${object.date.toISOString().split('T')[0]}</span>
<span style="float: right; font-size: 0.9em; font-style: italic">
${highlighted ?? ''}
</span>
</h3>
<div class=grid>
${keys(object).filter(k => k != 'date').map((k, i) => `
<span>
<span class=color-block
style="display: inline-block;
opacity: ${!highlighted || k == highlighted? 1 : 0.8};
background-color: ${d3.schemeTableau10[i % d3.schemeTableau10.length]}">
</span>
<span style="opacity: ${!highlighted || k == highlighted? 1 : 0.6}">
${k.split(':')
.reverse()
.map((segment, i) => i == 0? segment : segment[0])
.reverse()
.join(':')}:
$${object[k].toLocaleString()}
</span>
</span>
`).join(' ')}
</div>
` :
`<p>
This chart visualizes our net worth each week broken down by
asset/investment. It's useful for understanding what accounts are
contributing to increases/decreases and identifying potential issues
with investments or bookkeeping.
</p>
<p>
Hover over the chart to see the specific asset's name as well as every
asset's value at that time.
</p>`;
},
{
colors: (_, i) => d3.schemeTableau10[i % d3.schemeTableau10.length],
yAxis: y => y.tickFormat(d => `$${d.toLocaleString()}`)
});