forked from adieyal/vulekamali-viz-sandbox
-
Notifications
You must be signed in to change notification settings - Fork 3
/
dept-treemap.js
344 lines (286 loc) · 12.4 KB
/
dept-treemap.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
(function() {
var treemap;
var treemapOuterPadding = 0;
var treemapInnerPadding = 0;
var treemapOffsets = {
programme: { x: 4, y: 16 },
subprogramme : {
label: { x: 5, y: -24 },
budget: { x: 5, y: -8 }
},
label: {
programme: 52,
budget: 72,
separator: -25
},
treemap: {
margin: 5,
minSubprogrammeHeight: 70,
minSubprogrammeWidth: 100,
}
}
d3.select("head").append("script")
.attr("src", "https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js")
var container = d3.select(".department-subprogramme-treemap")
var mainConfig = {
container: container,
url: container.attr("data-aggregate-url"),
}
var model = JSON.parse(container.attr("data-openspending-model"));
var progNameRef = getProgNameRef(model);
var subprogNameRef = getSubprogNameRef(model);
var valueField = "value.sum"
var viewport = getViewportDimensions();
// set the dimensions and margins of the graph
var margin = {top: 0, right: 0, bottom: 0, left: 0},
width = viewport.width - margin.left - margin.right,
height = viewport.height - margin.top - margin.bottom
var largestBudget = function(d) {
var subprogrammes = d.parent.data.values;
return d3.max(subprogrammes, function(x) { return x[valueField] })
}
function addProgrammeLabels(d) {
var maxBudget = largestBudget(d)
var budget = d.data[valueField];
if (budget == maxBudget) {
return d.data[progNameRef]
}
}
function fadeProgramme(d, i) {
var maxBudget = largestBudget(d)
var budget = d.data[valueField];
if (budget == maxBudget) {
var minX = d3.min(d.parent.children, function(el) { return x(el.x0);})
var maxX = d3.max(d.parent.children, function(el) { return x(el.x1);})
var width = maxX - minX;
fade(d3.select(this), width)
}
}
function addSubprogrammeLabels(key, currency) {
return function(d) {
if (d.ry1 - d.ry0 > treemapOffsets.treemap.minSubprogrammeHeight && d.rx1 - d.rx0 > treemapOffsets.treemap.minSubprogrammeWidth) {
if (currency)
return rand_human_fmt(d.data[key])
return d.data[key]
} else {
return ""
}
}
}
function updateRangeCoordinates(data, x, y) {
data.map(function(d) {
d.rx0 = x(d.x0);
d.rx1 = x(d.x1);
d.ry0 = y(d.y0);
d.ry1 = y(d.y1);
})
}
function zoom(d) {
function displayLabels(d) {
if (d.x0 < x.domain()[0] || d.x1 > x.domain()[1]
|| d.y0 < y.domain()[0] || d.y1 > y.domain()[1])
return "none";
return "block"
}
var t = d3.transition()
.duration(800)
.ease(d3.easeCubicOut);
var parent = d.parent;
// unzoom if already zoomed
if (
x.domain()[0] == parent.x0 && x.domain()[1] == parent.x1
&& y.domain()[0] == parent.y0 && y.domain()[1] == parent.y1
) {
x.domain([0, width]);
y.domain([0, treemapHeight]);
parent = d.parent.parent;
} else {
x.domain([parent.x0, parent.x1]);
y.domain([parent.y0, parent.y1]);
}
updateRangeCoordinates(d3.selectAll(".box").data(), x, y)
d3.selectAll("rect.tile")
.transition(t)
.attr('x', function (d) { return x(d.x0) })
.attr('y', function (d) { return y(d.y0) })
.attr('width', function(d) { return x(d.x1) - x(d.x0)})
.attr('height', function(d) { return y(d.y1) - y(d.y0)})
treemap.selectAll(".programme-label tspan")
.transition(t)
.attr("x", function(d) { return x(d.x0) + treemapOffsets.programme.x})
.attr("y", function(d) { return y(d.y0) + treemapOffsets.programme.y})
.style("display", displayLabels)
.text(addProgrammeLabels)
.on("end", function() {
treemap.selectAll(".programme-label tspan")
.text(addProgrammeLabels)
.each(fadeProgramme)
})
d3.selectAll(".box .subprogramme-label tspan")
.transition(t)
.attr("x", function(d) { return x(d.x0) + treemapOffsets.subprogramme.label.x})
.attr("y", function(d) { return y(d.y1) + treemapOffsets.subprogramme.label.y})
.text(addSubprogrammeLabels(subprogNameRef))
.style("display", displayLabels)
d3.selectAll(".box .subprogramme-budget-label tspan")
.transition(t)
.attr("x", function(d) { return x(d.x0) + treemapOffsets.subprogramme.budget.x})
.attr("y", function(d) { return y(d.y1) + treemapOffsets.subprogramme.budget.y})
.text(addSubprogrammeLabels(valueField, true))
.style("display", displayLabels)
}
var svg = createSVG(mainConfig.container, viewport.width, viewport.height)
.attr("transform", "translate(" + margin.left + ", " + margin.top + ")")
.call(addLinearGradient, "text-fade", "rgb(255,255,255)", "rgb(255,255,255)", "95%", "100%", 1, 0)
var labels = svg.append("g")
.classed("top-labels", true);
var nester = d3
.nest()
.key(function(d) { return d[progNameRef]})
var programmeButton = labels
.append("g")
.classed("programme-button", true)
.call(createMainLabel, "PROGRAMME")
var programmeLabel = labels
.append("text")
.classed("programme-label", true)
.text("All programmes")
.attr("transform", "translate(0, " + treemapOffsets.label.programme + ")")
var programmeBudgetLabel = labels
.append("text")
.classed("programme-budget-label", true)
.text("R0")
.attr("transform", "translate(0, " + treemapOffsets.label.budget + ")")
var subprogrammeButton = labels
.append("g")
.classed("subprogramme-button", true)
.attr("transform", "translate(" + (width / 1.8 - 10) + ", 0)")
.call(createMainLabel, "SUB-PROGRAMME")
var subprogrammeLabel = subprogrammeButton
.append("text")
.classed("subprogramme-label", true)
.text("None selected")
.attr("transform", "translate(0, " + treemapOffsets.label.programme + ")")
var subprogrammeBudgetLabel = subprogrammeButton
.append("text")
.classed("subprogramme-budget-label", true)
.text("R0")
.attr("transform", "translate(0, " + treemapOffsets.label.budget + ")")
var labelDimensions = getDimensions(labels);
subprogrammeButton
.append("line")
.classed("label-separator", true)
.attr("x1", treemapOffsets.label.separator)
.attr("x2", treemapOffsets.label.separator)
.attr("y1", labelDimensions.y)
.attr("y2", labelDimensions.height + labelDimensions.y)
var saveButtonMargin = 10;
var saveButtonHeight = 30;
var saveButtonWidth = 140;
var treemapHeight = height - labelDimensions.height - labelDimensions.y - treemapOffsets.treemap.margin - saveButtonMargin - saveButtonHeight;
var labelsMargin = labelDimensions.y + labelDimensions.height + treemapOffsets.treemap.margin;
var x = d3.scaleLinear().domain([0, width]).range([0, width]).clamp(true)
var y = d3.scaleLinear().domain([0, treemapHeight]).range([0, treemapHeight]).clamp(true);
treemap = svg.append("g")
.attr("transform", "translate(" + -treemapOuterPadding * 2 + ", " + labelsMargin + ")")
var saveButtonContainer = createSaveButton(svg, {width: saveButtonWidth, height: saveButtonHeight}, viewport)
.attr("transform", "translate(" + (width - saveButtonWidth) + ", " + (treemapHeight + labelsMargin + saveButtonMargin) + ")")
d3.json(mainConfig.url, function(data) {
data = data.cells.sort(function(a, b) {
return b[valueField] - a[valueField];
});
var nested_data = nester.entries(data);
var root = d3.hierarchy(
{values:nested_data},
function(d) { return d.values }
)
.sum(function(d) { return d[valueField]})
// Then d3.treemap computes the position of each element of the hierarchy
d3.treemap()
.size([width, treemapHeight])
.padding(treemapInnerPadding)
.paddingOuter(treemapOuterPadding)
(root)
updateRangeCoordinates(root.leaves(), x, y)
var boxes = treemap
.selectAll("g")
.data(root.leaves())
.enter()
.append("g")
.classed("box", true)
// Add rectangles
boxes
.append("rect")
.classed("tile", true)
.attr('x', function (d) {return x(d.x0)})
.attr('y', function (d) { return y(d.y0)})
.attr('width', function (d) {return x(d.x1 - d.x0)})
.attr('height', function (d) { return y(d.y1 - d.y0)})
.attr("data-programme", function(d, i) {
return slugify(d.data[progNameRef])
})
.style("fill", function(d, idx) {
var programmes = root.data.values.map(function(d) { return d.key});
var subprogrammes = d.parent.data.values;
var subprogramme_labels = subprogrammes.map(function(d) { return d[subprogNameRef];});
var idx = programmes.indexOf(d.data[progNameRef])
var idx2 = subprogramme_labels.indexOf(d.data[subprogNameRef]);
var hues = colorMap[idx];
return hues[idx2];
})
.on("mouseover", function(d) {
programmeLabel.text(d.data[progNameRef])
subprogrammeLabel.text(d.data[subprogNameRef])
programmeBudgetLabel.text(rand_fmt(d.parent.value));
subprogrammeBudgetLabel.text(rand_fmt(d.value));
})
.on("mouseout", function(d) {
programmeBudgetLabel.text(rand_fmt(d.parent.parent.value))
programmeLabel.text("All programmes")
subprogrammeLabel.text("None selected")
subprogrammeBudgetLabel.text("R0")
})
.on("click", zoom)
// Add programme labels
treemap
.selectAll(".programme-label")
.data(root.leaves())
.enter()
.append("text")
.classed("programme-label", true)
.attr("x", function(d) { return d.x0 + treemapOffsets.programme.x})
.attr("y", function(d) { return d.y0 + treemapOffsets.programme.y})
.text(addProgrammeLabels)
.attr("font-size", "0.6em")
.attr("fill", "white")
.each(fadeProgramme)
// Add subprogramme labels
boxes
.append("text")
.classed("subprogramme-label", true)
.attr("x", function(d) { return d.x0 + treemapOffsets.subprogramme.label.x})
.attr("y", function(d) { return d.y1 + treemapOffsets.subprogramme.label.y})
.text(addSubprogrammeLabels(subprogNameRef))
.attr("font-size", "0.6em")
.attr("fill", "white")
.each(function(d, i) {
var width = d.x1 - d.x0;
fade(d3.select(this), width - 5)
})
// Add subprogramme budgets
boxes
.append("text")
.classed("subprogramme-budget-label", true)
.attr("x", function(d) { return d.x0 + treemapOffsets.subprogramme.budget.x})
.attr("y", function(d) { return d.y1 + treemapOffsets.subprogramme.budget.y})
.text(addSubprogrammeLabels(valueField, true))
.attr("font-size", "0.6em")
.attr("fill", "white")
.call(fade)
programmeBudgetLabel
.datum(root)
.text(function(d) {
return rand_fmt(d.value)
})
})
})()