Saturday 25 October 2014

Angular.js - Live grid updates using interval - Beginner



It has been a long time I looked at this space due to ‘no time’ [I believe this is a polite excuse for being lazy :-) ].
I heard a lot about Angluar.Js ,SignalR etc.. so thought of getting my hands on those frameworks.Being hard core windows developer/architect and spend most of the time compiling strongly typed languages,coding in Java script was little bit strange.After sometime I just let it go and believed in hidden 'dynamic' helping hand.     

Here is my first 15 min attempt to update a grid using '$interval'. (Disclaimer:- Data shown on grid is far from reality)

copy index.html,style.css,script.js  ; code is straightforward try it out.

I believe it's worth mentioning two code editors i) Bracket ii) Sublime text i found during my learning process.

index.html 

 <!doctype html>  
      <html ng-app="demoModule">  
           <head><title>This is a Angular.js Live update sample </title>  
           <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>  
           <link rel="stylesheet" type="text/css" href="http://angular-ui.github.com/ng-grid/css/ng-grid.css" />  
     <link rel="stylesheet" type="text/css" href="style.css" />  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>  
           <script type="text/javascript" src="http://angular-ui.github.com/ng-grid/lib/ng-grid.debug.js"></script>            
           <script src="script.js"></script>            
           </head>  
           <body ng-controller='SimpleController'>       
                <div>                                     
                     <input ng-click='Start()' type='button' value='Start!'/>  
                  <input ng-click='Stop()' type='button' value='Stop!'/>              
                 </div>   
                <div>                                     
                     <input type='text' ng-model='newStock.Id'/>  
                  <input ng-click='AddStockId()' type='button' value='Add'/>              
                 </div>  
                </br>  
                 <div class="gridStyle" ng-grid="gridOptions"/>       
                </br>  
           </body>  
      </html>  


script.js

 var module1 = angular.module('demoModule',['ngGrid']);  
 module1.controller('SimpleController',['$scope', '$interval',              
           function ($scope,$interval)  
           {  
                $scope.Stocks =  
                     [  
                      {name:'Google', high:25, low:30,close:28},  
                      {name:'MSFT', high:25, low:30,close:28},  
                      {name:'Apple', high:25, low:30,close:28},  
                      {name:'Infosys', high:25, low:30,close:28},  
                      {name:'Exxon', high:25, low:30,close:28},  
                      {name:'Tesco', high:25, low:30,close:28}            
                     ];                   
            $scope.colDefs=  
                 [  
                  {field:'name', displayName:'Name'},   
                  {field:'high', displayName:'High'},  
                  {field:'low', displayName:'Low'},  
                  {field:'close', displayName:'Close'}  
                 ];  
                $scope.gridOptions =  
                {   
                     data : 'Stocks',  
                     columnDefs:$scope.colDefs        
             };       
                $scope.AddStockId = function()  
                {  
                     var newStockDetails = { name:$scope.newStock.Id,high:0,low:0,close:0}  
                     $scope.Stocks.push(newStockDetails);  
                }            
                var promise;  
                $scope.Start = function()   
                     {  
                          if ( angular.isDefined(promise) ) return;  
                          promise =$interval(function()  
                           {  
                               for(var i=0;i<$scope.Stocks.length;i++)  
                               {                                     
                                    var curStock = $scope.Stocks[i];                                     
                                    curStock.low = curStock.low + Math.random();  
                                    curStock.high = curStock.high + Math.random();  
                                    curStock.close = (curStock.high + curStock.low)/2;  
                                    $scope.Stocks.splice(i,1,curStock);   
                               }  
                           }, 1000);                                
                     };  
                $scope.Stop = function()  
                     {  
                          if (angular.isDefined(promise))  
                          {  
                               $interval.cancel(promise);  
                               promise = undefined;  
                          }  
                     };  
                $scope.$on('$destroy',   
                     function()   
                          {  
                         // Make sure that the interval is destroyed too  
                                $scope.Stop();  
                           });  
      }]);  


style.css

 /*style.css*/  
 .gridStyle {  
   border: 1px solid rgb(212,212,212);  
   width: 400px;   
   height: 300px  
 }  

Saturday 16 June 2012

TechEd-2012 North America

I watched live streaming keynotes and other webcasts online presented in the recent TechEd 2012 held in orlando (TechEd-2012) and my main take away from this event is the way we should design UI going ahead and the way we should present the content to the end user.

Yes the main theme and highlight in TechEd was metro style applications.The metro style applications (which is rich in content and easy to interact with multiple input devices) showed in this event changed my view on the UI design.All the traditional GUIs I look at it now are rich in controls  ,some times GUI designers forget about the content what it displays.i.e I see those GUIs (including any application developed & running on windows platform.Note:- I'm a windows guy and not in position to comment about other OS platforms)  "very loud" in controls instead of being rich in content.Hmmm what did I do next day....

I installed windows 8 (download release preview from here here) in my laptop and started play with it to learn some design principles around metro style apps with an intension to apply some design principles in my new project.

I found below links useful,

http://msdn.microsoft.com/library/windows/apps/hh464920.aspx

http://msdn.microsoft.com/en-us/library/windows/apps/hh465424.aspx

http://channel9.msdn.com/Events/TechEd/NorthAmerica/2012/DEV354

 
Hopes you will find these useful too .....  

Thursday 2 February 2012

Deserializing XML to Dynamic Object in C#

Little bit background about this blog entry – most of the applications today are driven by (BIG) settings file (usually in XML format) , this is absolutely fine and will work as expected. Let’s look at a real world scenario of XML driven applications – suppose user wants an extra feature which is controlled based on an entry in the XML settings file that means we have to handle that extra settings parameter in our application.

Not a BIG deal; we could do it in following ways
#1) if you are using a custom XML Settings parser then change it (add XPATH/read node attribute etc.) - Old fashion approach :-(.
Or
#2) Create a new XML schema from XML and use XSD.exe to generate classes from the schema (if it simple change like adding attribute etc. then you can modify the auto generated class file directly ) ;And deserialize the XML to an object and use this object everywhere.
Or
#3) LinqToXML
etc...

Since I came from strong OOPS background I personally prefer to use everything as objects, so obviously my choice is second one (of course I won’t generate class file from schema for every change!!! )
These patterns are good still I was looking something DYNAMIC until DotNet 4.0 released with ‘dynamic’ type support.

Let’s look at an example with ‘Dynamic’ type(these are samples so keeping it simple as possible :-) to explain the idea)

In version 1.0 of settings (config) file [application will read this settings file and set the button text – simple yaaaa ],

<?xml version="1.0" encoding="utf-8" ?>
<UISettings >
<Button Text="Show Data" />
</UISettings>


//Code snippet
dynamic settingObject = SomeXmlToDynamicObjectParser.Parse(“Settings.xml”);
Then real beauty,
MyForm.btnData.Text = settingObject.Button.Text;

Now in version 2.0 of settings ,suppose we have added one more button setting (i.e. width)

<? version="1.0" encoding="utf-8" ?>
<?UISettings>
<Button Text="Show Data" Width="50" />
</UISettings>


//Code snippet
dynamic settingObject = SomeXmlToDynamicObjectParser.Parse(“Settings.xml”);
MyForm.btnData.Text = settingObject.Button.Text;
MyForm.btnData.Width = double.Parse(settingObject.Button.Width); (So simple yaaa!!!!)

I started to love this pattern and applied in many BIG xml driven production applications.(This is just my view if you have a different opinion or better approach please share in comment section).

You can find the implementation of ‘SomeXmlToDynamicObjectParserHere.

Happy Coding Days ….

Friday 8 July 2011

Abstract Vs Interface - Good read.

Everybody knows what are the points discussed here but still worth reading it as most of the time we forget basics since we are living in a abstract world of coding.

Saturday 18 June 2011

C# - Singleton Class using Lazy <T> in Dotnet .40

Code Snippet,

public sealed class Singleton
{
private static readonly Lazy lazy =
new Lazy(() => new Singleton());

public static Singleton Instance { get { return lazy.Value; } }

private Singleton()
{
}
}

Reference:-http://csharpindepth.com/Articles/General/Singleton.aspx

Tuesday 8 December 2009

Simple way to pass numeric array from c# to matlab.

This is a follow up screencast on Matlab Neural Network in C# Application (http://baijumax.blogspot.com/2009/05/matlab-neural-network-in-c-application.html),users often face issues with passing array from c# to matlab and based on their feedback I have decided to prepare this
screen cast in which I have shown simple solution to get around with syntactical issues of passing numeric array from c# to matlab by using simple string array.