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

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

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

    SunburstChart

    Step 1: Create a data source for Sunburst

    Add a new class to serve as the data source for Sunburst control.

    DataService
    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: Add a Sunburst control

    To add a Sunburst control to your layout, open the .axml file in your layout folder from the Solution Explorer and replace its code with the code below.

    XML
    Copy Code
    <?xml version="1.0" encoding="utf-8"?>
    <C1.Android.Chart.Sunburst xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/Sunburst"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"/>

    Alternatively, you can drag a Sunburst control from the Toolbox within the custom control tab onto your layout surface in designer mode. Then, inside your activity, add the following code to the OnCreate method to initialize your layout.

    XML
    Copy Code
    public class GettingStartedActivity : Activity
        {        
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                C1Sunburst sunburst = new C1Sunburst(this);
    
                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();
                LinearLayout layout = new LinearLayout(this);
                LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent);
                layout.AddView(sunburst, param);
                // Set our view from the "main" layout resource
                SetContentView(layout);
            }
        }

    Back to Top

    Step 3: Run the Project

    Press F5 to run the application.