-
Notifications
You must be signed in to change notification settings - Fork 9
/
HeatMap.java
executable file
·646 lines (557 loc) · 21.8 KB
/
HeatMap.java
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
644
645
646
import java.awt.*;
import java.awt.image.BufferedImage;
import java.text.DecimalFormat;
import javax.swing.*;
/**
*
* <p><strong>Title:</strong> HeatMap</p>
*
* <p>Description: HeatMap is a JPanel that displays a 2-dimensional array of
* data using a selected color gradient scheme.</p>
* <p>For specifying data, the first index into the double[][] array is the x-
* coordinate, and the second index is the y-coordinate. In the constructor and
* updateData method, the 'useGraphicsYAxis' parameter is used to control
* whether the row y=0 is displayed at the top or bottom. Since the usual
* graphics coordinate system has y=0 at the top, setting this parameter to
* true will draw the y=0 row at the top, and setting the parameter to false
* will draw the y=0 row at the bottom, like in a regular, mathematical
* coordinate system. This parameter was added as a solution to the problem of
* "Which coordinate system should we use? Graphics, or mathematical?", and
* allows the user to choose either coordinate system. Because the HeatMap will
* be plotting the data in a graphical manner, using the Java Swing framework
* that uses the standard computer graphics coordinate system, the user's data
* is stored internally with the y=0 row at the top.</p>
* <p>There are a number of defined gradient types (look at the static fields),
* but you can create any gradient you like by using either of the following
* functions in the Gradient class:
* <ul>
* <li>public static Color[] createMultiGradient(Color[] colors, int numSteps)</li>
* <li>public static Color[] createGradient(Color one, Color two, int numSteps)</li>
* </ul>
* You can then assign an arbitrary Color[] object to the HeatMap as follows:
* <pre>myHeatMap.updateGradient(Gradient.createMultiGradient(new Color[] {Color.red, Color.white, Color.blue}, 256));</pre>
* </p>
*
* <p>By default, the graph title, axis titles, and axis tick marks are not
* displayed. Be sure to set the appropriate title before enabling them.</p>
*
* <hr />
* <p><strong>Copyright:</strong> Copyright (c) 2007, 2008</p>
*
* <p>HeatMap is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.</p>
*
* <p>HeatMap is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU General Public License
* along with HeatMap; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA</p>
*
* @author Matthew Beckler (matthew@mbeckler.org)
* @author Josh Hayes-Sheen (grey@grevian.org), Converted to use BufferedImage.
* @author J. Keller (jpaulkeller@gmail.com), Added transparency (alpha) support, data ordering bug fix.
* @version 1.6
*/
public class HeatMap extends JPanel
{
private double[][] data;
private int[][] dataColorIndices;
// these four variables are used to print the axis labels
private double xMin;
private double xMax;
private double yMin;
private double yMax;
private String title;
private String xAxis;
private String yAxis;
private boolean drawTitle = false;
private boolean drawXTitle = false;
private boolean drawYTitle = false;
private boolean drawLegend = false;
private boolean drawXTicks = false;
private boolean drawYTicks = false;
private Color[] colors;
private Color bg = Color.white;
private Color fg = Color.black;
private BufferedImage bufferedImage;
private Graphics2D bufferedGraphics;
/**
* @param data The data to display, must be a complete array (non-ragged)
* @param useGraphicsYAxis If true, the data will be displayed with the y=0 row at the top of the screen. If false, the data will be displayed with they=0 row at the bottom of the screen.
* @param colors A variable of the type Color[]. See also {@link #createMultiGradient} and {@link #createGradient}.
*/
public HeatMap(double[][] data, boolean useGraphicsYAxis, Color[] colors)
{
super();
updateGradient(colors);
updateData(data, useGraphicsYAxis);
this.setPreferredSize(new Dimension(60+data.length, 60+data[0].length));
this.setDoubleBuffered(true);
this.bg = Color.white;
this.fg = Color.black;
// this is the expensive function that draws the data plot into a
// BufferedImage. The data plot is then cheaply drawn to the screen when
// needed, saving us a lot of time in the end.
drawData();
}
/**
* Specify the coordinate bounds for the map. Only used for the axis labels, which must be enabled seperately. Calls repaint() when finished.
* @param xMin The lower bound of x-values, used for axis labels
* @param xMax The upper bound of x-values, used for axis labels
*/
public void setCoordinateBounds(double xMin, double xMax, double yMin, double yMax)
{
this.xMin = xMin;
this.xMax = xMax;
this.yMin = yMin;
this.yMax = yMax;
repaint();
}
/**
* Specify the coordinate bounds for the X-range. Only used for the axis labels, which must be enabled seperately. Calls repaint() when finished.
* @param xMin The lower bound of x-values, used for axis labels
* @param xMax The upper bound of x-values, used for axis labels
*/
public void setXCoordinateBounds(double xMin, double xMax)
{
this.xMin = xMin;
this.xMax = xMax;
repaint();
}
/**
* Specify the coordinate bounds for the X Min. Only used for the axis labels, which must be enabled seperately. Calls repaint() when finished.
* @param xMin The lower bound of x-values, used for axis labels
*/
public void setXMinCoordinateBounds(double xMin)
{
this.xMin = xMin;
repaint();
}
/**
* Specify the coordinate bounds for the X Max. Only used for the axis labels, which must be enabled seperately. Calls repaint() when finished.
* @param xMax The upper bound of x-values, used for axis labels
*/
public void setXMaxCoordinateBounds(double xMax)
{
this.xMax = xMax;
repaint();
}
/**
* Specify the coordinate bounds for the Y-range. Only used for the axis labels, which must be enabled seperately. Calls repaint() when finished.
* @param yMin The lower bound of y-values, used for axis labels
* @param yMax The upper bound of y-values, used for axis labels
*/
public void setYCoordinateBounds(double yMin, double yMax)
{
this.yMin = yMin;
this.yMax = yMax;
repaint();
}
/**
* Specify the coordinate bounds for the Y Min. Only used for the axis labels, which must be enabled seperately. Calls repaint() when finished.
* @param yMin The lower bound of Y-values, used for axis labels
*/
public void setYMinCoordinateBounds(double yMin)
{
this.yMin = yMin;
repaint();
}
/**
* Specify the coordinate bounds for the Y Max. Only used for the axis labels, which must be enabled seperately. Calls repaint() when finished.
* @param yMax The upper bound of y-values, used for axis labels
*/
public void setYMaxCoordinateBounds(double yMax)
{
this.yMax = yMax;
repaint();
}
/**
* Updates the title. Calls repaint() when finished.
* @param title The new title
*/
public void setTitle(String title)
{
this.title = title;
repaint();
}
/**
* Updates the state of the title. Calls repaint() when finished.
* @param drawTitle Specifies if the title should be drawn
*/
public void setDrawTitle(boolean drawTitle)
{
this.drawTitle = drawTitle;
repaint();
}
/**
* Updates the X-Axis title. Calls repaint() when finished.
* @param xAxisTitle The new X-Axis title
*/
public void setXAxisTitle(String xAxisTitle)
{
this.xAxis = xAxisTitle;
repaint();
}
/**
* Updates the state of the X-Axis Title. Calls repaint() when finished.
* @param drawXAxisTitle Specifies if the X-Axis title should be drawn
*/
public void setDrawXAxisTitle(boolean drawXAxisTitle)
{
this.drawXTitle = drawXAxisTitle;
repaint();
}
/**
* Updates the Y-Axis title. Calls repaint() when finished.
* @param yAxisTitle The new Y-Axis title
*/
public void setYAxisTitle(String yAxisTitle)
{
this.yAxis = yAxisTitle;
repaint();
}
/**
* Updates the state of the Y-Axis Title. Calls repaint() when finished.
* @param drawYAxisTitle Specifies if the Y-Axis title should be drawn
*/
public void setDrawYAxisTitle(boolean drawYAxisTitle)
{
this.drawYTitle = drawYAxisTitle;
repaint();
}
/**
* Updates the state of the legend. Calls repaint() when finished.
* @param drawLegend Specifies if the legend should be drawn
*/
public void setDrawLegend(boolean drawLegend)
{
this.drawLegend = drawLegend;
repaint();
}
/**
* Updates the state of the X-Axis ticks. Calls repaint() when finished.
* @param drawXTicks Specifies if the X-Axis ticks should be drawn
*/
public void setDrawXTicks(boolean drawXTicks)
{
this.drawXTicks = drawXTicks;
repaint();
}
/**
* Updates the state of the Y-Axis ticks. Calls repaint() when finished.
* @param drawYTicks Specifies if the Y-Axis ticks should be drawn
*/
public void setDrawYTicks(boolean drawYTicks)
{
this.drawYTicks = drawYTicks;
repaint();
}
/**
* Updates the foreground color. Calls repaint() when finished.
* @param fg Specifies the desired foreground color
*/
public void setColorForeground(Color fg)
{
this.fg = fg;
repaint();
}
/**
* Updates the background color. Calls repaint() when finished.
* @param bg Specifies the desired background color
*/
public void setColorBackground(Color bg)
{
this.bg = bg;
repaint();
}
/**
* Updates the gradient used to display the data. Calls drawData() and
* repaint() when finished.
* @param colors A variable of type Color[]
*/
public void updateGradient(Color[] colors)
{
this.colors = (Color[]) colors.clone();
if (data != null)
{
updateDataColors();
drawData();
repaint();
}
}
/**
* This uses the current array of colors that make up the gradient, and
* assigns a color index to each data point, stored in the dataColorIndices
* array, which is used by the drawData() method to plot the points.
*/
private void updateDataColors()
{
//We need to find the range of the data values,
// in order to assign proper colors.
double largest = Double.MIN_VALUE;
double smallest = Double.MAX_VALUE;
for (int x = 0; x < data.length; x++)
{
for (int y = 0; y < data[0].length; y++)
{
largest = Math.max(data[x][y], largest);
smallest = Math.min(data[x][y], smallest);
}
}
double range = largest - smallest;
// dataColorIndices is the same size as the data array
// It stores an int index into the color array
dataColorIndices = new int[data.length][data[0].length];
//assign a Color to each data point
for (int x = 0; x < data.length; x++)
{
for (int y = 0; y < data[0].length; y++)
{
double norm = (data[x][y] - smallest) / range; // 0 < norm < 1
int colorIndex = (int) Math.floor(norm * (colors.length - 1));
dataColorIndices[x][y] = colorIndex;
}
}
}
/**
* This function generates data that is not vertically-symmetric, which
* makes it very useful for testing which type of vertical axis is being
* used to plot the data. If the graphics Y-axis is used, then the lowest
* values should be displayed at the top of the frame. If the non-graphics
* (mathematical coordinate-system) Y-axis is used, then the lowest values
* should be displayed at the bottom of the frame.
* @return double[][] data values of a simple vertical ramp
*/
public static double[][] generateRampTestData()
{
double[][] data = new double[10][10];
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
data[x][y] = y;
}
}
return data;
}
/**
* This function generates an appropriate data array for display. It uses
* the function: z = sin(x)*cos(y). The parameter specifies the number
* of data points in each direction, producing a square matrix.
* @param dimension Size of each side of the returned array
* @return double[][] calculated values of z = sin(x)*cos(y)
*/
public static double[][] generateSinCosData(int dimension)
{
if (dimension % 2 == 0)
{
dimension++; //make it better
}
double[][] data = new double[dimension][dimension];
double sX, sY; //s for 'Scaled'
for (int x = 0; x < dimension; x++)
{
for (int y = 0; y < dimension; y++)
{
sX = 2 * Math.PI * (x / (double) dimension); // 0 < sX < 2 * Pi
sY = 2 * Math.PI * (y / (double) dimension); // 0 < sY < 2 * Pi
data[x][y] = Math.sin(sX) * Math.cos(sY);
}
}
return data;
}
/**
* This function generates an appropriate data array for display. It uses
* the function: z = Math.cos(Math.abs(sX) + Math.abs(sY)). The parameter
* specifies the number of data points in each direction, producing a
* square matrix.
* @param dimension Size of each side of the returned array
* @return double[][] calculated values of z = Math.cos(Math.abs(sX) + Math.abs(sY));
*/
public static double[][] generatePyramidData(int dimension)
{
if (dimension % 2 == 0)
{
dimension++; //make it better
}
double[][] data = new double[dimension][dimension];
double sX, sY; //s for 'Scaled'
for (int x = 0; x < dimension; x++)
{
for (int y = 0; y < dimension; y++)
{
sX = 6 * (x / (double) dimension); // 0 < sX < 6
sY = 6 * (y / (double) dimension); // 0 < sY < 6
sX = sX - 3; // -3 < sX < 3
sY = sY - 3; // -3 < sY < 3
data[x][y] = Math.cos(Math.abs(sX) + Math.abs(sY));
}
}
return data;
}
/**
* Updates the data display, calls drawData() to do the expensive re-drawing
* of the data plot, and then calls repaint().
* @param data The data to display, must be a complete array (non-ragged)
* @param useGraphicsYAxis If true, the data will be displayed with the y=0 row at the top of the screen. If false, the data will be displayed with the y=0 row at the bottom of the screen.
*/
public void updateData(double[][] data, boolean useGraphicsYAxis)
{
this.data = new double[data.length][data[0].length];
for (int ix = 0; ix < data.length; ix++)
{
for (int iy = 0; iy < data[0].length; iy++)
{
// we use the graphics Y-axis internally
if (useGraphicsYAxis)
{
this.data[ix][iy] = data[ix][iy];
}
else
{
this.data[ix][iy] = data[ix][data[0].length - iy - 1];
}
}
}
updateDataColors();
drawData();
repaint();
}
/**
* Creates a BufferedImage of the actual data plot.
*
* After doing some profiling, it was discovered that 90% of the drawing
* time was spend drawing the actual data (not on the axes or tick marks).
* Since the Graphics2D has a drawImage method that can do scaling, we are
* using that instead of scaling it ourselves. We only need to draw the
* data into the bufferedImage on startup, or if the data or gradient
* changes. This saves us an enormous amount of time. Thanks to
* Josh Hayes-Sheen (grey@grevian.org) for the suggestion and initial code
* to use the BufferedImage technique.
*
* Since the scaling of the data plot will be handled by the drawImage in
* paintComponent, we take the easy way out and draw our bufferedImage with
* 1 pixel per data point. Too bad there isn't a setPixel method in the
* Graphics2D class, it seems a bit silly to fill a rectangle just to set a
* single pixel...
*
* This function should be called whenever the data or the gradient changes.
*/
private void drawData()
{
bufferedImage = new BufferedImage(data.length,data[0].length, BufferedImage.TYPE_INT_ARGB);
bufferedGraphics = bufferedImage.createGraphics();
for (int x = 0; x < data.length; x++)
{
for (int y = 0; y < data[0].length; y++)
{
bufferedGraphics.setColor(colors[dataColorIndices[x][y]]);
bufferedGraphics.fillRect(x, y, 1, 1);
}
}
}
/**
* The overridden painting method, now optimized to simply draw the data
* plot to the screen, letting the drawImage method do the resizing. This
* saves an extreme amount of time.
*/
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int width = this.getWidth();
int height = this.getHeight();
this.setOpaque(true);
// clear the panel
g2d.setColor(bg);
g2d.fillRect(0, 0, width, height);
// draw the heat map
if (bufferedImage == null)
{
// Ideally, we only call drawData in the constructor, or if we
// change the data or gradients. We include this just to be safe.
drawData();
}
// The data plot itself is drawn with 1 pixel per data point, and the
// drawImage method scales that up to fit our current window size. This
// is very fast, and is much faster than the previous version, which
// redrew the data plot each time we had to repaint the screen.
g2d.drawImage(bufferedImage,
31, 31,
width - 30,
height - 30,
0, 0,
bufferedImage.getWidth(), bufferedImage.getHeight(),
null);
// border
g2d.setColor(fg);
g2d.drawRect(30, 30, width - 60, height - 60);
// title
if (drawTitle && title != null)
{
g2d.drawString(title, (width / 2) - 4 * title.length(), 20);
}
// axis ticks - ticks start even with the bottom left coner, end very close to end of line (might not be right on)
int numXTicks = (width - 60) / 50;
int numYTicks = (height - 60) / 50;
String label = "";
DecimalFormat df = new DecimalFormat("##.##");
// Y-Axis ticks
if (drawYTicks)
{
int yDist = (int) ((height - 60) / (double) numYTicks); //distance between ticks
for (int y = 0; y <= numYTicks; y++)
{
g2d.drawLine(26, height - 30 - y * yDist, 30, height - 30 - y * yDist);
label = df.format(((y / (double) numYTicks) * (yMax - yMin)) + yMin);
int labelY = height - 30 - y * yDist - 4 * label.length();
//to get the text to fit nicely, we need to rotate the graphics
g2d.rotate(Math.PI / 2);
g2d.drawString(label, labelY, -14);
g2d.rotate( -Math.PI / 2);
}
}
// Y-Axis title
if (drawYTitle && yAxis != null)
{
//to get the text to fit nicely, we need to rotate the graphics
g2d.rotate(Math.PI / 2);
g2d.drawString(yAxis, (height / 2) - 4 * yAxis.length(), -3);
g2d.rotate( -Math.PI / 2);
}
// X-Axis ticks
if (drawXTicks)
{
int xDist = (int) ((width - 60) / (double) numXTicks); //distance between ticks
for (int x = 0; x <= numXTicks; x++)
{
g2d.drawLine(30 + x * xDist, height - 30, 30 + x * xDist, height - 26);
label = df.format(((x / (double) numXTicks) * (xMax - xMin)) + xMin);
int labelX = (31 + x * xDist) - 4 * label.length();
g2d.drawString(label, labelX, height - 14);
}
}
// X-Axis title
if (drawXTitle && xAxis != null)
{
g2d.drawString(xAxis, (width / 2) - 4 * xAxis.length(), height - 3);
}
// Legend
if (drawLegend)
{
g2d.drawRect(width - 20, 30, 10, height - 60);
for (int y = 0; y < height - 61; y++)
{
int yStart = height - 31 - (int) Math.ceil(y * ((height - 60) / (colors.length * 1.0)));
yStart = height - 31 - y;
g2d.setColor(colors[(int) ((y / (double) (height - 60)) * (colors.length * 1.0))]);
g2d.fillRect(width - 19, yStart, 9, 1);
}
}
}
}