#region Setup
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.Dark;
var theme = VisualTheme.Graphite();
var model = TrendLineRecipe.Create("compact", mode);
var context = new VisualRenderContext(
new VisualLayoutOptions(new VisualSize(360, 360)), theme, mode,
new VisualFrame("Request volume and its trend", "Weekly service desk requests, August to October 2026", 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());
#endregion
/// Builds a least-squares trend through weekly service desk requests while retaining the observations used to fit it.
public static class TrendLineRecipe {
/// Creates the selected trend example, including the small-drift precision case.
public static Chart Create(string variant = "wide", VisualThemeMode mode = VisualThemeMode.Light) {
if (variant is not ("wide" or "compact" or "sparse" or "options" or "compact-options" or "precision" or "compact-precision"))
throw new ArgumentOutOfRangeException(nameof(variant));
if (variant is "precision" or "compact-precision") {
var drift = ChartSeries.CreateTrendLine(new ChartSeriesId("least-squares-drift"), "Least-squares drift",
Enumerable.Range(0, 4096).Select(index => new ChartPoint(index % 10 + .5, index % 7 - 3)));
return Chart.Create().WithXAxis("Measurement").WithYAxis("Fitted drift").AddSeries(drift);
}
if (variant == "sparse") {
var sparse = new[] { 1, 3, 6 }.Select(index => new ChartPoint(index, 20 + index * 7 % 29, index == 6)).ToArray();
return Chart.Create().WithXLabels("Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
.AddSeries(ChartSeries.CreateScatter(new ChartSeriesId("observations"), "Observations", sparse))
.AddSeries(ChartSeries.CreateTrendLine(new ChartSeriesId("least-squares-trend"), "Least-squares trend", sparse));
}
// Service desk requests per week, August to October 2026.
var weeks = new[] { "Aug 3", "Aug 10", "Aug 17", "Aug 24", "Aug 31", "Sep 7", "Sep 14", "Sep 21", "Sep 28", "Oct 5", "Oct 12", "Oct 19", "Oct 26" };
var requests = new[] { 1180d, 1242, 1214, 1302, 1287, 1356, 1341, 1408, 1397, 1463, 1506, 1488, 1541 };
var points = requests.Select((value, index) => new ChartPoint(index + 1, value)).ToArray();
var observations = ChartSeries.CreateScatter(new ChartSeriesId("observations"), "Weekly requests", points);
var trend = ChartSeries.CreateTrendLine(new ChartSeriesId("least-squares-trend"), "Least-squares trend", points);
return Chart.Create().WithXLabels(weeks).WithYAxis("Requests").AddSeries(observations).AddSeries(trend);
}
}