This repository was archived by the owner on Dec 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathvistk-scatterplot.js
More file actions
224 lines (216 loc) · 8.38 KB
/
Copy pathvistk-scatterplot.js
File metadata and controls
224 lines (216 loc) · 8.38 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
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
import Ember from 'ember';
import numeral from 'numeral';
import ENV from '../config/environment';
const {computed, observer, $, get} = Ember;
const {apiURL} = ENV;
export default Ember.Component.extend({
i18n: Ember.inject.service(),
tagName: 'div',
height: 500,
varIndependent: 'code',
attributeBindings: ['width','height'],
classNames: ['buildermod__viz--white','buildermod__viz','scatterplot'],
id: computed('elementId', function() {
return `#${this.get('elementId')}`;
}),
config: computed('data.@each', 'dataType','eciValue','i18n.locale', function() {
let eci = this.get('eciValue');
let lang = this.get('i18n.locale') === 'en-col' ? 'en_EN': 'es_ES';
let keyFilter = this.get('keyFilter') || [];
let format = function(value) { return numeral(value).format('0.00'); };
return {
type: 'scatterplot',
margin: {top: 10, right: 20, bottom: 30, left: 30},
height: this.get('height'),
width: this.get('width'),
container: this.get('id'),
data: this.get('data'),
var_id: this.get('varIndependent'),
var_x: 'distance',
var_y: 'complexity',
var_r: this.get('varSize'),
var_group: 'group',
radius_min: 2,
radius_max: 10,
x_domain: this.get('x_domain'),
y_domain: this.get('y_domain'),
r_domain: this.get('r_domain'),
x_format: format,
y_format: format,
duration: 0,
var_text: this.get('varIndependent'),
x_text_custom: this.get('i18n').t('graph_builder.table.distance').string,
y_text_custom: this.get('i18n').t('graph_builder.table.complexity').string,
items: [{
marks: [{
type: 'circle',
fill: (d) => { return d.color ? d.color : '#ccc1b9'; }
}, {
var_mark: '__highlighted',
type: d3.scale.ordinal().domain([true, false]).range(['line_horizontal', 'none']),
offset_right: function(d, i, vars) {
return vars.x_scale[0]['func'].range()[1] - vars.x_scale[0]['func'](d[vars.var_x]) + vars.r_scale(d[vars.var_r]);
}
}, {
var_mark: '__highlighted',
type: d3.scale.ordinal().domain([true, false]).range(['line_vertical', 'none']),
offset_top: function(d, i, vars) {
return vars.r_scale(d[vars.var_r]);
}
}, {
var_mark: '__highlighted',
type: d3.scale.ordinal().domain([true, false]).range(['rect', 'none']),
translate: function(d, i, vars) {
return [-vars.x_scale[0]['func'](d[vars.var_x]) - 20, -10];
},
height: 25,
width: 50,
stroke: 'black',
stroke_width: '1.5px',
fill: function() { return 'white'; }
}, {
var_mark: '__highlighted',
type: d3.scale.ordinal().domain([true, false]).range(['text', 'none']),
translate: function(d, i, vars) {
return [-vars.x_scale[0]['func'](d[vars.var_x]) + 25, 0];
},
text_anchor: 'end',
text: function(d, i, vars) {
return format(d[vars.var_y]);
}
}, {
var_mark: '__highlighted',
type: d3.scale.ordinal().domain([true, false]).range(['rect', 'none']),
translate: function(d, i, vars) {
return [-25, -vars.y_scale[0]['func'](d[vars.var_y]) + vars.height - vars.margin.bottom - vars.margin.top];
},
height: 25,
width: 50,
stroke: 'black',
stroke_width: '1.5px',
fill: function() { return 'white'; }
}, {
var_mark: '__highlighted',
type: d3.scale.ordinal().domain([true, false]).range(['text', 'none']),
translate: function(d, i, vars) {
return [0, -vars.y_scale[0]['func'](d[vars.var_y]) + vars.height - vars.margin.bottom - vars.margin.top + 10];
},
text_anchor: 'middle',
text: function(d, i, vars) {
return format(d[vars.var_x]);
}
}, {
var_mark: '__highlighted',
type: d3.scale.ordinal().domain([false, true]).range(['none', 'div']),
class: function() {
return 'tooltip';
},
x: function(d, i, vars) {
return vars.x_scale[0]['func'](d[vars.var_x]) + vars.margin.left;
},
y: function(d, i, vars) {
return vars.y_scale[0]['func'](d[vars.var_y]);
},
text: (d) => {
var data = [{
'key': 'rca',
'value': get(d,this.get('rca'))
},{
'key': 'cog',
'value':get(d,'cog')
}];
var textItem = get(d, `name_short_${this.get('i18n').display}`) || d.code;
var tooltip_text = `<span style="color:${get(d, 'color')}">${textItem} - ${get(d, 'code')}</span>`;
data.forEach((datum) => {
if(datum.key) {
tooltip_text += '<br>' + this.get('i18n').t(`graph_builder.table.${get(datum,'key')}`) + ': ' + format(get(datum,'value'));
}
});
return tooltip_text;
},
translate: [0, 0],
width: 200,
height: 'auto'
}, {
type: 'line_horizontal',
filter: function(d, i) {
return typeof eci !== 'undefined' && i === 0;
},
offset_y: function(d, i, vars) {
return -(vars.y_scale[0]['func'](d[vars.var_y]) - vars.y_scale[0]['func'](eci));
}
}, {
type: 'text',
filter: function(d, i) {
return typeof eci !== 'undefined' && i === 0;
},
text: function() {
var label = lang === 'en_EN' ? 'Average complexity': 'Complejidad media';
return label + ': ' + format(eci);
},
text_anchor: 'end',
offset_y: function(d, i, vars) {
return -(vars.y_scale[0]['func'](d[vars.var_y]) - vars.y_scale[0]['func'](eci)) - 10;
},
offset_x: function(d, i, vars) {
return vars.x_scale[0]['func'].range()[1] - vars.x_scale[0]['func'](d[vars.var_x]);
}
}]
}],
filter: keyFilter,
lang: lang
}
}),
scatter: computed('data.@each', 'dataType','eciValue','i18n.locale', function() {
return vistk.viz().params(this.get('config'));
}),
varSize: computed('dataType', function() {
if(this.get('dataType') === 'products') { return 'cog'; }
if(this.get('dataType') === 'industries') { return 'cog'; }
}),
rca: computed('dataType', function() {
if(this.get('dataType') === 'products') { return 'export_rca'; }
if(this.get('dataType') === 'industries') { return 'rca'; }
}),
didInsertElement: function() {
$.getJSON(`${apiURL}/data/location?level=department`).then((response) => {
let id = this.get('entityId');
let year = this.get('endDate');
let data = get(response, 'data');
let datum = _.first(_.filter(data, {'year': parseInt(year), 'department_id': parseInt(id) }));
this.set('width', this.$().parent().width());
if(this.get('dataType') === 'products' && datum) {
this.set('eciValue', get(datum, 'eci'));
}
this.set('x_domain', vistk.utils.extent(this.get('immutableData'), 'distance'));
this.set('y_domain', vistk.utils.extent(this.get('immutableData'), 'complexity'));
this.set('r_domain', vistk.utils.extent(this.get('immutableData'), this.get('varSize')));
d3.select(this.get('id')).call(this.get('scatter'));
});
},
willDestroyElement: function() {
this.set('scatter', null);
this.removeObserver('i18n.locale', this, this.update);
this.removeObserver('data.[]', this, this.update);
},
refresh: observer('keyFilter', function() {
if(!this.element){ return ; } //do not redraw if not there
let keyFilter = this.get('keyFilter') || [];
Ember.run.later(this , function() {
if(this.get('scatter')) {
this.get('scatter').params({filter: keyFilter});
this.get('scatter').params().refresh = true;
d3.select(this.get('id')).call(this.get('scatter'));
}
});
}),
update: observer('data.@each', 'varRca', 'i18n.locale', 'dataType', function() {
if(!this.element){ return ; } //do not redraw if not there
d3.select(this.get('id')).select('svg').remove();
Ember.run.later(this , function() {
if(this.get('scatter')) {
d3.select(this.get('id')).call(this.get('scatter'));
}
});
})
});