#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 = BoxPlotRecipe.Create("horizontal-statistic-labels", mode); var context = new VisualRenderContext( new VisualLayoutOptions(new VisualSize(800, 440)), 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", VisualThemeMode mode = VisualThemeMode.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) }).WithPointFillPattern(2, ChartFillPattern.DiagonalForward); chart.WithXLabels("Standard", "Express", "Premium", "Bulk").AddSeries(expected).AddSeries(observed) .ConfigureBoxPlot(options => options.Orientation = horizontal ? ChartOrientation.Horizontal : ChartOrientation.Vertical) .WithDataLabels(); if (horizontal) chart.WithXAxis("Delivery time (minutes)").WithYAxis("Service"); else chart.WithYAxis("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.WithXLabels("Standard", "Express", "Premium", "Bulk", "Return").AddSeries(delivery); } return chart; } private static Chart StatisticLabels(string variant, VisualThemeMode mode) { var horizontal = variant.Contains("horizontal"); var chart = Chart.Create().WithTitle("Five-number delivery summaries").WithXLabels("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) }).WithPointLabel(0, "Standard delivery summary")) .ConfigureBoxPlot(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.WithFontSize(10); } options.MinimumLabel.Style.WithColor(mode == VisualThemeMode.Dark ? "#82b8ff" : "#235b9f"); options.MaximumLabel.Style.WithColor(mode == VisualThemeMode.Dark ? "#ffbe79" : "#975000"); options.MedianLabel.Style.WithFontSize(12).WithWeight("700"); }).WithDataLabels(); (horizontal ? chart.Options.XAxis : chart.Options.YAxis).WithBounds(0, 60); return horizontal ? chart.WithXAxis("Delivery time (minutes)").WithYAxis("Service") : chart.WithYAxis("Delivery time (minutes)"); } }