| 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 |
|
| 16 |
import * as d3 from 'd3'; |
| 17 |
// const d3 =Object.assign({}, require("d3-array"), require("d3-drag"), require("d3-selection"), require("d3-selection-multi")); |
| 18 |
const d3GetEvent = function () { |
| 19 |
return require('d3-selection').event; |
| 20 |
}.bind(this); |
| 21 |
import { Mark } from './Mark'; |
| 22 |
import { Scale } from './Scale'; |
| 23 |
import * as _ from 'underscore'; |
| 24 |
import { applyStyles } from './utils'; |
| 25 |
|
| 26 |
export abstract class ScatterBase extends Mark { |
| 27 |
render() { |
| 28 |
const base_creation_promise = super.render(); |
| 29 |
|
| 30 |
const that = this; |
| 31 |
// Warning: arrow functions actually breaks the drag |
| 32 |
this.drag_listener = d3 |
| 33 |
.drag() |
| 34 |
.subject((d: any) => { |
| 35 |
return { x: that.x_scale.scale(d.x), y: that.y_scale.scale(d.y) }; |
| 36 |
}) |
| 37 |
.on('start', function (d, i) { |
| 38 |
return that.drag_start(d, i, this); |
| 39 |
}) |
| 40 |
.on('drag', function (d, i) { |
| 41 |
return that.on_drag(d, i, this); |
| 42 |
}) |
| 43 |
.on('end', function (d, i) { |
| 44 |
return that.drag_ended(d, i, this); |
| 45 |
}); |
| 46 |
|
| 47 |
this.selected_style = this.model.get('selected_style'); |
| 48 |
this.unselected_style = this.model.get('unselected_style'); |
| 49 |
this.selected_indices = this.model.get('selected'); |
| 50 |
|
| 51 |
this.hovered_style = this.model.get('hovered_style'); |
| 52 |
this.unhovered_style = this.model.get('unhovered_style'); |
| 53 |
this.hovered_index = !this.model.get('hovered_point') |
| 54 |
? null |
| 55 |
: [this.model.get('hovered_point')]; |
| 56 |
|
| 57 |
this.display_el_classes = ['dot', 'legendtext']; //FIXME |
| 58 |
this.event_metadata = { |
| 59 |
mouse_over: { |
| 60 |
msg_name: 'hover', |
| 61 |
lookup_data: false, |
| 62 |
hit_test: true, |
| 63 |
}, |
| 64 |
legend_clicked: { |
| 65 |
msg_name: 'legend_click', |
| 66 |
hit_test: true, |
| 67 |
}, |
| 68 |
element_clicked: { |
| 69 |
msg_name: 'element_click', |
| 70 |
lookup_data: false, |
| 71 |
hit_test: true, |
| 72 |
}, |
| 73 |
parent_clicked: { |
| 74 |
msg_name: 'background_click', |
| 75 |
hit_test: false, |
| 76 |
}, |
| 77 |
}; |
| 78 |
this.displayed.then(() => { |
| 79 |
this.parent.tooltip_div.node().appendChild(this.tooltip_div.node()); |
| 80 |
this.create_tooltip(); |
| 81 |
}); |
| 82 |
|
| 83 |
return base_creation_promise.then(() => { |
| 84 |
this.event_listeners = {}; |
| 85 |
this.process_interactions(); |
| 86 |
this.create_listeners(); |
| 87 |
this.compute_view_padding(); |
| 88 |
this.draw(); |
| 89 |
}); |
| 90 |
} |
| 91 |
|
| 92 |
set_ranges() { |
| 93 |
const x_scale = this.scales.x, |
| 94 |
y_scale = this.scales.y, |
| 95 |
size_scale = this.scales.size, |
| 96 |
opacity_scale = this.scales.opacity, |
| 97 |
skew_scale = this.scales.skew, |
| 98 |
rotation_scale = this.scales.rotation; |
| 99 |
if (x_scale) { |
| 100 |
x_scale.set_range(this.parent.padded_range('x', x_scale.model)); |
| 101 |
} |
| 102 |
if (y_scale) { |
| 103 |
y_scale.set_range(this.parent.padded_range('y', y_scale.model)); |
| 104 |
} |
| 105 |
if (size_scale) { |
| 106 |
size_scale.set_range([0, this.model.get('default_size')]); |
| 107 |
} |
| 108 |
if (opacity_scale) { |
| 109 |
opacity_scale.set_range([0.2, 1]); |
| 110 |
} |
| 111 |
if (skew_scale) { |
| 112 |
skew_scale.set_range([0, 1]); |
| 113 |
} |
| 114 |
if (rotation_scale) { |
| 115 |
rotation_scale.set_range([0, 180]); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
set_positional_scales() { |
| 120 |
this.x_scale = this.scales.x; |
| 121 |
this.y_scale = this.scales.y; |
| 122 |
// If no scale for "x" or "y" is specified, figure scales are used. |
| 123 |
if (!this.x_scale) { |
| 124 |
this.x_scale = this.parent.scale_x; |
| 125 |
} |
| 126 |
if (!this.y_scale) { |
| 127 |
this.y_scale = this.parent.scale_y; |
| 128 |
} |
| 129 |
this.listenTo(this.x_scale, 'domain_changed', () => { |
| 130 |
if (!this.model.dirty) { |
| 131 |
const animate = true; |
| 132 |
this.update_position(animate); |
| 133 |
} |
| 134 |
}); |
| 135 |
this.listenTo(this.y_scale, 'domain_changed', () => { |
| 136 |
if (!this.model.dirty) { |
| 137 |
const animate = true; |
| 138 |
this.update_position(animate); |
| 139 |
} |
| 140 |
}); |
| 141 |
} |
| 142 |
|
| 143 |
initialize_additional_scales() { |
| 144 |
// function to create the additional scales and create the |
| 145 |
// listeners for the additional scales |
| 146 |
const color_scale = this.scales.color, |
| 147 |
size_scale = this.scales.size, |
| 148 |
opacity_scale = this.scales.opacity, |
| 149 |
skew_scale = this.scales.skew, |
| 150 |
rotation_scale = this.scales.rotation; |
| 151 |
// the following handlers are for changes in data that does not |
| 152 |
// impact the position of the elements |
| 153 |
if (color_scale) { |
| 154 |
this.listenTo(color_scale, 'domain_changed', () => { |
| 155 |
const animate = true; |
| 156 |
if (!this.model.dirty) { |
| 157 |
this.color_scale_updated(animate); |
| 158 |
} |
| 159 |
}); |
| 160 |
color_scale.on( |
| 161 |
'color_scale_range_changed', |
| 162 |
this.color_scale_updated, |
| 163 |
this |
| 164 |
); |
| 165 |
} |
| 166 |
if (size_scale) { |
| 167 |
this.listenTo(size_scale, 'domain_changed', () => { |
| 168 |
const animate = true; |
| 169 |
this.update_default_size(animate); |
| 170 |
}); |
| 171 |
} |
| 172 |
if (opacity_scale) { |
| 173 |
this.listenTo(opacity_scale, 'domain_changed', () => { |
| 174 |
const animate = true; |
| 175 |
this.update_opacities(animate); |
| 176 |
}); |
| 177 |
} |
| 178 |
if (skew_scale) { |
| 179 |
this.listenTo(skew_scale, 'domain_changed', () => { |
| 180 |
const animate = true; |
| 181 |
this.update_default_skew(animate); |
| 182 |
}); |
| 183 |
} |
| 184 |
if (rotation_scale) { |
| 185 |
this.listenTo(rotation_scale, 'domain_changed', () => { |
| 186 |
const animate = true; |
| 187 |
this.update_position(animate); |
| 188 |
}); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
create_listeners() { |
| 193 |
super.create_listeners(); |
| 194 |
this.d3el |
| 195 |
.on('mouseover', () => { |
| 196 |
this.event_dispatcher('mouse_over'); |
| 197 |
}) |
| 198 |
.on('mousemove', () => { |
| 199 |
this.event_dispatcher('mouse_move'); |
| 200 |
}) |
| 201 |
.on('mouseout', () => { |
| 202 |
this.event_dispatcher('mouse_out'); |
| 203 |
}); |
| 204 |
|
| 205 |
this.listenTo(this.model, 'data_updated', () => { |
| 206 |
//animate dots on data update |
| 207 |
const animate = true; |
| 208 |
this.draw(animate); |
| 209 |
}); |
| 210 |
this.listenTo(this.model, 'change:tooltip', this.create_tooltip); |
| 211 |
this.listenTo(this.model, 'change:enable_hover', () => { |
| 212 |
this.hide_tooltip(); |
| 213 |
}); |
| 214 |
this.listenTo(this.model, 'change:interactions', this.process_interactions); |
| 215 |
this.listenTo(this.model, 'change:enable_move', this.set_drag_behavior); |
| 216 |
this.listenTo(this.model, 'change:selected', this.update_selected); |
| 217 |
this.listenTo(this.model, 'change:hovered_point', this.update_hovered); |
| 218 |
this.listenTo( |
| 219 |
this.model, |
| 220 |
'change:hovered_style', |
| 221 |
this.hovered_style_updated |
| 222 |
); |
| 223 |
this.listenTo( |
| 224 |
this.model, |
| 225 |
'change:unhovered_style', |
| 226 |
this.unhovered_style_updated |
| 227 |
); |
| 228 |
this.listenTo(this.parent, 'bg_clicked', () => { |
| 229 |
this.event_dispatcher('parent_clicked'); |
| 230 |
}); |
| 231 |
} |
| 232 |
|
| 233 |
// The following functions are convenience functions to get |
| 234 |
// the opacity / size of an element given the data. |
| 235 |
// In fact they are more than convenience functions as they limit the |
| 236 |
// points of entry to that logic which makes it easier to manage and to |
| 237 |
// keep consistent across different places where we use it. |
| 238 |
get_element_size(data) { |
| 239 |
const size_scale = this.scales.size; |
| 240 |
if (size_scale && data.size !== undefined) { |
| 241 |
return size_scale.scale(data.size); |
| 242 |
} |
| 243 |
return this.model.get('default_size'); |
| 244 |
} |
| 245 |
|
| 246 |
get_element_opacity(data, index) { |
| 247 |
const opacity_scale = this.scales.opacity; |
| 248 |
const opacities = this.model.get('opacities'); |
| 249 |
const len = opacities.length; |
| 250 |
if (opacity_scale && data.opacity !== undefined) { |
| 251 |
return opacity_scale.scale(data.opacity); |
| 252 |
} |
| 253 |
return opacities[index % len]; |
| 254 |
} |
| 255 |
|
| 256 |
get_element_skew(data) { |
| 257 |
const skew_scale = this.scales.skew; |
| 258 |
if (skew_scale && data.skew !== undefined) { |
| 259 |
return skew_scale.scale(data.skew); |
| 260 |
} |
| 261 |
return this.model.get('default_skew'); |
| 262 |
} |
| 263 |
|
| 264 |
get_element_rotation(d) { |
| 265 |
const rotation_scale = this.scales.rotation; |
| 266 |
return !rotation_scale || !d.rotation |
| 267 |
? '' |
| 268 |
: 'rotate(' + rotation_scale.scale(d.rotation) + ')'; |
| 269 |
} |
| 270 |
|
| 271 |
relayout() { |
| 272 |
this.set_ranges(); |
| 273 |
this.update_position(); |
| 274 |
} |
| 275 |
|
| 276 |
update_position(animate?) { |
| 277 |
const x_scale = this.scales.x, |
| 278 |
y_scale = this.scales.y; |
| 279 |
const animation_duration = |
| 280 |
animate === true ? this.parent.model.get('animation_duration') : 0; |
| 281 |
|
| 282 |
this.d3el |
| 283 |
.selectAll('.object_grp') |
| 284 |
.transition('update_position') |
| 285 |
.duration(animation_duration) |
| 286 |
.attr('transform', (d: any) => { |
| 287 |
return ( |
| 288 |
'translate(' + |
| 289 |
(x_scale.scale(d.x) + x_scale.offset) + |
| 290 |
',' + |
| 291 |
(y_scale.scale(d.y) + y_scale.offset) + |
| 292 |
')' + |
| 293 |
this.get_element_rotation(d) |
| 294 |
); |
| 295 |
}); |
| 296 |
this.x_pixels = this.model.mark_data.map((el) => { |
| 297 |
return x_scale.scale(el.x) + x_scale.offset; |
| 298 |
}); |
| 299 |
this.y_pixels = this.model.mark_data.map((el) => { |
| 300 |
return y_scale.scale(el.y) + y_scale.offset; |
| 301 |
}); |
| 302 |
this.pixel_coords = this.model.mark_data.map((el) => { |
| 303 |
return [ |
| 304 |
x_scale.scale(el.x) + x_scale.offset, |
| 305 |
y_scale.scale(el.y) + y_scale.offset, |
| 306 |
]; |
| 307 |
}); |
| 308 |
} |
| 309 |
|
| 310 |
draw(animate?) { |
| 311 |
this.set_ranges(); |
| 312 |
|
| 313 |
const elements = this.d3el |
| 314 |
.selectAll('.object_grp') |
| 315 |
.data(this.model.mark_data, (d: any) => { |
| 316 |
return d.unique_id; |
| 317 |
}); |
| 318 |
|
| 319 |
const elements_added = elements |
| 320 |
.enter() |
| 321 |
.append('g') |
| 322 |
.attr('class', 'object_grp'); |
| 323 |
|
| 324 |
this.update_position(animate); |
| 325 |
|
| 326 |
this.set_drag_behavior(); |
| 327 |
elements_added.on('click', (d, i) => { |
| 328 |
this.event_dispatcher('element_clicked', { data: d, index: i }); |
| 329 |
}); |
| 330 |
elements_added.on('mouseover', (d, i) => { |
| 331 |
this.scatter_hover_handler({ data: d, index: i }); |
| 332 |
}); |
| 333 |
elements_added.on('mouseout', () => { |
| 334 |
this.reset_hover_points(); |
| 335 |
}); |
| 336 |
|
| 337 |
this.draw_elements(animate, elements_added); |
| 338 |
|
| 339 |
// Removed the transition on exit as it was causing issues. |
| 340 |
// Elements are not removed until the transition is complete and |
| 341 |
// hence the setting styles function doesn't behave as intended. |
| 342 |
// The only way to call the function after all of the elements are |
| 343 |
// removed is round-about and doesn't look very nice visually. |
| 344 |
elements.exit().remove(); |
| 345 |
} |
| 346 |
|
| 347 |
draw_elements(animate, elements_added) {} |
| 348 |
|
| 349 |
process_click(interaction) { |
| 350 |
super.process_click(interaction); |
| 351 |
switch (interaction) { |
| 352 |
case 'add': |
| 353 |
this.event_listeners.parent_clicked = this.add_element; |
| 354 |
this.event_listeners.element_clicked = () => {}; |
| 355 |
break; |
| 356 |
case 'delete': |
| 357 |
this.event_listeners.parent_clicked = () => {}; |
| 358 |
this.event_listeners.element_clicked = this.delete_element; |
| 359 |
break; |
| 360 |
case 'select': |
| 361 |
this.event_listeners.parent_clicked = this.reset_selection; |
| 362 |
this.event_listeners.element_clicked = this.scatter_click_handler; |
| 363 |
break; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
reset_hover_points() { |
| 368 |
this.model.set('hovered_point', null); |
| 369 |
this.hovered_index = null; |
| 370 |
this.touch(); |
| 371 |
} |
| 372 |
|
| 373 |
scatter_hover_handler(args) { |
| 374 |
const index = args.index; |
| 375 |
|
| 376 |
this.model.set('hovered_point', index, { updated_view: this }); |
| 377 |
this.touch(); |
| 378 |
} |
| 379 |
|
| 380 |
reset_selection() { |
| 381 |
this.model.set('selected', null); |
| 382 |
this.selected_indices = null; |
| 383 |
this.touch(); |
| 384 |
} |
| 385 |
|
| 386 |
scatter_click_handler(args) { |
| 387 |
const index: number = args.index; |
| 388 |
const idx = this.model.get('selected') || []; |
| 389 |
let selected: Array<number> = Array.from(idx); |
| 390 |
// index of bar i. Checking if it is already present in the list. |
| 391 |
const elem_index = selected.indexOf(index); |
| 392 |
// Replacement for "Accel" modifier. |
| 393 |
const accelKey = d3GetEvent().ctrlKey || d3GetEvent().metaKey; |
| 394 |
|
| 395 |
if (elem_index > -1 && accelKey) { |
| 396 |
// if the index is already selected and if accel key is |
| 397 |
// pressed, remove the element from the list |
| 398 |
selected.splice(elem_index, 1); |
| 399 |
} else { |
| 400 |
if (accelKey) { |
| 401 |
//If accel is pressed and the bar is not already selected |
| 402 |
//add the bar to the list of selected bars. |
| 403 |
selected.push(index); |
| 404 |
} |
| 405 |
// updating the array containing the bar indexes selected |
| 406 |
// and updating the style |
| 407 |
else { |
| 408 |
//if accel is not pressed, then clear the selected ones |
| 409 |
//and set the current element to the selected |
| 410 |
selected = []; |
| 411 |
selected.push(index); |
| 412 |
} |
| 413 |
} |
| 414 |
this.model.set( |
| 415 |
'selected', |
| 416 |
selected.length === 0 ? null : new Uint32Array(selected), |
| 417 |
{ updated_view: this } |
| 418 |
); |
| 419 |
this.touch(); |
| 420 |
let e = d3GetEvent(); |
| 421 |
if (!e) { |
| 422 |
e = window.event; |
| 423 |
} |
| 424 |
if (e.cancelBubble !== undefined) { |
| 425 |
// IE |
| 426 |
e.cancelBubble = true; |
| 427 |
} |
| 428 |
if (e.stopPropagation) { |
| 429 |
e.stopPropagation(); |
| 430 |
} |
| 431 |
e.preventDefault(); |
| 432 |
} |
| 433 |
|
| 434 |
// Hovered Style related functions |
| 435 |
hovered_style_updated(model, style) { |
| 436 |
this.hovered_style = style; |
| 437 |
this.clear_style(model.previous('hovered_style'), this.hovered_index); |
| 438 |
this.style_updated(style, this.hovered_index); |
| 439 |
} |
| 440 |
|
| 441 |
unhovered_style_updated(model, style) { |
| 442 |
this.unhovered_style = style; |
| 443 |
const hov_indices = this.hovered_index; |
| 444 |
const unhovered_indices = hov_indices |
| 445 |
? _.range(this.model.mark_data.length).filter((index) => { |
| 446 |
return hov_indices.indexOf(index) === -1; |
| 447 |
}) |
| 448 |
: []; |
| 449 |
this.clear_style(model.previous('unhovered_style'), unhovered_indices); |
| 450 |
this.style_updated(style, unhovered_indices); |
| 451 |
} |
| 452 |
|
| 453 |
draw_legend(elem, x_disp, y_disp, inter_x_disp, inter_y_disp) { |
| 454 |
this.legend_el = elem |
| 455 |
.selectAll('.legend' + this.uuid) |
| 456 |
.data([this.model.mark_data[0]]); |
| 457 |
const colors = this.model.get('colors'), |
| 458 |
len = colors.length; |
| 459 |
|
| 460 |
const rect_dim = inter_y_disp * 0.8; |
| 461 |
const legend = this.legend_el |
| 462 |
.enter() |
| 463 |
.append('g') |
| 464 |
.attr('class', 'legend' + this.uuid) |
| 465 |
.attr('transform', (d, i) => { |
| 466 |
return 'translate(0, ' + (i * inter_y_disp + y_disp) + ')'; |
| 467 |
}) |
| 468 |
.on('mouseover', () => { |
| 469 |
this.event_dispatcher('legend_mouse_over'); |
| 470 |
}) |
| 471 |
.on('mouseout', () => { |
| 472 |
this.event_dispatcher('legend_mouse_out'); |
| 473 |
}) |
| 474 |
.on('click', () => { |
| 475 |
this.event_dispatcher('legend_clicked'); |
| 476 |
}); |
| 477 |
|
| 478 |
this.draw_legend_elements(legend, rect_dim); |
| 479 |
|
| 480 |
legend |
| 481 |
.append('text') |
| 482 |
.attr('class', 'legendtext') |
| 483 |
.attr('x', rect_dim * 1.2) |
| 484 |
.attr('y', rect_dim / 2) |
| 485 |
.attr('dy', '0.35em') |
| 486 |
.text((d, i) => { |
| 487 |
return this.model.get('labels')[i]; |
| 488 |
}) |
| 489 |
.style('fill', (d, i) => { |
| 490 |
return colors[i % len]; |
| 491 |
}); |
| 492 |
|
| 493 |
this.legend_el = legend.merge(this.legend_el); |
| 494 |
|
| 495 |
const max_length = d3.max(this.model.get('labels'), (d: any) => { |
| 496 |
return Number(d.length); |
| 497 |
}); |
| 498 |
|
| 499 |
this.legend_el.exit().remove(); |
| 500 |
return [1, max_length]; |
| 501 |
} |
| 502 |
|
| 503 |
draw_legend_elements(elements_added, rect_dim) {} |
| 504 |
|
| 505 |
invert_point(pixel) { |
| 506 |
if (pixel === undefined) { |
| 507 |
this.model.set('selected', null); |
| 508 |
this.touch(); |
| 509 |
return; |
| 510 |
} |
| 511 |
|
| 512 |
const abs_diff = this.x_pixels.map((elem) => { |
| 513 |
return Math.abs(elem - pixel); |
| 514 |
}); |
| 515 |
const sel_index = abs_diff.indexOf(d3.min(abs_diff)); |
| 516 |
|
| 517 |
this.model.set('selected', new Uint32Array([sel_index])); |
| 518 |
this.touch(); |
| 519 |
} |
| 520 |
|
| 521 |
selector_changed(point_selector, rect_selector) { |
| 522 |
if (point_selector === undefined) { |
| 523 |
this.model.set('selected', null); |
| 524 |
this.touch(); |
| 525 |
return []; |
| 526 |
} |
| 527 |
const pixels = this.pixel_coords; |
| 528 |
const indices = new Uint32Array(_.range(pixels.length)); |
| 529 |
const selected = indices.filter((index) => { |
| 530 |
return point_selector(pixels[index]); |
| 531 |
}); |
| 532 |
this.model.set('selected', selected); |
| 533 |
this.touch(); |
| 534 |
} |
| 535 |
|
| 536 |
update_selected(model, value) { |
| 537 |
this.selected_indices = value; |
| 538 |
this.apply_styles(); |
| 539 |
} |
| 540 |
|
| 541 |
update_hovered(model, value) { |
| 542 |
this.hovered_index = value === null ? value : [value]; |
| 543 |
this.apply_styles(); |
| 544 |
} |
| 545 |
|
| 546 |
apply_styles(style_arr?) { |
| 547 |
if (style_arr === undefined || style_arr == null) { |
| 548 |
style_arr = [ |
| 549 |
this.selected_style, |
| 550 |
this.unselected_style, |
| 551 |
this.hovered_style, |
| 552 |
this.unhovered_style, |
| 553 |
]; |
| 554 |
} |
| 555 |
super.apply_styles([style_arr]); |
| 556 |
|
| 557 |
const all_indices = _.range(this.model.mark_data.length); |
| 558 |
|
| 559 |
this.set_style_on_elements(this.hovered_style, this.hovered_index); |
| 560 |
const unhovered_indices = !this.hovered_index |
| 561 |
? [] |
| 562 |
: _.difference(all_indices, this.hovered_index); |
| 563 |
this.set_style_on_elements(this.unhovered_style, unhovered_indices); |
| 564 |
} |
| 565 |
|
| 566 |
clear_style(style_dict, indices) { |
| 567 |
// Function to clear the style of a dict on some or all the elements of the |
| 568 |
// chart. If indices is null, clears the style on all elements. If |
| 569 |
// not, clears on only the elements whose indices are matching. |
| 570 |
// |
| 571 |
// This function is not used right now. But it can be used if we |
| 572 |
// decide to accommodate more properties than those set by default. |
| 573 |
// Because those have to cleared specifically. |
| 574 |
let elements = this.d3el.selectAll('.element'); |
| 575 |
if (indices) { |
| 576 |
elements = elements.filter((d, index) => { |
| 577 |
return indices.indexOf(index) !== -1; |
| 578 |
}); |
| 579 |
} |
| 580 |
const clearing_style = {}; |
| 581 |
for (const key in style_dict) { |
| 582 |
clearing_style[key] = null; |
| 583 |
} |
| 584 |
applyStyles(elements, clearing_style); |
| 585 |
} |
| 586 |
|
| 587 |
set_style_on_elements(style, indices) { |
| 588 |
// If the index array is undefined or of length=0, exit the |
| 589 |
// function without doing anything |
| 590 |
if (!indices || indices.length === 0) { |
| 591 |
return; |
| 592 |
} |
| 593 |
// Also, return if the style object itself is blank |
| 594 |
if (Object.keys(style).length === 0) { |
| 595 |
return; |
| 596 |
} |
| 597 |
let elements = this.d3el.selectAll('.element'); |
| 598 |
elements = elements.filter((data, index) => { |
| 599 |
return indices.indexOf(index) !== -1; |
| 600 |
}); |
| 601 |
applyStyles(elements, style); |
| 602 |
} |
| 603 |
|
| 604 |
compute_view_padding() { |
| 605 |
//This function computes the padding along the x and y directions. |
| 606 |
//The value is in pixels. |
| 607 |
const xPadding = Math.sqrt(this.model.get('default_size')) / 2 + 1.0; |
| 608 |
|
| 609 |
if (xPadding !== this.xPadding || xPadding !== this.yPadding) { |
| 610 |
this.xPadding = xPadding; |
| 611 |
this.yPadding = xPadding; |
| 612 |
this.trigger('mark_padding_updated'); |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
update_array(d, i) { |
| 617 |
const x_scale = this.scales.x, |
| 618 |
y_scale = this.scales.y; |
| 619 |
|
| 620 |
if (!this.model.get('restrict_y')) { |
| 621 |
const x = Object.assign(this.model.get('x').slice(), this.model.get('x')); // copy |
| 622 |
x[i] = x_scale.invert(d[0]); |
| 623 |
this.model.set('x', x); |
| 624 |
} |
| 625 |
if (!this.model.get('restrict_x')) { |
| 626 |
const y = Object.assign(this.model.get('y').slice(), this.model.get('y')); |
| 627 |
y[i] = y_scale.invert(d[1]); |
| 628 |
this.model.set('y', y); |
| 629 |
} |
| 630 |
this.touch(); |
| 631 |
} |
| 632 |
|
| 633 |
set_drag_behavior() { |
| 634 |
const elements = this.d3el.selectAll('.object_grp'); |
| 635 |
if (this.model.get('enable_move')) { |
| 636 |
elements.call(this.drag_listener); |
| 637 |
} else { |
| 638 |
elements.on('.drag', null); |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
set_drag_style(d, i, dragged_node) {} |
| 643 |
|
| 644 |
reset_drag_style(d, i, dragged_node) {} |
| 645 |
|
| 646 |
drag_start(d, i, dragged_node) { |
| 647 |
// d[0] and d[1] will contain the previous position (in pixels) |
| 648 |
// of the dragged point, for the length of the drag event |
| 649 |
const x_scale = this.scales.x, |
| 650 |
y_scale = this.scales.y; |
| 651 |
d[0] = x_scale.scale(d.x) + x_scale.offset; |
| 652 |
d[1] = y_scale.scale(d.y) + y_scale.offset; |
| 653 |
|
| 654 |
this.set_drag_style(d, i, dragged_node); |
| 655 |
|
| 656 |
this.send({ |
| 657 |
event: 'drag_start', |
| 658 |
point: { x: d.x, y: d.y }, |
| 659 |
index: i, |
| 660 |
}); |
| 661 |
} |
| 662 |
|
| 663 |
on_drag(d, i, dragged_node) { |
| 664 |
const x_scale = this.scales.x, |
| 665 |
y_scale = this.scales.y; |
| 666 |
// If restrict_x is true, then the move is restricted only to the X |
| 667 |
// direction. |
| 668 |
const restrict_x = this.model.get('restrict_x'), |
| 669 |
restrict_y = this.model.get('restrict_y'); |
| 670 |
if (restrict_x && restrict_y) { |
| 671 |
return; |
| 672 |
} |
| 673 |
if (!restrict_y) { |
| 674 |
d[0] = d3GetEvent().x; |
| 675 |
} |
| 676 |
if (!restrict_x) { |
| 677 |
d[1] = d3GetEvent().y; |
| 678 |
} |
| 679 |
|
| 680 |
d3.select(dragged_node).attr('transform', () => { |
| 681 |
return 'translate(' + d[0] + ',' + d[1] + ')'; |
| 682 |
}); |
| 683 |
this.send({ |
| 684 |
event: 'drag', |
| 685 |
origin: { x: d.x, y: d.y }, |
| 686 |
point: { |
| 687 |
x: x_scale.invert(d[0]), |
| 688 |
y: y_scale.invert(d[1]), |
| 689 |
}, |
| 690 |
index: i, |
| 691 |
}); |
| 692 |
if (this.model.get('update_on_move')) { |
| 693 |
// saving on move if flag is set |
| 694 |
this.update_array(d, i); |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
drag_ended(d, i, dragged_node) { |
| 699 |
const x_scale = this.scales.x, |
| 700 |
y_scale = this.scales.y; |
| 701 |
|
| 702 |
this.reset_drag_style(d, i, dragged_node); |
| 703 |
this.update_array(d, i); |
| 704 |
this.send({ |
| 705 |
event: 'drag_end', |
| 706 |
point: { |
| 707 |
x: x_scale.invert(d[0]), |
| 708 |
y: y_scale.invert(d[1]), |
| 709 |
}, |
| 710 |
index: i, |
| 711 |
}); |
| 712 |
} |
| 713 |
|
| 714 |
add_element() { |
| 715 |
const mouse_pos = d3.mouse(this.el); |
| 716 |
const curr_pos = [mouse_pos[0], mouse_pos[1]]; |
| 717 |
|
| 718 |
const x_scale = this.scales.x, |
| 719 |
y_scale = this.scales.y; |
| 720 |
//add the new point to data |
| 721 |
const x = this.model.get('x'); |
| 722 |
const y = this.model.get('y'); |
| 723 |
// copy data and fill in the last value |
| 724 |
const xn = new x.constructor(x.length + 1); |
| 725 |
const yn = new y.constructor(y.length + 1); |
| 726 |
xn.set(x); |
| 727 |
yn.set(y); |
| 728 |
xn[x.length] = x_scale.invert(curr_pos[0]); |
| 729 |
yn[y.length] = y_scale.invert(curr_pos[1]); |
| 730 |
this.model.set('x', xn); |
| 731 |
this.model.set('y', yn); |
| 732 |
this.touch(); |
| 733 |
// adding the point and saving the model automatically triggers a |
| 734 |
// draw which adds the new point because the data now has a new |
| 735 |
// point |
| 736 |
} |
| 737 |
|
| 738 |
delete_element(args) { |
| 739 |
const index = args.index; |
| 740 |
|
| 741 |
// copy data to avoid modifying in place (will not detect a change) |
| 742 |
let x = Object.assign(this.model.get('x').slice(), this.model.get('x')); |
| 743 |
let y = Object.assign(this.model.get('y').slice(), this.model.get('y')); |
| 744 |
x.copyWithin(index, index + 1, x.length); |
| 745 |
y.copyWithin(index, index + 1, y.length); |
| 746 |
x = x.slice(0, x.length - 1); |
| 747 |
y = y.slice(0, y.length - 1); |
| 748 |
|
| 749 |
this.model.set('x', x); |
| 750 |
this.model.set('y', y); |
| 751 |
this.touch(); |
| 752 |
} |
| 753 |
|
| 754 |
abstract color_scale_updated(animate?); |
| 755 |
abstract update_opacities(animate?); |
| 756 |
abstract update_default_skew(animate?); |
| 757 |
abstract update_default_size(animate?); |
| 758 |
|
| 759 |
hovered_index: number[]; |
| 760 |
hovered_style: { [key: string]: string }; |
| 761 |
unhovered_style: { [key: string]: string }; |
| 762 |
drag_listener: d3.DragBehavior<Element, unknown, unknown>; |
| 763 |
pixel_coords: number[]; |
| 764 |
legend_el: any; |
| 765 |
x_pixels: number[]; |
| 766 |
y_pixels: number[]; |
| 767 |
x_scale: Scale; |
| 768 |
y_scale: Scale; |
| 769 |
} |