forked from vulekamali/data-visualisations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dept-econ4.js
270 lines (217 loc) · 10 KB
/
dept-econ4.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
(function() {
var viewport = getViewportDimensions();
var isMobile = viewport.width < 1000
var margin = {top: 0, right: 0, bottom: 0, left: 0}
var container = d3.select(".department-econ4")
var model = JSON.parse(container.attr("data-openspending-model"))
var cfg = {
main: {
url: container.attr("data-aggregate-url")
},
data: {
category: getProgNameRef(model),
subCategory: getSubprogNameRef(model),
valueLabel: getEconClass4Ref(model),
valueField: "value.sum"
},
viz: {
width: viewport.width - margin.left - margin.right,
height: viewport.height - margin.top - margin.bottom,
isMobile: isMobile,
},
saveButton: {
margin: 10,
height: 30,
width: 140,
config: {
backgroundColor : "white",
left : -10,
width: viewport.width + 20,
top: -10,
height: viewport.height - 40,
filename: "economic-classifications.png",
}
},
topSection: {
height: 40,
},
leftSection: {
width: isMobile ? viewport.width * 1/2 : viewport.width * 2/5
},
rightSection: {
width: isMobile ? viewport.width * 1/2 : viewport.width * 3/5
},
bars: {
heightOffset: 0,
headingMargin: 15,
separatorMargin: 20,
programmeRowPadding: 10,
subprogrammeRowPadding: 8,
numTicks: 3,
}
}
var saveButtonContainer;
/* Create the select box (temporary until we have a design */
var topSection = container
.append("div").classed("top-section", true)
.style("height", cfg.topSection.height)
var selectBox = topSection.append("select")
var svg = createSVG(container, viewport.width, viewport.height)
.style("margin", 0)
.call(addLinearGradient, "bar-gradient")
.attr("transform", "translate(0, 5)")
/* Headings */
var programmesSection = svg
.append("g")
.classed("programmes-section", true)
var subprogrammesSection = createMainLabel(programmesSection, "SUB-PROGRAMMES")
var valueSection = createMainLabel(programmesSection, "VALUE")
.attr("transform", "translate(" + cfg.leftSection.width + ", 0)")
heightOffset = cfg.bars.heightOffset
heightOffset += getDimensions(programmesSection).height
heightOffset += cfg.bars.headingMargin
var headingLine = svg.append("line")
.attr("x1", 0)
.attr("x2", viewport.width)
.attr("y1", heightOffset)
.attr("y2", heightOffset)
.classed("heading-line", true)
//heightOffset = getDimensions(headingLine).y
heightOffset += cfg.bars.separatorMargin
var valuesSection = svg
.append("g")
.classed("values-section", true)
.attr("transform", "translate(0, " + heightOffset + ")")
bbox = getDimensions(container)
var saveButtonContainer = createSaveButton(
svg,
{width: cfg.saveButton.width, height: cfg.saveButton.height},
{width: cfg.saveButton.config.width, height: cfg.saveButton.config.height},
cfg.saveButton.config
)
var displayClassification = function(data, container) {
return function(classification) {
container.selectAll("*").remove()
filtered_data = data.filter(function(d) {
return d[cfg.data.valueLabel] == classification;
})
var max_value = d3.max(filtered_data, function(d) { return d[cfg.data.valueField]; })
var maxBarLength = cfg.rightSection.width * 2/3
scaleBar = d3.scaleLinear().domain([0, max_value]).range([0, maxBarLength])
var nestedData = d3.nest()
.key(function(d) { return d[cfg.data.category]})
.key(function(d) { return d[cfg.data.subCategory]})
.rollup(function(leaves) {
return d3.sum(leaves, function(leaf) { return leaf[cfg.data.valueField] });
})
.entries(filtered_data)
var offset = 0 // TODO fix this - not sure why the transform on the container isn't working
for (idx in nestedData) {
var programmeData = nestedData[idx];
programmeRow = container.append("g")
.classed("programme-row", true)
.attr("transform", "translate(0, " + offset + ")")
.append("text")
.text(programmeData.key)
.call(wrap, cfg.leftSection.width)
offset += getDimensions(programmeRow).height + cfg.bars.subprogrammeRowPadding
for (idx2 in programmeData.values) {
var row = container.append("g")
var backgroundRect = row.append("rect")
var subprogrammeData = programmeData.values[idx2];
var subprogrammeRow = row.append("g")
.classed("subprogramme-row", true)
.attr("transform", "translate(4, " + offset + ")")
.append("text")
.text(subprogrammeData.key)
.each(textBump)
.call(wrap, cfg.leftSection.width, 1.1)
var dimensions = getDimensions(subprogrammeRow);
backgroundRect
.classed("background-rect", true)
.attr("x", 0)
.attr("y", offset) // TODO Figure out how to calculate 10 which is the lineheight
.attr("width", cfg.leftSection.width - 5)
.attr("height", dimensions.height + 4)
row.attr("transform", "translate(0, -12)")
// Hack to display gradient - display the gradient full
// width then draw a white box over it to cover up the
// unused part of the rectangle
// TODO current assumption is that the background is white
// - might want to paramaterise it
barRect = container.append("rect")
.classed("bar-rect", true)
.attr("x", cfg.leftSection.width)
.attr("y", offset - 10)
.attr("width", maxBarLength)
.attr("height", 14)
barRect = container.append("rect")
.classed("bar-rect", true)
.attr("x", cfg.leftSection.width)
.attr("y", offset - 10)
.attr("width", maxBarLength - scaleBar(subprogrammeData.value) + 1)
.attr("height", 16)
.style("fill", "white")
.attr("transform", "translate(" + scaleBar(subprogrammeData.value) + ", -1)")
var gridLines = container
.append("g")
.selectAll("line")
.data(scaleBar.ticks(cfg.bars.numTicks))
.enter()
.append("line")
.classed("gridline", true)
value = container.append("text")
.classed("spend-value", true)
.text(rand_human_fmt(subprogrammeData.value, false))
.attr("transform", "translate(" + (cfg.leftSection.width + scaleBar(subprogrammeData.value) + 5) + ", " + (offset) + ")")
.style("fill", "black")
offset += getDimensions(subprogrammeRow).height + cfg.bars.subprogrammeRowPadding
}
offset += cfg.bars.programmeRowPadding
}
offset -= cfg.bars.programmeRowPadding;
var xAxis = d3.axisBottom(scaleBar)
.ticks(cfg.bars.numTicks)
.tickFormat(rand_human_fmt)
gridLines
.attr("x1", function(d) { return cfg.leftSection.width + scaleBar(d); })
.attr("x2", function(d) { return cfg.leftSection.width + scaleBar(d); })
.attr("y1", 0)
.attr("y2", offset)
container
.append("g")
.attr("transform", "translate(" + cfg.leftSection.width + ", " + offset + ")")
.call(xAxis)
var bottomPadding = 60;
bbox = getDimensions(container)
vizHeight = bbox.height + bottomPadding + cfg.saveButton.height
svg.attr("height", vizHeight)
cfg.saveButton.config.height = vizHeight - 10;
saveButtonContainer.attr("transform", "translate(" + (cfg.viz.width - cfg.saveButton.width) + ", " + (bbox.height + bottomPadding) + ")")
pymChild.sendHeight()
}
}
function getSelectedCategory(el) {
idx = el.selectedIndex
value = el.options[idx].value;
}
d3.json(cfg.main.url, function(data) {
data = data.cells;
var valueIndex = regroupByIndex(data, cfg.data.valueLabel);
var max_value = d3.max(data, function(d) { return d[cfg.data.valueField]; })
var funcDisplay = displayClassification(data, valuesSection);
selectBox
.on("change", function(d, i) {
getSelectedCategory(d3.event.srcElement)
funcDisplay(value)
})
.classed("account-select", true)
.selectAll("option")
.data(valueIndex)
.enter()
.append("option")
.text(function(d) { return d.key;})
getSelectedCategory(selectBox.node())
funcDisplay(value)
})
})()