-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathMatrixHeatmapView.java
More file actions
643 lines (554 loc) · 21.1 KB
/
Copy pathMatrixHeatmapView.java
File metadata and controls
643 lines (554 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
package edu.jhuapl.trinity.javafx.components;
import edu.jhuapl.trinity.utils.MatrixViewUtil;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Tooltip;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
/**
* MatrixHeatmapView
* -----------------
* Reusable JavaFX control to render a 2D numeric matrix as a heatmap.
* - Renders on Canvas (fast, lightweight).
* - Sequential and diverging palettes.
* - Auto or fixed range mapping.
* - Optional color legend (with optional title).
* - Axis labels (top / left).
* - Hover tooltips with (row, col, value).
* - Click callback with cell picking.
* <p>
* Public API is intentionally generic so it can be reused by multiple Trinity tools.
*
* @author Sean Phillips
*/
public final class MatrixHeatmapView extends BorderPane {
// -------------------- Types --------------------
public enum ValueMode {RAW, ABS_VALUE}
public enum ScaleMode {AUTO, FIXED}
/**
* Color palette styles.
*/
public enum PaletteKind {
SEQUENTIAL, // low -> high mapped 0..1
DIVERGING // < center on cool, > center on warm (center maps to mid color)
}
/**
* Simple click payload.
*/
public record MatrixClick(int row, int col, double value) {
}
// -------------------- State --------------------
private double[][] matrix = new double[0][0]; // row-major [rows][cols]
private String[] rowLabels = new String[0]; // optional
private String[] colLabels = new String[0]; // optional
// Rendering & layout knobs
private final Canvas canvas = new Canvas();
private final Insets padding = new Insets(6, 8, 8, 8);
// Reusable node for text measurement to avoid per-call allocations
private final Text measureText = new Text();
// Axis label styling
private Font labelFont = Font.font("Arial", 12);
private double labelMargin = 4.0;
// Legend controls
private boolean showLegend = true;
private double legendWidth = 16.0;
private double legendGap = 8.0;
private String legendTitle = null; // short, optional; rendered above legend inside canvas
// Palette & range
private PaletteKind paletteKind = PaletteKind.SEQUENTIAL;
private boolean autoRange = true;
private Double fixedMin = null;
private Double fixedMax = null;
private Double divergingCenter = 0.0; // for DIVERGING only
// Interaction
private Consumer<MatrixClick> onCellClick = null;
private final Tooltip hoverTip = new Tooltip();
private final DecimalFormat df = new DecimalFormat("0.####");
// Cached draw metrics
private double contentX;
private double contentY;
private double contentW;
private double contentH;
// Label behavior
private boolean compactColumnLabels = true; // default: strip "Comp " → just the index for columns
// -------------------- Construction --------------------
public MatrixHeatmapView() {
setPadding(padding);
setCenter(canvas);
// Resize canvas when control size changes
InvalidationListener resizer = new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
layoutAndDraw();
}
};
widthProperty().addListener(resizer);
heightProperty().addListener(resizer);
// Mouse events for hover + click
Tooltip.install(canvas, hoverTip);
canvas.addEventHandler(MouseEvent.MOUSE_MOVED, this::onMouseMove);
canvas.addEventHandler(MouseEvent.MOUSE_EXITED, e -> hoverTip.hide());
canvas.addEventHandler(MouseEvent.MOUSE_CLICKED, this::onMouseClick);
// Initial size preference (can be overridden by parent)
setPrefSize(640, 420);
canvas.setWidth(getPrefWidth());
canvas.setHeight(getPrefHeight());
}
// -------------------- Public API --------------------
/**
* Set a new matrix. Null or empty → clears view.
*/
public void setMatrix(double[][] values) {
if (values == null || values.length == 0 || values[0].length == 0) {
this.matrix = new double[0][0];
this.rowLabels = new String[0];
this.colLabels = new String[0];
} else {
int rows = values.length;
int cols = values[0].length;
this.matrix = new double[rows][cols];
for (int r = 0; r < rows; r++) {
if (values[r].length != cols) {
throw new IllegalArgumentException("Matrix rows must have equal length.");
}
System.arraycopy(values[r], 0, this.matrix[r], 0, cols);
}
if (rowLabels.length != rows) {
rowLabels = defaultLabels(rows, "r");
}
if (colLabels.length != cols) {
colLabels = defaultLabels(cols, "c");
}
}
layoutAndDraw();
}
/**
* Convenience: accept List<List<Double>>.
*/
public void setMatrix(List<List<Double>> values) {
if (values == null || values.isEmpty()) {
setMatrix((double[][]) null);
return;
}
int rows = values.size();
int cols = values.get(0).size();
double[][] arr = new double[rows][cols];
for (int r = 0; r < rows; r++) {
List<Double> row = values.get(r);
if (row.size() != cols) {
throw new IllegalArgumentException("Matrix rows must have equal length.");
}
for (int c = 0; c < cols; c++) {
Double v = row.get(c);
arr[r][c] = (v == null || Double.isNaN(v)) ? 0.0 : v.doubleValue();
}
}
setMatrix(arr);
}
public List<String> getRowLabels() {
return Arrays.asList(rowLabels);
}
/**
* Optional row labels (length must equal rows).
*/
public void setRowLabels(List<String> labels) {
if (labels == null || labels.isEmpty()) {
this.rowLabels = new String[rows()];
} else {
if (labels.size() != rows()) {
throw new IllegalArgumentException("Row labels length must match matrix rows.");
}
this.rowLabels = labels.toArray(new String[0]);
}
layoutAndDraw();
}
/**
* Optional column labels (length must equal cols). Applies compacting if enabled.
*/
public void setColLabels(List<String> labels) {
if (labels == null || labels.isEmpty()) {
this.colLabels = new String[cols()];
} else {
if (labels.size() != cols()) {
throw new IllegalArgumentException("Column labels length must match matrix cols.");
}
String[] incoming = labels.toArray(new String[0]);
if (compactColumnLabels) {
String[] compacted = new String[incoming.length];
for (int i = 0; i < incoming.length; i++) {
compacted[i] = MatrixViewUtil.compactCompLabel(incoming[i]);
}
this.colLabels = compacted;
} else {
this.colLabels = incoming;
}
}
layoutAndDraw();
}
/**
* Control whether columns compact "Comp 7" → "7". Default: true.
*/
public void setCompactColumnLabels(boolean compact) {
this.compactColumnLabels = compact;
// Re-apply compaction on currently loaded labels
if (this.colLabels != null && this.colLabels.length > 0) {
String[] out = new String[this.colLabels.length];
for (int i = 0; i < this.colLabels.length; i++) {
out[i] = compact ? MatrixViewUtil.compactCompLabel(this.colLabels[i]) : this.colLabels[i];
}
this.colLabels = out;
layoutAndDraw();
}
}
/**
* Set a uniform label prefix if none provided yet (e.g., "F0..F(n-1)").
*/
public void setDefaultAxisLabels(String rowPrefix, String colPrefix) {
if (rows() > 0 && (rowLabels == null || rowLabels.length != rows())) {
rowLabels = defaultLabels(rows(), rowPrefix == null ? "r" : rowPrefix);
}
if (cols() > 0 && (colLabels == null || colLabels.length != cols())) {
colLabels = defaultLabels(cols(), colPrefix == null ? "c" : colPrefix);
}
layoutAndDraw();
}
/**
* Optional legend title (short); rendered above the legend inside the canvas.
*/
public void setLegendTitle(String title) {
this.legendTitle = (title == null || title.isBlank()) ? null : title.trim();
layoutAndDraw();
}
/**
* Sequential palette.
*/
public void useSequentialPalette() {
this.paletteKind = PaletteKind.SEQUENTIAL;
layoutAndDraw();
}
/**
* Diverging palette (values below/above center split colors).
*/
public void useDivergingPalette(double center) {
this.paletteKind = PaletteKind.DIVERGING;
this.divergingCenter = center;
layoutAndDraw();
}
/**
* Auto-range on (min/max derived from current matrix).
*/
public void setAutoRange(boolean on) {
this.autoRange = on;
layoutAndDraw();
}
/**
* Fixed range mapping.
*/
public void setFixedRange(double vmin, double vmax) {
if (!(vmax > vmin)) {
throw new IllegalArgumentException("vmax must be greater than vmin.");
}
this.autoRange = false;
this.fixedMin = vmin;
this.fixedMax = vmax;
layoutAndDraw();
}
/**
* Show or hide the color legend bar.
*/
public void setShowLegend(boolean show) {
this.showLegend = show;
layoutAndDraw();
}
/**
* Set axis label font.
*/
public void setLabelFont(Font f) {
if (f != null) {
this.labelFont = f;
layoutAndDraw();
}
}
/**
* Set click handler for cell picks.
*/
public void setOnCellClick(Consumer<MatrixClick> handler) {
this.onCellClick = handler;
}
/**
* Return a defensive copy of the matrix (for external reads).
*/
public double[][] getMatrixCopy() {
int r = rows(), c = cols();
double[][] out = new double[r][c];
for (int i = 0; i < r; i++) {
System.arraycopy(matrix[i], 0, out[i], 0, c);
}
return out;
}
// -------------------- Internals: sizing + render --------------------
private int rows() {
return matrix.length;
}
private int cols() {
return (rows() == 0) ? 0 : matrix[0].length;
}
private void layoutAndDraw() {
double w = Math.max(1, getWidth() - getInsets().getLeft() - getInsets().getRight());
double h = Math.max(1, getHeight() - getInsets().getTop() - getInsets().getBottom());
canvas.setWidth(w);
canvas.setHeight(h);
draw();
}
private void draw() {
GraphicsContext g = canvas.getGraphicsContext2D();
double w = canvas.getWidth();
double h = canvas.getHeight();
// Clear
g.setFill(Color.web("#1E1E1E"));
g.fillRect(0, 0, w, h);
int r = rows();
int c = cols();
if (r == 0 || c == 0) {
drawCenteredMessage(g, w, h, "No data");
return;
}
// Compute layout boxes:
double topLabelH = estimateTopLabelHeight(g, c);
double leftLabelW = estimateLeftLabelWidth(g, r);
double legendW = showLegend ? (legendWidth + legendGap) : 0.0;
// Content box (matrix area)
contentX = Math.ceil(leftLabelW + labelMargin);
contentY = Math.ceil(topLabelH + labelMargin);
contentW = Math.max(1, w - contentX - legendW - 6);
contentH = Math.max(1, h - contentY - 6);
// Value mapping range
double vmin, vmax;
if (autoRange) {
double[] mm = MatrixViewUtil.minMax(matrix);
vmin = mm[0];
vmax = mm[1];
if (!(vmax > vmin)) vmax = vmin + 1e-9;
} else {
vmin = fixedMin == null ? 0.0 : fixedMin.doubleValue();
vmax = fixedMax == null ? 1.0 : fixedMax.doubleValue();
if (!(vmax > vmin)) vmax = vmin + 1e-9;
}
// Draw matrix
drawMatrix(g, vmin, vmax);
// Axes
drawAxisLabels(g);
// Legend
if (showLegend) {
drawLegend(g, vmin, vmax);
}
}
private void drawMatrix(GraphicsContext g, double vmin, double vmax) {
int r = rows();
int c = cols();
if (r == 0 || c == 0) return;
double cellW = contentW / c;
double cellH = contentH / r;
g.setImageSmoothing(false);
for (int i = 0; i < r; i++) {
double y = contentY + i * cellH;
for (int j = 0; j < c; j++) {
double x = contentX + j * cellW;
double v = MatrixViewUtil.sanitize(matrix[i][j]);
Color col = switch (paletteKind) {
case SEQUENTIAL -> MatrixViewUtil.sequentialColor(MatrixViewUtil.norm01(v, vmin, vmax));
case DIVERGING -> MatrixViewUtil.divergingColor(v, vmin, vmax,
divergingCenter != null ? divergingCenter.doubleValue() : 0.0);
};
g.setFill(col);
g.fillRect(x, y, Math.ceil(cellW), Math.ceil(cellH));
}
}
// Subtle grid lines
g.setStroke(Color.color(0, 0, 0, 0.15));
g.setLineWidth(1.0);
for (int j = 0; j <= c; j++) {
double x = contentX + j * cellW + 0.5;
g.strokeLine(x, contentY, x, contentY + contentH);
}
for (int i = 0; i <= r; i++) {
double y = contentY + i * cellH + 0.5;
g.strokeLine(contentX, y, contentX + contentW, y);
}
}
private void drawAxisLabels(GraphicsContext g) {
g.setFill(Color.LIGHTGRAY);
g.setFont(labelFont);
g.setTextAlign(TextAlignment.CENTER);
g.setTextBaseline(VPos.TOP);
int r = rows();
int c = cols();
if (r == 0 || c == 0) return;
double cellW = contentW / c;
double cellH = contentH / r;
// Top (columns)
for (int j = 0; j < c; j++) {
String lbl = (j < colLabels.length && colLabels[j] != null) ? colLabels[j] : ("c" + j);
double x = contentX + (j + 0.5) * cellW;
double y = contentY - labelMargin - textHeight(g, lbl);
g.fillText(lbl, x, Math.max(0, y));
}
// Left (rows)
g.setTextAlign(TextAlignment.RIGHT);
g.setTextBaseline(VPos.CENTER);
for (int i = 0; i < r; i++) {
String lbl = (i < rowLabels.length && rowLabels[i] != null) ? rowLabels[i] : ("r" + i);
double x = contentX - labelMargin;
double y = contentY + (i + 0.5) * cellH;
g.fillText(lbl, Math.max(0, x), y);
}
}
private void drawLegend(GraphicsContext g, double vmin, double vmax) {
double x0 = contentX + contentW + legendGap;
double y0 = contentY;
double w = legendWidth;
double h = contentH;
// Optional title above legend
if (legendTitle != null) {
g.save();
g.setFill(Color.LIGHTGRAY);
g.setFont(labelFont);
g.setTextAlign(TextAlignment.CENTER);
g.setTextBaseline(VPos.TOP);
double th = textHeight(g, legendTitle);
double ty = Math.max(0, y0 - Math.min(th + 2, 18));
g.fillText(legendTitle, x0 + w * 0.5, ty);
g.restore();
}
// Vertical gradient ramp
int steps = 200;
for (int k = 0; k < steps; k++) {
double t = 1.0 - (k / (double) (steps - 1)); // top (max) → bottom (min)
double v = vmin + t * (vmax - vmin);
Color col = switch (paletteKind) {
case SEQUENTIAL -> MatrixViewUtil.sequentialColor(MatrixViewUtil.norm01(v, vmin, vmax));
case DIVERGING -> MatrixViewUtil.divergingColor(v, vmin, vmax,
divergingCenter != null ? divergingCenter.doubleValue() : 0.0);
};
g.setFill(col);
double yy = y0 + k * (h / steps);
g.fillRect(x0, yy, w, (h / steps) + 1);
}
// Legend border
g.setStroke(Color.gray(0.2));
g.setLineWidth(1.0);
g.strokeRect(x0 + 0.5, y0 + 0.5, w - 1, h - 1);
// Ticks & labels inside the bar
double pad = 2.0;
g.setFill(Color.LIGHTGRAY);
g.setFont(labelFont);
g.setTextAlign(TextAlignment.LEFT);
g.setTextBaseline(VPos.CENTER);
g.fillText(df.format(vmax), x0 + pad, y0 + pad + textHeight(g, "X") * 0.5);
g.fillText(df.format(vmin), x0 + pad, y0 + h - pad - textHeight(g, "X") * 0.5);
if (paletteKind == PaletteKind.DIVERGING) {
double t = MatrixViewUtil.norm01(
divergingCenter != null ? divergingCenter.doubleValue() : 0.0, vmin, vmax);
double y = y0 + (1.0 - t) * h;
g.fillText(df.format(
divergingCenter != null ? divergingCenter.doubleValue() : 0.0), x0 + pad, y);
}
}
private void drawCenteredMessage(GraphicsContext g, double w, double h, String msg) {
g.setFill(Color.gray(0.7));
g.setFont(Font.font("Arial", 16));
g.setTextAlign(TextAlignment.CENTER);
g.setTextBaseline(VPos.CENTER);
g.fillText(msg == null ? "" : msg, w * 0.5, h * 0.5);
}
// -------------------- Interaction --------------------
private void onMouseMove(MouseEvent e) {
int r = rows(), c = cols();
if (r == 0 || c == 0) return;
int[] rc = pickCell(e.getX(), e.getY());
if (rc == null) {
hoverTip.hide();
return;
}
int i = rc[0], j = rc[1];
double v = MatrixViewUtil.sanitize(matrix[i][j]);
String rl = (i < rowLabels.length && rowLabels[i] != null) ? rowLabels[i] : ("r" + i);
String cl = (j < colLabels.length && colLabels[j] != null) ? colLabels[j] : ("c" + j);
hoverTip.setText(rl + ", " + cl + " = " + df.format(v));
if (!hoverTip.isShowing()) {
hoverTip.show(canvas, e.getScreenX() + 12, e.getScreenY() + 12);
} else {
hoverTip.setX(e.getScreenX() + 12);
hoverTip.setY(e.getScreenY() + 12);
}
}
private void onMouseClick(MouseEvent e) {
if (onCellClick == null) return;
int[] rc = pickCell(e.getX(), e.getY());
if (rc == null) return;
int i = rc[0], j = rc[1];
onCellClick.accept(new MatrixClick(i, j, MatrixViewUtil.sanitize(matrix[i][j])));
}
/**
* Return {row, col} or null if mouse is outside content box.
*/
private int[] pickCell(double x, double y) {
int r = rows(), c = cols();
if (r == 0 || c == 0) return null;
if (x < contentX || y < contentY || x > contentX + contentW || y > contentY + contentH) return null;
double cellW = contentW / c;
double cellH = contentH / r;
int j = (int) Math.floor((x - contentX) / cellW);
int i = (int) Math.floor((y - contentY) / cellH);
if (i < 0 || i >= r || j < 0 || j >= c) return null;
return new int[]{i, j};
}
// -------------------- Utilities --------------------
private static String[] defaultLabels(int n, String prefix) {
String p = (prefix == null || prefix.isBlank()) ? "a" : prefix.trim();
String[] out = new String[n];
for (int i = 0; i < n; i++) out[i] = p + i;
return out;
}
private double estimateTopLabelHeight(GraphicsContext g, int cols) {
if (cols == 0) return 0;
g.setFont(labelFont);
return Math.ceil(textHeight(g, "X") + 2);
}
private double estimateLeftLabelWidth(GraphicsContext g, int rows) {
if (rows == 0) return 0;
g.setFont(labelFont);
double maxW = 0;
int sample = Math.min(rows, 40);
int step = Math.max(1, rows / sample);
for (int i = 0; i < rows; i += step) {
String lbl = (i < rowLabels.length && rowLabels[i] != null) ? rowLabels[i] : ("r" + i);
double w = textWidth(g, lbl);
if (w > maxW) maxW = w;
}
return Math.ceil(maxW + 2);
}
private double textWidth(GraphicsContext g, String s) {
if (s == null || s.isEmpty()) return 0.0;
measureText.setText(s);
measureText.setFont(g.getFont());
return measureText.getLayoutBounds().getWidth();
}
private double textHeight(GraphicsContext g, String s) {
measureText.setText((s == null || s.isEmpty()) ? "Mg" : s);
measureText.setFont(g.getFont());
return measureText.getLayoutBounds().getHeight();
}
}