forked from cs4804-24c/final
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreemap.js
More file actions
76 lines (71 loc) · 2.01 KB
/
treemap.js
File metadata and controls
76 lines (71 loc) · 2.01 KB
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
// set the dimensions and margins of the graph
var margin = { top: 0, right: 0, bottom: 0, left: 0 },
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3
.select("#my_dataviz")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Read data
d3.csv("./treemap.csv", function (data) {
// stratify the data: reformatting for d3.js
var root = d3
.stratify()
.id(function (d) {
return d.name;
}) // Name of the entity (column name is name in csv)
.parentId(function (d) {
return d.parent;
})(
// Name of the parent (column name is parent in csv)
data
);
root.sum(function (d) {
return +d.value;
}); // Compute the numeric value for each entity
// Then d3.treemap computes the position of each element of the hierarchy
// The coordinates are added to the root object above
d3.treemap().size([width, height]).padding(10)(root);
console.log(root.leaves());
// use this information to add rectangles:
svg
.selectAll("rect")
.data(root.leaves())
.enter()
.append("rect")
.attr("x", function (d) {
return d.x0;
})
.attr("y", function (d) {
return d.y0;
})
.attr("width", function (d) {
return d.x1 - d.x0;
})
.attr("height", function (d) {
return d.y1 - d.y0;
})
.style("stroke", "black")
.style("fill", "#ffcc99");
// and to add the text labels
svg
.selectAll("text")
.data(root.leaves())
.enter()
.append("text")
.attr("x", function (d) {
return d.x0 + 10;
}) // +10 to adjust position (more right)
.attr("y", function (d) {
return d.y0 + 20;
}) // +20 to adjust position (lower)
.text(function (d) {
return d.data.name;
})
.attr("font-size", "10px")
.attr("fill", "black");
});