author | nowmad@nowmads-macbook-pro.local |
Thu, 21 Jan 2016 00:11:10 +0100 | |
changeset 93 | f7223e2f3023 |
parent 92 | 3437f5191645 |
child 95 | f7ab931581af |
permissions | -rw-r--r-- |
39
c693e8673d5b
add visu-langue with google treemap
nowmad@23.1.168.192.in-addr.arpa
parents:
diff
changeset
|
1 |
import Ember from 'ember'; |
c693e8673d5b
add visu-langue with google treemap
nowmad@23.1.168.192.in-addr.arpa
parents:
diff
changeset
|
2 |
|
c693e8673d5b
add visu-langue with google treemap
nowmad@23.1.168.192.in-addr.arpa
parents:
diff
changeset
|
3 |
export default Ember.Component.extend({ |
c693e8673d5b
add visu-langue with google treemap
nowmad@23.1.168.192.in-addr.arpa
parents:
diff
changeset
|
4 |
didInsertElement: function(){ |
51
70dff07a76ff
add click event on visu-carto and visu-langue and update the url with the selected element as filter parameter
nowmad@nowmads-macbook-pro.local
parents:
45
diff
changeset
|
5 |
var _this = this; |
70dff07a76ff
add click event on visu-carto and visu-langue and update the url with the selected element as filter parameter
nowmad@nowmads-macbook-pro.local
parents:
45
diff
changeset
|
6 |
|
84 | 7 |
var margin = {top: 20, right: 0, bottom: 0, left: 0}, |
8 |
width = 560, |
|
9 |
height = 600 - margin.top - margin.bottom, |
|
10 |
formatNumber = d3.format(",d"), |
|
11 |
transitioning; |
|
12 |
||
13 |
var x = d3.scale.linear() |
|
14 |
.domain([0, width]) |
|
15 |
.range([0, width]); |
|
16 |
||
17 |
var y = d3.scale.linear() |
|
18 |
.domain([0, height]) |
|
19 |
.range([0, height]); |
|
20 |
||
21 |
var treemap = d3.layout.treemap() |
|
22 |
.children(function(d, depth) { return depth ? null : d._children; }) |
|
23 |
.sort(function(a, b) { return a.value - b.value; }) |
|
24 |
.ratio(height / width * 0.5 * (1 + Math.sqrt(5))) |
|
25 |
.round(false); |
|
26 |
||
27 |
var svg = d3.select("#chart_div").append("svg") |
|
28 |
.attr("width", width + margin.left + margin.right) |
|
29 |
.attr("height", height + margin.bottom + margin.top) |
|
30 |
.style("margin-left", -margin.left + "px") |
|
31 |
.style("margin.right", -margin.right + "px") |
|
32 |
.append("g") |
|
33 |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")") |
|
34 |
.style("shape-rendering", "crispEdges"); |
|
35 |
||
36 |
var grandparent = svg.append("g") |
|
37 |
.attr("class", "grandparent"); |
|
38 |
||
39 |
grandparent.append("rect") |
|
40 |
.attr("y", -margin.top) |
|
41 |
.attr("width", width) |
|
42 |
.attr("height", margin.top); |
|
43 |
||
44 |
grandparent.append("text") |
|
45 |
.attr("x", 6) |
|
46 |
.attr("y", 6 - margin.top) |
|
47 |
.attr("dy", ".75em"); |
|
48 |
||
49 |
d3.json("langues.json", function(root) { |
|
50 |
initialize(root); |
|
51 |
accumulate(root); |
|
52 |
layout(root); |
|
53 |
display(root); |
|
54 |
||
55 |
function initialize(root) { |
|
56 |
root.x = root.y = 0; |
|
57 |
root.dx = width; |
|
58 |
root.dy = height; |
|
59 |
root.depth = 0; |
|
60 |
} |
|
61 |
||
62 |
// Aggregate the values for internal nodes. This is normally done by the |
|
63 |
// treemap layout, but not here because of our custom implementation. |
|
64 |
// We also take a snapshot of the original children (_children) to avoid |
|
65 |
// the children being overwritten when when layout is computed. |
|
66 |
function accumulate(d) { |
|
92 | 67 |
return (d._children = d.children) ? d.value = d.children.reduce(function(p, v) { return p + accumulate(v); }, 0) : d.value; |
84 | 68 |
} |
69 |
||
70 |
// Compute the treemap layout recursively such that each group of siblings |
|
71 |
// uses the same size (1×1) rather than the dimensions of the parent cell. |
|
72 |
// This optimizes the layout for the current zoom state. Note that a wrapper |
|
73 |
// object is created for the parent node for each group of siblings so that |
|
74 |
// the parent’s dimensions are not discarded as we recurse. Since each group |
|
75 |
// of sibling was laid out in 1×1, we must rescale to fit using absolute |
|
76 |
// coordinates. This lets us use a viewport to zoom. |
|
77 |
function layout(d) { |
|
78 |
if (d._children) { |
|
79 |
treemap.nodes({_children: d._children}); |
|
80 |
d._children.forEach(function(c) { |
|
81 |
c.x = d.x + c.x * d.dx; |
|
82 |
c.y = d.y + c.y * d.dy; |
|
83 |
c.dx *= d.dx; |
|
84 |
c.dy *= d.dy; |
|
85 |
c.parent = d; |
|
86 |
layout(c); |
|
87 |
}); |
|
88 |
} |
|
89 |
} |
|
90 |
||
91 |
function display(d) { |
|
92 |
grandparent |
|
93 |
.datum(d.parent) |
|
94 |
.on("click", transition) |
|
95 |
.select("text") |
|
96 |
.text(name(d)); |
|
39
c693e8673d5b
add visu-langue with google treemap
nowmad@23.1.168.192.in-addr.arpa
parents:
diff
changeset
|
97 |
|
84 | 98 |
var g1 = svg.insert("g", ".grandparent") |
99 |
.datum(d) |
|
100 |
.attr("class", "depth"); |
|
101 |
||
102 |
var g = g1.selectAll("g") |
|
103 |
.data(d._children) |
|
104 |
.enter().append("g"); |
|
105 |
||
92 | 106 |
g.classed("bla", true).on("click", selectHandler); |
84 | 107 |
|
108 |
g.filter(function(d) { return d._children; }) |
|
109 |
.classed("children", true) |
|
110 |
.on("click", transition); |
|
111 |
||
112 |
g.append("rect") |
|
113 |
.attr("class", "parent") |
|
114 |
// .attr("fill", (d.color || "#bbb")) |
|
115 |
.call(rect) |
|
116 |
.append("title") |
|
117 |
.text(function(d) { return formatNumber(d.value); }); |
|
118 |
||
119 |
g.append("text") |
|
120 |
.attr("dy", ".75em") |
|
121 |
.text(function(d) { return d.name; }) |
|
122 |
.call(text); |
|
123 |
||
124 |
function transition(d) { |
|
92 | 125 |
if (transitioning || !d) { return; } |
84 | 126 |
selectHandler(d); |
127 |
transitioning = true; |
|
128 |
||
129 |
var g2 = display(d), |
|
130 |
t1 = g1.transition().duration(750), |
|
131 |
t2 = g2.transition().duration(750); |
|
132 |
||
133 |
// Update the domain only after entering new elements. |
|
134 |
x.domain([d.x, d.x + d.dx]); |
|
135 |
y.domain([d.y, d.y + d.dy]); |
|
136 |
||
137 |
// Enable anti-aliasing during the transition. |
|
138 |
svg.style("shape-rendering", null); |
|
39
c693e8673d5b
add visu-langue with google treemap
nowmad@23.1.168.192.in-addr.arpa
parents:
diff
changeset
|
139 |
|
84 | 140 |
// Draw child nodes on top of parent nodes. |
141 |
svg.selectAll(".depth").sort(function(a, b) { return a.depth - b.depth; }); |
|
142 |
||
143 |
// Fade-in entering text. |
|
144 |
g2.selectAll("text").style("fill-opacity", 0); |
|
145 |
||
146 |
// Transition to the new view. |
|
147 |
t1.selectAll("text").call(text).style("fill-opacity", 0); |
|
148 |
t2.selectAll("text").call(text).style("fill-opacity", 1); |
|
149 |
t1.selectAll("rect").call(rect); |
|
150 |
t2.selectAll("rect").call(rect); |
|
151 |
||
152 |
// Remove the old node when the transition is finished. |
|
153 |
t1.remove().each("end", function() { |
|
154 |
svg.style("shape-rendering", "crispEdges"); |
|
155 |
transitioning = false; |
|
156 |
}); |
|
157 |
} |
|
158 |
||
159 |
function selectHandler (d){ |
|
160 |
if (d.name === "Global"){ |
|
161 |
return _this.sendAction('action', null); |
|
162 |
} |
|
163 |
_this.sendAction('action', d.name); |
|
164 |
} |
|
165 |
||
166 |
return g; |
|
167 |
} |
|
168 |
||
93 | 169 |
function text(text) { |
84 | 170 |
text.attr("x", function(d) { return x(d.x) + 6; }) |
171 |
.attr("y", function(d) { return y(d.y) + 6; }); |
|
172 |
} |
|
173 |
||
93 | 174 |
function rect(rect) { |
84 | 175 |
rect.attr("x", function(d) { return x(d.x); }) |
176 |
.attr("y", function(d) { return y(d.y); }) |
|
177 |
.attr("width", function(d) { return x(d.x + d.dx) - x(d.x); }) |
|
178 |
.attr("height", function(d) { return y(d.y + d.dy) - y(d.y); }) |
|
179 |
.attr("fill", function(d) { return (d.color || "#bbb")}); |
|
180 |
} |
|
181 |
||
182 |
function name(d) { |
|
92 | 183 |
return d.parent ? name(d.parent) + "." + d.name : d.name; |
84 | 184 |
} |
39
c693e8673d5b
add visu-langue with google treemap
nowmad@23.1.168.192.in-addr.arpa
parents:
diff
changeset
|
185 |
}); |
c693e8673d5b
add visu-langue with google treemap
nowmad@23.1.168.192.in-addr.arpa
parents:
diff
changeset
|
186 |
} |
c693e8673d5b
add visu-langue with google treemap
nowmad@23.1.168.192.in-addr.arpa
parents:
diff
changeset
|
187 |
}); |