| 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 widgets from '@jupyter-widgets/base'; |
| 17 |
import * as d3 from 'd3'; |
| 18 |
// var d3 =Object.assign({}, require("d3-selection"), require("d3-zoom")); |
| 19 |
import { d3GetEvent } from './utils'; |
| 20 |
import { Mark } from './Mark'; |
| 21 |
import { MapModel } from './MapModel'; |
| 22 |
|
| 23 |
export class Map extends Mark { |
| 24 |
render() { |
| 25 |
const base_render_promise = super.render(); |
| 26 |
|
| 27 |
this.map = this.d3el.append('svg').attr('viewBox', '0 0 1200 980'); |
| 28 |
this.width = this.parent.plotarea_width; |
| 29 |
this.height = this.parent.plotarea_height; |
| 30 |
this.map_id = widgets.uuid(); |
| 31 |
this.enable_hover = this.model.get('enable_hover'); |
| 32 |
this.display_el_classes = ['event_layer']; |
| 33 |
this.displayed.then(() => { |
| 34 |
this.parent.tooltip_div.node().appendChild(this.tooltip_div.node()); |
| 35 |
this.create_tooltip(); |
| 36 |
}); |
| 37 |
|
| 38 |
return base_render_promise.then(() => { |
| 39 |
this.event_listeners = {}; |
| 40 |
this.process_interactions(); |
| 41 |
this.create_listeners(); |
| 42 |
this.draw(); |
| 43 |
}); |
| 44 |
} |
| 45 |
|
| 46 |
set_ranges() {} |
| 47 |
|
| 48 |
relayout() {} |
| 49 |
|
| 50 |
set_positional_scales() { |
| 51 |
const geo_scale = this.scales.projection; |
| 52 |
this.listenTo(geo_scale, 'domain_changed', () => { |
| 53 |
if (!this.model.dirty) { |
| 54 |
this.draw(); |
| 55 |
} |
| 56 |
}); |
| 57 |
} |
| 58 |
|
| 59 |
initialize_additional_scales() { |
| 60 |
const color_scale = this.scales.color; |
| 61 |
if (color_scale) { |
| 62 |
this.listenTo(color_scale, 'domain_changed', () => { |
| 63 |
this.update_style(); |
| 64 |
}); |
| 65 |
color_scale.on('color_scale_range_changed', this.update_style, this); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
remove_map() { |
| 70 |
d3.selectAll('.world_map.map' + this.map_id).remove(); |
| 71 |
} |
| 72 |
|
| 73 |
draw() { |
| 74 |
this.set_ranges(); |
| 75 |
this.remove_map(); |
| 76 |
this.transformed_g = this.map |
| 77 |
.append('g') |
| 78 |
.attr('class', 'world_map map' + this.map_id); |
| 79 |
this.fill_g = this.transformed_g.append('g'); |
| 80 |
this.highlight_g = this.transformed_g.append('g'); |
| 81 |
this.stroke_g = this.transformed_g.append('g'); |
| 82 |
const projection = this.scales.projection; |
| 83 |
//Bind data and create one path per GeoJSON feature |
| 84 |
this.fill_g |
| 85 |
.selectAll('path') |
| 86 |
.data(this.model.geodata) |
| 87 |
.enter() |
| 88 |
.append('path') |
| 89 |
.attr('d', projection.path) |
| 90 |
.style('fill', (d, i) => { |
| 91 |
return this.fill_g_colorfill(d, i); |
| 92 |
}); |
| 93 |
this.stroke_g |
| 94 |
.selectAll('path') |
| 95 |
.data(this.model.geodata) |
| 96 |
.enter() |
| 97 |
.append('path') |
| 98 |
.attr('class', 'event_layer') |
| 99 |
.attr('d', projection.path) |
| 100 |
.style('fill-opacity', 0.0) |
| 101 |
.on('click', (d, i) => { |
| 102 |
return this.event_dispatcher('element_clicked', { data: d, index: i }); |
| 103 |
}); |
| 104 |
if (this.validate_color(this.model.get('stroke_color'))) { |
| 105 |
this.stroke_g |
| 106 |
.selectAll('path') |
| 107 |
.style('stroke', this.model.get('stroke_color')); |
| 108 |
} |
| 109 |
this.zoom = d3 |
| 110 |
.zoom() |
| 111 |
.scaleExtent([1, 8]) |
| 112 |
.on('zoom', () => { |
| 113 |
this.zoomed(this); |
| 114 |
}); |
| 115 |
this.parent.bg_events.call(this.zoom); |
| 116 |
|
| 117 |
this.parent.bg_events.on('dblclick.zoom', null); |
| 118 |
this.parent.bg_events.on('dblclick', () => { |
| 119 |
this.reset_zoom(this); |
| 120 |
}); |
| 121 |
} |
| 122 |
|
| 123 |
validate_color(color) { |
| 124 |
return color !== ''; |
| 125 |
} |
| 126 |
|
| 127 |
mouseover_handler() { |
| 128 |
if (!this.model.get('hover_highlight')) { |
| 129 |
return; |
| 130 |
} |
| 131 |
const el = d3.select(d3GetEvent().target); |
| 132 |
if (this.is_hover_element(el)) { |
| 133 |
const data: any = el.data()[0]; |
| 134 |
const idx = this.model.get('selected') || []; |
| 135 |
const select = Array.from(idx); |
| 136 |
const node = this.highlight_g.append(() => { |
| 137 |
return el.node().cloneNode(true); |
| 138 |
}); |
| 139 |
node.classed('hovered', true); |
| 140 |
node.classed('event_layer', false); |
| 141 |
|
| 142 |
if ( |
| 143 |
this.validate_color(this.model.get('hovered_styles').hovered_stroke) && |
| 144 |
select.indexOf(data.id) === -1 |
| 145 |
) { |
| 146 |
node |
| 147 |
.style('stroke', this.model.get('hovered_styles').hovered_stroke) |
| 148 |
.style( |
| 149 |
'stroke-width', |
| 150 |
this.model.get('hovered_styles').hovered_stroke_width |
| 151 |
); |
| 152 |
} |
| 153 |
if ( |
| 154 |
this.validate_color(this.model.get('hovered_styles').hovered_fill) && |
| 155 |
select.indexOf(data.id) === -1 |
| 156 |
) { |
| 157 |
node.style('fill-opacity', 1.0).style('fill', () => { |
| 158 |
return this.model.get('hovered_styles').hovered_fill; |
| 159 |
}); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
mouseout_handler() { |
| 165 |
if (!this.model.get('hover_highlight')) { |
| 166 |
return; |
| 167 |
} |
| 168 |
const el = d3.select(d3GetEvent().target); |
| 169 |
if (this.is_hover_element(el)) { |
| 170 |
el.transition('mouseout_handler') |
| 171 |
.style('fill', (d, i) => { |
| 172 |
return this.fill_g_colorfill(d, i); |
| 173 |
}) |
| 174 |
.style('stroke', (d, i) => { |
| 175 |
return this.hoverfill(d, i); |
| 176 |
}); |
| 177 |
this.highlight_g.selectAll('.hovered').remove(); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
click_handler() { |
| 182 |
const el = d3.select(d3GetEvent().target); |
| 183 |
if (this.is_hover_element(el)) { |
| 184 |
const data: any = el.data()[0]; |
| 185 |
const idx = this.model.get('selected') || []; |
| 186 |
const selected = Array.from(idx); |
| 187 |
const elem_index = selected.indexOf(data.id); |
| 188 |
if (elem_index > -1) { |
| 189 |
selected.splice(elem_index, 1); |
| 190 |
el.transition('click_handler').style('fill-opacity', 0.0); |
| 191 |
this.highlight_g.selectAll('.hovered').remove(); |
| 192 |
const choice = '#c'.concat(data.id.toString()); |
| 193 |
d3.select(choice).remove(); |
| 194 |
} else { |
| 195 |
this.highlight_g.selectAll('.hovered').remove(); |
| 196 |
this.highlight_g |
| 197 |
.append(() => { |
| 198 |
return el.node().cloneNode(true); |
| 199 |
}) |
| 200 |
.attr('id', 'c' + data.id) |
| 201 |
.classed('selected', true) |
| 202 |
.classed('event_layer', false); |
| 203 |
|
| 204 |
if ( |
| 205 |
this.validate_color(this.model.get('selected_styles').selected_fill) |
| 206 |
) { |
| 207 |
this.highlight_g |
| 208 |
.selectAll('.selected') |
| 209 |
.style('fill-opacity', 1.0) |
| 210 |
.style('fill', this.model.get('selected_styles').selected_fill); |
| 211 |
} |
| 212 |
|
| 213 |
if ( |
| 214 |
this.validate_color(this.model.get('selected_styles').selected_stroke) |
| 215 |
) { |
| 216 |
this.highlight_g |
| 217 |
.selectAll('.selected') |
| 218 |
.style('stroke', this.model.get('selected_styles').selected_stroke) |
| 219 |
.style( |
| 220 |
'stroke-width', |
| 221 |
this.model.get('selected_styles').selected_stroke_width |
| 222 |
); |
| 223 |
} |
| 224 |
selected.push(data.id); |
| 225 |
this.model.set('selected', selected); |
| 226 |
this.touch(); |
| 227 |
} |
| 228 |
this.model.set('selected', selected.length === 0 ? null : selected, { |
| 229 |
updated_view: this, |
| 230 |
}); |
| 231 |
this.touch(); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
reset_zoom(that) { |
| 236 |
that.zoom.transform(that.parent.bg, d3.zoomIdentity); |
| 237 |
} |
| 238 |
|
| 239 |
zoomed(that) { |
| 240 |
const tr = d3GetEvent().transform; |
| 241 |
const h = that.height / 3; |
| 242 |
const w = 2 * that.width; |
| 243 |
tr.x = Math.min( |
| 244 |
(that.width / 2) * (tr.k - 1), |
| 245 |
Math.max((w / 2) * (1 - tr.k), tr.x) |
| 246 |
); |
| 247 |
tr.y = Math.min( |
| 248 |
(that.height / 2) * (tr.k - 1) + this.height * tr.k, |
| 249 |
Math.max((h / 2) * (1 - tr.k) - that.width * tr.k, tr.y) |
| 250 |
); |
| 251 |
that.transformed_g.style('stroke-width', 1 / tr.k).attr('transform', tr); |
| 252 |
} |
| 253 |
|
| 254 |
create_listeners() { |
| 255 |
this.d3el |
| 256 |
.on('mouseover', () => { |
| 257 |
this.event_dispatcher('mouse_over'); |
| 258 |
}) |
| 259 |
.on('mousemove', () => { |
| 260 |
this.event_dispatcher('mouse_move'); |
| 261 |
}) |
| 262 |
.on('mouseout', () => { |
| 263 |
this.event_dispatcher('mouse_out'); |
| 264 |
}); |
| 265 |
|
| 266 |
this.listenTo(this.model, 'data_updated', this.draw); |
| 267 |
this.listenTo(this.model, 'change:color', this.update_style); |
| 268 |
this.listenTo(this.model, 'change:stroke_color', this.change_stroke_color); |
| 269 |
this.listenTo(this.model, 'change:colors', this.change_map_color); |
| 270 |
this.listenTo(this.model, 'change:selected', this.change_selected); |
| 271 |
this.listenTo(this.model, 'change:selected_styles', () => { |
| 272 |
this.change_selected_fill(); |
| 273 |
this.change_selected_stroke(); |
| 274 |
}); |
| 275 |
this.listenTo(this.model, 'change:interactions', this.process_interactions); |
| 276 |
this.listenTo(this.parent, 'bg_clicked', () => { |
| 277 |
this.event_dispatcher('parent_clicked'); |
| 278 |
}); |
| 279 |
} |
| 280 |
|
| 281 |
process_click(interaction) { |
| 282 |
super.process_click(interaction); |
| 283 |
if (interaction === 'select') { |
| 284 |
this.event_listeners.parent_clicked = this.reset_selection; |
| 285 |
this.event_listeners.element_clicked = this.click_handler; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
process_hover(interaction) { |
| 290 |
super.process_hover(interaction); |
| 291 |
if (interaction === 'tooltip') { |
| 292 |
this.event_listeners.mouse_over = () => { |
| 293 |
this.mouseover_handler(); |
| 294 |
return this.refresh_tooltip(); |
| 295 |
}; |
| 296 |
this.event_listeners.mouse_move = this.move_tooltip; |
| 297 |
this.event_listeners.mouse_out = () => { |
| 298 |
this.mouseout_handler(); |
| 299 |
return this.hide_tooltip(); |
| 300 |
}; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
change_selected_fill() { |
| 305 |
if (!this.validate_color(this.model.get('selected_styles').selected_fill)) { |
| 306 |
this.highlight_g.selectAll('.selected').style('fill-opacity', 0.0); |
| 307 |
} else { |
| 308 |
this.highlight_g |
| 309 |
.selectAll('.selected') |
| 310 |
.style('fill-opacity', 1.0) |
| 311 |
.style('fill', this.model.get('selected_styles').selected_fill); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
change_selected_stroke() { |
| 316 |
if ( |
| 317 |
!this.validate_color(this.model.get('selected_styles').selected_stroke) |
| 318 |
) { |
| 319 |
this.highlight_g.selectAll('.selected').style('stroke-width', 0.0); |
| 320 |
} else { |
| 321 |
this.highlight_g |
| 322 |
.selectAll('.selected') |
| 323 |
.style( |
| 324 |
'stroke-width', |
| 325 |
this.model.get('selected_styles').selected_stroke_width |
| 326 |
) |
| 327 |
.style('stroke', this.model.get('selected_styles').selected_stroke); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
change_selected() { |
| 332 |
this.highlight_g.selectAll('path').remove(); |
| 333 |
const idx = this.model.get('selected'); |
| 334 |
const select = idx ? idx : []; |
| 335 |
const temp = this.stroke_g.selectAll('path').data(); |
| 336 |
this.stroke_g.selectAll('path').style('stroke', (d, i) => { |
| 337 |
return this.hoverfill(d, i); |
| 338 |
}); |
| 339 |
const nodes = this.stroke_g.selectAll('path'); |
| 340 |
for (let i = 0; i < temp.length; i++) { |
| 341 |
if (select.indexOf((temp[i] as any).id) > -1) { |
| 342 |
this.highlight_g |
| 343 |
.append(() => { |
| 344 |
return (nodes.nodes()[i] as any).cloneNode(true); |
| 345 |
}) |
| 346 |
.attr('id', (temp[i] as any).id) |
| 347 |
.style('fill-opacity', () => { |
| 348 |
if ( |
| 349 |
this.validate_color( |
| 350 |
this.model.get('selected_styles').selected_fill |
| 351 |
) |
| 352 |
) { |
| 353 |
return 1.0; |
| 354 |
} else { |
| 355 |
return 0.0; |
| 356 |
} |
| 357 |
}) |
| 358 |
.style('fill', this.model.get('selected_styles').selected_fill) |
| 359 |
.style('stroke-opacity', () => { |
| 360 |
if ( |
| 361 |
this.validate_color( |
| 362 |
this.model.get('selected_styles').selected_stroke |
| 363 |
) |
| 364 |
) { |
| 365 |
return 1.0; |
| 366 |
} else { |
| 367 |
return 0.0; |
| 368 |
} |
| 369 |
}) |
| 370 |
.style('stroke', this.model.get('selected_styles').selected_stroke) |
| 371 |
.style( |
| 372 |
'stroke-width', |
| 373 |
this.model.get('selected_styles').selected_stroke_width |
| 374 |
) |
| 375 |
.classed('selected', true); |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
reset_selection() { |
| 381 |
this.model.set('selected', []); |
| 382 |
this.touch(); |
| 383 |
this.highlight_g.selectAll('.selected').remove(); |
| 384 |
d3.select(this.d3el.node().parentNode) |
| 385 |
.selectAll('path') |
| 386 |
.classed('selected', false); |
| 387 |
d3.select(this.d3el.node().parentNode) |
| 388 |
.selectAll('path') |
| 389 |
.classed('hovered', false); |
| 390 |
|
| 391 |
this.stroke_g.selectAll('path').style('stroke', (d, i) => { |
| 392 |
return this.hoverfill(d, i); |
| 393 |
}); |
| 394 |
this.fill_g |
| 395 |
.selectAll('path') |
| 396 |
.classed('selected', false) |
| 397 |
.style('fill', (d, i) => { |
| 398 |
return this.fill_g_colorfill(d, i); |
| 399 |
}); |
| 400 |
} |
| 401 |
|
| 402 |
change_stroke_color() { |
| 403 |
this.stroke_g |
| 404 |
.selectAll('path') |
| 405 |
.style('stroke', this.model.get('stroke_color')); |
| 406 |
} |
| 407 |
|
| 408 |
change_map_color() { |
| 409 |
if (!this.is_object_empty(this.model.get('color'))) { |
| 410 |
return; |
| 411 |
} |
| 412 |
this.fill_g.selectAll('path').style('fill', (d, i) => { |
| 413 |
return this.fill_g_colorfill(d, i); |
| 414 |
}); |
| 415 |
} |
| 416 |
|
| 417 |
update_style() { |
| 418 |
const color_data = this.model.get('color'); |
| 419 |
if (!this.is_object_empty(color_data)) { |
| 420 |
this.fill_g.selectAll('path').style('fill', (d, i) => { |
| 421 |
return this.fill_g_colorfill(d, i); |
| 422 |
}); |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
is_object_empty(object: any) { |
| 427 |
return Object.keys(object).length === 0 && object.constructor === Object; |
| 428 |
} |
| 429 |
|
| 430 |
hoverfill(d, j) { |
| 431 |
const idx = this.model.get('selected'); |
| 432 |
const select = idx ? idx : []; |
| 433 |
if ( |
| 434 |
select.indexOf(d.id) > -1 && |
| 435 |
this.validate_color(this.model.get('selected_styles').selected_stroke) |
| 436 |
) { |
| 437 |
return this.model.get('selected_styles').selected_stroke; |
| 438 |
} else { |
| 439 |
return this.model.get('stroke_color'); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
fill_g_colorfill(d, j) { |
| 444 |
const color_scale = this.scales.color; |
| 445 |
const idx = this.model.get('selected'); |
| 446 |
const selection = idx ? idx : []; |
| 447 |
const color_data = this.model.get('color'); |
| 448 |
const colors = this.model.get('colors'); |
| 449 |
|
| 450 |
if (selection.indexOf(d.id) > -1) { |
| 451 |
return this.model.get('selected_styles').selected_fill; |
| 452 |
} else if (this.is_object_empty(color_data)) { |
| 453 |
return colors[d.id] || colors.default_color; |
| 454 |
} else if ( |
| 455 |
color_data[d.id] === undefined || |
| 456 |
color_data[d.id] === null || |
| 457 |
color_scale === undefined |
| 458 |
) { |
| 459 |
return colors.default_color; |
| 460 |
} else { |
| 461 |
return color_scale.scale(color_data[d.id]); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
clear_style() {} |
| 466 |
compute_view_padding() {} |
| 467 |
set_default_style() {} |
| 468 |
set_style_on_elements() {} |
| 469 |
|
| 470 |
map: d3.Selection<SVGSVGElement, any, any, any>; |
| 471 |
map_id: string; |
| 472 |
width: number; |
| 473 |
height: number; |
| 474 |
enable_hover: boolean; |
| 475 |
stroke_g: d3.Selection<SVGGElement, any, any, any>; |
| 476 |
fill_g: d3.Selection<SVGGElement, any, any, any>; |
| 477 |
highlight_g: d3.Selection<SVGGElement, any, any, any>; |
| 478 |
transformed_g: d3.Selection<SVGGElement, any, any, any>; |
| 479 |
zoom: d3.ZoomBehavior<Element, any>; |
| 480 |
model: MapModel; |
| 481 |
} |