#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;
var mode = ThemeMode.Light;
var theme = VisualTheme.Graphite();
var model = BoxPlotRecipe.Create("compact-statistic-labels", mode);
var context = new VisualRenderContext(
new VisualLayoutOptions(new VisualSize(360, 360)), theme, mode,
new VisualFrame("Delivery time by service level", "", 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 delivery-time distributions from typed five-number summaries.
public static class BoxPlotRecipe {
/// Creates the selected box-plot 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 "statistic-labels" or "compact-statistic-labels" or "horizontal-statistic-labels" or "compact-horizontal-statistic-labels"))
throw new ArgumentOutOfRangeException(nameof(variant));
var chart = Chart.Create();
if (variant.Contains("statistic-labels")) chart = StatisticLabels(variant, mode);
else if (variant is "options" or "compact-options" or "horizontal" or "compact-horizontal") {
var horizontal = variant is "horizontal" or "compact-horizontal";
var expected = ChartSeries.CreateBoxPlot(new ChartSeriesId("expected"), "Expected", new[] {
new ChartBoxPlot(1, 10, 20, 25, 30, 40), new ChartBoxPlot(2, 5, 10, 15, 25, 35),
new ChartBoxPlot(3, 12, 20, 28, 35, 45), new ChartBoxPlot(4, 20, 30, 35, 45, 55)
});
var observed = ChartSeries.CreateBoxPlot(new ChartSeriesId("observed"), "Observed", new[] {
new ChartBoxPlot(1, 12, 22, 30, 38, 48), new ChartBoxPlot(2, 8, 12, 22, 30, 42),
new ChartBoxPlot(3, 15, 24, 32, 40, 50), new ChartBoxPlot(4, 22, 35, 40, 48, 58)
}).PointFillPattern(2, ChartFillPattern.DiagonalForward);
chart.XLabels("Standard", "Express", "Premium", "Bulk").AddSeries(expected).AddSeries(observed)
.BoxPlot(options => options.Orientation = horizontal ? ChartOrientation.Horizontal : ChartOrientation.Vertical)
.DataLabels();
if (horizontal) chart.XAxis(x => x.Title("Delivery time (minutes)")).YAxis(y => y.Title("Service"));
else chart.YAxis(y => y.Title("Delivery time (minutes)"));
}
else {
var count = variant == "sparse" ? 2 : 5;
var distributions = Enumerable.Range(1, count)
.Select(index => new ChartBoxPlot(index, 10 + index, 20 + index, 30 + index, 38 + index, 50 + index));
var delivery = ChartSeries.CreateBoxPlot(new ChartSeriesId("delivery-time"), "Delivery time", distributions);
chart.XLabels("Standard", "Express", "Premium", "Bulk", "Return").AddSeries(delivery);
}
return chart;
}
private static Chart StatisticLabels(string variant, ThemeMode mode) {
var horizontal = variant.Contains("horizontal");
var chart = Chart.Create().Title("Five-number delivery summaries").XLabels("Standard", "Express")
.AddSeries(ChartSeries.CreateBoxPlot(new ChartSeriesId("delivery"), "Delivery time", new[] {
new ChartBoxPlot(1, 10, 20, 30, 40, 50), new ChartBoxPlot(2, 5, 15, 25, 35, 45)
}).PointLabel(0, "Standard delivery summary"))
.BoxPlot(options => {
options.Orientation = horizontal ? ChartOrientation.Horizontal : ChartOrientation.Vertical;
options.LabelContent = ChartBoxPlotLabelContent.Statistics;
foreach (var label in new[] { options.MinimumLabel, options.Q1Label, options.MedianLabel, options.Q3Label, options.MaximumLabel }) {
label.Placement = horizontal ? ChartDataLabelPlacement.Above : ChartDataLabelPlacement.Right;
label.Style.FontSize(10);
}
options.MinimumLabel.Style.Color(ChartColor.FromHex(mode == ThemeMode.Dark ? "#82b8ff" : "#235b9f"));
options.MaximumLabel.Style.Color(ChartColor.FromHex(mode == ThemeMode.Dark ? "#ffbe79" : "#975000"));
options.MedianLabel.Style.FontSize(12).FontWeight(700);
}).DataLabels();
(horizontal ? chart.Options.XAxis : chart.Options.YAxis).Bounds(0, 60);
return horizontal ? chart.XAxis(x => x.Title("Delivery time (minutes)")).YAxis(y => y.Title("Service")) : chart.YAxis(y => y.Title("Delivery time (minutes)"));
}
}