Universal Windows Platform – Donut Control

Donut Control demonstrates how to create a Control to display values in the form of a Donut Control in the style of a Pie Chart with a Hole in the middle, in this case displaying the first few values of the famous Fibonacci sequence.

Step 1

If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.


Step 2

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…


Step 3

From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in the Name as DonutControl and select a Location and then select Ok to create the Project

Step 4

Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10, version 1803 (10.0; Build 17134) which is the April 2018 Update and the Minimum Version to be the same.


The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates

Step 5

Once done select from the Menu, Project, then Add New Item…


Step 6

From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Donut.cs before selecting Add to add the file to the Project


Step 7

Once in the Code View for Donut.cs the following should be entered:

using System;
using System.Collections.Generic;
using System.Linq;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;

namespace DonutControl
    public class Donut : Grid
        private const double circle = 360;
        private List<double> _items = new List<double>();
        private double _angle = 0;

        private List<double> Percentages()
            List<double> results = new List<double>();
            double total = _items.Sum();
            foreach (double item in _items)
                results.Add((item / total) * 100);
            return results.OrderBy(o => o).ToList();

        public Point Compute(double angle, double hole)
            double radians = (Math.PI / 180) * (angle - 90);
            double x = hole * Math.Cos(radians);
            double y = hole * Math.Sin(radians);
            return new Point(x += Radius, y += Radius);

        private Path GetSector(Color fill, double sweep, double hole)
            bool isLargeArc = sweep > 180.0;
            Point start = new Point(Radius, Radius);
            Point innerArcStart = Compute(_angle, hole);
            Point innerArcEnd = Compute(_angle + sweep, hole);
            Point outerArcStart = Compute(_angle, Radius);
            Point outerArcEnd = Compute(_angle + sweep, Radius);
            Size outerArcSize = new Size(Radius, Radius);
            Size innerArcSize = new Size(hole, hole);

            LineSegment lineOne = new LineSegment()
                Point = outerArcStart
            ArcSegment arcOne = new ArcSegment()
                SweepDirection = SweepDirection.Clockwise,
                IsLargeArc = isLargeArc,
                Point = outerArcEnd,
                Size = outerArcSize
            LineSegment lineTwo = new LineSegment()
                Point = innerArcEnd
            ArcSegment arcTwo = new ArcSegment()
                SweepDirection = SweepDirection.Counterclockwise,
                IsLargeArc = isLargeArc,
                Point = innerArcStart,
                Size = innerArcSize
            PathFigure figure = new PathFigure()
                StartPoint = innerArcStart,
                IsClosed = true,
                IsFilled = true
            PathGeometry pathGeometry = new PathGeometry();
            Path path = new Path
                Fill = new SolidColorBrush(fill),
                Data = pathGeometry
            _angle += sweep;
            return path;

        private void Layout()
            _angle = 0;
            double total = 100;
            double sweep = 0;
            double value = (circle / total);
            List<double> percentages = Percentages();
            Canvas canvas = new Canvas()
                Width = Radius * 2,
                Height = Radius * 2
            for (int index = 0; index < percentages.Count(); index++)
                double percentage = percentages[index];
                Color colour = (index < Palette.Count()) ? Palette[index] : Colors.Black;
                sweep = value * percentage;
                Path sector = GetSector(colour, sweep, Hole);
            Viewbox viewbox = new Viewbox()
                Child = canvas

        public List<Color> Palette { get; set; } = new List<Color>();

        public static readonly DependencyProperty RadiusProperty =
        DependencyProperty.Register("Radius", typeof(int),
        typeof(Donut), new PropertyMetadata(100));

        public static readonly DependencyProperty HoleProperty =
        DependencyProperty.Register("Hole", typeof(UIElement),
        typeof(Donut), new PropertyMetadata(50.0));

        public int Radius
            get { return (int)GetValue(RadiusProperty); }
            set { SetValue(RadiusProperty, value); Layout(); }

        public double Hole
            get { return (double)GetValue(HoleProperty); }
            set { SetValue(HoleProperty, value); Layout(); }

        public List<double> Items
            get { return _items; }
            set { _items = value; Layout(); }

        public void Fibonacci(params Color[] colours)
            Palette = colours.ToList();
            int fibonacci(int value) => value > 1 ?
            fibonacci(value - 1) + fibonacci(value - 2) : value;
            Items = Enumerable.Range(0, Palette.Count())
            .Select(fibonacci).Select(s => (double)s).ToList();

The Donut Class itself which inherits from a Grid and there are using statements to include the necessary functionality. There is a const for total degrees in a circle and there are Members for a List of double and double.

Also in the Donut Class there is a Percentages Method which works out the percentage of each item that’s needed for the Chart based on its Value. The Compute Method works out the position of a Point with Cos and Sin and will return a Point including an Offset. The GetSector Method is used to return a Path which uses the Compute Method to work out the Point Values needed and will use LineSegment and ArcSegment to produce the Path with the given Color. THe Layout Method is used to create the look-and-feel of the Control itself and will produce the Path elements needed to make up the Chart.

The Donut Class also has Properties for Palette which is a List of Color and two DependencyProperty for Radius and Hole there is also a Property for List of double. There’s a Fibonacci Method which allows the Chart to be populated based on the values of the Fibonacci sequence.

Step 8

Once done select from the Menu, Build, then Build Solution


Step 9

In the Solution Explorer select MainPage.xaml


Step 10

From the Menu choose View and then Designer


Step 11

The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:

<local:Donut Margin="50" x:Name="Display"/>

The MainPage just has the Donut Control itself.

Step 12

From the Menu choose View and then Code


Step 13

Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:

protected override void OnNavigatedTo(NavigationEventArgs e)

Below the MainPage() Method there is the OnNavigatedTo Event Handler which triggers the Fibonacci Method of the Donut Control.

Step 14

That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application


Step 15

After the Application has started it should then appear displaying a Donut Chart displaying the values of the first few numbers of the Fibonacci sequence


Step 16

To Exit the Application select the Close button in the top right of the Application


The Donut Control is used to display some of the Fibonacci numbers as an example and has a basic colour palette set which the example used to set how many items to add to the Chart, it can be used to display similar data to a Pie Chart and you could also display something inside the Hole of the Donut.

Creative Commons License

One thought on “Universal Windows Platform – Donut Control

  1. Hello Respected
    Thanks for a good demo
    using this i want to implement a radial menu.
    can you please help me to draw each viewbox from specified point


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s