Home

Awesome

HeatMaps

Easily create beautiful custom heat maps in only a few lines of Java code!

I originally developed this code for the NAPS Data Analysis Toolbox and now I have spun it out into its own project so others can also benefit from it.

Getting Started

Add the following to your pom.xml:

<dependency>
  <groupId>io.github.dbeaudoinfortin</groupId>
  <artifactId>heatmaps</artifactId>
  <version>0.0.3</version>
</dependency>

Customize your heat map using the builder class and render it by calling HeatMap.render():

HeatMap.builder()
  .withTitle(shortTitle)
  .withXAxis(xAxis)
  .withYAxis(yAxis)
  .withOptions(HeatMapOptions.builder()
    .withGradient(HeatMapGradient.BASIC_GRADIENT)
    .build())
  .build()
  .render(myOutputFile, myDataRecords);

The output will be a PNG file at the output path of myOutputFile.

The builder pattern makes it easy to customize your heat map. For example, the following will render a heat map with no titles, no labels, no legend no border, just the core heat map with blending enabled:

HeatMap.builder()
  .withTitle("")
  .withXAxis(xAxis)
  .withYAxis(yAxis)
  .withOptions(HeatMapOptions.builder()
    .withShowLegend(false)
    .withShowXAxisLabels(false)
    .withShowYAxisLabels(false)
    .withOutsidePadding(0)
    .withBlendColours(true)
    .withBlendColoursScale(5)
    .withColourScaleLowerBound(5)
    .withColourScaleUpperBound(20)
    .withGradient(HeatMapGradient.getCannedGradient(5)
    .build())
  .build()
  .render(myOutPutFile, myDataRecords);

The data is passed in as a Collection<DataRecord>. You can either use your own pojo and implement the interface com.dbf.heatmaps.data.DataRecord or use the provided com.dbf.heatmaps.data.BasicDataRecord pojo class. The interface is very simple:

public interface DataRecord {
  public Double getValue();
  public Object getX();
  public Object getY();
}

Two options are provided for defining the chart axes: IntegerAxis which is useful integer values (such as 1-31 for days of the month) and StringAxis which is useful for string values (such as Monday-Sunday for days of the week). In both cases, the axis entries are always treated as discrete values and rendered in the same order that each entry is added to the axis. For the IntegerAxis, a convenience method is provided to automatically populate the values between a given minimum and maximum. For example:

IntegerAxis xAxis = new IntegerAxis("Days of the Year", 1, 366);
StringAxis  yAxis = new StringAxis("Weird Cars", "BMC Landcrab", "Ford Probe", "Renault LeCar", "Subaru Brat", "Ferrari LaFerrari");

Examples

<p align="center"> <img src="https://github.com/user-attachments/assets/373d0966-0c9a-4739-b014-c8b2ddf6c1e6" width="400" /> <img src="https://github.com/user-attachments/assets/2ce8e484-b4ab-429a-8c59-d2b79cd66107" width="400" /> <img src="https://github.com/user-attachments/assets/00434021-e13b-4797-b5cb-4e236c591a29" width="800" /> <img src="https://github.com/user-attachments/assets/b814eb8f-5e93-4184-a764-a91d6f990880" width="400" /> <img src="https://github.com/user-attachments/assets/650c4387-c5b4-4005-ad42-3c344b5436ba" width="400" /> </p>

Customization Options

The heat maps are highly customizable, down to colours, fonts, gradients, titles, layout, etc. The following customization options are provided out of the box:

Heat Map Rendering

Titles & Labels

Legend

All of these options can be set in the HeatMapOptions class. The following table summarizes all the possible configuration options.

OptionDefault ValueDescription
backgroundColourWhite, unless it conflicts with the colour gradient, then grey.Sets the background colour of the whole chart.
cellWidth50pxSets the desired cell width in pixels. Will be enlarged if it's too small to fit the labels.
cellHeight50pxSets the desired cell width in pixels . Will be enlarged if it's too small to fit the labels.
showGridlinesfalseToggles the rendering of grid lines on the heat map between the cells.
gridLineWidth1pxSets the width, in pixels, of the grid lines.
gridLineColourBlackSets the colour of the grid lines.
showXAxisLabelstrueToggles the rendering of the labels for the X-axis.
xAxisLabelsBelowfalseToggles the rendering of the X-axis labels below the heat map instead of above.
xAxisLabelsRotatefalseForces the X-axis labels to be rotated 90 degrees and rendered vertically. Otherwise, the X-axis labels will be automatically rotated if they are bigger than the cell width. When the labels are rendered above the heat map they are vertically aligned to the bottom, and when they are rendered below the heat map they are vertically aligned to the top.
showYAxisLabelstrueToggles the rendering of the labels for the Y-axis.
axisLabelFontCalibri, Plain, 20ptsSets the font used to render the X-axis and Y-axis labels.
axisLabelFontColourBlackSets the colour used to render the X-axis and Y-axis labels.
axisTitleFontCalibri, Bold, 20ptsSets the font used to render the X-axis and Y-axis titles.
axisTitleFontColourBlackSets the colour used to render the X-axis and Y-axis titles.
heatMapTitleFontCalibri, Bold, 36ptsSets the font used to render the overall chart title.
heatMapTitleFontColourBlackSets the colour used to render the overall chart title.
showGridValuesfalseToggles the rendering of the values within each cell of the heat map.
gridValuesFormat0.#Sets the decimal format used to display the values within each cell of the heat map. The Double to String conversion makes use of the Java DecimalFormat class.
gridValuesFontCalibri, Plain, 20ptsSets the font used to render the values within each cell of the heat map.
gridValuesFontColourBlackSets the colour used to render the values within each cell of the heat map.
blendColoursfalseToggles the blending of colours between adjacent cells of the heat map grid. See the section below for details on how this works.
blendColoursScale3Sets the amount (strength) of blending to use. This corresponds to the scaling factor of the bilinear interpolation, essentially how "smooth" the result will be. See the section below for details on how this works.
axisLabelPadding10pxSets the amount of blank space (padding), in pixels, between the X-axis and Y-axis labels and the heat map grid.
axisTitlePadding20pxSets the amount of blank space (padding), in pixels, below (for the X-axis) and to the right (for the Y-axis) of the axis titles.
heatMapTitlePadding40pxSets the amount of blank space (padding), in pixels, below the overall chart title.
outsidePadding5pxSets the amount of blank space (padding), in pixels, on the perimeter of the entire chart.
legendPadding40pxSets the amount of blank space (padding), in pixels, between the legend and the heat map grid.
showLegendtrueToggles the rendering of the legend, including the legend labels.
legendTextFormat0.##Sets the decimal format used to display the values of the legend labels. The Double to String conversion makes use of the Java DecimalFormat class.
legendLabelFontCalibri, Plain, 20ptsSets the font used to render the legend labels.
legendLabelFontColourBlackSets the colour used to render the legend labels.
legendStepsThe greater of the number of cells of the Y-axis and 5Sets the number of discrete colour steps to include in the legend. The minimum value is 2.
gradientHeatMapGradient.BASIC_GRADIENTSets the colour gradient for the heat map. See the section below for details on how this works.
colourScaleLowerBoundAutomatically calculated based on the lowest data value.Restricts the minimum value (low bound) of the heat map gradient. Any value below this threshold will be assigned the same minimum colour according to the chosen gradient.
colourScaleUpperBoundAutomatically calculated based on the highest data value.Restricts the maximum value (upper bound) of the heat map gradient. Any value above this threshold will be assigned the same maximum colour according to the chosen gradient.

Heat Map Gradients

There are 9 pre-defined gradients to choose from. I plan to eventually add more in the future. The current palettes are the following:

  1. A smooth gradient based on the colour wheel from blue to red. All of the colours are fully saturated.
  2. A 12 step gradient from blue to red with less saturation than the first colour palette.
  3. A simplified 5 step gradient from blue to red.
  4. A two colour gradient from blue to red, with purple mixed in-between.
  5. A 5 step colour blind friendly gradient of greenish-yellow to dark orange.
  6. A 3 step black-red-orange gradient, similar to black-body radiation, up to approximately 1300 degrees Kelvin.
  7. Same as number 6 but two more steps are added to extend the scale up to approximately 6500k degrees Kelvin.
  8. A 50 step colour gradient based on Dave Green's ‘cubehelix’ colour scheme
  9. A 2 step grey-scale gradient that should be used for non-colour screen/print-outs.

The default colour palette, if not specified, is set to number 3. Here are examples of what the colour palette look like, in order:

Gradient_1 Gradient_2 Gradient_3 Gradient_4 Gradient_5 Gradient_6 Gradient_7 Gradient_8 Gradient_9

Colour Blending

Enabling the blendColours option will result in a linear colour interpolation being applied to the cells of the heat map in order to "smooth out" the values. This effect works by first rendering each cell as a single pixel, then applying a bilinear upscaler (with a variable scaling factor), followed by applying an alpha mask to avoid blending with empty cells, and finally applying a nearest-neighbour up or down scaling to the final output resolution. The strength of this effect can be controlled via the blendColoursScale option, which supports values between 2 and 20, inclusive. By default, a value of 3 is used which will upscale each cell to a 3x3 grid, ensuring that the centre of the grid always represents the true colour of the corresponding data. Here is an example of what colour blending looks like, starting with no effect, then a strength of 2, 3, 5,and 10, respectively.

<p align="center"> <img src="https://github.com/user-attachments/assets/f64e9520-7f8b-4473-be04-34be30771b69" width="200" /> <img src="https://github.com/user-attachments/assets/1b3cdab1-97a9-4e4a-9f47-670644505911" width="200" /> <img src="https://github.com/user-attachments/assets/6ecc18c4-0482-4f50-ad7d-a1f0d5dbf9f7" width="200" /> <img src="https://github.com/user-attachments/assets/259f2575-b1f9-4aa1-9962-969fa3b6ed7c" width="200" /> <img src="https://github.com/user-attachments/assets/657d4e69-3a81-47a2-84ca-e1bbafcbd57c" width="200" /> </p>

Implementation Details

This library is built from scratch using the Java 2D graphics classes. To make it easy to integrate with your project, there are no external dependencies, only Java 8 or later.

(more documentation coming soon.)

Requirements

Legal Stuff

Copyright (c) 2024 David Fortin

This software is provided by David Fortin under the MIT License, meaning you are free to use it however you want, as long as you include the original copyright notice (above) and license notice in any copy you make. You just can't hold me liable in case something goes wrong. License details can be read here