Debian Codegraph @

bqplot/js/src/Scatter.ts

raw
1 /* Copyright 2015 Bloomberg Finance L.P.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 import { ScatterBase } from './ScatterBase';
16 import * as markers from './Markers';
17 import * as d3 from 'd3';
18 // const d3 =Object.assign({}, require("d3-selection"));
19
20 const bqSymbol: any = markers.symbol;
21
22 export class Scatter extends ScatterBase {
23 render() {
24 this.dot = bqSymbol()
25 .type(this.model.get('marker'))
26 .size(this.model.get('default_size'))
27 .skew(this.model.get('default_skew'));
28
29 return super.render();
30 }
31
32 create_listeners() {
33 super.create_listeners();
34 this.listenTo(this.model, 'change:colors', this.update_colors);
35 this.listenTo(this.model, 'change:stroke', this.update_stroke);
36 this.listenTo(this.model, 'change:stroke_width', this.update_stroke_width);
37 this.listenTo(this.model, 'change:opacities', this.update_opacities);
38 this.listenTo(this.model, 'change:default_skew', this.update_default_skew);
39 this.listenTo(this.model, 'change:marker', this.update_marker);
40 this.listenTo(this.model, 'change:default_size', this.update_default_size);
41 this.listenTo(this.model, 'change:fill', this.update_fill);
42 this.listenTo(this.model, 'change:display_names', this.update_names);
43 this.listenTo(
44 this.model,
45 'change:label_display_horizontal_offset change:label_display_vertical_offset',
46 this.update_names
47 );
48 }
49
50 update_colors(model, new_colors) {
51 if (!this.model.dirty) {
52 const stroke = this.model.get('stroke');
53 const len = new_colors.length;
54 this.d3el
55 .selectAll('.dot')
56 .style(
57 'fill',
58 this.model.get('fill') ? this.get_mark_color.bind(this) : 'none'
59 )
60 .style('stroke', stroke ? stroke : this.get_mark_color.bind(this));
61
62 if (this.legend_el) {
63 this.legend_el
64 .select('path')
65 .style('fill', (d, i) => {
66 return new_colors[i % len];
67 })
68 .style(
69 'stroke',
70 stroke
71 ? stroke
72 : (d, i) => {
73 return new_colors[i % len];
74 }
75 );
76 this.legend_el.select('text').style(
77 'fill',
78 this.model.get('fill')
79 ? (d, i) => {
80 return new_colors[i % len];
81 }
82 : 'none'
83 );
84 }
85 }
86 this.apply_styles();
87 }
88
89 update_fill(model, fill) {
90 const colors = this.model.get('colors');
91 const len = colors.length;
92 this.d3el
93 .selectAll('.dot')
94 .style('fill', fill ? this.get_mark_color.bind(this) : 'none');
95 if (this.legend_el) {
96 this.legend_el.selectAll('path').style(
97 'fill',
98 fill
99 ? (d, i) => {
100 return colors[i % len];
101 }
102 : 'none'
103 );
104 }
105 }
106
107 update_stroke_width() {
108 const stroke_width = this.model.get('stroke_width');
109
110 this.d3el.selectAll('.dot').style('stroke-width', stroke_width);
111
112 if (this.legend_el) {
113 this.legend_el.selectAll('path').style('stroke-width', stroke_width);
114 }
115 }
116
117 update_stroke(model, fill) {
118 const stroke = this.model.get('stroke');
119 this.d3el
120 .selectAll('.dot')
121 .style('stroke', stroke ? stroke : this.get_mark_color.bind(this));
122
123 if (this.legend_el) {
124 this.legend_el.selectAll('path').style('stroke', stroke);
125 }
126 }
127
128 update_opacities(animate) {
129 if (!this.model.dirty) {
130 const opacities = this.model.get('opacities');
131 const colors = this.model.get('colors');
132 const len = colors.length;
133 const len_opac = opacities.length;
134 const animation_duration =
135 animate === true ? this.parent.model.get('animation_duration') : 0;
136
137 // update opacity scale range?
138 const that = this;
139 this.d3el
140 .selectAll('.dot')
141 .transition('update_opacities')
142 .duration(animation_duration)
143 .style('opacity', (d, i) => {
144 return that.get_element_opacity(d, i);
145 });
146 if (this.legend_el) {
147 this.legend_el
148 .select('path')
149 .style('opacity', (d, i) => {
150 return opacities[i % len_opac];
151 })
152 .style('fill', (d, i) => {
153 return colors[i % len];
154 });
155 }
156 }
157 }
158
159 update_marker(model, marker) {
160 if (!this.model.dirty) {
161 this.d3el
162 .selectAll('.dot')
163 .transition('update_marker')
164 .duration(this.parent.model.get('animation_duration'))
165 .attr('d', this.dot.type(marker));
166 if (this.legend_el) {
167 this.legend_el.select('path').attr('d', this.dot.type(marker));
168 }
169 }
170 }
171
172 update_default_skew(animate) {
173 if (!this.model.dirty) {
174 const animation_duration =
175 animate === true ? this.parent.model.get('animation_duration') : 0;
176 const that = this;
177 this.d3el
178 .selectAll('.dot')
179 .transition('update_default_skew')
180 .duration(animation_duration)
181 .attr(
182 'd',
183 this.dot.skew((d) => {
184 return that.get_element_skew(d);
185 })
186 );
187 }
188 }
189
190 update_default_size(animate) {
191 this.compute_view_padding();
192 // update size scale range?
193 if (!this.model.dirty) {
194 const animation_duration =
195 animate === true ? this.parent.model.get('animation_duration') : 0;
196 const that = this;
197 this.d3el
198 .selectAll('.dot')
199 .transition('update_default_size')
200 .duration(animation_duration)
201 .attr(
202 'd',
203 this.dot.size((d) => {
204 return that.get_element_size(d);
205 })
206 );
207 // Label positions also need to change
208 this.update_names(animate);
209 }
210 }
211
212 update_names(animate) {
213 const that = this;
214 const names = this.model.get('names') || [];
215 const show_names = this.model.get('display_names') && names.length !== 0;
216 const animation_duration = animate
217 ? this.parent.model.get('animation_duration')
218 : 0;
219
220 this.d3el
221 .selectAll('.object_grp')
222 .select('text')
223 .text((d: any) => {
224 return d.name;
225 })
226 .transition('update_names')
227 .duration(animation_duration)
228 .attr('transform', (d) => {
229 const label_display_horizontal_offset = that.model.get(
230 'label_display_horizontal_offset'
231 );
232 const label_display_vertical_offset = that.model.get(
233 'label_display_vertical_offset'
234 );
235 const text_loc = Math.sqrt(that.get_element_size(d)) / 2.0;
236 return (
237 'translate(' +
238 (label_display_horizontal_offset
239 ? label_display_horizontal_offset
240 : text_loc) +
241 ',' +
242 (label_display_vertical_offset
243 ? -label_display_vertical_offset
244 : -text_loc) +
245 ')'
246 );
247 })
248 .attr('display', (d) => {
249 return show_names ? 'inline' : 'none';
250 });
251 }
252
253 color_scale_updated(animate) {
254 const fill = this.model.get('fill'),
255 stroke = this.model.get('stroke');
256 const animation_duration =
257 animate === true ? this.parent.model.get('animation_duration') : 0;
258
259 this.d3el
260 .selectAll('.object_grp')
261 .select('path')
262 .transition('color_scale_updated')
263 .duration(animation_duration)
264 .style('fill', fill ? this.get_mark_color.bind(this) : 'none')
265 .style('stroke', stroke ? stroke : this.get_mark_color.bind(this));
266 }
267
268 draw_elements(animate, elements_added) {
269 const that = this;
270
271 const animation_duration =
272 animate === true ? this.parent.model.get('animation_duration') : 0;
273 const elements = this.d3el.selectAll('.object_grp');
274
275 elements_added.append('path').attr('class', 'dot element');
276 elements_added.append('text').attr('class', 'dot_text');
277 elements
278 .select('path')
279 .transition('draw_elements')
280 .duration(animation_duration)
281 .attr(
282 'd',
283 this.dot
284 .size((d) => {
285 return that.get_element_size(d);
286 })
287 .skew((d) => {
288 return that.get_element_skew(d);
289 })
290 );
291
292 this.update_names(animate);
293 this.apply_styles();
294 }
295
296 draw_legend_elements(elements_added, rect_dim) {
297 const colors = this.model.get('colors'),
298 len = colors.length,
299 stroke = this.model.get('stroke');
300
301 elements_added
302 .append('path')
303 .attr('transform', (d, i) => {
304 return 'translate( ' + rect_dim / 2 + ', ' + rect_dim / 2 + ')';
305 })
306 .attr('d', this.dot.size(64))
307 .style(
308 'fill',
309 this.model.get('fill')
310 ? (d, i) => {
311 return colors[i % len];
312 }
313 : 'none'
314 )
315 .style(
316 'stroke',
317 stroke
318 ? stroke
319 : (d, i) => {
320 return colors[i % len];
321 }
322 );
323 }
324
325 set_default_style(indices) {
326 // For all the elements with index in the list indices, the default
327 // style is applied.
328 if (!indices || indices.length === 0) {
329 return;
330 }
331 const elements = this.d3el.selectAll('.element').filter((data, index) => {
332 return indices.indexOf(index) !== -1;
333 });
334 const fill = this.model.get('fill'),
335 stroke = this.model.get('stroke'),
336 stroke_width = this.model.get('stroke_width'),
337 that = this;
338 elements
339 .style('fill', fill ? this.get_mark_color.bind(this) : 'none')
340 .style('stroke', stroke ? stroke : this.get_mark_color.bind(this))
341 .style('opacity', (d, i) => {
342 return that.get_element_opacity(d, i);
343 })
344 .style('stroke-width', stroke_width);
345 }
346
347 set_drag_style(d, i, dragged_node) {
348 d3.select(dragged_node)
349 .select('path')
350 .classed('drag_scatter', true)
351 .transition('set_drag_style')
352 .attr('d', this.dot.size(5 * this.model.get('default_size')));
353
354 const drag_color = this.model.get('drag_color');
355 if (drag_color) {
356 d3.select(dragged_node)
357 .select('path')
358 .style('fill', drag_color)
359 .style('stroke', drag_color);
360 }
361 }
362
363 reset_drag_style(d, i, dragged_node) {
364 const stroke = this.model.get('stroke'),
365 original_color = this.get_mark_color(d, i);
366
367 d3.select(dragged_node)
368 .select('path')
369 .classed('drag_scatter', false)
370 .transition('reset_drag_style')
371 .attr('d', this.dot.size(this.get_element_size(d)));
372
373 if (this.model.get('drag_color')) {
374 d3.select(dragged_node)
375 .select('path')
376 .style('fill', original_color)
377 .style('stroke', stroke ? stroke : original_color);
378 }
379 }
380
381 dot: any;
382 }