public class HeatChart
extends java.lang.Object
HeatChart
class describes a chart which can display
3-dimensions of values - x,y and z, where x and y are the usual 2-dimensional
axis and z is portrayed by colour intensity. Heat charts are sometimes known
as heat maps.
Use of this chart would typically involve 3 steps:
getChartImage()
or
saveToFile(String)
.
Construction of a new HeatChart
instance is through its one
constructor which takes a 2-dimensional array of doubles which
should contain the z-values for the chart. Consider this array to be the grid
of values which will instead be represented as colours in the chart.
Setting of the x-values and y-values which are displayed along the
appropriate axis is optional, and by default will simply display the values 0
to n-1, where n is the number of rows or columns. Otherwise, the x/y axis
values can be set with the setXValues
and setYValues
methods. Both methods are overridden with two forms:
The simplist way to set the axis values is to use the methods which take an array of Object[]. This array must have the same length as the number of columns for setXValues and same as the number of rows for setYValues. The string representation of the objects will then be used as the axis values.
This is convenient way of defining numerical values along the axis. One of the two methods takes an interval and an offset for either the x or y axis. These parameters supply the necessary information to describe the values based upon the z-value indexes. The quantity of x-values and y-values is already known from the lengths of the z-values array dimensions. Then the offset parameters indicate what the first value will be, with the intervals providing the increment from one column or row to the next.
Consider an example:
double[][] zValues = new double[][] { { 1.2, 1.3, 1.5 }, { 1.0, 1.1, 1.6 }, { 0.7, 0.9, 1.3 } }; double xOffset = 1.0; double yOffset = 0.0; double xInterval = 1.0; double yInterval = 2.0; chart.setXValues(xOffset, xInterval); chart.setYValues(yOffset, yInterval);
In this example, the z-values range from 0.7 to 1.6. The x-values range from the xOffset value 1.0 to 4.0, which is calculated as the number of x-values multiplied by the xInterval, shifted by the xOffset of 1.0. The y-values are calculated in the same way to give a range of values from 0.0 to 6.0.
This step is optional. By default the heat chart will be generated without a title or labels on the axis, and the colouring of the heat map will be in grayscale. A large range of configuration options are available to customise the chart. All customisations are available through simple accessor methods. See the javadoc of each of the methods for more information.
The generated heat chart can be obtained in two forms, using the following methods:
BufferedImage
object that can be used in any number of ways,
most notably it can be inserted into a Swing component, for use in a GUI
application.Modifier and Type | Field and Description |
---|---|
static double |
SCALE_EXPONENTIAL
A basic exponential scale value of 3.0.
|
static double |
SCALE_LINEAR
The linear scale value of 1.0.
|
static double |
SCALE_LOGARITHMIC
A basic logarithmic scale value of 0.3.
|
Constructor and Description |
---|
HeatChart(double[][] zValues,
boolean useAbsValZ)
Constructs a heatmap for the given z-values against x/y-values that by
default will be the values 0 to n-1, where n is the number of columns or
rows.
|
HeatChart(double[][] zValues,
double low,
double high)
Constructs a heatmap for the given z-values against x/y-values that by
default will be the values 0 to n-1, where n is the number of columns or
rows.
|
Modifier and Type | Method and Description |
---|---|
java.awt.Color |
getAxisColour()
Returns the colour that is set to be used for the axis bars.
|
java.awt.Color |
getAxisLabelColour()
Returns the current colour of the axis labels.
|
java.awt.Font |
getAxisLabelsFont()
Returns the font that describes the visual style of the labels of the axis.
|
int |
getAxisThickness()
Returns the width of the axis bars in pixels.
|
java.awt.Color |
getAxisValuesColour()
Returns the colour of the axis values as they will be painted along the
axis bars.
|
java.awt.Font |
getAxisValuesFont()
Returns the font which describes the visual style of the axis values.
|
java.awt.Color |
getBackgroundColour()
Returns an object that represents the colour to be used as the background
for the whole chart.
|
int |
getCellHeight()
Deprecated.
As of release 0.6, replaced by
getCellSize() |
java.awt.Dimension |
getCellSize()
Returns the size of each individual data cell that constitutes a value in
the x,y,z space.
|
int |
getCellWidth()
Deprecated.
As of release 0.6, replaced by
getCellSize() |
int |
getChartHeight()
Deprecated.
As of release 0.6, replaced by
getChartSize() |
java.awt.Image |
getChartImage()
Generates and returns a new chart
Image configured according
to this object's currently held settings. |
java.awt.Image |
getChartImage(boolean alpha)
Generates and returns a new chart
Image configured according
to this object's currently held settings. |
int |
getChartMargin()
Returns the width of the margin in pixels to be left as empty space around
the heat map element.
|
java.awt.Dimension |
getChartSize()
Returns the size of the chart in pixels as calculated according to the cell
dimensions, chart margin and other size settings.
|
int |
getChartWidth()
Deprecated.
As of release 0.6, replaced by
getChartSize() |
double |
getColourScale()
Returns the scale that is currently in use to map z-value to colour.
|
double |
getHighValue()
Returns the high value.
|
java.awt.Color |
getHighValueColour()
Returns the colour that is currently to be displayed for the heat map cells
with the highest z-value in the dataset.
|
double |
getLowValue()
Returns the low value.
|
java.awt.Color |
getLowValueColour()
Returns the colour that is currently to be displayed for the heat map cells
with the lowest z-value in the dataset.
|
java.lang.String |
getTitle()
Returns the String that will be used as the title of any successive calls
to generate a chart.
|
java.awt.Color |
getTitleColour()
Returns the
Color that represents the colour the title text
should be painted in. |
java.awt.Font |
getTitleFont()
Returns the
Font that describes the visual style of the title. |
boolean |
getUseAbsValZ() |
java.lang.String |
getXAxisLabel()
Returns the String that will be displayed as a description of the x-axis in
any generated charts.
|
int |
getXAxisValuesFrequency()
Returns the frequency of the values displayed along the x-axis.
|
java.lang.Object[] |
getXValues()
Returns the x-values which are currently set to display along the x-axis.
|
java.lang.String |
getYAxisLabel()
Returns the String that will be displayed as a description of the y-axis in
any generated charts.
|
int |
getYAxisValuesFrequency()
Returns the frequency of the values displayed along the y-axis.
|
java.lang.Object[] |
getYValues()
Returns the y-values which are currently set to display along the y-axis.
|
double[][] |
getZValues()
Returns the 2-dimensional array of z-values currently in use.
|
boolean |
isShowXAxisValues()
Returns whether axis values are to be shown at all for the x-axis.
|
boolean |
isShowYAxisValues()
Returns whether axis values are to be shown at all for the y-axis.
|
boolean |
isXValuesHorizontal()
Returns whether the text of the values along the x-axis are to be drawn
horizontally left-to-right, or vertically top-to-bottom.
|
boolean |
isYValuesHorizontal()
Returns whether the text of the values along the y-axis are to be drawn
horizontally left-to-right, or vertically top-to-bottom.
|
static double |
max(double[][] values)
Finds and returns the maximum value in a 2-dimensional array of doubles.
|
static double |
min(double[][] values,
boolean useAbsValZ)
Finds and returns the minimum value in a 2-dimensional array of doubles.
|
void |
saveToFile(java.io.File outputFile)
Generates a new chart
Image based upon the currently held
settings and then attempts to save that image to disk, to the location
provided as a File parameter. |
void |
setAxisColour(java.awt.Color axisColour)
Sets the colour to be used on the axis bars.
|
void |
setAxisLabelColour(java.awt.Color axisLabelColour)
Sets the colour of the text displayed as axis labels.
|
void |
setAxisLabelsFont(java.awt.Font axisLabelsFont)
Sets the font that describes the visual style of the axis labels.
|
void |
setAxisThickness(int axisThickness)
Sets the width of the axis bars in pixels.
|
void |
setAxisValuesColour(java.awt.Color axisValuesColour)
Sets the colour to be used for the axis values as they will be painted
along the axis bars.
|
void |
setAxisValuesFont(java.awt.Font axisValuesFont)
Sets the font which describes the visual style of the axis values.
|
void |
setBackgroundColour(java.awt.Color backgroundColour)
Sets the colour to be used on the background of the chart.
|
void |
setCellHeight(int cellHeight)
Deprecated.
As of release 0.6, replaced by
setCellSize(Dimension) |
void |
setCellSize(java.awt.Dimension cellSize)
Sets the size of each individual cell that constitutes a value in x,y,z
data space.
|
void |
setCellWidth(int cellWidth)
Deprecated.
As of release 0.6, replaced by
setCellSize(Dimension) |
void |
setChartMargin(int margin)
Sets the width of the margin in pixels to be left as empty space around the
heat map element.
|
void |
setColourScale(double colourScale)
Sets the scale that is currently in use to map z-value to colour.
|
void |
setHighValueColour(java.awt.Color highValueColour)
Sets the colour to be used to fill cells of the heat map with the highest
z-values in the dataset.
|
void |
setLowValueColour(java.awt.Color lowValueColour)
Sets the colour to be used to fill cells of the heat map with the lowest
z-values in the dataset.
|
void |
setShowXAxisValues(boolean showXAxisValues)
Sets whether axis values are to be shown at all for the x-axis.
|
void |
setShowYAxisValues(boolean showYAxisValues)
Sets whether axis values are to be shown at all for the y-axis.
|
void |
setTitle(java.lang.String title)
Sets the String that will be used as the title of any successive calls to
generate a chart.
|
void |
setTitleColour(java.awt.Color titleColour)
Sets the
Color that describes the colour to be used for the
chart title String. |
void |
setTitleFont(java.awt.Font titleFont)
Sets a new
Font to be used in rendering the chart's title
String. |
void |
setUseAbsValZ(boolean b) |
void |
setXAxisLabel(java.lang.String xAxisLabel)
Sets the String that will be displayed as a description of the x-axis in
any generated charts.
|
void |
setXAxisValuesFrequency(int axisValuesFrequency)
Sets the frequency of the values displayed along the x-axis.
|
void |
setXValues(double xOffset,
double xInterval)
Sets the x-values which are plotted along the x-axis.
|
void |
setXValues(java.lang.Object[] xValues)
Sets the x-values which are plotted along the x-axis.
|
void |
setXValuesHorizontal(boolean xValuesHorizontal)
Sets whether the text of the values along the x-axis should be drawn
horizontally left-to-right, or vertically top-to-bottom.
|
void |
setYAxisLabel(java.lang.String yAxisLabel)
Sets the String that will be displayed as a description of the y-axis in
any generated charts.
|
void |
setYAxisValuesFrequency(int axisValuesFrequency)
Sets the frequency of the values displayed along the y-axis.
|
void |
setYValues(double yOffset,
double yInterval)
Sets the y-values which are plotted along the y-axis.
|
void |
setYValues(java.lang.Object[] yValues)
Sets the y-values which are plotted along the y-axis.
|
void |
setYValuesHorizontal(boolean yValuesHorizontal)
Sets whether the text of the values along the y-axis should be drawn
horizontally left-to-right, or vertically top-to-bottom.
|
void |
setZValues(double[][] zValues)
Replaces the z-values array.
|
void |
setZValues(double[][] zValues,
double low,
double high)
Replaces the z-values array.
|
public static final double SCALE_LOGARITHMIC
public static final double SCALE_LINEAR
public static final double SCALE_EXPONENTIAL
public HeatChart(double[][] zValues, boolean useAbsValZ)
zValues
- the z-values, where each element is a row of z-values in the
resultant heat chart.public HeatChart(double[][] zValues, double low, double high)
zValues
- the z-values, where each element is a row of z-values in the
resultant heat chart.low
- the minimum possible value, which may or may not appear in the
z-values.high
- the maximum possible value, which may or may not appear in the
z-values.public void setUseAbsValZ(boolean b)
public boolean getUseAbsValZ()
public double getLowValue()
public double getHighValue()
public double[][] getZValues()
public void setZValues(double[][] zValues)
setZValues(double[][], double, double)
method for an example of
z-values. The smallest and largest values in the array are used as the
minimum and maximum values respectively.zValues
- the array to replace the current array with. The number of
elements in each inner array must be identical.public void setZValues(double[][] zValues, double low, double high)
new double[][]{ {1.0,1.2,1.4}, {1.2,1.3,1.5}, {0.9,1.3,1.2}, {0.8,1.6,1.1} };
1.0 | 1.2 | 1.4 | |
1.2 | 1.3 | 1.5 | |
0.9 | 1.3 | 1.2 | |
0.8 | 1.6 | 1.1 | |
zValues
- the array to replace the current array with. The number of
elements in each inner array must be identical.low
- the minimum possible value, which may or may not appear in the
z-values.high
- the maximum possible value, which may or may not appear in the
z-values.public void setXValues(double xOffset, double xInterval)
x-value = x-offset + (column-index * x-interval)
The x-interval defines the gap between each x-value and the x-offset is applied to each value to offset them all from zero.
Alternatively the x-values can be set more directly with the
setXValues(Object[])
method.
xOffset
- an offset value to be applied to the index of each z-value
element.xInterval
- an interval that will separate each x-value item.public void setXValues(java.lang.Object[] xValues)
xValues
- an array of elements to be displayed as values along the
x-axis.public void setYValues(double yOffset, double yInterval)
y-value = y-offset + (column-index * y-interval)
The y-interval defines the gap between each y-value and the y-offset is applied to each value to offset them all from zero.
Alternatively the y-values can be set more directly with the
setYValues(Object[])
method.
yOffset
- an offset value to be applied to the index of each z-value
element.yInterval
- an interval that will separate each y-value item.public void setYValues(java.lang.Object[] yValues)
yValues
- an array of elements to be displayed as values along the
y-axis.public java.lang.Object[] getXValues()
setXValues(Object[])
or that was generated from the offset and
interval that were given to setXValues(double, double)
, in
which case the object type of each element will be Double
.public java.lang.Object[] getYValues()
setYValues(Object[])
or that was generated from the offset and
interval that were given to setYValues(double, double)
, in
which case the object type of each element will be Double
.public void setXValuesHorizontal(boolean xValuesHorizontal)
xValuesHorizontal
- true if x-values should be drawn horizontally,
false if they should be drawn vertically.public boolean isXValuesHorizontal()
public void setYValuesHorizontal(boolean yValuesHorizontal)
yValuesHorizontal
- true if y-values should be drawn horizontally,
false if they should be drawn vertically.public boolean isYValuesHorizontal()
@Deprecated public void setCellWidth(int cellWidth)
setCellSize(Dimension)
cellWidth
- the new width to use for each individual data cell.@Deprecated public int getCellWidth()
getCellSize()
@Deprecated public void setCellHeight(int cellHeight)
setCellSize(Dimension)
cellHeight
- the new height to use for each individual data cell.@Deprecated public int getCellHeight()
getCellSize()
public void setCellSize(java.awt.Dimension cellSize)
cellSize
- the new size to use for each individual data cell.public java.awt.Dimension getCellSize()
@Deprecated public int getChartWidth()
getChartSize()
@Deprecated public int getChartHeight()
getChartSize()
public java.awt.Dimension getChartSize()
public java.lang.String getTitle()
public void setTitle(java.lang.String title)
If the title is set to null then no title will be displayed.
Defaults to null.
title
- the chart title to set.public java.lang.String getXAxisLabel()
public void setXAxisLabel(java.lang.String xAxisLabel)
If the xAxisLabel is set to null then no label will be displayed.
Defaults to null.
xAxisLabel
- the label to be displayed describing the x-axis.public java.lang.String getYAxisLabel()
public void setYAxisLabel(java.lang.String yAxisLabel)
If the yAxisLabel is set to null then no label will be displayed.
Defaults to null.
yAxisLabel
- the label to be displayed describing the y-axis.public int getChartMargin()
public void setChartMargin(int margin)
Defaults to 20 pixels.
margin
- the new margin to be left as blank space around the heat map.public java.awt.Color getBackgroundColour()
public void setBackgroundColour(java.awt.Color backgroundColour)
Defaults to Color.WHITE
.
backgroundColour
- the new colour to be set as the background fill.public java.awt.Font getTitleFont()
Font
that describes the visual style of the title.public void setTitleFont(java.awt.Font titleFont)
Font
to be used in rendering the chart's title
String.
Defaults to Sans-Serif, BOLD, 16 pixels.
titleFont
- the Font that should be used when rendering the chart
title.public java.awt.Color getTitleColour()
Color
that represents the colour the title text
should be painted in.public void setTitleColour(java.awt.Color titleColour)
Color
that describes the colour to be used for the
chart title String.
Defaults to Color.BLACK
.
titleColour
- the colour to paint the chart's title String.public int getAxisThickness()
public void setAxisThickness(int axisThickness)
Defaults to 2 pixels.
axisThickness
- the thickness to use for the axis bars in any newly
generated charts.public java.awt.Color getAxisColour()
public void setAxisColour(java.awt.Color axisColour)
Defaults to Color.BLACK
.
axisColour
- the colour to be set for use on the axis bars.public java.awt.Font getAxisLabelsFont()
public void setAxisLabelsFont(java.awt.Font axisLabelsFont)
Defaults to Sans-Serif, PLAIN, 12 pixels.
axisLabelsFont
- the font to be used to define the visual style of the
axis labels.public java.awt.Color getAxisLabelColour()
public void setAxisLabelColour(java.awt.Color axisLabelColour)
Defaults to Color.BLACK.
axisLabelColour
- the colour to use for the axis label text.public java.awt.Font getAxisValuesFont()
public void setAxisValuesFont(java.awt.Font axisValuesFont)
Defaults to Sans-Serif, PLAIN, 10 pixels.
axisValuesFont
- the font that should be used for the axis values.public java.awt.Color getAxisValuesColour()
public void setAxisValuesColour(java.awt.Color axisValuesColour)
Defaults to Color.BLACK.
axisValuesColour
- the new colour to be used for the axis bar values.public int getXAxisValuesFrequency()
public void setXAxisValuesFrequency(int axisValuesFrequency)
Defaults to 1. Every column is given a value.
axisValuesFrequency
- the frequency of the values displayed against
columns, where 1 is every column and 2 is every other column.public int getYAxisValuesFrequency()
public void setYAxisValuesFrequency(int axisValuesFrequency)
Defaults to 1. Every row is given a value.
axisValuesFrequency
- the frequency of the values displayed against
rows, where 1 is every row and 2 is every other row.public boolean isShowXAxisValues()
If axis values are not shown then more space is allocated to the heat map.
public void setShowXAxisValues(boolean showXAxisValues)
If axis values are not shown then more space is allocated to the heat map.
Defaults to true.
showXAxisValues
- true if x-axis values should be displayed, false if
they should be hidden.public boolean isShowYAxisValues()
If axis values are not shown then more space is allocated to the heat map.
public void setShowYAxisValues(boolean showYAxisValues)
If axis values are not shown then more space is allocated to the heat map.
Defaults to true.
showYAxisValues
- true if y-axis values should be displayed, false if
they should be hidden.public java.awt.Color getHighValueColour()
The full colour range will go through each RGB step between the high value colour and the low value colour.
public void setHighValueColour(java.awt.Color highValueColour)
The full colour range will go through each RGB step between the high value colour and the low value colour.
Defaults to Color.BLACK.
highValueColour
- the colour to use for cells of the highest z-value.public java.awt.Color getLowValueColour()
The full colour range will go through each RGB step between the high value colour and the low value colour.
public void setLowValueColour(java.awt.Color lowValueColour)
The full colour range will go through each RGB step between the high value colour and the low value colour.
Defaults to Color.WHITE.
lowValueColour
- the colour to use for cells of the lowest z-value.public double getColourScale()
public void setColourScale(double colourScale)
Defaults to a linear scale value of 1.0.
colourScale
- the scale that should be used to map from z-value to
colour.public void saveToFile(java.io.File outputFile) throws java.io.IOException
Image
based upon the currently held
settings and then attempts to save that image to disk, to the location
provided as a File parameter. The image type of the saved file will equal
the extension of the filename provided, so it is essential that a suitable
extension be included on the file name.
All supported ImageIO
file types are supported, including PNG,
JPG and GIF.
No chart will be generated until this or the related
getChartImage()
method are called. All successive calls will
result in the generation of a new chart image, no caching is used.
outputFile
- the file location that the generated image file should be
written to. The File must have a suitable filename, with an
extension of a valid image format (as supported by
ImageIO
).java.io.IOException
- if the output file's filename has no extension or if
there the file is unable to written to. Reasons for this include
a non-existant file location (check with the File exists() method
on the parent directory), or the permissions of the write
location may be incorrect.public java.awt.Image getChartImage(boolean alpha)
Image
configured according
to this object's currently held settings. The given parameter determines
whether transparency should be enabled for the generated image.
No chart will be generated until this or the related
saveToFile(File)
method are called. All successive calls will
result in the generation of a new chart image, no caching is used.
alpha
- whether to enable transparency.Image
. The returned image is a
BufferedImage
.public java.awt.Image getChartImage()
Image
configured according
to this object's currently held settings. By default the image is generated
with no transparency.
No chart will be generated until this or the related
saveToFile(File)
method are called. All successive calls will
result in the generation of a new chart image, no caching is used.
Image
. The returned image is a
BufferedImage
.public static double max(double[][] values)
public static double min(double[][] values, boolean useAbsValZ)