# Temperature by grid position · Heatmap

Preview

![Temperature by grid position](family-heatmap-color-dimension-light.png)

![Temperature by grid position](family-heatmap-color-dimension-dark.png)

800 × 440 pixels

## Create this example

Create a console project, add the ChartForgeX package, and replace Program.cs with this source. Run it to write SVG, PNG and HTML with the default system fonts.

For the same typography as the gallery, [download the gallery-font recipe](family-heatmap-color-dimension-light.gallery.csharp.txt) and copy the gallery’s fonts directory beside your program.

```
#region Setup
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using ChartForgeX;
using ChartForgeX.Core;
using ChartForgeX.Primitives;
using ChartForgeX.Rendering;
using ChartForgeX.Themes;
using ChartForgeX.Topology;
using ChartForgeX.Typography;
using ChartForgeX.VisualArtifacts;
using ChartForgeX.VisualBlocks;

var mode = ThemeMode.Light;
var theme = VisualTheme.Graphite();
var model = HeatmapColorDimensionRecipe.Create("color-dimension", mode);
var context = new VisualRenderContext(
    new VisualLayoutOptions(new VisualSize(800, 440)), theme, mode,
    new VisualFrame("Temperature by grid position", "Temperature captions use their own format and culture", showLegend: null, showSurface: false));
var prepared = model.Prepare(context);
File.WriteAllText("visual.svg", prepared.ToSvg());
File.WriteAllBytes("visual.png", prepared.ToPng());
File.WriteAllText("visual.html", prepared.ToArtifact("example", VisualArtifactKind.Chart).ToHtmlPage());
#endregion

/// <summary>Formats heatmap temperatures with independent color-dimension captions and legends.</summary>
public static class HeatmapColorDimensionRecipe {
    /// <summary>Creates a temperature dimension with automatic, gradient or swatch legend presentation.</summary>
    public static Chart Create(string variant = "color-dimension", ThemeMode mode = ThemeMode.Light) {
        if (variant is not ("color-dimension" or "compact-color-dimension" or "gradient-color-legend" or "compact-gradient-color-legend"
            or "swatch-color-legend" or "compact-swatch-color-legend")) throw new ArgumentOutOfRangeException(nameof(variant));
        var colors = VisualTheme.Graphite().Resolve(mode);
        var gradient = variant is "gradient-color-legend" or "compact-gradient-color-legend";
        var swatches = variant is "swatch-color-legend" or "compact-swatch-color-legend";
        var values = new double?[] { -30, -20, -3, 0, 2.5, 5, 10, 22.5, 40, 55, null, null };
        return Chart.Create().Labels(labels => labels.NoData = "Not sampled").XLabels("A", "B", "C", "D", "E", "F")
            .AddHeatmapRow("Primary", values.Take(6), id: new ChartSeriesId("primary-measurements"))
            .AddHeatmapRow("Secondary", values.Skip(6), id: new ChartSeriesId("secondary-measurements"))
            .Heatmap(h => h.ColorScale(TemperatureScale().WithNoDataColor(colors.Border)))
            .Heatmap(heatmap => heatmap.ColorDimension(options => {
                options.Name = "Temperature"; options.ValueFormat = ChartValueFormat.Number("#,0.### '°C'", CultureInfo.InvariantCulture);
                options.LegendStyle = gradient ? ChartColorLegendStyle.Gradient : swatches ? ChartColorLegendStyle.Swatches : ChartColorLegendStyle.Auto;
            })).Heatmap(h => h.CellGap(3).CellRadius(3)).DataLabels();
    }

    private static ChartColorScale TemperatureScale() => ChartColorScale.Diverging(new[] {
        new ChartColorStop(-20, ChartColor.FromHex("#2166AC")), new ChartColorStop(-3, ChartColor.FromHex("#67A9CF"))
    }, new ChartColorStop(0, ChartColor.FromHex("#D1D5DB")), new[] {
        new ChartColorStop(5, ChartColor.FromHex("#D9A441")), new ChartColorStop(40, ChartColor.FromHex("#B2182B"))
    }).WithLabels("Cold", "Freezing", "Warm");
}
```

```
#region Setup
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using ChartForgeX;
using ChartForgeX.Core;
using ChartForgeX.Primitives;
using ChartForgeX.Rendering;
using ChartForgeX.Themes;
using ChartForgeX.Topology;
using ChartForgeX.Typography;
using ChartForgeX.VisualArtifacts;
using ChartForgeX.VisualBlocks;

var mode = ThemeMode.Dark;
var theme = VisualTheme.Graphite();
var model = HeatmapColorDimensionRecipe.Create("color-dimension", mode);
var context = new VisualRenderContext(
    new VisualLayoutOptions(new VisualSize(800, 440)), theme, mode,
    new VisualFrame("Temperature by grid position", "Temperature captions use their own format and culture", showLegend: null, showSurface: false));
var prepared = model.Prepare(context);
File.WriteAllText("visual.svg", prepared.ToSvg());
File.WriteAllBytes("visual.png", prepared.ToPng());
File.WriteAllText("visual.html", prepared.ToArtifact("example", VisualArtifactKind.Chart).ToHtmlPage());
#endregion

/// <summary>Formats heatmap temperatures with independent color-dimension captions and legends.</summary>
public static class HeatmapColorDimensionRecipe {
    /// <summary>Creates a temperature dimension with automatic, gradient or swatch legend presentation.</summary>
    public static Chart Create(string variant = "color-dimension", ThemeMode mode = ThemeMode.Light) {
        if (variant is not ("color-dimension" or "compact-color-dimension" or "gradient-color-legend" or "compact-gradient-color-legend"
            or "swatch-color-legend" or "compact-swatch-color-legend")) throw new ArgumentOutOfRangeException(nameof(variant));
        var colors = VisualTheme.Graphite().Resolve(mode);
        var gradient = variant is "gradient-color-legend" or "compact-gradient-color-legend";
        var swatches = variant is "swatch-color-legend" or "compact-swatch-color-legend";
        var values = new double?[] { -30, -20, -3, 0, 2.5, 5, 10, 22.5, 40, 55, null, null };
        return Chart.Create().Labels(labels => labels.NoData = "Not sampled").XLabels("A", "B", "C", "D", "E", "F")
            .AddHeatmapRow("Primary", values.Take(6), id: new ChartSeriesId("primary-measurements"))
            .AddHeatmapRow("Secondary", values.Skip(6), id: new ChartSeriesId("secondary-measurements"))
            .Heatmap(h => h.ColorScale(TemperatureScale().WithNoDataColor(colors.Border)))
            .Heatmap(heatmap => heatmap.ColorDimension(options => {
                options.Name = "Temperature"; options.ValueFormat = ChartValueFormat.Number("#,0.### '°C'", CultureInfo.InvariantCulture);
                options.LegendStyle = gradient ? ChartColorLegendStyle.Gradient : swatches ? ChartColorLegendStyle.Swatches : ChartColorLegendStyle.Auto;
            })).Heatmap(h => h.CellGap(3).CellRadius(3)).DataLabels();
    }

    private static ChartColorScale TemperatureScale() => ChartColorScale.Diverging(new[] {
        new ChartColorStop(-20, ChartColor.FromHex("#2166AC")), new ChartColorStop(-3, ChartColor.FromHex("#67A9CF"))
    }, new ChartColorStop(0, ChartColor.FromHex("#D1D5DB")), new[] {
        new ChartColorStop(5, ChartColor.FromHex("#D9A441")), new ChartColorStop(40, ChartColor.FromHex("#B2182B"))
    }).WithLabels("Cold", "Freezing", "Warm");
}
```

<!-- PowerForge markdown artifact sha256:E189F48238DAAE4E41EEF5F784ABEA0D0CDF08B178B5284DBD8FF0296BFC3F01 -->
