Monday, November 30, 2015

Showing Summary in a Bar Chart

This post is about how to show the details in a bar chart in WPF.

STEP 1


Install WPF Charting ToolKit

We have to install charting toolkit for our project
     Install WPF Toolkit from this site:
Add new WPF application in Visual Studio.
If you are not able to view chart controls in Toolbox, right click Toolbox and select Choose Items. Then click on WPF components and select chart controls (the ones mentioned in the title). This will add the controls to your toolbox and you should be able to drag and drop them on the XAML form.

STEP 2


 XAML Coding for Charting tool

  XAML (Extensible Application Markup Language) is a markup language for declarative application programming. If you are interested in knowing more about XAML, please refer to the MSDN documentation athttp://msdn.microsoft.com/en-us/library/ms747122.aspx.


<Grid Height="921">
<chartingToolkit:Chart Height="442" HorizontalAlignment="Left"
                 Margin="39,0,0,130" Name="columnChart" Title=""  
                 VerticalAlignment="Bottom" Width="620">
                <chartingToolkit:ColumnSeries DependentValuePath="Value"
                 IndependentValuePath="Key" ItemsSource="{Binding}" />             
            </chartingToolkit:Chart>

</ Grid>

(in here I am showing Bar Chart as this you can try any type of chart.)
I’m using three attributes. DependentValuePath and IndependentValuePath are related to the Axis of the Chart (i.e. X-axis, Y-axis). “Value” and “Key” as assigned to them respectively - this is because I’m using KeyValuePair<> data type in my data model (which has Key and Value)

STEP 3


Assign data to the model 
  
   in the MainWindow.xaml.cs file, it is pretty straightforward with the way we are assigning data model.

   /// <summary>
        /// Load data to the graph Sample Code
        /// </summary>
        public void Show_Inventory_Chart(string itemCategory)
        {
//Initializing the new key value pair

            List<KeyValuePair<string, double>> InventoryValue = new List<KeyValuePair<string, double>>();

            double itemQuantity = 0;
            try
            {
// In here I assign all values in db to a list (Code base model)
                var itemList = itemService.GetChartItemList(companyId, itemCategory).GroupBy(s => s.ItemId).Select(s =>s.First()).ToList();

                foreach (var item in itemList)
                {
                    itemQuantity = FillChartStockQuantity(item, itemCategory);

                    string itemName = "";
                    string itemId = "";

                    FormatChartItemNames(item, out itemName, out itemId);

// Inserting a value for chart

                    InventoryValue.Add(new KeyValuePair<string, double>((itemId + Environment.NewLine + itemName), itemQuantity));

                    itemQuantity = 0;

                }
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("No Item in Database, Please add item in Administration Section", "No Items", MessageBoxButton.OK, MessageBoxImage.Warning);
            }

            columnChart.Title = itemCategory + " Inventory Summary";
            columnChart.DataContext = InventoryValue;

        }

STEP 4 

Compile and Run

Here you can see the final out put 

Tuesday, November 3, 2015

Facade Design Pattern ( façade pattern)

A design pattern is a common way of solving a recurring problem. Classes in all design patterns are just normal classes. What is important is how they are structured and how they work together to solve a given problem in the best possible way.



  A Facade shields the user from the complex details of the system and provides them with a simplified view of it which is easy to use  It also decouples the code that uses the system from the details of the subsystems, making it easier to modify the system later.

  Facade design pattern is commonly used with Object Oriented Programming.  It Provide a unified interface to a set of interfaces in a sub system. Facade defines a higher-level interface that makes the sub system easier to use. 

Structure of Facade


Fig 1-1







Facade
The facade class abstracts Packages 1, 2, and 3 from the rest of the application.
Clients
The objects are using the Facade Pattern to access resources from the Packages.



References:
Fig 1-1 "Example of Facade design pattern in UML" by Fuhrmanator 
https://en.wikipedia.org/wiki/Facade_pattern :Facade Design Pattern From Wikipedia, the free encyclopedia