| 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 |
import { d3GetEvent } from './utils'; |
| 18 |
import * as _ from 'underscore'; |
| 19 |
import { Mark } from './Mark'; |
| 20 |
import { GridCellData, AxisMode, GridHeatMapModel } from './GridHeatMapModel'; |
| 21 |
import { applyStyles } from './utils'; |
| 22 |
|
| 23 |
export class GridHeatMap extends Mark { |
| 24 |
async render() { |
| 25 |
const base_render_promise = super.render(); |
| 26 |
|
| 27 |
// TODO: create_listeners is put inside the promise success handler |
| 28 |
// because some of the functions depend on child scales being |
| 29 |
// created. Make sure none of the event handler functions make that |
| 30 |
// assumption. |
| 31 |
this.displayed.then(() => { |
| 32 |
this.parent.tooltip_div.node().appendChild(this.tooltip_div.node()); |
| 33 |
this.create_tooltip(); |
| 34 |
}); |
| 35 |
|
| 36 |
this.selected_indices = this.model.get('selected'); |
| 37 |
this.selected_style = this.model.get('selected_style'); |
| 38 |
this.unselected_style = this.model.get('unselected_style'); |
| 39 |
this.anchorStyle = this.model.get('anchor_style'); |
| 40 |
this.display_el_classes = ['heatmapcell']; |
| 41 |
|
| 42 |
await base_render_promise; |
| 43 |
|
| 44 |
this.event_listeners = {}; |
| 45 |
this.process_interactions(); |
| 46 |
this.create_listeners(); |
| 47 |
this.compute_view_padding(); |
| 48 |
this.draw(); |
| 49 |
} |
| 50 |
|
| 51 |
initialize_additional_scales() { |
| 52 |
if (this.scales.color) { |
| 53 |
this.listenTo( |
| 54 |
this.scales.color, |
| 55 |
'domain_changed', |
| 56 |
this.apply_styles.bind(this) |
| 57 |
); |
| 58 |
this.scales.color.on( |
| 59 |
'color_scale_range_changed', |
| 60 |
this.apply_styles, |
| 61 |
this |
| 62 |
); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
set_ranges() { |
| 67 |
if (this.scales.row) { |
| 68 |
this.scales.row.set_range( |
| 69 |
this.parent.padded_range('y', this.scales.row.model) |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
if (this.scales.column) { |
| 74 |
this.scales.column.set_range( |
| 75 |
this.parent.padded_range('x', this.scales.column.model) |
| 76 |
); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
set_positional_scales() { |
| 81 |
this.listenTo(this.scales.column, 'domain_changed', () => { |
| 82 |
if (!this.model.dirty) { |
| 83 |
this.draw(); |
| 84 |
} |
| 85 |
}); |
| 86 |
this.listenTo(this.scales.row, 'domain_changed', () => { |
| 87 |
if (!this.model.dirty) { |
| 88 |
this.draw(); |
| 89 |
} |
| 90 |
}); |
| 91 |
} |
| 92 |
|
| 93 |
private expandScaleDomain( |
| 94 |
scale, |
| 95 |
data: number[], |
| 96 |
mode: AxisMode, |
| 97 |
start: boolean |
| 98 |
) { |
| 99 |
// This function expands the domain so that the heatmap has the |
| 100 |
// minimum area needed to draw itself. |
| 101 |
let currentPixels; |
| 102 |
let minDiff; |
| 103 |
|
| 104 |
if (mode === AxisMode.ExpandOne) { |
| 105 |
currentPixels = data.map((el) => scale.scale(el)); |
| 106 |
const diffs = currentPixels |
| 107 |
.slice(1) |
| 108 |
.map((el: number, index: number) => el - currentPixels[index]); |
| 109 |
|
| 110 |
//TODO: Explain what is going on here. |
| 111 |
minDiff = 0; |
| 112 |
if (diffs[0] < 0) { |
| 113 |
start = !start; |
| 114 |
// diffs are negative. So max instead of min |
| 115 |
minDiff = d3.max(diffs); |
| 116 |
} else { |
| 117 |
minDiff = d3.min(diffs); |
| 118 |
} |
| 119 |
if (start) { |
| 120 |
const newPixel = currentPixels[currentPixels.length - 1] + minDiff; |
| 121 |
return [data[0], scale.invert(newPixel)]; |
| 122 |
} else { |
| 123 |
const newPixel = currentPixels[0] - minDiff; |
| 124 |
return [scale.invert(newPixel), data[currentPixels.length - 1]]; |
| 125 |
} |
| 126 |
} else if (mode === AxisMode.ExpandTwo) { |
| 127 |
currentPixels = data.map((el) => scale.scale(el)); |
| 128 |
minDiff = d3.min( |
| 129 |
currentPixels |
| 130 |
.slice(1) |
| 131 |
.map((el: number, index: number) => el - currentPixels[index]) |
| 132 |
); |
| 133 |
const newEnd = currentPixels[currentPixels.length - 1] + minDiff; |
| 134 |
const newStart = currentPixels[0] - minDiff; |
| 135 |
return [scale.invert(newStart), scale.invert(newEnd)]; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
get numRows(): number { |
| 140 |
return this.model.colors.length; |
| 141 |
} |
| 142 |
|
| 143 |
get numColumns(): number { |
| 144 |
return this.model.colors[0].length; |
| 145 |
} |
| 146 |
|
| 147 |
create_listeners(): void { |
| 148 |
super.create_listeners(); |
| 149 |
|
| 150 |
this.listenTo(this.model, 'change:stroke', this.updateStroke); |
| 151 |
this.listenTo(this.model, 'change:opacity', this.updateOpacity); |
| 152 |
|
| 153 |
this.d3el |
| 154 |
.on('mouseover', () => this.event_dispatcher('mouse_over')) |
| 155 |
.on('mousemove', () => this.event_dispatcher('mouse_move')) |
| 156 |
.on('mouseout', () => this.event_dispatcher('mouse_out')); |
| 157 |
this.listenTo(this.parent, 'bg_clicked', () => |
| 158 |
this.event_dispatcher('parent_clicked') |
| 159 |
); |
| 160 |
|
| 161 |
this.listenTo(this.model, 'data_updated', this.draw); |
| 162 |
this.listenTo(this.model, 'change:tooltip', this.create_tooltip); |
| 163 |
this.model.on_some_change( |
| 164 |
['display_format', 'font_style'], |
| 165 |
this.updateLabels, |
| 166 |
this |
| 167 |
); |
| 168 |
this.listenTo(this.model, 'change:selected', this.updateSelected); |
| 169 |
this.listenTo(this.model, 'change:interactions', this.process_interactions); |
| 170 |
} |
| 171 |
|
| 172 |
private handleClick(args) { |
| 173 |
const index = args.rowNum * this.numColumns + args.columnNum; |
| 174 |
const row = args.rowNum; |
| 175 |
const column = args.columnNum; |
| 176 |
let idx: [number, number][] = Array.from(this.model.get('selected') || []); |
| 177 |
let selected = this.indicesToCellNums(idx); |
| 178 |
const elem_index = selected.indexOf(index); |
| 179 |
const accelKey = d3GetEvent().ctrlKey || d3GetEvent().metaKey; |
| 180 |
//TODO: This is a shim for when accelKey is supported by chrome. |
| 181 |
// index of slice i. Checking if it is already present in the |
| 182 |
// list |
| 183 |
if (elem_index > -1 && accelKey) { |
| 184 |
// if the index is already selected and if ctrl key is |
| 185 |
// pressed, remove the element from the list |
| 186 |
idx.splice(elem_index, 1); |
| 187 |
} else { |
| 188 |
if (!accelKey) { |
| 189 |
selected = []; |
| 190 |
idx = []; |
| 191 |
} |
| 192 |
idx.push([row, column]); |
| 193 |
selected.push(this.indicesToCellNums([[row, column]])[0]); |
| 194 |
if (d3GetEvent().shiftKey) { |
| 195 |
//If shift is pressed and the element is already |
| 196 |
//selected, do not do anything |
| 197 |
if (elem_index > -1) { |
| 198 |
return; |
| 199 |
} |
| 200 |
//Add elements before or after the index of the current |
| 201 |
//slice which has been clicked |
| 202 |
const row_index = selected.length !== 0 ? this.anchorCellIndex[0] : row; |
| 203 |
const col_index = |
| 204 |
selected.length !== 0 ? this.anchorCellIndex[1] : column; |
| 205 |
_.range(Math.min(row, row_index), Math.max(row, row_index) + 1).forEach( |
| 206 |
(i) => { |
| 207 |
_.range( |
| 208 |
Math.min(column, col_index), |
| 209 |
Math.max(column, col_index) + 1 |
| 210 |
).forEach((j) => { |
| 211 |
const cell_num = this.indicesToCellNums([[i, j]])[0]; |
| 212 |
if (selected.indexOf(cell_num) === -1) { |
| 213 |
selected.push(cell_num); |
| 214 |
idx.push([i, j]); |
| 215 |
} |
| 216 |
}); |
| 217 |
} |
| 218 |
); |
| 219 |
} else { |
| 220 |
// updating the array containing the slice indexes selected |
| 221 |
// and updating the style |
| 222 |
this.anchorCellIndex = [row, column]; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
this.model.set('selected', idx.length === 0 ? null : idx, { |
| 227 |
updated_view: this, |
| 228 |
}); |
| 229 |
this.touch(); |
| 230 |
|
| 231 |
let e = d3GetEvent(); |
| 232 |
if (!e) { |
| 233 |
e = window.event; |
| 234 |
} |
| 235 |
if (e.cancelBubble !== undefined) { |
| 236 |
// IE |
| 237 |
e.cancelBubble = true; |
| 238 |
} |
| 239 |
if (e.stopPropagation) { |
| 240 |
e.stopPropagation(); |
| 241 |
} |
| 242 |
e.preventDefault(); |
| 243 |
|
| 244 |
this.selected_indices = idx; |
| 245 |
this.apply_styles(); |
| 246 |
} |
| 247 |
|
| 248 |
private updateSelected() { |
| 249 |
this.selected_indices = this.model.get('selected'); |
| 250 |
this.apply_styles(); |
| 251 |
} |
| 252 |
|
| 253 |
set_style_on_elements(style, indices, elements) { |
| 254 |
// If the index array is undefined or of length=0, exit the |
| 255 |
// function without doing anything |
| 256 |
if ( |
| 257 |
!indices || |
| 258 |
(indices.length === 0 && (!elements || elements.length === 0)) |
| 259 |
) { |
| 260 |
return; |
| 261 |
} |
| 262 |
// Also, return if the style object itself is blank |
| 263 |
if (Object.keys(style).length === 0) { |
| 264 |
return; |
| 265 |
} |
| 266 |
elements = |
| 267 |
!elements || elements.length === 0 |
| 268 |
? this.filterCellsByNum(this.indicesToCellNums(indices)) |
| 269 |
: elements; |
| 270 |
applyStyles(elements, style); |
| 271 |
} |
| 272 |
|
| 273 |
set_default_style(indices, elements) { |
| 274 |
// For all the elements with index in the list indices, the default |
| 275 |
// style is applied. |
| 276 |
if ( |
| 277 |
!indices || |
| 278 |
(indices.length === 0 && (!elements || elements.length === 0)) |
| 279 |
) { |
| 280 |
return; |
| 281 |
} |
| 282 |
elements = |
| 283 |
!elements || elements.length === 0 |
| 284 |
? this.filterCellsByNum(this.indicesToCellNums(indices)) |
| 285 |
: elements; |
| 286 |
const stroke = this.model.get('stroke'); |
| 287 |
const opacity = this.model.get('opacity'); |
| 288 |
|
| 289 |
elements |
| 290 |
.style('fill', (d) => this.get_element_fill(d)) |
| 291 |
.style('opacity', opacity) |
| 292 |
.style('stroke', stroke); |
| 293 |
} |
| 294 |
|
| 295 |
clear_style(style_dict, indices?, elements?) { |
| 296 |
// Function to clear the style of a dict on some or all the elements of the |
| 297 |
// chart. If indices is null, clears the style on all elements. If |
| 298 |
// not, clears on only the elements whose indices are matching. |
| 299 |
// |
| 300 |
// If elements are passed, then indices are ignored and the style |
| 301 |
// is cleared only on the elements that are passed. |
| 302 |
// |
| 303 |
// This can be used if we decide to accommodate more properties than |
| 304 |
// those set by default. Because those have to cleared specifically. |
| 305 |
// |
| 306 |
if (Object.keys(style_dict).length === 0) { |
| 307 |
// No style to clear |
| 308 |
return; |
| 309 |
} |
| 310 |
|
| 311 |
if (!elements || elements.length === 0) { |
| 312 |
if (indices) { |
| 313 |
elements = this.filterCellsByNum(this.indicesToCellNums(indices)); |
| 314 |
} else { |
| 315 |
elements = this.displayCells; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
const clearing_style = {}; |
| 320 |
for (let key in style_dict) { |
| 321 |
clearing_style[key] = null; |
| 322 |
} |
| 323 |
applyStyles(elements, clearing_style); |
| 324 |
} |
| 325 |
|
| 326 |
private filterCellsByNum( |
| 327 |
cell_numbers: number[] |
| 328 |
): d3.Selection<any, any, any, any> { |
| 329 |
if (cell_numbers === null || cell_numbers === undefined) { |
| 330 |
// FIXME: return an empty selection |
| 331 |
return this.d3el.selectAll('thisNameDoesntExist'); |
| 332 |
} |
| 333 |
|
| 334 |
return this.displayCells.filter( |
| 335 |
(el: GridCellData) => cell_numbers.indexOf(el.cellNum) !== -1 |
| 336 |
); |
| 337 |
} |
| 338 |
|
| 339 |
selected_style_updated(model, style) { |
| 340 |
this.selected_style = style; |
| 341 |
this.clear_style( |
| 342 |
model.previous('selected_style'), |
| 343 |
this.selected_indices, |
| 344 |
this.selectedElements |
| 345 |
); |
| 346 |
this.style_updated(style, this.selected_indices, this.selectedElements); |
| 347 |
} |
| 348 |
|
| 349 |
unselected_style_updated(model, style) { |
| 350 |
this.unselected_style = style; |
| 351 |
this.clear_style( |
| 352 |
model.previous('unselected_style'), |
| 353 |
[], |
| 354 |
this.unselectedElements |
| 355 |
); |
| 356 |
this.style_updated(style, [], this.unselectedElements); |
| 357 |
} |
| 358 |
|
| 359 |
apply_styles() { |
| 360 |
this.clear_style(this.selected_style); |
| 361 |
this.clear_style(this.unselected_style); |
| 362 |
this.clear_style(this.anchorStyle); |
| 363 |
|
| 364 |
this.set_default_style([], this.displayCells); |
| 365 |
|
| 366 |
const selected_cell_nums = this.indicesToCellNums( |
| 367 |
this.selected_indices as [number, number][] |
| 368 |
); |
| 369 |
const unsel_cell_nums = |
| 370 |
selected_cell_nums === null || selected_cell_nums.length === 0 |
| 371 |
? [] |
| 372 |
: _.difference( |
| 373 |
_.range(this.numRows * this.numColumns), |
| 374 |
selected_cell_nums |
| 375 |
); |
| 376 |
|
| 377 |
this.selectedElements = this.filterCellsByNum(selected_cell_nums); |
| 378 |
this.set_style_on_elements( |
| 379 |
this.selected_style, |
| 380 |
this.selected_indices, |
| 381 |
this.selectedElements |
| 382 |
); |
| 383 |
|
| 384 |
this.unselectedElements = this.filterCellsByNum(unsel_cell_nums); |
| 385 |
this.set_style_on_elements( |
| 386 |
this.unselected_style, |
| 387 |
[], |
| 388 |
this.unselectedElements |
| 389 |
); |
| 390 |
|
| 391 |
if (this.anchorCellIndex !== null && this.anchorCellIndex !== undefined) { |
| 392 |
const anchorNum = this.indicesToCellNums([this.anchorCellIndex]); |
| 393 |
this.set_style_on_elements( |
| 394 |
this.anchorStyle, |
| 395 |
[], |
| 396 |
this.filterCellsByNum(anchorNum) |
| 397 |
); |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
style_updated(new_style, indices, elements) { |
| 402 |
// reset the style of the elements and apply the new style |
| 403 |
this.set_default_style(indices, elements); |
| 404 |
this.set_style_on_elements(new_style, indices, elements); |
| 405 |
} |
| 406 |
|
| 407 |
private resetSelection() { |
| 408 |
this.model.set('selected', null); |
| 409 |
this.touch(); |
| 410 |
this.selected_indices = null; |
| 411 |
this.clear_style(this.selected_style); |
| 412 |
this.clear_style(this.unselected_style); |
| 413 |
this.clear_style(this.anchorStyle); |
| 414 |
|
| 415 |
this.set_default_style([], this.displayCells); |
| 416 |
} |
| 417 |
|
| 418 |
relayout() { |
| 419 |
this.set_ranges(); |
| 420 |
this.compute_view_padding(); |
| 421 |
//TODO: The call to draw has to be changed to something less |
| 422 |
//expensive. |
| 423 |
this.draw(); |
| 424 |
} |
| 425 |
|
| 426 |
private indicesToCellNums(indices: [number, number][]): number[] { |
| 427 |
if (indices === null || indices === undefined) { |
| 428 |
return null; |
| 429 |
} |
| 430 |
|
| 431 |
return indices.map((i) => i[0] * this.numColumns + i[1]); |
| 432 |
} |
| 433 |
|
| 434 |
invert_point(pixel) { |
| 435 |
// For now, an index selector is not supported for the heatmap |
| 436 |
} |
| 437 |
|
| 438 |
selector_changed(point_selector, rect_selector) { |
| 439 |
if (point_selector === undefined) { |
| 440 |
this.model.set('selected', null); |
| 441 |
this.touch(); |
| 442 |
return []; |
| 443 |
} |
| 444 |
const rowIndices = _.range(this.numRows); |
| 445 |
const colIndices = _.range(this.numColumns); |
| 446 |
const selCols = _.filter(colIndices, (index) => { |
| 447 |
return rect_selector([this.columnPixels[index], []]); |
| 448 |
}); |
| 449 |
const selRows = _.filter(rowIndices, (index) => { |
| 450 |
return rect_selector([[], this.rowPixels[index]]); |
| 451 |
}); |
| 452 |
let selected = selCols.map((s) => { |
| 453 |
return selRows.map((r) => [r, s]); |
| 454 |
}); |
| 455 |
// @ts-ignore |
| 456 |
selected = _.flatten(selected, true); |
| 457 |
this.model.set('selected', selected); |
| 458 |
this.touch(); |
| 459 |
} |
| 460 |
|
| 461 |
draw() { |
| 462 |
this.set_ranges(); |
| 463 |
|
| 464 |
const rowScale = this.scales.row; |
| 465 |
const columnScale = this.scales.column; |
| 466 |
|
| 467 |
const rowStartAligned = this.model.get('row_align') === 'start'; |
| 468 |
const colStartAligned = this.model.get('column_align') === 'start'; |
| 469 |
let new_domain; |
| 470 |
|
| 471 |
if ( |
| 472 |
this.model.modes.row !== AxisMode.Middle && |
| 473 |
this.model.modes.row !== AxisMode.Boundaries |
| 474 |
) { |
| 475 |
new_domain = this.expandScaleDomain( |
| 476 |
rowScale, |
| 477 |
this.model.rows, |
| 478 |
this.model.modes.row, |
| 479 |
rowStartAligned |
| 480 |
); |
| 481 |
if ( |
| 482 |
d3.min(new_domain) < d3.min(rowScale.model.domain) || |
| 483 |
d3.max(new_domain) > d3.max(rowScale.model.domain) |
| 484 |
) { |
| 485 |
// Update domain if domain has changed |
| 486 |
rowScale.model.compute_and_set_domain( |
| 487 |
new_domain, |
| 488 |
rowScale.model.model_id |
| 489 |
); |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
if ( |
| 494 |
this.model.modes.column !== AxisMode.Middle && |
| 495 |
this.model.modes.column !== AxisMode.Boundaries |
| 496 |
) { |
| 497 |
new_domain = this.expandScaleDomain( |
| 498 |
columnScale, |
| 499 |
this.model.columns, |
| 500 |
this.model.modes.column, |
| 501 |
colStartAligned |
| 502 |
); |
| 503 |
if ( |
| 504 |
d3.min(new_domain) < d3.min(columnScale.model.domain) || |
| 505 |
d3.max(new_domain) > d3.max(columnScale.model.domain) |
| 506 |
) { |
| 507 |
// Update domain if domain has changed |
| 508 |
columnScale.model.compute_and_set_domain( |
| 509 |
new_domain, |
| 510 |
columnScale.model.model_id |
| 511 |
); |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
const rowPlotData = this.getTilePlottingData( |
| 516 |
rowScale, |
| 517 |
this.model.rows, |
| 518 |
this.model.modes.row, |
| 519 |
rowStartAligned |
| 520 |
); |
| 521 |
const columnPlotData = this.getTilePlottingData( |
| 522 |
columnScale, |
| 523 |
this.model.columns, |
| 524 |
this.model.modes.column, |
| 525 |
colStartAligned |
| 526 |
); |
| 527 |
|
| 528 |
this.rowPixels = rowPlotData.start.map((d, i) => [ |
| 529 |
d, |
| 530 |
d + rowPlotData.widths[i], |
| 531 |
]); |
| 532 |
this.columnPixels = columnPlotData.start.map((d, i) => [ |
| 533 |
d, |
| 534 |
d + columnPlotData.widths[i], |
| 535 |
]); |
| 536 |
|
| 537 |
this.displayRows = this.d3el |
| 538 |
.selectAll('.heatmaprow') |
| 539 |
.data(_.range(this.numRows)) |
| 540 |
.join('g') |
| 541 |
.attr('class', 'heatmaprow') |
| 542 |
.attr('transform', (d: number) => { |
| 543 |
return 'translate(0, ' + rowPlotData.start[d] + ')'; |
| 544 |
}); |
| 545 |
|
| 546 |
const dataArray = _.range(this.numRows).map((row) => { |
| 547 |
return _.range(this.numColumns).map( |
| 548 |
(col) => this.model.mark_data[row * this.numColumns + col] |
| 549 |
); |
| 550 |
}); |
| 551 |
|
| 552 |
this.displayCells = this.displayRows |
| 553 |
.selectAll('.heatmapcell') |
| 554 |
.data((d, i: number) => dataArray[i]) |
| 555 |
.join('rect') |
| 556 |
.attr('class', 'heatmapcell') |
| 557 |
.on('click', () => this.event_dispatcher('element_clicked')); |
| 558 |
|
| 559 |
this.displayCells |
| 560 |
.attr('x', (d, i: number) => columnPlotData.start[i]) |
| 561 |
.attr('y', 0) |
| 562 |
.attr('width', (d, i: number) => columnPlotData.widths[i]) |
| 563 |
.attr('height', (d: GridCellData) => rowPlotData.widths[d.rowNum]); |
| 564 |
|
| 565 |
// cell labels |
| 566 |
this.displayRows |
| 567 |
.selectAll('.heatmapcell_label') |
| 568 |
.data((d, i: number) => dataArray[i]) |
| 569 |
.join('text') |
| 570 |
.attr('class', 'heatmapcell_label') |
| 571 |
.attr( |
| 572 |
'x', |
| 573 |
(d, i) => columnPlotData.start[i] + columnPlotData.widths[i] / 2 |
| 574 |
) |
| 575 |
.attr('y', (d: GridCellData) => rowPlotData.widths[d.rowNum] / 2) |
| 576 |
.style('text-anchor', 'middle') |
| 577 |
.style('fill', 'black') |
| 578 |
.style('pointer-events', 'none') |
| 579 |
.style('dominant-baseline', 'central'); |
| 580 |
|
| 581 |
this.apply_styles(); |
| 582 |
this.updateLabels(); |
| 583 |
|
| 584 |
this.displayCells.on('click', (d: GridCellData, i: number) => { |
| 585 |
return this.event_dispatcher('element_clicked', { |
| 586 |
data: d.color, |
| 587 |
index: i, |
| 588 |
rowNum: d.rowNum, |
| 589 |
columnNum: d.columnNum, |
| 590 |
}); |
| 591 |
}); |
| 592 |
} |
| 593 |
|
| 594 |
private updateStroke() { |
| 595 |
this.displayCells.style('stroke', this.model.get('stroke')); |
| 596 |
} |
| 597 |
|
| 598 |
private updateOpacity() { |
| 599 |
this.displayCells.style('opacity', this.model.get('opacity')); |
| 600 |
} |
| 601 |
|
| 602 |
private updateLabels() { |
| 603 |
const displayFormatStr = this.model.get('display_format'); |
| 604 |
const displayFormat = displayFormatStr ? d3.format(displayFormatStr) : null; |
| 605 |
|
| 606 |
let fonts = this.d3el |
| 607 |
.selectAll('.heatmapcell_label') |
| 608 |
.text((d: GridCellData, i) => { |
| 609 |
return displayFormat ? displayFormat(d.color) : null; |
| 610 |
}); |
| 611 |
|
| 612 |
const fontStyle = this.model.get('font_style'); |
| 613 |
for (const styleKey in fontStyle) { |
| 614 |
fonts = fonts.style(styleKey, fontStyle[styleKey]); |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
private getTilePlottingData( |
| 619 |
scale, |
| 620 |
data: number[], |
| 621 |
mode: AxisMode, |
| 622 |
start: boolean |
| 623 |
): { start: number[]; widths: number[] } { |
| 624 |
// This function returns the starting points and widths of the |
| 625 |
// cells based on the parameters passed. |
| 626 |
// |
| 627 |
// scale is the scale and data is the data for which the plot data |
| 628 |
// is to be generated. mode refers to the expansion of the data to |
| 629 |
// generate the plotting data and start is a boolean indicating the |
| 630 |
// alignment of the data w.r.t the cells. |
| 631 |
let startPoints = data.map((d) => scale.scale(d)); |
| 632 |
let widths = []; |
| 633 |
data = Array.from(data); // copy to Array |
| 634 |
|
| 635 |
if (mode === AxisMode.Middle) { |
| 636 |
widths = data.map((d) => scale.scale.bandwidth()); |
| 637 |
} |
| 638 |
|
| 639 |
if (mode === AxisMode.Boundaries) { |
| 640 |
const pixelPoints = data.map((d) => scale.scale(d)); |
| 641 |
|
| 642 |
for (let i = 1; i < pixelPoints.length; ++i) { |
| 643 |
widths[i - 1] = Math.abs(pixelPoints[i] - pixelPoints[i - 1]); |
| 644 |
} |
| 645 |
|
| 646 |
startPoints = |
| 647 |
pixelPoints[1] > pixelPoints[0] |
| 648 |
? pixelPoints.slice(0, -1) |
| 649 |
: pixelPoints.slice(1); |
| 650 |
} |
| 651 |
|
| 652 |
if (mode === AxisMode.ExpandOne) { |
| 653 |
let bounds; |
| 654 |
|
| 655 |
if (start) { |
| 656 |
widths = startPoints.slice(1).map((d, ind) => { |
| 657 |
// Absolute value is required as the order of the data |
| 658 |
// can be increasing or decreasing in terms of pixels |
| 659 |
return Math.abs(d - startPoints[ind]); |
| 660 |
}); |
| 661 |
|
| 662 |
// Now we have n-1 widths. We have to add the last or the |
| 663 |
// first width depending on scale is increasing or |
| 664 |
// decreasing. |
| 665 |
bounds = d3.max(scale.scale.range()); |
| 666 |
if (startPoints[0] < startPoints[1]) { |
| 667 |
widths = Array.prototype.concat(widths, [ |
| 668 |
Math.abs(bounds - d3.max(startPoints)), |
| 669 |
]); |
| 670 |
} else { |
| 671 |
widths = Array.prototype.concat( |
| 672 |
[Math.abs(bounds - d3.max(startPoints))], |
| 673 |
widths |
| 674 |
); |
| 675 |
} |
| 676 |
} else { |
| 677 |
widths = startPoints.slice(1).map((d, ind) => { |
| 678 |
// Absolute value is required as the order of the data |
| 679 |
// can be increasing or decreasing in terms of pixels |
| 680 |
return Math.abs(d - startPoints[ind]); |
| 681 |
}); |
| 682 |
|
| 683 |
bounds = d3.min(scale.scale.range()); |
| 684 |
bounds = d3.min(scale.scale.range()); |
| 685 |
if (startPoints[1] > startPoints[0]) { |
| 686 |
// The point corresponding to the bounds is added at |
| 687 |
// the start of the array. Hence it has to be added to |
| 688 |
// the startPoints and the last start_point can be |
| 689 |
// removed. |
| 690 |
startPoints.unshift(Math.abs(bounds)); |
| 691 |
widths.splice(0, 0, startPoints[1] - startPoints[0]); |
| 692 |
startPoints.pop(); |
| 693 |
} else { |
| 694 |
// The point for the bounds is added to the end of the |
| 695 |
// array. The first start point can now be removed as |
| 696 |
// this will be the last end point. |
| 697 |
widths = Array.prototype.concat(widths, [ |
| 698 |
Math.abs(bounds - startPoints.slice(-1)[0]), |
| 699 |
]); |
| 700 |
startPoints = Array.prototype.concat(startPoints, bounds); |
| 701 |
startPoints.splice(0, 1); |
| 702 |
} |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
if (mode === AxisMode.ExpandTwo) { |
| 707 |
const isPositive = startPoints[1] - startPoints[0] > 0; |
| 708 |
let bound: number = isPositive |
| 709 |
? d3.min(scale.scale.range() as number[]) |
| 710 |
: d3.max(scale.scale.range() as number[]); |
| 711 |
|
| 712 |
startPoints.splice(0, 0, bound); |
| 713 |
widths = startPoints |
| 714 |
.slice(1) |
| 715 |
.map((d, ind) => Math.abs(d - startPoints[ind])); |
| 716 |
bound = isPositive |
| 717 |
? d3.max(scale.scale.range() as number[]) |
| 718 |
: d3.min(scale.scale.range() as number[]); |
| 719 |
widths[widths.length] = Math.abs(bound - startPoints.slice(-1)[0]); |
| 720 |
} |
| 721 |
|
| 722 |
return { start: startPoints, widths }; |
| 723 |
} |
| 724 |
|
| 725 |
get_element_fill(dat) { |
| 726 |
if (dat.color === null) { |
| 727 |
return this.model.get('null_color'); |
| 728 |
} |
| 729 |
return this.scales.color.scale(dat.color); |
| 730 |
} |
| 731 |
|
| 732 |
process_click(interaction) { |
| 733 |
super.process_click(interaction); |
| 734 |
|
| 735 |
if (interaction === 'select') { |
| 736 |
this.event_listeners.parent_clicked = this.resetSelection; |
| 737 |
this.event_listeners.element_clicked = this.handleClick; |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
compute_view_padding() {} |
| 742 |
|
| 743 |
private anchorStyle: { [key: string]: string }; |
| 744 |
private anchorCellIndex: [number, number]; |
| 745 |
private displayCells: d3.Selection<any, any, any, any>; |
| 746 |
private displayRows: d3.Selection<any, number, HTMLElement, any>; |
| 747 |
private selectedElements: d3.Selection<any, any, any, any>; |
| 748 |
private unselectedElements: d3.Selection<any, any, any, any>; |
| 749 |
private rowPixels: [number, number][]; |
| 750 |
private columnPixels: [number, number][]; |
| 751 |
|
| 752 |
model: GridHeatMapModel; |
| 753 |
} |