# Service states by region and work window · Heatmap

Preview

![Service states by region and work window](heatmap-hierarchical-axes-states-wide-light.png)

![Service states by region and work window](heatmap-hierarchical-axes-states-wide-dark.png)

1000 × 560 pixels

## Create this example

Create a console project, add the ChartForgeX and ChartForgeX.Interactivity.Html packages, 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](heatmap-hierarchical-axes-states-wide-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 = HeatmapHierarchicalAxesRecipe.Create("states-wide", mode);
var context = new VisualRenderContext(
    new VisualLayoutOptions(new VisualSize(1000, 560)), theme, mode,
    new VisualFrame("Service states by region and work window", "Columns bind to raw coordinates; row groups bind to one-based source slots", 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>Groups rectangular matrix columns and source row slots through the ordinary axis-label owner.</summary>
public static class HeatmapHierarchicalAxesRecipe {
    /// <summary>Creates numeric or categorical rows with complete paths, missing cells and independent source IDs.</summary>
    /// <param name="variant">Use wide, compact, states-wide or states-compact. Numeric and categorical cells remain separate models.</param>
    /// <param name="mode">The paired theme mode.</param>
    /// <returns>A complete native chart ready for SVG, PNG or the optional HTML adapter.</returns>
    public static Chart Create(string variant = "wide", ThemeMode mode = ThemeMode.Light) {
        if (variant is not ("wide" or "compact" or "states-wide" or "states-compact")) throw new ArgumentOutOfRangeException(nameof(variant));
        var compact = variant.EndsWith("compact", StringComparison.Ordinal); var states = variant.StartsWith("states", StringComparison.Ordinal);
        var theme = VisualTheme.Graphite(); var colors = theme.Resolve(mode);
        var chart = Chart.Create().Size(compact ? 390 : 1000, 560).Theme(theme).ThemeMode(mode)
            .Title(states ? "Service states by region and work window" : "Service measurements by region and work window")
            .XAxis(x => x.Title("Work window")).YAxis(y => y.Title("Source row slots")).DataLabels(false)
            .Heatmap(h => h.CellGap(3));
        if (states) {
            chart.StateCategories(new ChartStateCategory("pass", "Available", colors.Status.Pass.Fill),
                new ChartStateCategory("review", "Review", colors.Status.Medium.Fill));
            chart.AddSeries(States("west", "West service", new[] { "pass", null, "review", "pass" }));
            chart.AddSeries(States("unknown", "Unknown service", new string?[] { null, null, null, null }));
            chart.AddSeries(States("east", "East service", new[] { "review", "pass", "pass", "review" }));
            chart.AddSeries(States("return", "Return service", new[] { null, "pass", "review", "pass" }));
        } else {
            chart.AddHeatmapRow("West service", new double?[] { 0, null, 12, 24 }, id: new ChartSeriesId("west"))
                .AddHeatmapRow("Unknown service", new double?[] { null, null, null, null }, id: new ChartSeriesId("unknown"))
                .AddHeatmapRow("East service", new double?[] { 20, 10, 0, 5 }, id: new ChartSeriesId("east"))
                .AddHeatmapRow("Return service", new double?[] { null, 2, 3, 4 }, id: new ChartSeriesId("return"));
            foreach (var row in chart.Series) row.ObservationIds(row.Points.Select(point => new ChartObservationId("column-" + point.X.ToString("0", CultureInfo.InvariantCulture))));
            chart.Heatmap(h => h.ColorScale(ChartColorScale.Sequential(colors.Surface, colors.Accent).WithValueRange(0, 24)));
        }
        chart.XLabels(new[] {
            new ChartAxisLabel(1, "A", "Morning", "Work"), new ChartAxisLabel(2, "B", "Morning", "Work"),
            new ChartAxisLabel(3, "A", "Evening", "Work"), new ChartAxisLabel(4, "B", "Evening", "Work")
        });
        // Y labels bind one-based SOURCE ROW SLOTS, not intensities or series IDs.
        // Reorder these mappings deliberately when changing row membership; mirroring axes leaves them intact.
        chart.YAxis(axis => axis.Labels.AddRange(new[] {
            new ChartAxisLabel(1, "West", "North", "Core"), new ChartAxisLabel(2, "Unknown", "North", "Core"),
            new ChartAxisLabel(3, "East", "South", "Core"), new ChartAxisLabel(4, "Return", "North", "Core")
        }));
        return chart;
    }

    private static ChartSeries States(string id, string name, string?[] values) => ChartSeries.CreateHeatmapCategoryRow(new ChartSeriesId(id), name,
        values.Select(value => value == null ? (ChartHeatmapCell?)null : new ChartHeatmapCell(value)))
        .ObservationIds(Enumerable.Range(1, values.Length).Select(column => new ChartObservationId("column-" + column.ToString(CultureInfo.InvariantCulture))));
}
```

```
#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 = HeatmapHierarchicalAxesRecipe.Create("states-wide", mode);
var context = new VisualRenderContext(
    new VisualLayoutOptions(new VisualSize(1000, 560)), theme, mode,
    new VisualFrame("Service states by region and work window", "Columns bind to raw coordinates; row groups bind to one-based source slots", 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>Groups rectangular matrix columns and source row slots through the ordinary axis-label owner.</summary>
public static class HeatmapHierarchicalAxesRecipe {
    /// <summary>Creates numeric or categorical rows with complete paths, missing cells and independent source IDs.</summary>
    /// <param name="variant">Use wide, compact, states-wide or states-compact. Numeric and categorical cells remain separate models.</param>
    /// <param name="mode">The paired theme mode.</param>
    /// <returns>A complete native chart ready for SVG, PNG or the optional HTML adapter.</returns>
    public static Chart Create(string variant = "wide", ThemeMode mode = ThemeMode.Light) {
        if (variant is not ("wide" or "compact" or "states-wide" or "states-compact")) throw new ArgumentOutOfRangeException(nameof(variant));
        var compact = variant.EndsWith("compact", StringComparison.Ordinal); var states = variant.StartsWith("states", StringComparison.Ordinal);
        var theme = VisualTheme.Graphite(); var colors = theme.Resolve(mode);
        var chart = Chart.Create().Size(compact ? 390 : 1000, 560).Theme(theme).ThemeMode(mode)
            .Title(states ? "Service states by region and work window" : "Service measurements by region and work window")
            .XAxis(x => x.Title("Work window")).YAxis(y => y.Title("Source row slots")).DataLabels(false)
            .Heatmap(h => h.CellGap(3));
        if (states) {
            chart.StateCategories(new ChartStateCategory("pass", "Available", colors.Status.Pass.Fill),
                new ChartStateCategory("review", "Review", colors.Status.Medium.Fill));
            chart.AddSeries(States("west", "West service", new[] { "pass", null, "review", "pass" }));
            chart.AddSeries(States("unknown", "Unknown service", new string?[] { null, null, null, null }));
            chart.AddSeries(States("east", "East service", new[] { "review", "pass", "pass", "review" }));
            chart.AddSeries(States("return", "Return service", new[] { null, "pass", "review", "pass" }));
        } else {
            chart.AddHeatmapRow("West service", new double?[] { 0, null, 12, 24 }, id: new ChartSeriesId("west"))
                .AddHeatmapRow("Unknown service", new double?[] { null, null, null, null }, id: new ChartSeriesId("unknown"))
                .AddHeatmapRow("East service", new double?[] { 20, 10, 0, 5 }, id: new ChartSeriesId("east"))
                .AddHeatmapRow("Return service", new double?[] { null, 2, 3, 4 }, id: new ChartSeriesId("return"));
            foreach (var row in chart.Series) row.ObservationIds(row.Points.Select(point => new ChartObservationId("column-" + point.X.ToString("0", CultureInfo.InvariantCulture))));
            chart.Heatmap(h => h.ColorScale(ChartColorScale.Sequential(colors.Surface, colors.Accent).WithValueRange(0, 24)));
        }
        chart.XLabels(new[] {
            new ChartAxisLabel(1, "A", "Morning", "Work"), new ChartAxisLabel(2, "B", "Morning", "Work"),
            new ChartAxisLabel(3, "A", "Evening", "Work"), new ChartAxisLabel(4, "B", "Evening", "Work")
        });
        // Y labels bind one-based SOURCE ROW SLOTS, not intensities or series IDs.
        // Reorder these mappings deliberately when changing row membership; mirroring axes leaves them intact.
        chart.YAxis(axis => axis.Labels.AddRange(new[] {
            new ChartAxisLabel(1, "West", "North", "Core"), new ChartAxisLabel(2, "Unknown", "North", "Core"),
            new ChartAxisLabel(3, "East", "South", "Core"), new ChartAxisLabel(4, "Return", "North", "Core")
        }));
        return chart;
    }

    private static ChartSeries States(string id, string name, string?[] values) => ChartSeries.CreateHeatmapCategoryRow(new ChartSeriesId(id), name,
        values.Select(value => value == null ? (ChartHeatmapCell?)null : new ChartHeatmapCell(value)))
        .ObservationIds(Enumerable.Range(1, values.Length).Select(column => new ChartObservationId("column-" + column.ToString(CultureInfo.InvariantCulture))));
}
```

<!-- PowerForge markdown artifact sha256:64E92EA30EE6EBCF2954C91FDF933DE6BC83C248882E722B5CF4AA2E60E1B01D -->
