#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 = WaterfallRecipe.Create("portable", mode);
var context = new VisualRenderContext(
new VisualLayoutOptions(new VisualSize(800, 440)), theme, mode,
new VisualFrame("Changes in available capacity", "", 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
/// Builds running-balance waterfalls with signed changes and explicit checkpoints.
public static class WaterfallRecipe {
/// Creates the selected waterfall example and its orientation.
public static Chart Create(string variant = "wide", ThemeMode mode = ThemeMode.Light) {
if (variant is not ("wide" or "compact" or "sparse" or "options" or "compact-options" or "horizontal" or "compact-horizontal" or "portable" or "compact-portable" or "portable-horizontal" or "compact-portable-horizontal"))
throw new ArgumentOutOfRangeException(nameof(variant));
if (variant is "portable" or "compact-portable" or "portable-horizontal" or "compact-portable-horizontal") {
var horizontalPortable = variant.EndsWith("horizontal", StringComparison.Ordinal);
var authored = Create(horizontalPortable ? "horizontal" : "options", mode)
.Title("Editable imported waterfall").Theme(VisualTheme.Graphite()).ThemeMode(mode);
var bytes = authored.ToVisualArtifact("portable-movement").ToInterchangeUtf8Json();
var imported = VisualArtifactInterchangeEnvelope.FromUtf8Json(bytes).ToWaterfallChart();
// An imported delta remains authored; totals and checkpoint contributors are recalculated by the native owner.
imported.Series[0].UpdateWaterfallItem(3, ChartWaterfallItem.Delta(4, -125));
return imported;
}
var chart = Chart.Create();
if (variant is "options" or "compact-options" or "horizontal" or "compact-horizontal") {
var horizontal = variant is "horizontal" or "compact-horizontal";
var movement = ChartSeries.CreateWaterfall(new ChartSeriesId("movement"), "Movement", new[] {
ChartWaterfallItem.OpeningBalance(1, 120), ChartWaterfallItem.Delta(2, -30), ChartWaterfallItem.Subtotal(3),
ChartWaterfallItem.Delta(4, -130), ChartWaterfallItem.Subtotal(5), ChartWaterfallItem.Delta(6, 70),
ChartWaterfallItem.Total(7)
}).ObservationId(3, new ChartObservationId("cost-change"))
.ObservationId(4, new ChartObservationId("cost-checkpoint"))
.PointColor(4, ChartColor.FromHex("#A5358A"));
chart.XLabels("Open", "Adjust", "Stage", "Costs", "Cost sum", "Recover", "Balance").AddSeries(movement)
.Waterfall(options => options.Orientation = horizontal ? ChartOrientation.Horizontal : ChartOrientation.Vertical);
if (horizontal) chart.XAxis(x => x.Title("Running balance")).YAxis(y => y.Title("Stage")).Bars(b => b.Style(ChartBarStyle.SegmentedCapsule));
else chart.YAxis(y => y.Title("Running balance"));
chart.Options.XAxis.Reversed(!horizontal || variant == "compact-horizontal");
chart.Options.YAxis.Reversed();
chart.DataLabels();
}
else {
var changes = new ChartSeries(new ChartSeriesId("changes"), "Changes", ChartSeriesKind.Waterfall, new[] {
new ChartPoint(1, 60), new ChartPoint(2, -15), new ChartPoint(3, 25), new ChartPoint(4, -10), new ChartPoint(5, 12)
});
chart.XLabels("North", "South", "East", "West", "Central", "Total").AddSeries(changes);
}
return chart;
}
}