This example demonstrates adding labels to the cells, adding gaps between cells and using a custom color scale.
This example is similar to
Discrete Heat Map, with the following additions:
perldemo\heatmapcelllabels.pl
#!/usr/bin/perl
# The ChartDirector for Perl module is assumed to be in "../lib"
use File::Basename;
use lib (dirname($0)."/../lib") =~ /(.*)/;
use perlchartdir;
# The x-axis and y-axis labels
my $xLabels = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"];
my $yLabels = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
# Random data for the 10 x 10 cells
my $rand = new RanSeries(2);
my $zData = $rand->get2DSeries(scalar(@$xLabels), scalar(@$yLabels), 0, 100);
# We set the middle 5 x 5 cells to NoValue to remove them from the chart
for(my $x = 3; $x < 7; ++$x) {
for(my $y = 3; $y < 7; ++$y) {
$zData->[$y * scalar(@$xLabels) + $x] = $perlchartdir::NoValue;
}
}
# Create an XYChart object of size 480 x 540 pixels.
my $c = new XYChart(480, 540);
# Set the plotarea at (50, 40) and of size 400 x 400 pixels. Set the background, border, and grid
# lines to transparent.
my $p = $c->setPlotArea(50, 40, 400, 400, -1, -1, $perlchartdir::Transparent,
$perlchartdir::Transparent);
# Create a discrete heat map with 10 x 10 cells
my $layer = $c->addDiscreteHeatMapLayer($zData, scalar(@$xLabels));
# Set the x-axis labels. Use 8pt Arial Bold font. Set axis stem to transparent, so only the labels
# are visible. Set 0.5 offset to position the labels in between the grid lines. Position the x-axis
# at the top of the chart.
$c->xAxis()->setLabels($xLabels);
$c->xAxis()->setLabelStyle("Arial Bold", 8);
$c->xAxis()->setColors($perlchartdir::Transparent, $perlchartdir::TextColor);
$c->xAxis()->setLabelOffset(0.5);
$c->xAxis()->setTitle("X axis title placeholder", "Arial Bold", 12);
$c->setXAxisOnTop();
# Set the y-axis labels. Use 8pt Arial Bold font. Set axis stem to transparent, so only the labels
# are visible. Set 0.5 offset to position the labels in between the grid lines. Reverse the y-axis
# so that the labels are flowing top-down instead of bottom-up.
$c->yAxis()->setLabels($yLabels);
$c->yAxis()->setLabelStyle("Arial Bold", 8);
$c->yAxis()->setColors($perlchartdir::Transparent, $perlchartdir::TextColor);
$c->yAxis()->setLabelOffset(0.5);
$c->yAxis()->setTitle("Y axis title placeholder", "Arial Bold", 12);
$c->yAxis()->setReverse();
# Set a 3-pixel gap between cells
$layer->setCellGap(3);
# Use the z value as the cell label
$layer->setDataLabelFormat("{z|0}");
# Position the color axis 20 pixels below the plot area and of the width as the plot area. Put the
# labels at the bottom side of the color axis. Use 8pt Arial Bold font for the labels.
my $cAxis = $layer->setColorAxis($p->getLeftX(), $p->getBottomY() + 20, $perlchartdir::TopLeft,
$p->getWidth(), $perlchartdir::Bottom);
$cAxis->setLabelStyle("Arial Bold", 8);
$cAxis->setTitle("Color legend title placeholder", "Arial Bold", 12);
# Set the color stops and scale of the color axis
my $colorScale = [0, 0x00ff00, 50, 0xffff00, 80, 0xff6600, 100, 0xff0000];
$cAxis->setColorScale($colorScale);
$cAxis->setLinearScale(0, 100, 10);
# Output the chart
$c->makeChart("heatmapcelllabels.png");
© 2021 Advanced Software Engineering Limited. All rights reserved.