Stacked Control demonstrates how to create a Control to display values in the form of a horizontal stacked bar chart – 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 StackedControl 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 Stacked.cs before selecting Add to add the file to the Project
Step 7
Once in the Code View for Stacked.cs the following should be entered:
using System.Collections.Generic; using System.Linq; using Windows.UI; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Shapes; namespace StackedControl { public class Stacked : Grid { private List<double> _items = new List<double>(); 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(); } private Rectangle GetRectangle(Color colour, int column) { Rectangle rect = new Rectangle() { Fill = new SolidColorBrush(colour) }; rect.SetValue(Grid.ColumnProperty, column); return rect; } private void Layout() { List<double> percentages = Percentages(); this.ColumnDefinitions.Clear(); for (int index = 0; index < percentages.Count(); index++) { double percentage = percentages[index]; ColumnDefinition column = new ColumnDefinition() { Width = new GridLength(percentage, GridUnitType.Star) }; this.ColumnDefinitions.Add(column); Color colour = (index < Palette.Count()) ? Palette[index] : Colors.Black; this.Children.Add(GetRectangle(colour, index)); } } public List<Color> Palette { get; set; } = new List<Color>(); 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 Stacked Class itself which inherits from a Grid and has a List of double to represent the values of the Chart. Tne Percentages Method works out the percentage of each item that’s needed for the Chart based on it’s Value, GetRectangle is used to get a Rectangle to use as a Chart item, Layout is used to create the look-and-feel of the Chart, Pallette is the List of Color to be used in the Chart and a Property which encapsulates the Chart items. 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:Stacked Margin="50" x:Name="Display"/>
The MainPage just has the Stacked 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) { Display.Fibonacci( Windows.UI.Colors.Black, Windows.UI.Colors.Gray, Windows.UI.Colors.Red, Windows.UI.Colors.Orange, Windows.UI.Colors.Yellow, Windows.UI.Colors.Green, Windows.UI.Colors.Cyan, Windows.UI.Colors.Blue, Windows.UI.Colors.Magenta, Windows.UI.Colors.Purple); }
Below the MainPage() Method there is the OnNavigatedTo Event Handler which triggers the Fibonacci Method of the Stacked 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 Stacked Chart displaying 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
This Control is used to display some of the Fibonacci numbers as an example and has a basic colour palette set which the example uses to set how many item to add to the chart and it could be changed so you could also set an “Orientation” or style it in any way desired and used for things such as a space used indicator for example.