| 1 |
// some helper functions to quickly create widgets |
| 2 |
|
| 3 |
export async function create_model_bqplot( |
| 4 |
manager, |
| 5 |
name: string, |
| 6 |
id: string, |
| 7 |
args: Object |
| 8 |
) { |
| 9 |
return create_model(manager, 'bqplot', `${name}Model`, name, id, args); |
| 10 |
} |
| 11 |
|
| 12 |
export async function create_model( |
| 13 |
manager, |
| 14 |
module: string, |
| 15 |
model: string, |
| 16 |
view: string, |
| 17 |
id: string, |
| 18 |
args = {} |
| 19 |
) { |
| 20 |
const model_widget = await manager.new_widget( |
| 21 |
{ |
| 22 |
model_module: module, |
| 23 |
model_name: model, |
| 24 |
model_module_version: '*', |
| 25 |
view_module: module, |
| 26 |
view_name: view, |
| 27 |
view_module_version: '*', |
| 28 |
model_id: id, |
| 29 |
}, |
| 30 |
args |
| 31 |
); |
| 32 |
return model_widget; |
| 33 |
} |
| 34 |
|
| 35 |
export async function create_view(manager, model, options = {}) { |
| 36 |
const view = await manager.create_view(model, options); |
| 37 |
return view; |
| 38 |
} |
| 39 |
|
| 40 |
export async function create_widget( |
| 41 |
manager, |
| 42 |
name: string, |
| 43 |
id: string, |
| 44 |
args: Object |
| 45 |
) { |
| 46 |
const model = await create_model_bqplot(manager, name, id, args); |
| 47 |
const view = await manager.create_view(model); |
| 48 |
await manager.display_view(undefined, view); |
| 49 |
return { model: model, view: view }; |
| 50 |
} |
| 51 |
|
| 52 |
export async function create_figure_scatter( |
| 53 |
manager, |
| 54 |
x, |
| 55 |
y, |
| 56 |
mega = false, |
| 57 |
log = false |
| 58 |
) { |
| 59 |
const layout = await create_model( |
| 60 |
manager, |
| 61 |
'@jupyter-widgets/base', |
| 62 |
'LayoutModel', |
| 63 |
'LayoutView', |
| 64 |
'layout_figure1', |
| 65 |
{ _dom_classes: '', width: '400px', height: '500px' } |
| 66 |
); |
| 67 |
let scale_x; |
| 68 |
let scale_y; |
| 69 |
if (log) { |
| 70 |
scale_x = await create_model_bqplot(manager, 'LogScale', 'scale_x', { |
| 71 |
min: 0.01, |
| 72 |
max: 100, |
| 73 |
allow_padding: false, |
| 74 |
}); |
| 75 |
scale_y = await create_model_bqplot(manager, 'LogScale', 'scale_y', { |
| 76 |
min: 0.1, |
| 77 |
max: 10, |
| 78 |
allow_padding: false, |
| 79 |
}); |
| 80 |
} else { |
| 81 |
scale_x = await create_model_bqplot(manager, 'LinearScale', 'scale_x', { |
| 82 |
min: 0, |
| 83 |
max: 1, |
| 84 |
allow_padding: false, |
| 85 |
}); |
| 86 |
scale_y = await create_model_bqplot(manager, 'LinearScale', 'scale_y', { |
| 87 |
min: 2, |
| 88 |
max: 3, |
| 89 |
allow_padding: false, |
| 90 |
}); |
| 91 |
} |
| 92 |
// TODO: the default values for the ColorScale should not be required, but defined in the defaults method |
| 93 |
const scale_color = await create_model_bqplot( |
| 94 |
manager, |
| 95 |
'ColorScale', |
| 96 |
'scale_color', |
| 97 |
{ scheme: 'RdYlGn', colors: [] } |
| 98 |
); |
| 99 |
const scales = { |
| 100 |
x: scale_x.toJSON(), |
| 101 |
y: scale_y.toJSON(), |
| 102 |
color: scale_color.toJSON(), |
| 103 |
}; |
| 104 |
const color = null; |
| 105 |
const size = null; |
| 106 |
const opacity = null; |
| 107 |
const rotation = null; |
| 108 |
const skew = null; |
| 109 |
|
| 110 |
const scatterModel = await create_model_bqplot( |
| 111 |
manager, |
| 112 |
mega ? 'ScatterGL' : 'Scatter', |
| 113 |
'scatter1', |
| 114 |
{ |
| 115 |
scales: scales, |
| 116 |
x: x, |
| 117 |
y: y, |
| 118 |
color: color, |
| 119 |
size: size, |
| 120 |
opacity: opacity, |
| 121 |
rotation: rotation, |
| 122 |
skew: skew, |
| 123 |
colors: ['steelblue'], |
| 124 |
visible: true, |
| 125 |
default_size: 64, |
| 126 |
selected_style: {}, |
| 127 |
unselected_style: {}, |
| 128 |
hovered_style: {}, |
| 129 |
unhovered_style: {}, |
| 130 |
preserve_domain: {}, |
| 131 |
_view_module_version: '*', |
| 132 |
_view_module: 'bqplot', |
| 133 |
} |
| 134 |
); |
| 135 |
let figureModel; |
| 136 |
try { |
| 137 |
figureModel = await create_model_bqplot(manager, 'Figure', 'figure1', { |
| 138 |
scale_x: scales['x'], |
| 139 |
scale_y: scales['y'], |
| 140 |
layout: layout.toJSON(), |
| 141 |
_dom_classes: [], |
| 142 |
figure_padding_y: 0, |
| 143 |
fig_margin: { bottom: 0, left: 0, right: 0, top: 0 }, |
| 144 |
marks: [scatterModel.toJSON()], |
| 145 |
}); |
| 146 |
} catch (e) { |
| 147 |
console.error('error', e); |
| 148 |
} |
| 149 |
const figure = await create_view(manager, figureModel); |
| 150 |
await manager.display_view(undefined, figure); |
| 151 |
await figure._initial_marks_created; |
| 152 |
return { figure: figure, scatter: await figure.mark_views.views[0] }; |
| 153 |
} |
| 154 |
|
| 155 |
export async function create_figure_lines(manager, x, y, default_scales = {}) { |
| 156 |
const layout = await create_model( |
| 157 |
manager, |
| 158 |
'@jupyter-widgets/base', |
| 159 |
'LayoutModel', |
| 160 |
'LayoutView', |
| 161 |
'layout_figure1', |
| 162 |
{ _dom_classes: '', width: '400px', height: '500px' } |
| 163 |
); |
| 164 |
const scale_x = await create_model_bqplot(manager, 'LinearScale', 'scale_x', { |
| 165 |
min: 0, |
| 166 |
max: 1, |
| 167 |
allow_padding: false, |
| 168 |
}); |
| 169 |
const scale_y = await create_model_bqplot(manager, 'LinearScale', 'scale_y', { |
| 170 |
min: 2, |
| 171 |
max: 3, |
| 172 |
allow_padding: false, |
| 173 |
}); |
| 174 |
const scales = { x: scale_x.toJSON(), y: scale_y.toJSON() }; |
| 175 |
const scales_mark = { |
| 176 |
x: default_scales['x'] || scale_x.toJSON(), |
| 177 |
y: default_scales['y'] || scale_y.toJSON(), |
| 178 |
}; |
| 179 |
const color = null; |
| 180 |
const size = null; |
| 181 |
const opacity = null; |
| 182 |
const rotation = null; |
| 183 |
const skew = null; |
| 184 |
|
| 185 |
const linesModel = await create_model_bqplot(manager, 'Lines', 'lines1', { |
| 186 |
scales: scales_mark, |
| 187 |
x: x, |
| 188 |
y: y, |
| 189 |
color: color, |
| 190 |
size: size, |
| 191 |
opacity: opacity, |
| 192 |
rotation: rotation, |
| 193 |
skew: skew, |
| 194 |
visible: true, |
| 195 |
default_size: 64, |
| 196 |
preserve_domain: {}, |
| 197 |
_view_module_version: '*', |
| 198 |
_view_module: 'bqplot', |
| 199 |
}); |
| 200 |
let figureModel; |
| 201 |
try { |
| 202 |
figureModel = await create_model_bqplot(manager, 'Figure', 'figure1', { |
| 203 |
scale_x: scales['x'], |
| 204 |
scale_y: scales['y'], |
| 205 |
layout: layout.toJSON(), |
| 206 |
_dom_classes: [], |
| 207 |
figure_padding_y: 0, |
| 208 |
fig_margin: { bottom: 0, left: 0, right: 0, top: 0 }, |
| 209 |
marks: [linesModel.toJSON()], |
| 210 |
}); |
| 211 |
} catch (e) { |
| 212 |
console.error('error', e); |
| 213 |
} |
| 214 |
const figure = await create_view(manager, figureModel); |
| 215 |
await manager.display_view(undefined, figure); |
| 216 |
await figure._initial_marks_created; |
| 217 |
return { figure: figure, lines: await figure.mark_views.views[0] }; |
| 218 |
} |
| 219 |
|
| 220 |
export async function create_figure_pie(manager, sizes, labels) { |
| 221 |
const layout = await create_model( |
| 222 |
manager, |
| 223 |
'@jupyter-widgets/base', |
| 224 |
'LayoutModel', |
| 225 |
'LayoutView', |
| 226 |
'layout_figure1', |
| 227 |
{ _dom_classes: '', width: '400px', height: '500px' } |
| 228 |
); |
| 229 |
|
| 230 |
const scale_x = await create_model_bqplot(manager, 'LinearScale', 'scale_x', { |
| 231 |
allow_padding: false, |
| 232 |
}); |
| 233 |
const scale_y = await create_model_bqplot(manager, 'LinearScale', 'scale_y', { |
| 234 |
allow_padding: false, |
| 235 |
}); |
| 236 |
|
| 237 |
const pieModel = await create_model_bqplot(manager, 'Pie', 'pie1', { |
| 238 |
sizes: sizes, |
| 239 |
labels: labels, |
| 240 |
visible: true, |
| 241 |
default_size: 64, |
| 242 |
preserve_domain: {}, |
| 243 |
_view_module_version: '*', |
| 244 |
_view_module: 'bqplot', |
| 245 |
}); |
| 246 |
let figureModel; |
| 247 |
try { |
| 248 |
figureModel = await create_model_bqplot(manager, 'Figure', 'figure1', { |
| 249 |
scale_x: scale_x.toJSON(), |
| 250 |
scale_y: scale_y.toJSON(), |
| 251 |
layout: layout.toJSON(), |
| 252 |
_dom_classes: [], |
| 253 |
figure_padding_y: 0, |
| 254 |
fig_margin: { bottom: 0, left: 0, right: 0, top: 0 }, |
| 255 |
marks: [pieModel.toJSON()], |
| 256 |
}); |
| 257 |
} catch (e) { |
| 258 |
console.error('error', e); |
| 259 |
} |
| 260 |
const figure = await create_view(manager, figureModel); |
| 261 |
await manager.display_view(undefined, figure); |
| 262 |
await figure._initial_marks_created; |
| 263 |
return { figure: figure, pie: await figure.mark_views.views[0] }; |
| 264 |
} |
| 265 |
|
| 266 |
export async function create_figure_bars(manager, x, y) { |
| 267 |
const layout = await create_model( |
| 268 |
manager, |
| 269 |
'@jupyter-widgets/base', |
| 270 |
'LayoutModel', |
| 271 |
'LayoutView', |
| 272 |
'layout_figure1', |
| 273 |
{ _dom_classes: '', width: '400px', height: '500px' } |
| 274 |
); |
| 275 |
const scale_x = await create_model_bqplot(manager, 'LinearScale', 'scale_x', { |
| 276 |
min: 0, |
| 277 |
max: 1, |
| 278 |
allow_padding: false, |
| 279 |
}); |
| 280 |
const scale_y = await create_model_bqplot(manager, 'LinearScale', 'scale_y', { |
| 281 |
min: 0, |
| 282 |
max: 3, |
| 283 |
allow_padding: false, |
| 284 |
}); |
| 285 |
const scales = { x: scale_x.toJSON(), y: scale_y.toJSON() }; |
| 286 |
const color = null; |
| 287 |
const size = null; |
| 288 |
const opacity = null; |
| 289 |
const rotation = null; |
| 290 |
const skew = null; |
| 291 |
|
| 292 |
const barsModel = await create_model_bqplot(manager, 'Bars', 'bars1', { |
| 293 |
scales: scales, |
| 294 |
x: x, |
| 295 |
y: y, |
| 296 |
color: color, |
| 297 |
size: size, |
| 298 |
opacity: opacity, |
| 299 |
rotation: rotation, |
| 300 |
skew: skew, |
| 301 |
visible: true, |
| 302 |
default_size: 64, |
| 303 |
preserve_domain: {}, |
| 304 |
_view_module_version: '*', |
| 305 |
_view_module: 'bqplot', |
| 306 |
}); |
| 307 |
let figureModel; |
| 308 |
try { |
| 309 |
figureModel = await create_model_bqplot(manager, 'Figure', 'figure1', { |
| 310 |
scale_x: scales['x'], |
| 311 |
scale_y: scales['y'], |
| 312 |
layout: layout.toJSON(), |
| 313 |
_dom_classes: [], |
| 314 |
figure_padding_y: 0, |
| 315 |
fig_margin: { bottom: 0, left: 0, right: 0, top: 0 }, |
| 316 |
marks: [barsModel.toJSON()], |
| 317 |
}); |
| 318 |
} catch (e) { |
| 319 |
console.error('error', e); |
| 320 |
} |
| 321 |
const figure = await create_view(manager, figureModel); |
| 322 |
await manager.display_view(undefined, figure); |
| 323 |
await figure._initial_marks_created; |
| 324 |
return { figure: figure, bars: await figure.mark_views.views[0] }; |
| 325 |
} |
| 326 |
|
| 327 |
export async function create_figure_hist(manager, sample, bins) { |
| 328 |
const layout = await create_model( |
| 329 |
manager, |
| 330 |
'@jupyter-widgets/base', |
| 331 |
'LayoutModel', |
| 332 |
'LayoutView', |
| 333 |
'layout_figure1', |
| 334 |
{ _dom_classes: '', width: '400px', height: '500px' } |
| 335 |
); |
| 336 |
const scale_sample = await create_model_bqplot( |
| 337 |
manager, |
| 338 |
'LinearScale', |
| 339 |
'scale_sample', |
| 340 |
{ allow_padding: false } |
| 341 |
); |
| 342 |
const scale_count = await create_model_bqplot( |
| 343 |
manager, |
| 344 |
'LinearScale', |
| 345 |
'scale_count', |
| 346 |
{ allow_padding: false } |
| 347 |
); |
| 348 |
const scales = { sample: scale_sample.toJSON(), count: scale_count.toJSON() }; |
| 349 |
|
| 350 |
const histModel = await create_model_bqplot(manager, 'Hist', 'hist1', { |
| 351 |
scales: scales, |
| 352 |
sample: sample, |
| 353 |
bins: bins, |
| 354 |
visible: true, |
| 355 |
default_size: 64, |
| 356 |
preserve_domain: {}, |
| 357 |
_view_module_version: '*', |
| 358 |
_view_module: 'bqplot', |
| 359 |
}); |
| 360 |
let figureModel; |
| 361 |
try { |
| 362 |
figureModel = await create_model_bqplot(manager, 'Figure', 'figure1', { |
| 363 |
scale_x: scales.sample, |
| 364 |
scale_y: scales.count, |
| 365 |
layout: layout.toJSON(), |
| 366 |
_dom_classes: [], |
| 367 |
figure_padding_y: 0, |
| 368 |
fig_margin: { bottom: 0, left: 0, right: 0, top: 0 }, |
| 369 |
marks: [histModel.toJSON()], |
| 370 |
}); |
| 371 |
} catch (e) { |
| 372 |
console.error('error', e); |
| 373 |
} |
| 374 |
const figure = await create_view(manager, figureModel); |
| 375 |
await manager.display_view(undefined, figure); |
| 376 |
await figure._initial_marks_created; |
| 377 |
return { figure: figure, hist: await figure.mark_views.views[0] }; |
| 378 |
} |
| 379 |
|
| 380 |
export async function create_figure_gridheatmap(manager, color) { |
| 381 |
const layout = await create_model( |
| 382 |
manager, |
| 383 |
'@jupyter-widgets/base', |
| 384 |
'LayoutModel', |
| 385 |
'LayoutView', |
| 386 |
'layout_figure1', |
| 387 |
{ _dom_classes: '', width: '400px', height: '500px' } |
| 388 |
); |
| 389 |
const scale_x = await create_model_bqplot(manager, 'LinearScale', 'scale_x', { |
| 390 |
min: 0, |
| 391 |
max: 1, |
| 392 |
allow_padding: false, |
| 393 |
}); |
| 394 |
const scale_y = await create_model_bqplot(manager, 'LinearScale', 'scale_y', { |
| 395 |
min: 0, |
| 396 |
max: 1, |
| 397 |
allow_padding: false, |
| 398 |
}); |
| 399 |
const scale_row = await create_model_bqplot( |
| 400 |
manager, |
| 401 |
'OrdinalScale', |
| 402 |
'scale_row', |
| 403 |
{ reverse: true } |
| 404 |
); |
| 405 |
const scale_column = await create_model_bqplot( |
| 406 |
manager, |
| 407 |
'OrdinalScale', |
| 408 |
'scale_column', |
| 409 |
{} |
| 410 |
); |
| 411 |
const scale_color = await create_model_bqplot( |
| 412 |
manager, |
| 413 |
'ColorScale', |
| 414 |
'scale_color', |
| 415 |
{ scheme: 'RdYlGn', colors: [] } |
| 416 |
); |
| 417 |
const scales = { |
| 418 |
color: scale_color.toJSON(), |
| 419 |
row: scale_row.toJSON(), |
| 420 |
column: scale_column.toJSON(), |
| 421 |
}; |
| 422 |
|
| 423 |
const gridModel = await create_model_bqplot( |
| 424 |
manager, |
| 425 |
'GridHeatMap', |
| 426 |
'gridheatmap1', |
| 427 |
{ |
| 428 |
scales: scales, |
| 429 |
color: color, |
| 430 |
visible: true, |
| 431 |
default_size: 64, |
| 432 |
preserve_domain: {}, |
| 433 |
_view_module_version: '*', |
| 434 |
_view_module: 'bqplot', |
| 435 |
} |
| 436 |
); |
| 437 |
let figureModel; |
| 438 |
try { |
| 439 |
figureModel = await create_model_bqplot(manager, 'Figure', 'figure1', { |
| 440 |
scale_x: scale_x.toJSON(), |
| 441 |
scale_y: scale_y.toJSON(), |
| 442 |
layout: layout.toJSON(), |
| 443 |
_dom_classes: [], |
| 444 |
padding_y: 0, |
| 445 |
fig_margin: { bottom: 0, left: 0, right: 0, top: 0 }, |
| 446 |
marks: [gridModel.toJSON()], |
| 447 |
}); |
| 448 |
} catch (e) { |
| 449 |
console.error('error', e); |
| 450 |
} |
| 451 |
const figure = await create_view(manager, figureModel); |
| 452 |
await manager.display_view(undefined, figure); |
| 453 |
await figure._initial_marks_created; |
| 454 |
return { figure: figure, grid: await figure.mark_views.views[0] }; |
| 455 |
} |
| 456 |
|
| 457 |
export async function create_figure_image(manager, ipywidgetImage) { |
| 458 |
const layout = await create_model( |
| 459 |
manager, |
| 460 |
'@jupyter-widgets/base', |
| 461 |
'LayoutModel', |
| 462 |
'LayoutView', |
| 463 |
'layout_figure1', |
| 464 |
{ _dom_classes: '', width: '400px', height: '500px' } |
| 465 |
); |
| 466 |
|
| 467 |
const scale_x = await create_model_bqplot(manager, 'LinearScale', 'scale_x', { |
| 468 |
allow_padding: false, |
| 469 |
}); |
| 470 |
const scale_y = await create_model_bqplot(manager, 'LinearScale', 'scale_y', { |
| 471 |
allow_padding: false, |
| 472 |
}); |
| 473 |
|
| 474 |
const scales = { x: scale_x.toJSON(), y: scale_y.toJSON() }; |
| 475 |
const imageModel = await create_model_bqplot(manager, 'Image', 'image1', { |
| 476 |
scales: scales, |
| 477 |
image: ipywidgetImage.toJSON(), |
| 478 |
preserve_domain: {}, |
| 479 |
_view_module_version: '*', |
| 480 |
_view_module: 'bqplot', |
| 481 |
}); |
| 482 |
let figureModel; |
| 483 |
try { |
| 484 |
figureModel = await create_model_bqplot(manager, 'Figure', 'figure1', { |
| 485 |
scale_x: scale_x.toJSON(), |
| 486 |
scale_y: scale_y.toJSON(), |
| 487 |
layout: layout.toJSON(), |
| 488 |
_dom_classes: [], |
| 489 |
figure_padding_y: 0, |
| 490 |
fig_margin: { bottom: 0, left: 0, right: 0, top: 0 }, |
| 491 |
marks: [imageModel.toJSON()], |
| 492 |
}); |
| 493 |
} catch (e) { |
| 494 |
console.error('error', e); |
| 495 |
} |
| 496 |
const figure = await create_view(manager, figureModel); |
| 497 |
await manager.display_view(undefined, figure); |
| 498 |
await figure._initial_marks_created; |
| 499 |
return { figure: figure, image: await figure.mark_views.views[0] }; |
| 500 |
} |
| 501 |
export function getFills(selection: any) { |
| 502 |
return selection.nodes().map((el) => el.style.fill); |
| 503 |
} |
| 504 |
|
| 505 |
export function getStrokes(selection: any) { |
| 506 |
return selection.nodes().map((el) => el.style.stroke); |
| 507 |
} |
| 508 |
|
| 509 |
export function getStrokeWidth(selection: any) { |
| 510 |
return selection.nodes().map((el) => el.style.strokeWidth); |
| 511 |
} |