author | ymh <ymh.work@gmail.com> |
Thu, 02 Jun 2016 18:16:17 +0200 | |
changeset 167 | 009efee954f7 |
parent 142 | 86fcf0c837c0 |
child 188 | d2cb39155997 |
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'; |
123
4c97e9da1416
Remove use of global variables + remove ember-cli-content-security-policy from package.json to avoid bogus csp violation warnings
ymh <ymh.work@gmail.com>
parents:
95
diff
changeset
|
2 |
import d3 from 'd3'; |
126
e87a340711a4
improve on dataloading. add fixture management with proper interface to load data.
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
3 |
import ENV from 'app-client/config/environment'; |
e87a340711a4
improve on dataloading. add fixture management with proper interface to load data.
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
4 |
import _ from 'lodash/lodash'; |
39
c693e8673d5b
add visu-langue with google treemap
nowmad@23.1.168.192.in-addr.arpa
parents:
diff
changeset
|
5 |
|
c693e8673d5b
add visu-langue with google treemap
nowmad@23.1.168.192.in-addr.arpa
parents:
diff
changeset
|
6 |
export default Ember.Component.extend({ |
126
e87a340711a4
improve on dataloading. add fixture management with proper interface to load data.
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
7 |
constants: Ember.inject.service(), |
39
c693e8673d5b
add visu-langue with google treemap
nowmad@23.1.168.192.in-addr.arpa
parents:
diff
changeset
|
8 |
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
|
9 |
var _this = this; |
84 | 10 |
var margin = {top: 20, right: 0, bottom: 0, left: 0}, |
95 | 11 |
width = Ember.$("#chart_div").width(), |
84 | 12 |
height = 600 - margin.top - margin.bottom, |
13 |
formatNumber = d3.format(",d"), |
|
14 |
transitioning; |
|
15 |
||
16 |
var x = d3.scale.linear() |
|
17 |
.domain([0, width]) |
|
18 |
.range([0, width]); |
|
19 |
||
20 |
var y = d3.scale.linear() |
|
21 |
.domain([0, height]) |
|
22 |
.range([0, height]); |
|
23 |
||
24 |
var treemap = d3.layout.treemap() |
|
25 |
.children(function(d, depth) { return depth ? null : d._children; }) |
|
26 |
.sort(function(a, b) { return a.value - b.value; }) |
|
27 |
.ratio(height / width * 0.5 * (1 + Math.sqrt(5))) |
|
28 |
.round(false); |
|
29 |
||
30 |
var svg = d3.select("#chart_div").append("svg") |
|
31 |
.attr("width", width + margin.left + margin.right) |
|
32 |
.attr("height", height + margin.bottom + margin.top) |
|
33 |
.style("margin-left", -margin.left + "px") |
|
34 |
.style("margin.right", -margin.right + "px") |
|
35 |
.append("g") |
|
36 |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")") |
|
37 |
.style("shape-rendering", "crispEdges"); |
|
38 |
||
39 |
var grandparent = svg.append("g") |
|
40 |
.attr("class", "grandparent"); |
|
41 |
||
42 |
grandparent.append("rect") |
|
43 |
.attr("y", -margin.top) |
|
44 |
.attr("width", width) |
|
45 |
.attr("height", margin.top); |
|
46 |
||
47 |
grandparent.append("text") |
|
48 |
.attr("x", 6) |
|
49 |
.attr("y", 6 - margin.top) |
|
50 |
.attr("dy", ".75em"); |
|
51 |
||
126
e87a340711a4
improve on dataloading. add fixture management with proper interface to load data.
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
52 |
var baseurl = ENV.baseURL.replace(/\/$/,"")+'/api/v1'; |
e87a340711a4
improve on dataloading. add fixture management with proper interface to load data.
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
53 |
d3.json(baseurl+"/languages", function(languages) { |
e87a340711a4
improve on dataloading. add fixture management with proper interface to load data.
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
54 |
|
e87a340711a4
improve on dataloading. add fixture management with proper interface to load data.
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
55 |
var root = _.cloneDeep(_this.constants.LANGUAGES_TREEMAP); |
e87a340711a4
improve on dataloading. add fixture management with proper interface to load data.
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
56 |
|
84 | 57 |
initialize(root); |
58 |
accumulate(root); |
|
59 |
layout(root); |
|
60 |
display(root); |
|
61 |
||
62 |
function initialize(root) { |
|
63 |
root.x = root.y = 0; |
|
64 |
root.dx = width; |
|
65 |
root.dy = height; |
|
66 |
root.depth = 0; |
|
67 |
} |
|
68 |
||
69 |
// Aggregate the values for internal nodes. This is normally done by the |
|
70 |
// treemap layout, but not here because of our custom implementation. |
|
71 |
// We also take a snapshot of the original children (_children) to avoid |
|
72 |
// the children being overwritten when when layout is computed. |
|
73 |
function accumulate(d) { |
|
142
86fcf0c837c0
change language treemap. id in struct can be a list of lexvo references
ymh <ymh.work@gmail.com>
parents:
126
diff
changeset
|
74 |
d._children = d.children; |
86fcf0c837c0
change language treemap. id in struct can be a list of lexvo references
ymh <ymh.work@gmail.com>
parents:
126
diff
changeset
|
75 |
if(d.children) { |
86fcf0c837c0
change language treemap. id in struct can be a list of lexvo references
ymh <ymh.work@gmail.com>
parents:
126
diff
changeset
|
76 |
d.value = d.children.reduce(function(p, v) { return p + accumulate(v); }, 0); |
86fcf0c837c0
change language treemap. id in struct can be a list of lexvo references
ymh <ymh.work@gmail.com>
parents:
126
diff
changeset
|
77 |
} else if (_.isArray(d.id)) { |
86fcf0c837c0
change language treemap. id in struct can be a list of lexvo references
ymh <ymh.work@gmail.com>
parents:
126
diff
changeset
|
78 |
d.value = d.id.reduce( |
86fcf0c837c0
change language treemap. id in struct can be a list of lexvo references
ymh <ymh.work@gmail.com>
parents:
126
diff
changeset
|
79 |
function(s,lid) { return s + (languages[lid]?languages[lid]:0); }, |
86fcf0c837c0
change language treemap. id in struct can be a list of lexvo references
ymh <ymh.work@gmail.com>
parents:
126
diff
changeset
|
80 |
0 |
86fcf0c837c0
change language treemap. id in struct can be a list of lexvo references
ymh <ymh.work@gmail.com>
parents:
126
diff
changeset
|
81 |
); |
86fcf0c837c0
change language treemap. id in struct can be a list of lexvo references
ymh <ymh.work@gmail.com>
parents:
126
diff
changeset
|
82 |
} |
86fcf0c837c0
change language treemap. id in struct can be a list of lexvo references
ymh <ymh.work@gmail.com>
parents:
126
diff
changeset
|
83 |
else { |
86fcf0c837c0
change language treemap. id in struct can be a list of lexvo references
ymh <ymh.work@gmail.com>
parents:
126
diff
changeset
|
84 |
d.value = languages[d.id]?languages[d.id]:0; |
86fcf0c837c0
change language treemap. id in struct can be a list of lexvo references
ymh <ymh.work@gmail.com>
parents:
126
diff
changeset
|
85 |
} |
86fcf0c837c0
change language treemap. id in struct can be a list of lexvo references
ymh <ymh.work@gmail.com>
parents:
126
diff
changeset
|
86 |
return d.value; |
84 | 87 |
} |
88 |
||
89 |
// Compute the treemap layout recursively such that each group of siblings |
|
90 |
// uses the same size (1×1) rather than the dimensions of the parent cell. |
|
91 |
// This optimizes the layout for the current zoom state. Note that a wrapper |
|
92 |
// object is created for the parent node for each group of siblings so that |
|
93 |
// the parent’s dimensions are not discarded as we recurse. Since each group |
|
94 |
// of sibling was laid out in 1×1, we must rescale to fit using absolute |
|
95 |
// coordinates. This lets us use a viewport to zoom. |
|
96 |
function layout(d) { |
|
97 |
if (d._children) { |
|
98 |
treemap.nodes({_children: d._children}); |
|
99 |
d._children.forEach(function(c) { |
|
100 |
c.x = d.x + c.x * d.dx; |
|
101 |
c.y = d.y + c.y * d.dy; |
|
102 |
c.dx *= d.dx; |
|
103 |
c.dy *= d.dy; |
|
104 |
c.parent = d; |
|
105 |
layout(c); |
|
106 |
}); |
|
107 |
} |
|
108 |
} |
|
109 |
||
110 |
function display(d) { |
|
111 |
grandparent |
|
112 |
.datum(d.parent) |
|
113 |
.on("click", transition) |
|
126
e87a340711a4
improve on dataloading. add fixture management with proper interface to load data.
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
114 |
.select("text") |
84 | 115 |
.text(name(d)); |
39
c693e8673d5b
add visu-langue with google treemap
nowmad@23.1.168.192.in-addr.arpa
parents:
diff
changeset
|
116 |
|
84 | 117 |
var g1 = svg.insert("g", ".grandparent") |
118 |
.datum(d) |
|
119 |
.attr("class", "depth"); |
|
120 |
||
121 |
var g = g1.selectAll("g") |
|
122 |
.data(d._children) |
|
126
e87a340711a4
improve on dataloading. add fixture management with proper interface to load data.
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
123 |
.enter().append("g"); |
84 | 124 |
|
92 | 125 |
g.classed("bla", true).on("click", selectHandler); |
84 | 126 |
|
127 |
g.filter(function(d) { return d._children; }) |
|
128 |
.classed("children", true) |
|
129 |
.on("click", transition); |
|
130 |
||
131 |
g.append("rect") |
|
132 |
.attr("class", "parent") |
|
133 |
// .attr("fill", (d.color || "#bbb")) |
|
134 |
.call(rect) |
|
135 |
.append("title") |
|
136 |
.text(function(d) { return formatNumber(d.value); }); |
|
137 |
||
138 |
g.append("text") |
|
139 |
.attr("dy", ".75em") |
|
140 |
.text(function(d) { return d.name; }) |
|
141 |
.call(text); |
|
142 |
||
143 |
function transition(d) { |
|
92 | 144 |
if (transitioning || !d) { return; } |
84 | 145 |
selectHandler(d); |
146 |
transitioning = true; |
|
147 |
||
148 |
var g2 = display(d), |
|
149 |
t1 = g1.transition().duration(750), |
|
150 |
t2 = g2.transition().duration(750); |
|
151 |
||
152 |
// Update the domain only after entering new elements. |
|
153 |
x.domain([d.x, d.x + d.dx]); |
|
154 |
y.domain([d.y, d.y + d.dy]); |
|
155 |
||
156 |
// Enable anti-aliasing during the transition. |
|
157 |
svg.style("shape-rendering", null); |
|
39
c693e8673d5b
add visu-langue with google treemap
nowmad@23.1.168.192.in-addr.arpa
parents:
diff
changeset
|
158 |
|
84 | 159 |
// Draw child nodes on top of parent nodes. |
160 |
svg.selectAll(".depth").sort(function(a, b) { return a.depth - b.depth; }); |
|
161 |
||
162 |
// Fade-in entering text. |
|
163 |
g2.selectAll("text").style("fill-opacity", 0); |
|
164 |
||
165 |
// Transition to the new view. |
|
166 |
t1.selectAll("text").call(text).style("fill-opacity", 0); |
|
167 |
t2.selectAll("text").call(text).style("fill-opacity", 1); |
|
168 |
t1.selectAll("rect").call(rect); |
|
169 |
t2.selectAll("rect").call(rect); |
|
170 |
||
171 |
// Remove the old node when the transition is finished. |
|
172 |
t1.remove().each("end", function() { |
|
173 |
svg.style("shape-rendering", "crispEdges"); |
|
174 |
transitioning = false; |
|
175 |
}); |
|
176 |
} |
|
177 |
||
178 |
function selectHandler (d){ |
|
179 |
if (d.name === "Global"){ |
|
180 |
return _this.sendAction('action', null); |
|
181 |
} |
|
182 |
_this.sendAction('action', d.name); |
|
183 |
} |
|
184 |
||
185 |
return g; |
|
186 |
} |
|
187 |
||
93 | 188 |
function text(text) { |
84 | 189 |
text.attr("x", function(d) { return x(d.x) + 6; }) |
190 |
.attr("y", function(d) { return y(d.y) + 6; }); |
|
191 |
} |
|
192 |
||
93 | 193 |
function rect(rect) { |
84 | 194 |
rect.attr("x", function(d) { return x(d.x); }) |
195 |
.attr("y", function(d) { return y(d.y); }) |
|
196 |
.attr("width", function(d) { return x(d.x + d.dx) - x(d.x); }) |
|
197 |
.attr("height", function(d) { return y(d.y + d.dy) - y(d.y); }) |
|
123
4c97e9da1416
Remove use of global variables + remove ember-cli-content-security-policy from package.json to avoid bogus csp violation warnings
ymh <ymh.work@gmail.com>
parents:
95
diff
changeset
|
198 |
.attr("fill", function(d) { return (d.color || "#bbb"); }); |
84 | 199 |
} |
200 |
||
201 |
function name(d) { |
|
92 | 202 |
return d.parent ? name(d.parent) + "." + d.name : d.name; |
84 | 203 |
} |
39
c693e8673d5b
add visu-langue with google treemap
nowmad@23.1.168.192.in-addr.arpa
parents:
diff
changeset
|
204 |
}); |
c693e8673d5b
add visu-langue with google treemap
nowmad@23.1.168.192.in-addr.arpa
parents:
diff
changeset
|
205 |
} |
c693e8673d5b
add visu-langue with google treemap
nowmad@23.1.168.192.in-addr.arpa
parents:
diff
changeset
|
206 |
}); |