← Gallery

Donut

Three independent service mixes

Explore the example, download the output or use the C# source in your own project.

Preview
Three independent service mixesChart with 3 data series: Period 1, Period 2, Period 3.
Three independent service mixes
Three independent service mixesChart with 3 data series: Period 1, Period 2, Period 3.
Three independent service mixes
390 × 600 pixels

Create this example

Create a console project, add the ChartForgeX package, and replace Program.cs with this source. Run it to write SVG, PNG and HTML with the default system fonts.

For the same typography as the gallery, download the gallery-font recipe and copy the gallery’s fonts directory beside your program.

#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 = AutomaticRadialBandsRecipe.Create("compact-automatic-rings", mode);
var context = new VisualRenderContext(
    new VisualLayoutOptions(new VisualSize(390, 600)), theme, mode,
    new VisualFrame("Three independent service mixes", "Source order allocates rings; every period keeps its own total", 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

/// <summary>Three independent service mixes use source-ordered automatic Donut bands.</summary>
public static class AutomaticRadialBandsRecipe {
    /// <summary>Creates immutable allocation without assigning or changing authored RadialBand values.</summary>
    public static Chart Create(string variant = "automatic-rings", ThemeMode mode = ThemeMode.Light) {
        if (variant is not ("automatic-rings" or "compact-automatic-rings")) throw new ArgumentOutOfRangeException(nameof(variant));
        var chart = Chart.Create().XLabels("Subscriptions", "Consulting", "Support")
            .RadialBandAllocation(new ChartRadialBandAllocation(.2, .04)).Pie(p => p.ShowCenterLabel(false).SliceLabelContent(ChartPieSliceLabelContent.LabelAndPercent));
        var values = new[] { new[] { 50d, 30, 20 }, new[] { 90d, 45, 15 }, new[] { 110d, 66, 44 } };
        for (var index = 0; index < values.Length; index++) {
            var series = new ChartSeries(new ChartSeriesId("period-" + index), "Period " + (index + 1), ChartSeriesKind.Donut,
                values[index].Select((value, category) => new ChartPoint(category + 1, value)))
                .ObservationIds(new[] { new ChartObservationId("subscriptions"), new ChartObservationId("consulting"), new ChartObservationId("support") })
                .ShowDataLabels(index == values.Length - 1);
            if (index == values.Length - 1) {
                series.DataLabelPlacement = ChartDataLabelPlacement.Outside;
                series.PointSliceRadiusRatio(0, .35).PointSliceRadiusRatio(1, .7).PointSliceRadiusRatio(2, 1);
            }
            chart.AddSeries(series);
        }
        return chart;
    }
}
#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.Dark;
var theme = VisualTheme.Graphite();
var model = AutomaticRadialBandsRecipe.Create("compact-automatic-rings", mode);
var context = new VisualRenderContext(
    new VisualLayoutOptions(new VisualSize(390, 600)), theme, mode,
    new VisualFrame("Three independent service mixes", "Source order allocates rings; every period keeps its own total", 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

/// <summary>Three independent service mixes use source-ordered automatic Donut bands.</summary>
public static class AutomaticRadialBandsRecipe {
    /// <summary>Creates immutable allocation without assigning or changing authored RadialBand values.</summary>
    public static Chart Create(string variant = "automatic-rings", ThemeMode mode = ThemeMode.Light) {
        if (variant is not ("automatic-rings" or "compact-automatic-rings")) throw new ArgumentOutOfRangeException(nameof(variant));
        var chart = Chart.Create().XLabels("Subscriptions", "Consulting", "Support")
            .RadialBandAllocation(new ChartRadialBandAllocation(.2, .04)).Pie(p => p.ShowCenterLabel(false).SliceLabelContent(ChartPieSliceLabelContent.LabelAndPercent));
        var values = new[] { new[] { 50d, 30, 20 }, new[] { 90d, 45, 15 }, new[] { 110d, 66, 44 } };
        for (var index = 0; index < values.Length; index++) {
            var series = new ChartSeries(new ChartSeriesId("period-" + index), "Period " + (index + 1), ChartSeriesKind.Donut,
                values[index].Select((value, category) => new ChartPoint(category + 1, value)))
                .ObservationIds(new[] { new ChartObservationId("subscriptions"), new ChartObservationId("consulting"), new ChartObservationId("support") })
                .ShowDataLabels(index == values.Length - 1);
            if (index == values.Length - 1) {
                series.DataLabelPlacement = ChartDataLabelPlacement.Outside;
                series.PointSliceRadiusRatio(0, .35).PointSliceRadiusRatio(1, .7).PointSliceRadiusRatio(2, 1);
            }
            chart.AddSeries(series);
        }
        return chart;
    }
}