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;
// Optional exact gallery typography; copy the gallery's fonts directory beside this program.
FontRegistry.Register("CFX Proof Carlito", "fonts/Carlito-Regular.ttf", 400);
FontRegistry.Register("CFX Proof Carlito", "fonts/Carlito-Bold.ttf", 700);
var mode = VisualThemeMode.Light;
var theme = VisualTheme.Graphite();
var model = TileMapColorDimensionRecipe.Create("compact-swatch-color-legend", mode);
var context = new VisualRenderContext(
new VisualLayoutOptions(new VisualSize(360, 360)), theme, mode,
new VisualFrame("Temperature by state", "A sampled key keeps the continuous measurement and its color domain", showLegend: null, showSurface: false), FontSpec.FromFamily("CFX Proof Carlito"));
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 independently formatted temperature values and color legends.
public static class TileMapColorDimensionRecipe {
/// Creates the source model for a gallery variant without choosing an export backend.
public static Chart Create(string variant = "color-dimension", VisualThemeMode mode = VisualThemeMode.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 codes = new[] { "CA", "TX", "NY", "FL", "WA", "IL", "CO" };
var values = new[] { 0d, 2.5, 5, 10, 22.5, 40, 55 };
var scale = 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").WithNoDataColor(colors.Border);
return Chart.Create().ConfigureLabels(labels => labels.NoData = "Not sampled")
.AddTileMap("Daily mean", ChartTileMapCatalog.Get("us-states"),
codes.Select((code, index) => new ChartRegionMapItem(code, values[index])), id: new ChartSeriesId("state-temperatures"))
.WithMapLabels().WithMapColorScale(scale)
.ConfigureMapColorDimension(options => {
options.Name = "Temperature";
options.ValueFormat = ChartValueFormat.Number("0.0 '°C'", System.Globalization.CultureInfo.GetCultureInfo("pl-PL"));
options.LegendStyle = variant is "gradient-color-legend" or "compact-gradient-color-legend" ? ChartColorLegendStyle.Gradient
: variant is "swatch-color-legend" or "compact-swatch-color-legend" ? ChartColorLegendStyle.Swatches : ChartColorLegendStyle.Auto;
});
}
}