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 = TileMapPartialColorRangeRecipe.Create("partial-color-range", mode);
var context = new VisualRenderContext(
new VisualLayoutOptions(new VisualSize(800, 440)), theme, mode,
new VisualFrame("Requests by state", "Zero stays fixed; the color peak follows reported counts", 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());
/// Creates a tile map with a fixed zero and an observed upper color bound.
public static class TileMapPartialColorRangeRecipe {
/// Creates the source model for a gallery variant without choosing an export backend.
public static Chart Create(string variant = "partial-color-range", VisualThemeMode mode = VisualThemeMode.Light) {
if (variant is not ("partial-color-range" or "compact-partial-color-range"))
throw new ArgumentOutOfRangeException(nameof(variant));
var colors = VisualTheme.Graphite().Resolve(mode);
var codes = new[] { "CA", "TX", "NY", "FL", "WA", "IL" };
var counts = new[] { 0d, 18, 42, 76, 29, 51 };
return Chart.Create().ConfigureLabels(labels => labels.NoData = "Not reported")
.AddTileMap("Requests", ChartTileMapCatalog.Get("us-states"),
codes.Select((code, index) => new ChartRegionMapItem(code, counts[index])), id: new ChartSeriesId("state-requests"))
.WithMapLabels()
.WithMapColorScale(ChartColorScale.Sequential(colors.SequentialRamp)
.WithValueRange(0, null).WithLabels("None", null, "Observed peak").WithNoDataColor(colors.Border))
.ConfigureMapColorDimension(options => {
options.Name = "Requests";
options.ValueFormat = ChartValueFormat.Number("0", System.Globalization.CultureInfo.InvariantCulture);
options.LegendStyle = ChartColorLegendStyle.Gradient;
})
.WithValueFormat(ChartValueFormat.Number("0.##", System.Globalization.CultureInfo.InvariantCulture));
}
}