#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; // Native PNG and standalone SVG use bundled Archivo. // Add ChartFontAssets.StyleSheet once for other text on a host page. var mode = ThemeMode.Light; var theme = VisualTheme.Graphite(); var model = HeatmapHierarchicalAxesRecipe.Create("wide", mode); var context = new VisualRenderContext( new VisualLayoutOptions(new VisualSize(1000, 560)), theme, mode, new VisualFrame("Service measurements by region and work window", "Columns bind to raw coordinates; row groups bind to one-based source slots", showLegend: null, showSurface: false), FontSpec.FromFamily(ChartFontStacks.ProductSans)); 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 /// Groups rectangular matrix columns and source row slots through the ordinary axis-label owner. public static class HeatmapHierarchicalAxesRecipe { /// Creates numeric or categorical rows with complete paths, missing cells and independent source IDs. /// Use wide, compact, states-wide or states-compact. Numeric and categorical cells remain separate models. /// The paired theme mode. /// A complete native chart ready for SVG, PNG or the optional HTML adapter. 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)))); }