using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
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 = VisualThemeMode.Light;
var theme = VisualTheme.Graphite();
var model = HexbinHeatmapColorScaleRecipe.Create("compact-color-scale", mode);
var context = new VisualRenderContext(
new VisualLayoutOptions(new VisualSize(360, 360)), theme, mode,
new VisualFrame("Service coverage in hexagonal cells", "Named numeric intervals keep their exact boundaries", 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());
/// Groups numeric hexbin measurements into named color bands.
public static class HexbinHeatmapColorScaleRecipe {
/// Creates low, medium and high measurement bands.
public static Chart Create(string variant = "color-scale", VisualThemeMode mode = VisualThemeMode.Light) {
if (variant is not ("color-scale" or "compact-color-scale")) throw new ArgumentOutOfRangeException(nameof(variant));
var colors = VisualTheme.Graphite().Resolve(mode);
return Chart.Create().WithXLabels("Mon", "Tue", "Wed", "Thu", "Fri", "Sat").WithDataLabels()
.AddHexbinHeatmapRow("Primary", new double?[] { 0, 30, 40, null, 80, 100 }, id: new ChartSeriesId("primary-measurements"))
.AddHexbinHeatmapRow("Secondary", new double?[] { 15, 40, 60, 80, null, 120 }, id: new ChartSeriesId("secondary-measurements"))
.WithHeatmapColorScale(ChartColorScale.Discrete(new[] {
new ChartColorBand(40, colors.SequentialRamp[0], "Low"),
new ChartColorBand(80, colors.SequentialRamp[colors.SequentialRamp.Count / 2], "Medium"),
new ChartColorBand(null, colors.SequentialRamp[colors.SequentialRamp.Count - 1], "High")
})).WithValueFormat(ChartValueFormat.Number("0.##", CultureInfo.InvariantCulture));
}
}