-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
159 lines (138 loc) · 5 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js" charset="utf-8"></script>
<!--
Should use the module instead, however I'm using the web version for simplicity sake for when
people download the zip (since submodules aren't also downloaded.
<script src="nvd3/build/nv.d3.js"></script>
<link href="nvd3/build/nv.d3.css" rel="stylesheet" type="text/css">
<script src="PapaParse/papaparse.min.js"></script>
-->
<script src="https://cdn.rawgit.com/ConstantinoSchillebeeckx/nvd3/2d2db73d65c90eb328ee4d788b6c03a2ed2ab020/build/nv.d3.js"></script>
<script src="https://cdn.rawgit.com/mholt/PapaParse/418ea803b830dc4b3a1f01d56fa776ecf1fbe6da/papaparse.min.js"></script>
<link href="https://cdn.rawgit.com/ConstantinoSchillebeeckx/nvd3/2d2db73d65c90eb328ee4d788b6c03a2ed2ab020/build/nv.d3.css"></script>
<script src="mint_to_sankey.js"></script>
<style>
text {
font: 12px sans-serif;
}
svg {
display: block;
}
html, body, svg {
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
}
.nvd3 {
margin-top: 10px;
margin-bottom: 10px;
}
/* --- Sankey chart styles --- */
.node rect {
cursor: move;
fill-opacity: .9;
shape-rendering: crispEdges;
}
.node text {
pointer-events: none;
text-shadow: 0 1px 0 #fff;
}
.link {
fill: none;
stroke: #000;
stroke-opacity: .2;
}
.link:hover {
stroke-opacity: .5;
}
</style>
</head>
<body>
<input type="file" id="upload" accepts=".csv" onchange="handleFiles(this.files[0])">
<div id="sankey" class="nvd3"></div>
<script>
function handleFiles(file) {
var tmp = Papa.parse(file,{
header: true,
complete: function (results) { render(results.data) }
})
}
function render(data_raw) {
var width = 1000,
height = 800;
var data = prepData(data_raw, 'Income');
console.log(data)
nv.addGraph(function() {
function cleanName(name) {
// parent nodes will have a prepended '_' on its name
// to allow a child by the same name
// this returns any node name with the removed '_'
// if it exists
return name.substring(0,1) == '_' ? name.substring(1,name.length) : name
}
var chart = nv.models.sankeyChart()
.width(width)
.height(height)
.nodeId(function(d) { return d.name })
.nodeWidth(10)
.nodePadding(10)
.margin({left: 5})
.linkTitle(function (d) {
return '$' + d3.format(',.0f')(d.value)
})
.nodeStyle({
title: function(d) {
return cleanName(d.name) + '\n' + '$' + d3.format(',.0f')(d.value);
},
label: function(d) {
return cleanName(d.name);
},
fillColor: function(d) {
var domain = [
'Income',
'Auto & Transport',
'Bills & Utilities',
'Business Services',
'Education',
'Entertainment',
'Fees & Charges',
'Financial',
'Food & Dining',
'Gifts & Donations',
'Health & Fitness',
'Home',
'Misc Expenses',
'Personal Care',
'Pets',
'Shopping',
'Taxes',
'Travel',
'Uncategorized',
]
var color = d3.scale.category20()
.domain(domain)
var name = cleanName(d.name);
if (d.sourceLinks.length === 0) {
// if child debit, grab parent name
name = cleanName(d.targetLinks[0].source.name);
} else if (d['Transaction Type'] == 'credit') {
name = 'Income';
}
return color(name)
}
})
d3.select('#sankey')
.attr('width', width)
.attr('height', height)
.datum(data)
.call(chart);
return chart;
});
}
</script>
</body>
</html>