author | Chloe Laisne <chloe.laisne@gmail.com> |
Tue, 18 Oct 2016 19:46:23 +0530 | |
changeset 359 | 6274a5fdd348 |
parent 344 | 70451a4dc9ae |
child 392 | 4fbe94af93e8 |
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({ |
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
7 |
|
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
8 |
constants: Ember.inject.service(), |
198 | 9 |
filter: Ember.inject.service(), |
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
10 |
|
295
e1f154f20f96
Add selected state to treemap
Chloe Laisne <chloe.laisne@gmail.com>
parents:
294
diff
changeset
|
11 |
filterObserver: Ember.observer('filter.language', function() { |
e1f154f20f96
Add selected state to treemap
Chloe Laisne <chloe.laisne@gmail.com>
parents:
294
diff
changeset
|
12 |
Ember.$('.node').removeClass("selected"); |
e1f154f20f96
Add selected state to treemap
Chloe Laisne <chloe.laisne@gmail.com>
parents:
294
diff
changeset
|
13 |
Ember.$('.node[data-id="' + this.get('filter').get('language') + '"]').addClass("selected"); |
e1f154f20f96
Add selected state to treemap
Chloe Laisne <chloe.laisne@gmail.com>
parents:
294
diff
changeset
|
14 |
}), |
e1f154f20f96
Add selected state to treemap
Chloe Laisne <chloe.laisne@gmail.com>
parents:
294
diff
changeset
|
15 |
|
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
16 |
didInsertElement: function(){ |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
17 |
var self = this; |
319
78990a8a069b
Work on front and back integration, correct the expected data format
ymh <ymh.work@gmail.com>
parents:
318
diff
changeset
|
18 |
var baseurl = (ENV.APP.backRootURL || ENV.rootURL).replace(/\/$/,"")+'/api/v1'; |
84 | 19 |
|
319
78990a8a069b
Work on front and back integration, correct the expected data format
ymh <ymh.work@gmail.com>
parents:
318
diff
changeset
|
20 |
d3.json(baseurl+"/stats/languages", function(data) { |
286
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
21 |
var margin = { top: 30, right: 0, bottom: 0, left: 0 }; |
306
3fccf43160a7
Some more changes linked to the change of api organization + some jshint error cleaning
ymh <ymh.work@gmail.com>
parents:
305
diff
changeset
|
22 |
var width = Ember.$('#' + self.get('elementId')).width(); |
3fccf43160a7
Some more changes linked to the change of api organization + some jshint error cleaning
ymh <ymh.work@gmail.com>
parents:
305
diff
changeset
|
23 |
var height = Ember.$('#' + self.get('elementId')).height() - margin.top - margin.bottom; |
84 | 24 |
|
319
78990a8a069b
Work on front and back integration, correct the expected data format
ymh <ymh.work@gmail.com>
parents:
318
diff
changeset
|
25 |
var languages = data['languages']; |
286
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
26 |
var array = Object.keys(languages).map(function (key) { return languages[key]; }); |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
27 |
var oldMin = Math.min(...array), |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
28 |
oldMax = Math.max(...array); |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
29 |
var sum = array.reduce(function(a, b) { return a + b; }); |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
30 |
var average = sum / array.length; |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
31 |
var newMin = Math.floor((average - oldMin)), |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
32 |
newMax = Math.floor((oldMax - average)); |
305
ff6cf3fc5f40
api reorganisation and propagation if the changes + some code cleaning
ymh <ymh.work@gmail.com>
parents:
295
diff
changeset
|
33 |
|
84 | 34 |
|
286
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
35 |
var x = d3.scale.linear() |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
36 |
.domain([0, width]) |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
37 |
.range([0, width]), |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
38 |
y = d3.scale.linear() |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
39 |
.domain([0, height]) |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
40 |
.range([0, height]); |
84 | 41 |
|
286
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
42 |
var treemap = d3.layout.treemap() |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
43 |
.children(function(d, depth) { return depth ? null : d._children; }) |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
44 |
.sort(function(a, b) { return a.value - b.value; }) |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
45 |
.value(function(d){ |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
46 |
return Math.floor((((d.value - oldMin) * (newMax - newMin)) / (oldMax - oldMin)) + newMin); |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
47 |
}) |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
48 |
.round(false); |
84 | 49 |
|
344 | 50 |
console.log('width', width, Ember.$('#' + self.get('elementId')).parent().width(), Ember.$('#' + self.get('elementId')).parent().parent().width(), Ember.$('#' + self.get('elementId')).parent().parent().parent().width()); |
286
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
51 |
var element = d3.select('#' + self.get('elementId')) |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
52 |
.style("width", width + margin.left + margin.right + 'px') |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
53 |
.style("height", height + margin.bottom + margin.top + 'px') |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
54 |
.style("margin-left", -margin.left + "px") |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
55 |
.style("margin-right", -margin.right + "px") |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
56 |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")") |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
57 |
.style("shape-rendering", "crispEdges"); |
84 | 58 |
|
286
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
59 |
var breadcrumbs = element.insert("div", ":first-child") |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
60 |
.attr("class", "breadcrumbs") |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
61 |
.attr("y", -margin.top) |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
62 |
.style("width", width + 'px') |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
63 |
.style("height", margin.top + 'px'); |
126
e87a340711a4
improve on dataloading. add fixture management with proper interface to load data.
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
64 |
|
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
65 |
var root = _.cloneDeep(self.constants.LANGUAGES_TREEMAP); |
286
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
66 |
root.x = root.y = 0; |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
67 |
root.dx = width; |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
68 |
root.dy = height; |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
69 |
root.depth = 0; |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
70 |
var transitioning = false; |
126
e87a340711a4
improve on dataloading. add fixture management with proper interface to load data.
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
71 |
|
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
72 |
accumulate(root); |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
73 |
layout(root); |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
74 |
display(root); |
84 | 75 |
|
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
76 |
function accumulate(d) { |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
77 |
d._children = d.children; |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
78 |
if(d.children) { |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
79 |
d.value = d.children.reduce(function(p, v) { return p + accumulate(v); }, 0); |
329
0a2c2ad49d75
Improvce language visualization. Generalize language node selection, change language query parameters, add resolution of node name (corpus-) to lexvo controler
ymh <ymh.work@gmail.com>
parents:
319
diff
changeset
|
80 |
} else if (d.values) { |
0a2c2ad49d75
Improvce language visualization. Generalize language node selection, change language query parameters, add resolution of node name (corpus-) to lexvo controler
ymh <ymh.work@gmail.com>
parents:
319
diff
changeset
|
81 |
d.value = d.values.reduce(function(p, v) { return p + (languages[v] ? languages[v] : 0); }, 0); |
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
82 |
} else { |
286
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
83 |
d.value = languages[d.id] ? languages[d.id] : 0; |
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
84 |
} |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
85 |
return d.value; |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
86 |
} |
84 | 87 |
|
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
88 |
function layout(d) { |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
89 |
if (d._children) { |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
90 |
treemap.nodes({_children: d._children}); |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
91 |
d._children.forEach(function(c) { |
286
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
92 |
function getCount(node, count = 0) { |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
93 |
var c = languages[node.id]; |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
94 |
if(typeof c === 'undefined') { |
329
0a2c2ad49d75
Improvce language visualization. Generalize language node selection, change language query parameters, add resolution of node name (corpus-) to lexvo controler
ymh <ymh.work@gmail.com>
parents:
319
diff
changeset
|
95 |
if(node._children) { |
0a2c2ad49d75
Improvce language visualization. Generalize language node selection, change language query parameters, add resolution of node name (corpus-) to lexvo controler
ymh <ymh.work@gmail.com>
parents:
319
diff
changeset
|
96 |
node._children.forEach(function(child) { |
0a2c2ad49d75
Improvce language visualization. Generalize language node selection, change language query parameters, add resolution of node name (corpus-) to lexvo controler
ymh <ymh.work@gmail.com>
parents:
319
diff
changeset
|
97 |
count = getCount(child, count); |
0a2c2ad49d75
Improvce language visualization. Generalize language node selection, change language query parameters, add resolution of node name (corpus-) to lexvo controler
ymh <ymh.work@gmail.com>
parents:
319
diff
changeset
|
98 |
}); |
0a2c2ad49d75
Improvce language visualization. Generalize language node selection, change language query parameters, add resolution of node name (corpus-) to lexvo controler
ymh <ymh.work@gmail.com>
parents:
319
diff
changeset
|
99 |
} else if(node.values) { |
0a2c2ad49d75
Improvce language visualization. Generalize language node selection, change language query parameters, add resolution of node name (corpus-) to lexvo controler
ymh <ymh.work@gmail.com>
parents:
319
diff
changeset
|
100 |
count = node.values.reduce(function(p, v) { return p + (languages[v] ? languages[v] : 0); }, count); |
0a2c2ad49d75
Improvce language visualization. Generalize language node selection, change language query parameters, add resolution of node name (corpus-) to lexvo controler
ymh <ymh.work@gmail.com>
parents:
319
diff
changeset
|
101 |
} |
0a2c2ad49d75
Improvce language visualization. Generalize language node selection, change language query parameters, add resolution of node name (corpus-) to lexvo controler
ymh <ymh.work@gmail.com>
parents:
319
diff
changeset
|
102 |
return count; |
286
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
103 |
} else { |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
104 |
return count + c; |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
105 |
} |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
106 |
} |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
107 |
c.count = getCount(c); |
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
108 |
c.x = d.x + c.x * d.dx; |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
109 |
c.y = d.y + c.y * d.dy; |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
110 |
c.dx *= d.dx; |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
111 |
c.dy *= d.dy; |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
112 |
c.parent = d; |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
113 |
layout(c); |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
114 |
}); |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
115 |
} |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
116 |
} |
84 | 117 |
|
287
4e199fd29689
Fix treemap transition
Chloe Laisne <chloe.laisne@gmail.com>
parents:
286
diff
changeset
|
118 |
function position() { |
4e199fd29689
Fix treemap transition
Chloe Laisne <chloe.laisne@gmail.com>
parents:
286
diff
changeset
|
119 |
return this.style("width", function(d) { return x(d.x + d.dx) - x(d.x) + 'px'; }) |
4e199fd29689
Fix treemap transition
Chloe Laisne <chloe.laisne@gmail.com>
parents:
286
diff
changeset
|
120 |
.style("height", function(d) { return y(d.y + d.dy) - y(d.y) + 'px'; }) |
4e199fd29689
Fix treemap transition
Chloe Laisne <chloe.laisne@gmail.com>
parents:
286
diff
changeset
|
121 |
.style("left", function(d) { return x(d.x) + 'px'; }) |
4e199fd29689
Fix treemap transition
Chloe Laisne <chloe.laisne@gmail.com>
parents:
286
diff
changeset
|
122 |
.style("top", function(d) { return y(d.y) + 'px'; }); |
4e199fd29689
Fix treemap transition
Chloe Laisne <chloe.laisne@gmail.com>
parents:
286
diff
changeset
|
123 |
} |
4e199fd29689
Fix treemap transition
Chloe Laisne <chloe.laisne@gmail.com>
parents:
286
diff
changeset
|
124 |
|
313
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
125 |
function hexadecimalToInteger(hexadecimal) { |
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
126 |
var integer = []; |
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
127 |
for(var i = 1; i < 7; i += 2) { |
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
128 |
integer.push(parseInt(hexadecimal.slice(i, i + 2), 16)); |
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
129 |
} |
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
130 |
return integer; |
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
131 |
} |
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
132 |
|
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
133 |
function display(d) { |
286
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
134 |
breadcrumbs |
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
135 |
.datum(d.parent) |
286
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
136 |
.html(name(d)) |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
137 |
.on("click", transition); |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
138 |
|
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
139 |
var nodes = element.append("div") |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
140 |
.attr("class", "nodes") |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
141 |
.datum(d); |
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
142 |
|
286
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
143 |
var node = nodes.selectAll() |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
144 |
.data(d._children) |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
145 |
.enter() |
295
e1f154f20f96
Add selected state to treemap
Chloe Laisne <chloe.laisne@gmail.com>
parents:
294
diff
changeset
|
146 |
.append("div") |
e1f154f20f96
Add selected state to treemap
Chloe Laisne <chloe.laisne@gmail.com>
parents:
294
diff
changeset
|
147 |
.attr("data-id", function(d) { return d.id; }); |
39
c693e8673d5b
add visu-langue with google treemap
nowmad@23.1.168.192.in-addr.arpa
parents:
diff
changeset
|
148 |
|
313
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
149 |
var dMin = Math.min.apply(null, d._children.map(function(d){ return d.count; })); |
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
150 |
var dMax = Math.max.apply(null, d._children.map(function(d){ return d.count; })); |
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
151 |
|
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
152 |
function shade(d) { |
359 | 153 |
var color = "#777777"; |
313
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
154 |
var aColor = hexadecimalToInteger(color); |
359 | 155 |
var solidColor = "#333333"; |
313
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
156 |
var aSolidColor = hexadecimalToInteger(solidColor); |
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
157 |
var aFillColor = []; |
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
158 |
for(var i = 0; i < 3; i++) { |
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
159 |
aFillColor.push((d.count - dMin) * (aSolidColor[i] - aColor[i]) / (dMax - dMin) + aColor[i]); |
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
160 |
} |
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
161 |
return '#' + (aFillColor.map(i => parseInt(i).toString(16))).join(''); |
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
162 |
} |
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
163 |
|
295
e1f154f20f96
Add selected state to treemap
Chloe Laisne <chloe.laisne@gmail.com>
parents:
294
diff
changeset
|
164 |
node.attr("class", function(d) { return "node" + ( d.id === self.get('filter').get('language') ? " selected" : "" ); }) |
287
4e199fd29689
Fix treemap transition
Chloe Laisne <chloe.laisne@gmail.com>
parents:
286
diff
changeset
|
165 |
.call(position) |
313
706f10bcdc3c
Treemap shades to node count
Chloe Laisne <chloe.laisne@gmail.com>
parents:
295
diff
changeset
|
166 |
.style("background-color", function(d) { return shade(d); }) |
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
167 |
.on("click", selectHandler); |
84 | 168 |
|
286
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
169 |
node.filter(function(d) { return d._children; }) |
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
170 |
.classed("children", true) |
294
f3dae62a0d8a
Add folder icon to treemap
Chloe Laisne <chloe.laisne@gmail.com>
parents:
287
diff
changeset
|
171 |
.on("click", transition) |
f3dae62a0d8a
Add folder icon to treemap
Chloe Laisne <chloe.laisne@gmail.com>
parents:
287
diff
changeset
|
172 |
.append("i") |
f3dae62a0d8a
Add folder icon to treemap
Chloe Laisne <chloe.laisne@gmail.com>
parents:
287
diff
changeset
|
173 |
.attr("class", "fa fa-folder-o"); |
84 | 174 |
|
286
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
175 |
node.append("span") |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
176 |
.html(function(d) { return d.name + ' <span class="count">(' + d.count + ')</span>'; }); |
84 | 177 |
|
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
178 |
function transition(d) { |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
179 |
if (transitioning || !d) { |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
180 |
return; |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
181 |
} |
261
02e2396bcbbc
Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents:
198
diff
changeset
|
182 |
|
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
183 |
selectHandler(d); |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
184 |
transitioning = true; |
84 | 185 |
|
287
4e199fd29689
Fix treemap transition
Chloe Laisne <chloe.laisne@gmail.com>
parents:
286
diff
changeset
|
186 |
var newNode = display(d), |
4e199fd29689
Fix treemap transition
Chloe Laisne <chloe.laisne@gmail.com>
parents:
286
diff
changeset
|
187 |
transitionNodes = node.transition().duration(750), |
4e199fd29689
Fix treemap transition
Chloe Laisne <chloe.laisne@gmail.com>
parents:
286
diff
changeset
|
188 |
transitionNewNodes = newNode.transition().duration(750); |
84 | 189 |
|
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
190 |
x.domain([d.x, d.x + d.dx]); |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
191 |
y.domain([d.y, d.y + d.dy]); |
39
c693e8673d5b
add visu-langue with google treemap
nowmad@23.1.168.192.in-addr.arpa
parents:
diff
changeset
|
192 |
|
286
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
193 |
element.style("shape-rendering", null); |
84 | 194 |
|
287
4e199fd29689
Fix treemap transition
Chloe Laisne <chloe.laisne@gmail.com>
parents:
286
diff
changeset
|
195 |
element.selectAll(".node").sort(function(a, b) { return a.depth - b.depth; }); |
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
196 |
|
287
4e199fd29689
Fix treemap transition
Chloe Laisne <chloe.laisne@gmail.com>
parents:
286
diff
changeset
|
197 |
newNode.selectAll().style("fill-opacity", 0); |
84 | 198 |
|
287
4e199fd29689
Fix treemap transition
Chloe Laisne <chloe.laisne@gmail.com>
parents:
286
diff
changeset
|
199 |
transitionNodes.style("opacity", 0) |
4e199fd29689
Fix treemap transition
Chloe Laisne <chloe.laisne@gmail.com>
parents:
286
diff
changeset
|
200 |
.call(position); |
84 | 201 |
|
287
4e199fd29689
Fix treemap transition
Chloe Laisne <chloe.laisne@gmail.com>
parents:
286
diff
changeset
|
202 |
transitionNewNodes.style("opacity", 1) |
4e199fd29689
Fix treemap transition
Chloe Laisne <chloe.laisne@gmail.com>
parents:
286
diff
changeset
|
203 |
.call(position); |
286
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
204 |
|
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
205 |
transitionNodes.remove().each("end", function() { |
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
206 |
element.style("shape-rendering", "crispEdges"); |
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
207 |
transitioning = false; |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
208 |
}); |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
209 |
} |
84 | 210 |
|
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
211 |
function selectHandler (d){ |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
212 |
if (d.id){ |
198 | 213 |
self.get('filter').set('language', d.id); |
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
214 |
} |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
215 |
} |
84 | 216 |
|
286
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
217 |
return node; |
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
218 |
} |
84 | 219 |
|
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
220 |
function name(d) { |
286
57762c0b601e
Treemap layout: Resize node values to get bigger rect when value is smal + Create and display count + Node colors
Chloe Laisne <chloe.laisne@gmail.com>
parents:
261
diff
changeset
|
221 |
return d.parent ? name(d.parent) + '<span class="level">' + d.name + '</span>' : '<span class="root">' + d.name + '</span>'; |
188
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
222 |
} |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
223 |
|
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
224 |
}); |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
225 |
} |
d2cb39155997
/langue endpoint ID querystring parameter
Chloe Laisne <chloe.laisne@gmail.com>
parents:
142
diff
changeset
|
226 |
|
39
c693e8673d5b
add visu-langue with google treemap
nowmad@23.1.168.192.in-addr.arpa
parents:
diff
changeset
|
227 |
}); |