| 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-selection-multi")); |
| 19 |
import * as _ from 'underscore'; |
| 20 |
import * as popperreference from './PopperReference'; |
| 21 |
import popper from 'popper.js'; |
| 22 |
import * as THREE from 'three'; |
| 23 |
import { Dict, WidgetModel, WidgetView } from '@jupyter-widgets/base'; |
| 24 |
import { applyAttrs, applyStyles, getEffectiveBackgroundColor } from './utils'; |
| 25 |
import { Scale } from './Scale'; |
| 26 |
import { ScaleModel } from './ScaleModel'; |
| 27 |
import { AxisModel } from './AxisModel'; |
| 28 |
import { Mark } from './Mark'; |
| 29 |
import { MarkModel } from './MarkModel'; |
| 30 |
import { Interaction } from './Interaction'; |
| 31 |
|
| 32 |
THREE.ShaderChunk['scales'] = |
| 33 |
require('raw-loader!../shaders/scales.glsl').default; |
| 34 |
|
| 35 |
interface IFigureSize { |
| 36 |
width: number; |
| 37 |
height: number; |
| 38 |
} |
| 39 |
|
| 40 |
export class Figure extends widgets.DOMWidgetView { |
| 41 |
initialize() { |
| 42 |
this.debouncedRelayout = _.debounce(() => { |
| 43 |
this.relayout(); |
| 44 |
}, 300); |
| 45 |
// Internet Explorer does not support classList for svg elements |
| 46 |
this.el.classList.add('bqplot'); |
| 47 |
this.el.classList.add('figure'); |
| 48 |
this.el.classList.add('jupyter-widgets'); |
| 49 |
this.change_theme(); |
| 50 |
|
| 51 |
const svg = document.createElementNS( |
| 52 |
d3.namespaces.svg, |
| 53 |
'svg' |
| 54 |
) as SVGElement; |
| 55 |
svg.classList.add('svg-figure'); |
| 56 |
this.svg = d3.select<SVGElement, any>(svg); |
| 57 |
|
| 58 |
const svg_background = document.createElementNS( |
| 59 |
d3.namespaces.svg, |
| 60 |
'svg' |
| 61 |
) as SVGElement; |
| 62 |
svg_background.classList.add('svg-background'); |
| 63 |
this.svg_background = d3.select<SVGElement, any>(svg_background); |
| 64 |
|
| 65 |
this.el.appendChild(svg_background); |
| 66 |
this.el.appendChild(svg); |
| 67 |
|
| 68 |
// For testing we need to know when the mark_views is created, the tests |
| 69 |
// can wait for this promise. |
| 70 |
this._initial_marks_created = new Promise((resolve) => { |
| 71 |
this._initial_marks_created_resolve = resolve; |
| 72 |
}); |
| 73 |
|
| 74 |
this.intersectObserver = new IntersectionObserver( |
| 75 |
(entries: IntersectionObserverEntry[]) => { |
| 76 |
if (entries[entries.length - 1].isIntersecting) { |
| 77 |
this.visible = true; |
| 78 |
this.debouncedRelayout(); |
| 79 |
} else if (entries[entries.length - 1].rootBounds != null) { |
| 80 |
/* When 'rootBounds' is null, 'isIntersecting' is 'false', but the plot is visible, so only change 'visible' |
| 81 |
* if rootBonds is set. I can't find any info on this behaviour. */ |
| 82 |
this.visible = false; |
| 83 |
} |
| 84 |
}, |
| 85 |
{ threshold: 0 } |
| 86 |
); |
| 87 |
this.intersectObserver.observe(this.el); |
| 88 |
|
| 89 |
this.resizeObserver = new ResizeObserver( |
| 90 |
(entries: ResizeObserverEntry[]) => { |
| 91 |
this.debouncedRelayout(); |
| 92 |
} |
| 93 |
); |
| 94 |
|
| 95 |
this.resizeObserver.observe(this.el); |
| 96 |
|
| 97 |
super.initialize.apply(this, arguments); |
| 98 |
} |
| 99 |
|
| 100 |
protected getFigureSize(): IFigureSize { |
| 101 |
const figureSize: IFigureSize = this.el.getBoundingClientRect(); |
| 102 |
const clientRectRatio = figureSize.width / figureSize.height; |
| 103 |
|
| 104 |
const minRatio: number = this.model.get('min_aspect_ratio'); |
| 105 |
const maxRatio: number = this.model.get('max_aspect_ratio'); |
| 106 |
|
| 107 |
if (clientRectRatio < minRatio) { |
| 108 |
// Too much vertical space: Keep horizontal space but compute height from min aspect ratio |
| 109 |
figureSize.height = figureSize.width / minRatio; |
| 110 |
} else if (clientRectRatio > maxRatio) { |
| 111 |
// Too much horizontal space: Keep vertical space but compute width from max aspect ratio |
| 112 |
figureSize.width = figureSize.height * maxRatio; |
| 113 |
} |
| 114 |
|
| 115 |
return figureSize; |
| 116 |
} |
| 117 |
|
| 118 |
render() { |
| 119 |
// we cannot use Promise.all here, since this.layoutPromise is resolved, and will be overwritten later on |
| 120 |
this.displayed.then(() => { |
| 121 |
// make sure we render after all layouts styles are set, since they can affect the size |
| 122 |
this.layoutPromise.then(this.renderImpl.bind(this)); |
| 123 |
}); |
| 124 |
} |
| 125 |
|
| 126 |
protected async renderImpl() { |
| 127 |
const figureSize = this.getFigureSize(); |
| 128 |
this.width = figureSize.width; |
| 129 |
this.height = figureSize.height; |
| 130 |
|
| 131 |
this.id = widgets.uuid(); |
| 132 |
|
| 133 |
// Dictionary which contains the mapping for each of the marks id |
| 134 |
// to it's padding. Dictionary is required to not recompute |
| 135 |
// everything when a mark is removed. |
| 136 |
this.x_pad_dict = {}; |
| 137 |
this.y_pad_dict = {}; |
| 138 |
|
| 139 |
// this is the net padding in pixel to be applied to the x and y. |
| 140 |
// If there is no restriction on the plottable area of the figure, |
| 141 |
// then these two variables are the maximum of the values in the |
| 142 |
// corresponding variables x_pad_dict, y_pad_dict. |
| 143 |
this.xPaddingArr = {}; |
| 144 |
this.yPaddingArr = {}; |
| 145 |
this.figure_padding_x = this.model.get('padding_x'); |
| 146 |
this.figure_padding_y = this.model.get('padding_y'); |
| 147 |
this.clip_id = 'clip_path_' + this.id; |
| 148 |
this.margin = this.model.get('fig_margin'); |
| 149 |
|
| 150 |
this.update_plotarea_dimensions(); |
| 151 |
// we hide it when the plot area is too small |
| 152 |
if (this.plotarea_width < 1 || this.plotarea_height < 1) { |
| 153 |
this.el.style.visibility = 'hidden'; |
| 154 |
} else { |
| 155 |
this.el.style.visibility = ''; |
| 156 |
} |
| 157 |
// this.fig is the top <g> element to be impacted by a rescaling / change of margins |
| 158 |
|
| 159 |
this.fig = this.svg |
| 160 |
.append('g') |
| 161 |
.attr( |
| 162 |
'transform', |
| 163 |
'translate(' + this.margin.left + ',' + this.margin.top + ')' |
| 164 |
); |
| 165 |
this.fig_background = this.svg_background |
| 166 |
.append('g') |
| 167 |
.attr( |
| 168 |
'transform', |
| 169 |
'translate(' + this.margin.left + ',' + this.margin.top + ')' |
| 170 |
); |
| 171 |
this.tooltip_div = d3 |
| 172 |
.select(document.createElement('div')) |
| 173 |
.attr('class', 'tooltip_div'); |
| 174 |
this.popper_reference = new popperreference.PositionReference({ |
| 175 |
x: 0, |
| 176 |
y: 0, |
| 177 |
width: 20, |
| 178 |
height: 20, |
| 179 |
}); |
| 180 |
this.popper = new popper(this.popper_reference, this.tooltip_div.node(), { |
| 181 |
placement: 'auto', |
| 182 |
}); |
| 183 |
|
| 184 |
this.bg = this.fig_background |
| 185 |
.append('rect') |
| 186 |
.attr('class', 'plotarea_background') |
| 187 |
.attr('x', 0) |
| 188 |
.attr('y', 0) |
| 189 |
.attr('width', this.plotarea_width) |
| 190 |
.attr('height', this.plotarea_height) |
| 191 |
.style('pointer-events', 'inherit'); |
| 192 |
applyStyles(this.bg, this.model.get('background_style')); |
| 193 |
|
| 194 |
this.bg_events = this.fig |
| 195 |
.append('rect') |
| 196 |
.attr('class', 'plotarea_events') |
| 197 |
.attr('x', 0) |
| 198 |
.attr('y', 0) |
| 199 |
.attr('width', this.plotarea_width) |
| 200 |
.attr('height', this.plotarea_height) |
| 201 |
.style('pointer-events', 'inherit'); |
| 202 |
this.bg_events.on('click', () => { |
| 203 |
this.trigger('bg_clicked'); |
| 204 |
}); |
| 205 |
|
| 206 |
this.fig_axes = this.fig_background.append('g'); |
| 207 |
this.fig_marks = this.fig.append('g'); |
| 208 |
this.interaction = this.fig.append('g'); |
| 209 |
|
| 210 |
/* |
| 211 |
* The following was the structure of the DOM element constructed |
| 212 |
* |
| 213 |
<div class="bqplot figure jupyter-widgets"> |
| 214 |
<svg> |
| 215 |
<g class="svg-figure" transform="margin translation"> |
| 216 |
<g class="svg-axes"></g> |
| 217 |
<g class="svg-marks"></g> |
| 218 |
<g class="svg-interaction"></g> |
| 219 |
</g> |
| 220 |
</svg> |
| 221 |
</div> |
| 222 |
|
| 223 |
To allow the main/interaction layer on top, and also allowing us to draw |
| 224 |
on top of the canvas (e.g. selectors), we create a new DOM structure. |
| 225 |
When creating a screenshot/image, we collapse all this into one svg. |
| 226 |
|
| 227 |
<div class="bqplot figure jupyter-widgets"> |
| 228 |
<svg class="svg-background"> |
| 229 |
<g transform="margin translation"> |
| 230 |
<g class="svg-axes"></g> |
| 231 |
</g> |
| 232 |
</svg> |
| 233 |
<canvas> |
| 234 |
</canvas> |
| 235 |
<svg class="svg-figure"> |
| 236 |
<g transform="margin translation"> |
| 237 |
<g class="svg-marks"></g> |
| 238 |
<g class="svg-interaction"></g> |
| 239 |
</g> |
| 240 |
</svg> |
| 241 |
</div> |
| 242 |
*/ |
| 243 |
|
| 244 |
this.clip_path = this.svg |
| 245 |
.append('svg:defs') |
| 246 |
.append('svg:clipPath') |
| 247 |
.attr('id', this.clip_id) |
| 248 |
.append('rect') |
| 249 |
.attr('x', 0) |
| 250 |
.attr('y', 0) |
| 251 |
.attr('width', this.plotarea_width) |
| 252 |
.attr('height', this.plotarea_height); |
| 253 |
|
| 254 |
this.title = this.fig |
| 255 |
.append('text') |
| 256 |
.attr('class', 'mainheading') |
| 257 |
.attr('x', 0.5 * this.plotarea_width) |
| 258 |
.attr('y', -(this.margin.top / 2.0)) |
| 259 |
.attr('dy', '1em'); |
| 260 |
applyStyles(this.title, this.model.get('title_style')); |
| 261 |
|
| 262 |
this.title.text(this.model.get('title')); |
| 263 |
|
| 264 |
// TODO: remove the save png event mechanism. |
| 265 |
this.model.on('save_png', this.save_png, this); |
| 266 |
this.model.on('save_svg', this.save_svg, this); |
| 267 |
this.model.on('upload_png', this.upload_png, this); |
| 268 |
|
| 269 |
const figure_scale_promise = this.create_figure_scales(); |
| 270 |
|
| 271 |
await figure_scale_promise; |
| 272 |
|
| 273 |
this.mark_views = new widgets.ViewList( |
| 274 |
this.add_mark, |
| 275 |
this.remove_mark, |
| 276 |
this |
| 277 |
); |
| 278 |
|
| 279 |
const mark_views_updated = this.mark_views |
| 280 |
.update(this.model.get('marks')) |
| 281 |
.then((views) => { |
| 282 |
this.replace_dummy_nodes(views); |
| 283 |
this.update_marks(views); |
| 284 |
this.update_legend(); |
| 285 |
// Update Interaction layer |
| 286 |
// This has to be done after the marks are created |
| 287 |
this.set_interaction(this.model.get('interaction')); |
| 288 |
this._initial_marks_created_resolve(); |
| 289 |
this.update_gl(); |
| 290 |
}); |
| 291 |
|
| 292 |
this.axis_views = new widgets.ViewList(this.add_axis, null, this); |
| 293 |
const axis_views_updated = this.axis_views.update(this.model.get('axes')); |
| 294 |
|
| 295 |
// TODO: move to the model |
| 296 |
this.model.on_some_change( |
| 297 |
['fig_margin', 'min_aspect_ratio', 'max_aspect_ratio'], |
| 298 |
() => { |
| 299 |
this.should_relayout = true; |
| 300 |
this.debouncedRelayout(); |
| 301 |
}, |
| 302 |
this |
| 303 |
); |
| 304 |
this.model.on_some_change( |
| 305 |
['padding_x', 'padding_y'], |
| 306 |
() => { |
| 307 |
this.figure_padding_x = this.model.get('padding_x'); |
| 308 |
this.figure_padding_y = this.model.get('padding_y'); |
| 309 |
this.trigger('margin_updated'); |
| 310 |
}, |
| 311 |
this |
| 312 |
); |
| 313 |
this.model.on( |
| 314 |
'change:axes', |
| 315 |
(model, value, options) => { |
| 316 |
this.axis_views.update(value); |
| 317 |
}, |
| 318 |
this |
| 319 |
); |
| 320 |
this.model.on( |
| 321 |
'change:marks', |
| 322 |
(model, value, options) => { |
| 323 |
this.mark_views.update(value).then((views) => { |
| 324 |
this.replace_dummy_nodes(views); |
| 325 |
this.update_marks(views); |
| 326 |
this.update_legend(); |
| 327 |
this.update_gl(); |
| 328 |
}); |
| 329 |
}, |
| 330 |
this |
| 331 |
); |
| 332 |
this.model.on('change:legend_location', this.update_legend, this); |
| 333 |
this.model.on('change:title', this.update_title, this); |
| 334 |
|
| 335 |
this.model.on( |
| 336 |
'change:interaction', |
| 337 |
(model, value) => { |
| 338 |
Promise.all(this.mark_views.views).then((views) => { |
| 339 |
// Like above: |
| 340 |
// This has to be done after the marks are created |
| 341 |
this.set_interaction(value); |
| 342 |
}); |
| 343 |
}, |
| 344 |
this |
| 345 |
); |
| 346 |
|
| 347 |
document.body.appendChild(this.tooltip_div.node()); |
| 348 |
this.create_listeners(); |
| 349 |
|
| 350 |
return Promise.all([mark_views_updated, axis_views_updated]); |
| 351 |
} |
| 352 |
|
| 353 |
createWebGLRenderer() { |
| 354 |
// a shared webgl context for all marks |
| 355 |
if (!this.renderer) { |
| 356 |
this.renderer = new THREE.WebGLRenderer({ |
| 357 |
antialias: true, |
| 358 |
alpha: true, |
| 359 |
premultipliedAlpha: true, |
| 360 |
}); |
| 361 |
|
| 362 |
this.renderer.setSize(100, 100); |
| 363 |
this.renderer.setClearAlpha(0); |
| 364 |
this.renderer.setPixelRatio( |
| 365 |
this.model.get('pixel_ratio') || window.devicePixelRatio |
| 366 |
); |
| 367 |
} |
| 368 |
|
| 369 |
if (this.renderer && !this.el.contains(this.renderer.domElement)) { |
| 370 |
this.el.insertBefore(this.renderer.domElement, this.el.childNodes[1]); |
| 371 |
} |
| 372 |
this.layout_webgl_canvas(); |
| 373 |
} |
| 374 |
|
| 375 |
replace_dummy_nodes(views: Mark[]) { |
| 376 |
for (const view of views) { |
| 377 |
const dummyNode = this.dummyNodes[view.cid]; |
| 378 |
|
| 379 |
// It could be that the dummy node is removed before we got a change to replace it |
| 380 |
// This happens when the marks list is changed rapidly |
| 381 |
if (dummyNode !== null && dummyNode.parentNode) { |
| 382 |
dummyNode.parentNode.replaceChild(view.el, dummyNode); |
| 383 |
this.dummyNodes[view.cid] = null; |
| 384 |
this.displayed.then(() => { |
| 385 |
view.trigger('displayed'); |
| 386 |
}); |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
create_listeners() { |
| 392 |
this.listenTo(this.model, 'change:title_style', this.title_style_updated); |
| 393 |
this.listenTo( |
| 394 |
this.model, |
| 395 |
'change:background_style', |
| 396 |
this.background_style_updated |
| 397 |
); |
| 398 |
this.listenTo(this.model, 'change:layout', this.change_layout); |
| 399 |
this.listenTo(this.model, 'change:legend_style', this.legend_style_updated); |
| 400 |
this.listenTo(this.model, 'change:legend_text', this.legend_text_updated); |
| 401 |
this.listenTo(this.model, 'change:pixel_ratio', () => { |
| 402 |
if (this.renderer) { |
| 403 |
this.renderer.setPixelRatio( |
| 404 |
this.model.get('pixel_ratio') || window.devicePixelRatio |
| 405 |
); |
| 406 |
this.update_gl(); |
| 407 |
} |
| 408 |
}); |
| 409 |
this.listenTo(this.model, 'change:theme', this.change_theme); |
| 410 |
} |
| 411 |
|
| 412 |
title_style_updated() { |
| 413 |
applyStyles(this.title, this.model.get('title_style')); |
| 414 |
} |
| 415 |
|
| 416 |
background_style_updated() { |
| 417 |
applyStyles(this.bg, this.model.get('background_style')); |
| 418 |
} |
| 419 |
|
| 420 |
legend_style_updated() { |
| 421 |
applyStyles( |
| 422 |
this.fig_marks |
| 423 |
.selectAll('.g_legend') |
| 424 |
.selectAll('.axis') |
| 425 |
.selectAll('rect'), |
| 426 |
this.model.get('legend_style') |
| 427 |
); |
| 428 |
} |
| 429 |
|
| 430 |
legend_text_updated() { |
| 431 |
applyStyles( |
| 432 |
this.fig_marks.selectAll('.g_legend').selectAll('text.legendtext'), |
| 433 |
this.model.get('legend_text') |
| 434 |
); |
| 435 |
} |
| 436 |
|
| 437 |
create_figure_scales() { |
| 438 |
// Creates the absolute scales for the figure: default domain is [0,1], range is [0,width] and [0,height]. |
| 439 |
// See the scale_x and scale_y attributes of the python Figure |
| 440 |
const that = this; |
| 441 |
const x_scale_promise = this.create_child_view( |
| 442 |
this.model.get('scale_x') |
| 443 |
).then((view) => { |
| 444 |
that.scale_x = view as WidgetView as Scale; |
| 445 |
( |
| 446 |
that.scale_x.scale as |
| 447 |
| d3.ScaleLinear<number, number> |
| 448 |
| d3.ScaleTime<Date, number> |
| 449 |
| d3.ScaleLogarithmic<number, number> |
| 450 |
).clamp(true); |
| 451 |
that.scale_x.set_range([0, that.plotarea_width]); |
| 452 |
}); |
| 453 |
|
| 454 |
const y_scale_promise = this.create_child_view( |
| 455 |
this.model.get('scale_y') |
| 456 |
).then((view) => { |
| 457 |
that.scale_y = view as WidgetView as Scale; |
| 458 |
( |
| 459 |
that.scale_y.scale as |
| 460 |
| d3.ScaleLinear<number, number> |
| 461 |
| d3.ScaleTime<Date, number> |
| 462 |
| d3.ScaleLogarithmic<number, number> |
| 463 |
).clamp(true); |
| 464 |
that.scale_y.set_range([that.plotarea_height, 0]); |
| 465 |
}); |
| 466 |
return Promise.all([x_scale_promise, y_scale_promise]); |
| 467 |
} |
| 468 |
|
| 469 |
padded_range(direction: 'x' | 'y', scale_model: ScaleModel) { |
| 470 |
// Functions to be called by mark which respects padding. |
| 471 |
// Typically all marks do this. Axis do not do this. |
| 472 |
// Also, if a mark does not set the domain, it can potentially call |
| 473 |
// the unpadded ranges. |
| 474 |
if (!scale_model.get('allow_padding')) { |
| 475 |
return this.range(direction); |
| 476 |
} |
| 477 |
const scale_id = scale_model.model_id; |
| 478 |
|
| 479 |
if (direction === 'x') { |
| 480 |
const scale_padding = |
| 481 |
this.xPaddingArr[scale_id] !== undefined |
| 482 |
? this.xPaddingArr[scale_id] |
| 483 |
: 0; |
| 484 |
const fig_padding = this.plotarea_width * this.figure_padding_x; |
| 485 |
return [ |
| 486 |
fig_padding + scale_padding, |
| 487 |
this.plotarea_width - fig_padding - scale_padding, |
| 488 |
]; |
| 489 |
} else if (direction === 'y') { |
| 490 |
const scale_padding = |
| 491 |
this.yPaddingArr[scale_id] !== undefined |
| 492 |
? this.yPaddingArr[scale_id] |
| 493 |
: 0; |
| 494 |
const fig_padding = this.plotarea_height * this.figure_padding_y; |
| 495 |
return [ |
| 496 |
this.plotarea_height - scale_padding - fig_padding, |
| 497 |
scale_padding + fig_padding, |
| 498 |
]; |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
range(direction: 'x' | 'y') { |
| 503 |
if (direction === 'x') { |
| 504 |
return [0, this.plotarea_width]; |
| 505 |
} else if (direction === 'y') { |
| 506 |
return [this.plotarea_height, 0]; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
get_mark_plotarea_height(scaleModel: ScaleModel) { |
| 511 |
if (!scaleModel.get('allow_padding')) { |
| 512 |
return this.plotarea_height; |
| 513 |
} |
| 514 |
const scale_id = scaleModel.model_id; |
| 515 |
const scale_padding = |
| 516 |
this.yPaddingArr[scale_id] !== undefined ? this.yPaddingArr[scale_id] : 0; |
| 517 |
return ( |
| 518 |
this.plotarea_height * (1 - this.figure_padding_y) - |
| 519 |
scale_padding - |
| 520 |
scale_padding |
| 521 |
); |
| 522 |
} |
| 523 |
|
| 524 |
get_mark_plotarea_width(scaleModel: ScaleModel) { |
| 525 |
if (!scaleModel.get('allow_padding')) { |
| 526 |
return this.plotarea_width; |
| 527 |
} |
| 528 |
|
| 529 |
const scale_id = scaleModel.model_id; |
| 530 |
const scale_padding = |
| 531 |
this.xPaddingArr[scale_id] !== undefined ? this.xPaddingArr[scale_id] : 0; |
| 532 |
return ( |
| 533 |
this.plotarea_width * (1 - this.figure_padding_x) - |
| 534 |
scale_padding - |
| 535 |
scale_padding |
| 536 |
); |
| 537 |
} |
| 538 |
|
| 539 |
async add_axis(model: AxisModel) { |
| 540 |
// Called when an axis is added to the axes list. |
| 541 |
const view = await this.create_child_view(model); |
| 542 |
|
| 543 |
this.fig_axes.node().appendChild(view.el); |
| 544 |
this.displayed.then(() => { |
| 545 |
view.trigger('displayed'); |
| 546 |
}); |
| 547 |
|
| 548 |
return view; |
| 549 |
} |
| 550 |
|
| 551 |
remove_from_padding_dict(dict, mark_view: Mark, scale_model: ScaleModel) { |
| 552 |
if (scale_model === undefined || scale_model === null) { |
| 553 |
return; |
| 554 |
} |
| 555 |
const scale_id = scale_model.model_id; |
| 556 |
if (dict[scale_id] !== undefined) { |
| 557 |
delete dict[scale_id][mark_view.model.model_id + '_' + mark_view.cid]; |
| 558 |
if (Object.keys(dict[scale_id]).length === 0) { |
| 559 |
delete dict[scale_id]; |
| 560 |
} |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
update_padding_dict(dict, mark_view: Mark, scale_model: ScaleModel, value) { |
| 565 |
const scale_id = scale_model.model_id; |
| 566 |
if (!dict[scale_id]) { |
| 567 |
dict[scale_id] = {}; |
| 568 |
} |
| 569 |
dict[scale_id][mark_view.model.model_id + '_' + mark_view.cid] = value; |
| 570 |
} |
| 571 |
|
| 572 |
mark_scales_updated(view: Mark) { |
| 573 |
const model = view.model; |
| 574 |
const prev_scale_models = model.previous('scales'); |
| 575 |
this.remove_from_padding_dict( |
| 576 |
this.x_pad_dict, |
| 577 |
view, |
| 578 |
prev_scale_models[model.get_key_for_orientation('horizontal')] |
| 579 |
); |
| 580 |
this.remove_from_padding_dict( |
| 581 |
this.y_pad_dict, |
| 582 |
view, |
| 583 |
prev_scale_models[model.get_key_for_orientation('vertical')] |
| 584 |
); |
| 585 |
|
| 586 |
const scale_models = model.get('scales'); |
| 587 |
this.update_padding_dict( |
| 588 |
this.x_pad_dict, |
| 589 |
view, |
| 590 |
scale_models[model.get_key_for_orientation('horizontal')], |
| 591 |
view.xPadding |
| 592 |
); |
| 593 |
this.update_padding_dict( |
| 594 |
this.y_pad_dict, |
| 595 |
view, |
| 596 |
scale_models[model.get_key_for_orientation('vertical')], |
| 597 |
view.yPadding |
| 598 |
); |
| 599 |
|
| 600 |
this.update_paddings(); |
| 601 |
} |
| 602 |
|
| 603 |
mark_padding_updated(view: Mark) { |
| 604 |
const model = view.model; |
| 605 |
const scale_models = model.get('scales'); |
| 606 |
|
| 607 |
this.update_padding_dict( |
| 608 |
this.x_pad_dict, |
| 609 |
view, |
| 610 |
scale_models[model.get_key_for_orientation('horizontal')], |
| 611 |
view.xPadding |
| 612 |
); |
| 613 |
this.update_padding_dict( |
| 614 |
this.y_pad_dict, |
| 615 |
view, |
| 616 |
scale_models[model.get_key_for_orientation('vertical')], |
| 617 |
view.yPadding |
| 618 |
); |
| 619 |
|
| 620 |
this.update_paddings(); |
| 621 |
} |
| 622 |
|
| 623 |
update_marks(mark_views) { |
| 624 |
this.update_paddings(); |
| 625 |
} |
| 626 |
|
| 627 |
remove_mark(view: Mark) { |
| 628 |
// Called when a mark is removed from the mark list. |
| 629 |
const model = view.model; |
| 630 |
model.off('redraw_legend', null, this); |
| 631 |
model.off('data_updated', null, this); |
| 632 |
model.off('scales_updated', null, this); |
| 633 |
model.off('mark_padding_updated', null, this); |
| 634 |
|
| 635 |
const scale_models = model.get('scales'); |
| 636 |
this.remove_from_padding_dict( |
| 637 |
this.x_pad_dict, |
| 638 |
view, |
| 639 |
scale_models[model.get_key_for_orientation('horizontal')] |
| 640 |
); |
| 641 |
this.remove_from_padding_dict( |
| 642 |
this.y_pad_dict, |
| 643 |
view, |
| 644 |
scale_models[model.get_key_for_orientation('vertical')] |
| 645 |
); |
| 646 |
view.remove(); |
| 647 |
} |
| 648 |
|
| 649 |
async add_mark(model: MarkModel) { |
| 650 |
model.state_change.then(() => { |
| 651 |
model.on('data_updated redraw_legend', this.update_legend, this); |
| 652 |
}); |
| 653 |
|
| 654 |
const dummy_node = this.fig_marks |
| 655 |
.node() |
| 656 |
.appendChild(document.createElementNS(d3.namespaces.svg, 'g')); |
| 657 |
|
| 658 |
// @ts-ignore: We should use the type argument: this.create_child_view<Mark> |
| 659 |
const view: Mark = await this.create_child_view(model, { |
| 660 |
clip_id: this.clip_id, |
| 661 |
}); |
| 662 |
|
| 663 |
this.dummyNodes[view.cid] = dummy_node; |
| 664 |
|
| 665 |
view.on( |
| 666 |
'mark_padding_updated', |
| 667 |
() => { |
| 668 |
this.mark_padding_updated(view); |
| 669 |
}, |
| 670 |
this |
| 671 |
); |
| 672 |
view.on( |
| 673 |
'mark_scales_updated', |
| 674 |
() => { |
| 675 |
this.mark_scales_updated(view); |
| 676 |
}, |
| 677 |
this |
| 678 |
); |
| 679 |
|
| 680 |
let child_x_scale = |
| 681 |
view.model.get('scales')[view.model.get_key_for_dimension('x')]; |
| 682 |
let child_y_scale = |
| 683 |
view.model.get('scales')[view.model.get_key_for_dimension('y')]; |
| 684 |
if (child_x_scale === undefined) { |
| 685 |
child_x_scale = this.scale_x.model; |
| 686 |
} |
| 687 |
if (child_y_scale === undefined) { |
| 688 |
child_y_scale = this.scale_y.model; |
| 689 |
} |
| 690 |
this.update_padding_dict( |
| 691 |
this.x_pad_dict, |
| 692 |
view, |
| 693 |
child_x_scale, |
| 694 |
view.xPadding |
| 695 |
); |
| 696 |
this.update_padding_dict( |
| 697 |
this.y_pad_dict, |
| 698 |
view, |
| 699 |
child_y_scale, |
| 700 |
view.yPadding |
| 701 |
); |
| 702 |
|
| 703 |
// If the mark needs a WebGL renderer, we create it |
| 704 |
if (view['render_gl']) { |
| 705 |
this.createWebGLRenderer(); |
| 706 |
} |
| 707 |
// If the marks list changes before replace_dummy_nodes is called, we are not DOM |
| 708 |
// attached, and view.remove() in Figure.remove_mark will not remove this view from the DOM |
| 709 |
// but a later call to Figure.replace_dummy_nodes will attach it to the DOM again, leading |
| 710 |
// to a 'ghost' mark, originally reported in https://github.com/spacetelescope/jdaviz/issues/270 |
| 711 |
view.on('remove', () => { |
| 712 |
if (dummy_node.parentNode) { |
| 713 |
dummy_node.remove(); |
| 714 |
} |
| 715 |
}); |
| 716 |
|
| 717 |
return view; |
| 718 |
} |
| 719 |
|
| 720 |
update_paddings() { |
| 721 |
// Iterate over the paddings of the marks for each scale and store |
| 722 |
// the maximum padding for each scale on the X and Y in |
| 723 |
// xPaddingArr and yPaddingArr |
| 724 |
|
| 725 |
this.xPaddingArr = {}; |
| 726 |
this.yPaddingArr = {}; |
| 727 |
|
| 728 |
_.forEach(this.x_pad_dict, (dict: any, scale_id) => { |
| 729 |
let max = 0; |
| 730 |
_.forEach(dict, (value: number, key) => { |
| 731 |
max = Math.max(max, value); |
| 732 |
}); |
| 733 |
this.xPaddingArr[scale_id] = max; |
| 734 |
}); |
| 735 |
|
| 736 |
_.forEach(this.y_pad_dict, (dict: any, scale_id) => { |
| 737 |
let max = 0; |
| 738 |
_.forEach(dict, (value: number, key) => { |
| 739 |
max = Math.max(max, value); |
| 740 |
}); |
| 741 |
this.yPaddingArr[scale_id] = max; |
| 742 |
}); |
| 743 |
// This is for the figure to relayout everything to account for the |
| 744 |
// updated margins. |
| 745 |
this.trigger('margin_updated'); |
| 746 |
} |
| 747 |
|
| 748 |
// Phosphor shims |
| 749 |
update_plotarea_dimensions() { |
| 750 |
this.plotarea_width = this.width - this.margin.left - this.margin.right; |
| 751 |
this.plotarea_height = this.height - this.margin.top - this.margin.bottom; |
| 752 |
} |
| 753 |
|
| 754 |
relayout() { |
| 755 |
const relayoutImpl = () => { |
| 756 |
this.relayoutRequested = false; // reset relayout request |
| 757 |
const figureSize = this.getFigureSize(); |
| 758 |
|
| 759 |
if ( |
| 760 |
(this.width == figureSize.width && |
| 761 |
this.height == figureSize.height && |
| 762 |
!this.should_relayout) || |
| 763 |
!this.visible |
| 764 |
) { |
| 765 |
// Bypass relayout |
| 766 |
return; |
| 767 |
} |
| 768 |
this.should_relayout = false; |
| 769 |
|
| 770 |
this.width = figureSize.width; |
| 771 |
this.height = figureSize.height; |
| 772 |
// update ranges |
| 773 |
this.margin = this.model.get('fig_margin'); |
| 774 |
this.update_plotarea_dimensions(); |
| 775 |
// we hide it when the plot area is too small |
| 776 |
if (this.plotarea_width < 1 || this.plotarea_width < 1) { |
| 777 |
this.el.style.visibility = 'hidden'; |
| 778 |
return; // no need to continue setting properties, which can only produce errors in the js console |
| 779 |
} else { |
| 780 |
this.el.style.visibility = ''; |
| 781 |
} |
| 782 |
|
| 783 |
if (this.scale_x !== undefined && this.scale_x !== null) { |
| 784 |
this.scale_x.set_range([0, this.plotarea_width]); |
| 785 |
} |
| 786 |
|
| 787 |
if (this.scale_y !== undefined && this.scale_y !== null) { |
| 788 |
this.scale_y.set_range([this.plotarea_height, 0]); |
| 789 |
} |
| 790 |
|
| 791 |
// transform figure |
| 792 |
this.fig.attr( |
| 793 |
'transform', |
| 794 |
'translate(' + this.margin.left + ',' + this.margin.top + ')' |
| 795 |
); |
| 796 |
this.fig_background.attr( |
| 797 |
'transform', |
| 798 |
'translate(' + this.margin.left + ',' + this.margin.top + ')' |
| 799 |
); |
| 800 |
applyAttrs(this.title, { |
| 801 |
x: 0.5 * this.plotarea_width, |
| 802 |
y: -(this.margin.top / 2.0), |
| 803 |
dy: '1em', |
| 804 |
}); |
| 805 |
|
| 806 |
this.bg |
| 807 |
.attr('width', this.plotarea_width) |
| 808 |
.attr('height', this.plotarea_height); |
| 809 |
this.bg_events |
| 810 |
.attr('width', this.plotarea_width) |
| 811 |
.attr('height', this.plotarea_height); |
| 812 |
|
| 813 |
this.clip_path |
| 814 |
.attr('width', this.plotarea_width) |
| 815 |
.attr('height', this.plotarea_height); |
| 816 |
|
| 817 |
this.trigger('margin_updated'); |
| 818 |
this.update_legend(); |
| 819 |
this.layout_webgl_canvas(); |
| 820 |
}; |
| 821 |
|
| 822 |
if (!this.relayoutRequested) { |
| 823 |
this.relayoutRequested = true; // avoid scheduling a relayout twice |
| 824 |
requestAnimationFrame(relayoutImpl.bind(this)); |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
layout_webgl_canvas() { |
| 829 |
if (this.renderer) { |
| 830 |
this.renderer.domElement.style = |
| 831 |
'left: ' + |
| 832 |
this.margin.left + |
| 833 |
'px; ' + |
| 834 |
'top: ' + |
| 835 |
this.margin.top + |
| 836 |
'px;'; |
| 837 |
this.renderer.setSize(this.plotarea_width, this.plotarea_height); |
| 838 |
this.update_gl(); |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
update_legend() { |
| 843 |
this.fig_marks.selectAll('.g_legend').remove(); |
| 844 |
|
| 845 |
const legend_height = 14; |
| 846 |
const legend_width = 24; |
| 847 |
const legend_location = this.model.get('legend_location'); |
| 848 |
|
| 849 |
const legend_g = this.fig_marks.append('g').attr('class', 'g_legend'); |
| 850 |
|
| 851 |
let count = 1; |
| 852 |
let max_label_len = 1; |
| 853 |
|
| 854 |
if (this.mark_views !== undefined && this.mark_views !== null) { |
| 855 |
Promise.all(this.mark_views.views).then((views) => { |
| 856 |
views.forEach((mark_view: any) => { |
| 857 |
if (mark_view.model.get('display_legend')) { |
| 858 |
const child_count = mark_view.draw_legend( |
| 859 |
legend_g, |
| 860 |
0, |
| 861 |
count * (legend_height + 2), |
| 862 |
0, |
| 863 |
legend_height + 2 |
| 864 |
); |
| 865 |
count = count + child_count[0]; |
| 866 |
max_label_len = child_count[1] |
| 867 |
? Math.max(max_label_len, child_count[1]) |
| 868 |
: max_label_len; |
| 869 |
} |
| 870 |
}); |
| 871 |
|
| 872 |
const coords = this.get_legend_coords( |
| 873 |
legend_location, |
| 874 |
legend_width, |
| 875 |
(count + 1) * (legend_height + 2), |
| 876 |
0 |
| 877 |
); |
| 878 |
if (count !== 1) { |
| 879 |
legend_g |
| 880 |
.insert('g', ':first-child') |
| 881 |
.attr('class', 'axis') |
| 882 |
.append('rect') |
| 883 |
.attr('y', (legend_height + 2) / 2.0) |
| 884 |
.attr('x', -0.5 * (legend_height + 2)) |
| 885 |
.attr('width', max_label_len + 2 + 'em') |
| 886 |
.attr('height', count * (legend_height + 2)); |
| 887 |
} |
| 888 |
max_label_len = |
| 889 |
legend_location === 'top-right' || |
| 890 |
legend_location === 'right' || |
| 891 |
legend_location === 'bottom-right' |
| 892 |
? -(max_label_len + 2) |
| 893 |
: 1; |
| 894 |
const em = 16; |
| 895 |
legend_g.attr( |
| 896 |
'transform', |
| 897 |
'translate(' + |
| 898 |
String(coords[0] + max_label_len * em) + |
| 899 |
' ' + |
| 900 |
String(coords[1]) + |
| 901 |
') ' |
| 902 |
); |
| 903 |
|
| 904 |
applyStyles( |
| 905 |
legend_g.selectAll('text.legendtext'), |
| 906 |
this.model.get('legend_text') |
| 907 |
); |
| 908 |
|
| 909 |
applyStyles( |
| 910 |
legend_g.selectAll('.axis').selectAll('rect'), |
| 911 |
this.model.get('legend_style') |
| 912 |
); |
| 913 |
}); |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
get_legend_coords( |
| 918 |
legend_location: |
| 919 |
| 'top' |
| 920 |
| 'top-right' |
| 921 |
| 'right' |
| 922 |
| 'bottom-right' |
| 923 |
| 'bottom' |
| 924 |
| 'bottom-left' |
| 925 |
| 'left', |
| 926 |
width: number, |
| 927 |
height: number, |
| 928 |
disp: number |
| 929 |
) { |
| 930 |
let x_start = 0; |
| 931 |
let y_start = 0; |
| 932 |
const fig_width = this.plotarea_width; |
| 933 |
const fig_height = this.plotarea_height; |
| 934 |
|
| 935 |
switch (legend_location) { |
| 936 |
case 'top': |
| 937 |
x_start = fig_width * 0.5 - width; |
| 938 |
y_start = 0; |
| 939 |
break; |
| 940 |
case 'top-right': |
| 941 |
x_start = fig_width - disp; |
| 942 |
y_start = 0; |
| 943 |
break; |
| 944 |
case 'right': |
| 945 |
x_start = fig_width - disp; |
| 946 |
y_start = fig_height * 0.5 - height; |
| 947 |
break; |
| 948 |
case 'bottom-right': |
| 949 |
x_start = fig_width - disp; |
| 950 |
y_start = fig_height - height; |
| 951 |
break; |
| 952 |
case 'bottom': |
| 953 |
x_start = fig_width * 0.5 - width; |
| 954 |
y_start = fig_height - height; |
| 955 |
break; |
| 956 |
case 'bottom-left': |
| 957 |
x_start = 0; |
| 958 |
y_start = fig_height - height; |
| 959 |
break; |
| 960 |
case 'left': |
| 961 |
x_start = 0; |
| 962 |
y_start = fig_height * 0.5 - height; |
| 963 |
break; |
| 964 |
default: |
| 965 |
x_start = 0; |
| 966 |
y_start = 0; |
| 967 |
} |
| 968 |
return [x_start, y_start]; |
| 969 |
} |
| 970 |
|
| 971 |
async set_interaction(model: WidgetModel | null): Promise<WidgetView> { |
| 972 |
if (model) { |
| 973 |
// Sets the child interaction |
| 974 |
await model.state_change; |
| 975 |
|
| 976 |
// Sets the child interaction |
| 977 |
// @ts-ignore: We should use the type argument: this.create_child_view<Interaction> |
| 978 |
const view: Interaction = await this.create_child_view(model); |
| 979 |
|
| 980 |
if (this.interaction_view) { |
| 981 |
this.interaction_view.remove(); |
| 982 |
} |
| 983 |
this.interaction_view = view; |
| 984 |
this.interaction.node().appendChild(view.el); |
| 985 |
this.displayed.then(() => { |
| 986 |
view.trigger('displayed'); |
| 987 |
}); |
| 988 |
return view; |
| 989 |
} else { |
| 990 |
if (this.interaction_view) { |
| 991 |
this.interaction_view.remove(); |
| 992 |
} |
| 993 |
return Promise.resolve(null); |
| 994 |
} |
| 995 |
} |
| 996 |
|
| 997 |
update_title(model, title) { |
| 998 |
this.title.text(this.model.get('title')); |
| 999 |
} |
| 1000 |
|
| 1001 |
remove() { |
| 1002 |
if (this.mark_views !== undefined && this.mark_views !== null) { |
| 1003 |
this.mark_views.remove(); |
| 1004 |
} |
| 1005 |
if (this.axis_views !== undefined && this.axis_views !== null) { |
| 1006 |
this.axis_views.remove(); |
| 1007 |
} |
| 1008 |
if (this.tooltip_div !== undefined) { |
| 1009 |
this.tooltip_div.remove(); |
| 1010 |
} |
| 1011 |
this.intersectObserver.disconnect(); |
| 1012 |
this.resizeObserver.disconnect(); |
| 1013 |
return super.remove.apply(this, arguments); |
| 1014 |
} |
| 1015 |
|
| 1016 |
get_svg() { |
| 1017 |
// Returns the outer html of the figure svg |
| 1018 |
|
| 1019 |
const replaceAll = (find, replace, str) => { |
| 1020 |
return str.replace(new RegExp(find, 'g'), replace); |
| 1021 |
}; |
| 1022 |
|
| 1023 |
const get_css = (node, regs) => { |
| 1024 |
/** |
| 1025 |
* Gathers all the css rules applied to elements of the svg |
| 1026 |
* node. Removes the parent element selectors specified in |
| 1027 |
* argument `regs`. |
| 1028 |
*/ |
| 1029 |
let css = ''; |
| 1030 |
const sheets = document.styleSheets; |
| 1031 |
let selector; |
| 1032 |
for (let i = 0; i < sheets.length; i++) { |
| 1033 |
let rules: any = null; |
| 1034 |
// due to CORS we may have some sheets we cannot access, instead of checking we always try |
| 1035 |
try { |
| 1036 |
rules = (sheets[i] as CSSStyleSheet).cssRules; |
| 1037 |
} catch (e) { |
| 1038 |
// ignore CORS errors |
| 1039 |
} |
| 1040 |
if (rules) { |
| 1041 |
for (let j = 0; j < rules.length; j++) { |
| 1042 |
const rule = rules[j]; |
| 1043 |
if (typeof rule.style !== 'undefined') { |
| 1044 |
let match = null; |
| 1045 |
try { |
| 1046 |
match = node.querySelectorAll(rule.selectorText); |
| 1047 |
} catch (err) { |
| 1048 |
console.warn( |
| 1049 |
"Invalid CSS selector '" + rule.selectorText + "'", |
| 1050 |
err |
| 1051 |
); |
| 1052 |
} |
| 1053 |
if (match) { |
| 1054 |
const elems = node.querySelectorAll(rule.selectorText); |
| 1055 |
if (elems.length > 0) { |
| 1056 |
selector = rule.selectorText; |
| 1057 |
for (let r = 0; r < regs.length; r++) { |
| 1058 |
selector = replaceAll(regs[r], '', selector); |
| 1059 |
} |
| 1060 |
css += `${selector} { ${rule.style.cssText} } |
| 1061 |
`; |
| 1062 |
} |
| 1063 |
} else if (rule.cssText.match(/^@font-face/)) { |
| 1064 |
css += rule.cssText + '\n'; |
| 1065 |
} |
| 1066 |
} |
| 1067 |
} |
| 1068 |
} |
| 1069 |
} |
| 1070 |
// TODO: this is terrible. The previous loop over style sheets |
| 1071 |
// does not catch document's top-level properties. |
| 1072 |
css += 'svg { font-size: 10px; }\n'; |
| 1073 |
return css; |
| 1074 |
}; |
| 1075 |
|
| 1076 |
// Even though the canvas may display the rendering already, it is not guaranteed it can be read of the canvas |
| 1077 |
// or we have to set preserveDrawingBuffer to true, which may impact performance. |
| 1078 |
// Instead, we render again, and directly afterwards we do get the pixel data using canvas.toDataURL |
| 1079 |
return this.render_gl().then(async () => { |
| 1080 |
// Create standalone SVG string |
| 1081 |
const node_background: any = this.svg_background.node(); |
| 1082 |
const node_foreground: any = this.svg.node(); |
| 1083 |
const width = this.plotarea_width; |
| 1084 |
const height = this.plotarea_height; |
| 1085 |
|
| 1086 |
// Creates a standalone SVG string from an inline SVG element |
| 1087 |
// containing all the computed style attributes. |
| 1088 |
const svg = node_foreground.cloneNode(true); |
| 1089 |
// images can contain blob urls, we transform those to data urls |
| 1090 |
const blobToDataUrl = async (el) => { |
| 1091 |
const blob = await (await fetch(el.getAttribute('href'))).blob(); |
| 1092 |
const reader = new FileReader(); |
| 1093 |
const readerPromise = new Promise((resolve, reject) => { |
| 1094 |
reader.onloadend = resolve; |
| 1095 |
reader.onerror = reject; |
| 1096 |
reader.abort = reject; |
| 1097 |
}); |
| 1098 |
reader.readAsDataURL(blob); |
| 1099 |
await readerPromise; |
| 1100 |
el.setAttribute('href', reader.result); |
| 1101 |
}; |
| 1102 |
const images = [...svg.querySelectorAll('image')]; |
| 1103 |
await Promise.all(images.map(blobToDataUrl)); |
| 1104 |
svg.setAttribute('version', '1.1'); |
| 1105 |
svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg'); |
| 1106 |
svg.setAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink'); |
| 1107 |
svg.setAttribute('width', this.width); |
| 1108 |
svg.setAttribute('height', this.height); |
| 1109 |
svg.style.backgroundColor = getEffectiveBackgroundColor(this.el); |
| 1110 |
|
| 1111 |
const computedStyle = window.getComputedStyle(this.el); |
| 1112 |
const cssCode = |
| 1113 |
get_css(this.el, [ |
| 1114 |
'.theme-dark', |
| 1115 |
'.theme-light', |
| 1116 |
'.bqplot > ', |
| 1117 |
':root', |
| 1118 |
]) + '\n'; |
| 1119 |
// extract all CSS variables, and generate a piece of css to define the variables |
| 1120 |
const cssVariables = cssCode.match(/(--\w[\w-]*)/g) || []; |
| 1121 |
const cssVariableCode = |
| •1122 |
// @ts-ignore |
| 1123 |
cssVariables.reduce((cssCode, variable) => { |
| 1124 |
const value = computedStyle.getPropertyValue(variable); |
| 1125 |
return `${cssCode}\n\t${variable}: ${value};`; |
| 1126 |
}, ':root {') + '\n}\n'; |
| 1127 |
|
| 1128 |
// and put the CSS in a style element |
| 1129 |
const styleElement = document.createElement('style'); |
| 1130 |
styleElement.setAttribute('type', 'text/css'); |
| 1131 |
styleElement.innerHTML = |
| 1132 |
'<![CDATA[\n' + cssVariableCode + cssCode + ']]>'; |
| 1133 |
|
| 1134 |
const defs = document.createElement('defs'); |
| 1135 |
defs.appendChild(styleElement); |
| 1136 |
// we put the svg background part before the marks |
| 1137 |
const g_root = svg.children[0]; |
| 1138 |
const svg_background = node_background.cloneNode(true); |
| 1139 |
// first the axes |
| 1140 |
g_root.insertBefore( |
| 1141 |
svg_background.children[0].children[1], |
| 1142 |
g_root.children[0] |
| 1143 |
); |
| 1144 |
// and the background as first element |
| 1145 |
g_root.insertBefore( |
| 1146 |
svg_background.children[0].children[0], |
| 1147 |
g_root.children[0] |
| 1148 |
); |
| 1149 |
|
| 1150 |
// and add the webgl canvas as an image |
| 1151 |
if (this.renderer) { |
| 1152 |
const data_url = this.renderer.domElement.toDataURL('image/png'); |
| 1153 |
const marks = d3.select(g_root.children[3]); |
| 1154 |
marks |
| 1155 |
.insert('image', ':first-child') |
| 1156 |
.attr('x', 0) |
| 1157 |
.attr('y', 0) |
| 1158 |
.attr('width', 1) |
| 1159 |
.attr('height', 1) |
| 1160 |
.attr('preserveAspectRatio', 'none') |
| 1161 |
.attr('transform', 'scale(' + width + ', ' + height + ')') |
| 1162 |
.attr('href', data_url); |
| 1163 |
} |
| 1164 |
|
| 1165 |
svg.insertBefore(defs, svg.firstChild); |
| 1166 |
// Getting the outer HTML |
| 1167 |
return svg.outerHTML; |
| 1168 |
}); |
| 1169 |
} |
| 1170 |
|
| 1171 |
async get_rendered_canvas(scale): Promise<HTMLCanvasElement> { |
| 1172 |
// scale up the underlying canvas for high dpi screens |
| 1173 |
// such that image is of the same quality |
| 1174 |
scale = scale || window.devicePixelRatio; |
| 1175 |
// Render a SVG data into a canvas and |
| 1176 |
const xml = await this.get_svg(); |
| 1177 |
|
| 1178 |
return new Promise((resolve) => { |
| 1179 |
const image = new Image(); |
| 1180 |
image.onload = () => { |
| 1181 |
const canvas = document.createElement('canvas'); |
| 1182 |
canvas.classList.add('bqplot'); |
| 1183 |
canvas.width = this.width * scale; |
| 1184 |
canvas.height = this.height * scale; |
| 1185 |
canvas.style.width = this.width.toString(); |
| 1186 |
canvas.style.height = this.height.toString(); |
| 1187 |
const context = canvas.getContext('2d'); |
| 1188 |
context.scale(scale, scale); |
| 1189 |
context.drawImage(image, 0, 0); |
| 1190 |
resolve(canvas); |
| 1191 |
}; |
| 1192 |
image.src = |
| 1193 |
'data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(xml))); |
| 1194 |
}); |
| 1195 |
} |
| 1196 |
|
| 1197 |
async upload_png(model, scale) { |
| 1198 |
const canvas = await this.get_rendered_canvas(scale); |
| 1199 |
canvas.toBlob(async (blob) => { |
| 1200 |
const buff = await blob.arrayBuffer(); |
| 1201 |
model.send( |
| 1202 |
{ |
| 1203 |
event: 'upload_png', |
| 1204 |
}, |
| 1205 |
null, |
| 1206 |
[buff] |
| 1207 |
); |
| 1208 |
}); |
| 1209 |
} |
| 1210 |
|
| 1211 |
save_png(filename, scale) { |
| 1212 |
// Render a SVG data into a canvas and download as PNG. |
| 1213 |
|
| 1214 |
this.get_rendered_canvas(scale).then((canvas: any) => { |
| 1215 |
const a = document.createElement('a'); |
| 1216 |
a.download = filename || 'image.png'; |
| 1217 |
a.href = canvas.toDataURL('image/png'); |
| 1218 |
document.body.appendChild(a); |
| 1219 |
a.click(); |
| 1220 |
}); |
| 1221 |
} |
| 1222 |
|
| 1223 |
save_svg(filename: string) { |
| 1224 |
this.get_svg().then((xml) => { |
| 1225 |
const a = document.createElement('a'); |
| 1226 |
a.download = filename || 'bqplot.svg'; |
| 1227 |
a.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(xml); |
| 1228 |
document.body.appendChild(a); |
| 1229 |
a.click(); |
| 1230 |
document.body.removeChild(a); |
| 1231 |
}); |
| 1232 |
} |
| 1233 |
|
| 1234 |
async getPixel(x, y) { |
| 1235 |
const canvas = await this.get_rendered_canvas(window.devicePixelRatio); |
| 1236 |
const context = canvas.getContext('2d'); |
| 1237 |
const pixel = context.getImageData( |
| 1238 |
x * window.devicePixelRatio, |
| 1239 |
y * window.devicePixelRatio, |
| 1240 |
1, |
| 1241 |
1 |
| 1242 |
); |
| 1243 |
return pixel.data; |
| 1244 |
} |
| 1245 |
|
| 1246 |
update_gl() { |
| 1247 |
if (!this._update_requested) { |
| 1248 |
this._update_requested = true; |
| 1249 |
requestAnimationFrame(this._update_gl.bind(this)); |
| 1250 |
} |
| 1251 |
} |
| 1252 |
|
| 1253 |
_update_gl() { |
| 1254 |
this.render_gl(); |
| 1255 |
this._update_requested = false; |
| 1256 |
} |
| 1257 |
|
| 1258 |
async render_gl(): Promise<void> { |
| 1259 |
// Nothing to render using a WebGL context |
| 1260 |
if (!this.renderer) { |
| 1261 |
return Promise.resolve(); |
| 1262 |
} |
| 1263 |
|
| 1264 |
const views = await Promise.all(this.mark_views.views); |
| 1265 |
|
| 1266 |
// render all marks that have a render_gl method |
| 1267 |
this.renderer.autoClear = false; |
| 1268 |
this.renderer.autoClearColor = new (THREE.Color as (x) => void)(0x000000); |
| 1269 |
this.renderer.clear(); |
| 1270 |
const marks_gl = _.filter(views, (view: Mark) => view['render_gl']); |
| 1271 |
|
| 1272 |
for (const mark of marks_gl) { |
| 1273 |
mark['render_gl'](); |
| 1274 |
} |
| 1275 |
} |
| 1276 |
|
| 1277 |
change_theme() { |
| 1278 |
this.el.classList.remove(this.model.previous('theme')); |
| 1279 |
this.el.classList.add(this.model.get('theme')); |
| 1280 |
} |
| 1281 |
|
| 1282 |
axis_views: widgets.ViewList<widgets.DOMWidgetView>; |
| 1283 |
bg: d3.Selection<SVGRectElement, any, any, any>; |
| 1284 |
bg_events: d3.Selection<SVGRectElement, any, any, any>; |
| 1285 |
change_layout: () => void; |
| 1286 |
clip_id: string; |
| 1287 |
clip_path: d3.Selection<SVGRectElement, any, any, any>; |
| 1288 |
debouncedRelayout: () => void; |
| 1289 |
fig_axes: d3.Selection<SVGGraphicsElement, any, any, any>; |
| 1290 |
fig_marks: d3.Selection<SVGGraphicsElement, any, any, any>; |
| 1291 |
fig_background: d3.Selection<SVGGraphicsElement, any, any, any>; |
| 1292 |
fig: d3.Selection<SVGGraphicsElement, any, any, any>; |
| 1293 |
figure_padding_x: number; |
| 1294 |
figure_padding_y: number; |
| 1295 |
height: number; |
| 1296 |
interaction_view: Interaction; |
| 1297 |
interaction: d3.Selection<SVGGElement, any, any, any>; |
| 1298 |
margin: { top: number; bottom: number; left: number; right: number }; |
| 1299 |
mark_views: widgets.ViewList<Mark>; |
| 1300 |
plotarea_height: number; |
| 1301 |
plotarea_width: number; |
| 1302 |
popper_reference: popper.ReferenceObject; |
| 1303 |
popper: popper; |
| 1304 |
renderer: THREE.WebGLRenderer | null; |
| 1305 |
scale_x: Scale; |
| 1306 |
scale_y: Scale; |
| 1307 |
svg: d3.Selection<SVGElement, any, any, any>; |
| 1308 |
svg_background: d3.Selection<SVGElement, any, any, any>; |
| 1309 |
title: d3.Selection<SVGTextElement, any, any, any>; |
| 1310 |
tooltip_div: d3.Selection<HTMLDivElement, any, any, any>; |
| 1311 |
width: number; |
| 1312 |
x_pad_dict: { [id: string]: number }; |
| 1313 |
xPaddingArr: { [id: string]: number }; |
| 1314 |
y_pad_dict: { [id: string]: number }; |
| 1315 |
yPaddingArr: { [id: string]: number }; |
| 1316 |
intersectObserver: IntersectionObserver; |
| 1317 |
resizeObserver: ResizeObserver; |
| 1318 |
visible: boolean; |
| 1319 |
|
| 1320 |
private dummyNodes: Dict<any> = {}; |
| 1321 |
|
| 1322 |
private _update_requested: boolean; |
| 1323 |
private relayoutRequested: boolean = false; |
| 1324 |
|
| 1325 |
// this is public for the test framework, but considered a private API |
| 1326 |
public _initial_marks_created: Promise<any>; |
| 1327 |
private _initial_marks_created_resolve: Function; |
| 1328 |
private should_relayout = false; |
| 1329 |
} |