Xamarin.iOS Documentation | ComponentOne
Controls / Sunburst Chart / QuickStart
In This Topic
    QuickStart
    In This Topic

    This section describes how to add a Sunburst control to your iOS app and add data to it. This topic consists of three steps:

    The following image shows how the Sunburst appears, after completing the steps above:

    Sunburst

    Step 1: Create a data source for Sunburst

    Add a new class (For example: DataService.cs) to serve as the data source for Sunburst.

    C#
    Copy Code
        public class DataService
        {
            Random rnd = new Random();
            static DataService _default;
    
            public static DataService Instance
            {
                get
                {
                    if (_default == null)
                    {
                        _default = new DataService();
                    }
    
                    return _default;
                }
            }
    
            public static List<SunburstDataItem> CreateHierarchicalData()
            {
                Random rnd = Instance.rnd;
    
                List<string> years = new List<string>();
                List<List<string>> times = new List<List<string>>()
                {
                    new List<string>() { "Jan", "Feb", "Mar"},
                    new List<string>() { "Apr", "May", "June"},
                    new List<string>() { "Jul", "Aug", "Sep"},
                    new List<string>() { "Oct", "Nov", "Dec" }
                };
    
                List<SunburstDataItem> items = new List<SunburstDataItem>();
                var yearLen = Math.Max((int)Math.Round(Math.Abs(5 - Instance.rnd.NextDouble() * 10)), 3);
                int currentYear = DateTime.Now.Year;
                for (int i = yearLen; i > 0; i--)
                {
                    years.Add((currentYear - i).ToString());
                }
                var quarterAdded = false;
    
                foreach (string y in years)
                {
                    var i = years.IndexOf(y);
                    var addQuarter = Instance.rnd.NextDouble() > 0.5;
                    if (!quarterAdded && i == years.Count - 1)
                    {
                        addQuarter = true;
                    }
                    var year = new SunburstDataItem() { Year = y };
                    if (addQuarter)
                    {
                        quarterAdded = true;
    
                        foreach (List<string> q in times)
                        {
                            var addMonth = Instance.rnd.NextDouble() > 0.5;
                            int idx = times.IndexOf(q);
                            var quar = "Q" + (idx + 1);
                            var quarters = new SunburstDataItem() { Year = y, Quarter = quar };
                            if (addMonth)
                            {
                                foreach (string m in q)
                                {
                                    quarters.Items.Add(new SunburstDataItem()
                                    {
                                        Year = y,
                                        Quarter = quar,
                                        Month = m,
                                        Value = rnd.Next(20, 30)
                                    });
                                };
                            }
                            else
                            {
                                quarters.Value = rnd.Next(80, 100);
                            }
                            year.Items.Add(quarters);
                        };
                    }
                    else
                    {
                        year.Value = rnd.Next(80, 100);
                    }
                    items.Add(year);
                };
    
                return items;
            }
    
            public static List<FlatDataItem> CreateFlatData()
            {
                Random rnd = Instance.rnd;
                List<string> years = new List<string>();
                List<List<string>> times = new List<List<string>>()
                {
                    new List<string>() { "Jan", "Feb", "Mar"},
                    new List<string>() { "Apr", "May", "June"},
                    new List<string>() { "Jul", "Aug", "Sep"},
                    new List<string>() { "Oct", "Nov", "Dec" }
                };
    
                List<FlatDataItem> items = new List<FlatDataItem>();
                var yearLen = Math.Max((int)Math.Round(Math.Abs(5 - rnd.NextDouble() * 10)), 3);
                int currentYear = DateTime.Now.Year;
                for (int i = yearLen; i > 0; i--)
                {
                    years.Add((currentYear - i).ToString());
                }
                var quarterAdded = false;
                foreach (string y in years)
                {
    
                    var i = years.IndexOf(y);
                    var addQuarter = rnd.NextDouble() > 0.5;
                    if (!quarterAdded && i == years.Count - 1)
                    {
                        addQuarter = true;
                    }
                    if (addQuarter)
                    {
                        quarterAdded = true;
                        foreach (List<string> q in times)
                        {
                            var addMonth = rnd.NextDouble() > 0.5;
                            int idx = times.IndexOf(q);
                            var quar = "Q" + (idx + 1);
                            if (addMonth)
                            {
                                foreach (string m in q)
                                {
                                    items.Add(new FlatDataItem()
                                    {
                                        Year = y,
                                        Quarter = quar,
                                        Month = m,
                                        Value = rnd.Next(30, 40)
                                    });
                                };
                            }
                            else
                            {
                                items.Add(new FlatDataItem()
                                {
                                    Year = y,
                                    Quarter = quar,
                                    Value = rnd.Next(80, 100)
                                });
                            }
                        };
                    }
                    else
                    {
                        items.Add(new FlatDataItem()
                        {
                            Year = y.ToString(),
                            Value = rnd.Next(80, 100)
                        });
                    }
                };
    
                return items;
            }
    
        public class FlatDataItem
        {
            public string Year { get; set; }
            public string Quarter { get; set; }
            public string Month { get; set; }
            public double Value { get; set; }
        }
        public class SunburstDataItem
        {
            List<SunburstDataItem> _items;
    
            public string Year { get; set; }
            public string Quarter { get; set; }
            public string Month { get; set; }
            public double Value { get; set; }
            public List<SunburstDataItem> Items
            {
                get
                {
                    if (_items == null)
                    {
                        _items = new List<SunburstDataItem>();
                    }
    
                    return _items;
                }
            }
        }
        public class Item
        {
            public int Year { get; set; }
            public string Quarter { get; set; }
            public string MonthName { get; set; }
            public int MonthValue { get; set; }
            public double Value { get; set; }
        }
    

    Back to Top

    Step 2: Initialize Sunburst control in Code

    To initialize the Sunburst control, open the ViewController file from the Solution Explorer and replace its content with the code below. This overrides the ViewDidLoad method of the View controller in order to initialize Sunburst.

    C#
    Copy Code
    public override void ViewDidLoad()
            {
                base.ViewDidLoad();
               // Perform any additional setup after loading the view, typically from a nib.
               C1Sunburst sunburst = new C1Sunburst();
               sunburst.Binding = "Value";
               sunburst.BindingName = "Year,Quarter,Month";
               sunburst.ToolTipContent = "{}{name}\n{y}";
               sunburst.DataLabel.Position = PieLabelPosition.Center;
               sunburst.DataLabel.Content = "{}{name}";
               sunburst.ItemsSource = DataService.CreateFlatData();
               this.Add(sunburst);
            }
            public override void ViewDidLayoutSubviews()
            {
                base.ViewDidLayoutSubviews();
                CGRect rect = new CGRect(this.View.Frame.X, this.View.Frame.Y + 80,
                                         this.View.Frame.Width, this.View.Frame.Height - 80);
                sunburst.Frame = new CGRect(rect.X, rect.Y, rect.Width, rect.Height  - 10);
            }
    

    Back to Top

    Step 3: Run the Application

    Press F5 to run the application.

    Back to Top