Building a WinJS dashboard in few simple steps

Marina / Monday, October 07, 2013

Every developer wants to create an application that’s  innovative and in line with the latest technology trends, that is why in this blog we are going to see how to build a Windows 8 application using WinJS.  Relying on the Windows Library and Infragistics Windows UI Data Chart and Grid HTML controls we are going to create a dashboard. For the purpose of our sample we are going to use OData Northwind resources .

 You can download the HTML Sample Browser from Windows Store directly to your Window 8-powered device and thus you can look through the features and functionality of  Infragistics Windows UI controls.

Adding references

Generally, when we create a Blank App, Visual Studio creates for us default.html, default.js and default.css files. The WinJS references are already included in the default.html file, so the only thing we need to add are the Infragistics scripts after them just like that:

  2.  <script src="/Infragistics.UI.13.1/js/jquery-1.7.2.js">script>
  3.     <script src="/Infragistics.UI.13.1/js/jquery-ui.js">script>
  6.  <link href="///Infragistics.UI.13.1/css/structure/infragistics.css" rel="stylesheet" />
  7.  <link href="///Infragistics.UI.13.1/css/themes/metro/infragistics.theme.css" rel="stylesheet" />
  8.  <script src="///Infragistics.UI.13.1/js/infragistics.core.js">script>
  9.  <script src="///Infragistics.UI.13.1/js/infragistics.lob.js">script>
  10.  <script src="///Infragistics.UI.13.1/js/infragistics.dv.js">script>
  11.  <script src="///Infragistics.UI.13.1/js/infragistics.grid.winjs.js">script>
  12.  <script src="///Infragistics.UI.13.1/js/infragistics.datachart.winjs.js">script>

Don’t forget to add the Infragistics UI Library for JavaScript to the references. The reference will be visible in Visual Studio after you install the Infragistics Windows UI package.

Now we are ready to get started with our dashboard.

The Data Chart

To begin with, we must mention that both of the controls we are using are build over the Ignite UI controls and of course they have the same options. You can find more about those controls in the API documentation.

The Data Chart control for Windows UI allows you to display the data in an interactive and attractive way. The HTML Data Chart control offers a full set of dynamic business charts right out of the box. You can choose between different chart designs such as: Line, Spline, Area, Waterfall and other. To initialize the chart control you will need the following lines:

  1. <div id="chart" data-win-control="Infragistics.UI.DataChart"
  2.     data-win-options='
  3.     {
  4.         width: "50%",
  5.         height: "400px",                 
  6.         leftMargin: 5,
  7.         topMargin: 15,
  8.         axes: [{
  9.             name: "xAxis",
  10.             type: "categoryX",
  11.             label: "OrderID"
  12.         }, {
  13.             name: "yAxis",
  14.             type: "numericY"
  15.         }],
  16.         series: [{
  17.             name: "series1",
  18.             type: "spline",
  19.             xAxis: "xAxis",
  20.             yAxis: "yAxis",
  21.             valueMemberPath: "Quantity",
  22.             thickness: 5
  23.         }],
  24.         horizontalZoomable: true,
  25.         verticalZoomable: true,
  26.         windowResponse: "immediate"
  27.     }'>
  28. div>

As you can see from the code in the type option you can set the chosen control. The spline chart looks like this:

type: Line                                                                         

type: Waterfall

type: Area

You may have noticed that we are not setting the data source option, that is because we are doing it dynamically. Later in the blog you can see how.

Grid initialization

The HTML grid control with all of its options and events allows you to get more interactivity out of your static HTML tables and data. In our project we will use two tables. The first one will be the main table, which will visualize the products – their name, price and units on order and the second table will visualize the details of a particular product (selected by the user). Let’s start with the basic initialization of the grid and then we will see how to handle the selection event.

To get the information for our tables we will use OData Northwind resources. Use the following code to implement your Grid.

  1. <table id="Table2" data-win-control="Infragistics.UI.Grid"
  2.     data-win-options='{
  3.     dataSource: "$format=json",
  4.     responseDataKey:"value",
  5.     autoGenerateColumns: false,                    
  6.     columns: [{headerText:"ProductID", key:"ProductID"},{headerText:"ProductName",key:"ProductName"},
  7.               {headerText:"UnitPrice",key:"UnitPrice"} ,{headerText:"UnitsOnOrder",key:"UnitsOnOrder"}                 
  8.     ],
  9.     width: "49%",
  10.     height:"400px",
  11.     features: [
  12.     {
  13.             name: "Sorting",
  14.             enabled: true,
  15.             type: "local"  
  16.         },
  17.         {
  18.             name: "Paging",
  19.             type:"local",
  20.             enabled: true,
  21.             pageSize: 5,
  22.             defaultDropDownWidth: 90
  23.         }
  24.     ]
  25. }'>
  26. table>

This is how we are creating our main table.

When it comes to the second one the only difference is that like in the data chart we are not setting the dataSource option. The code for it  looks like this:

  1. <table id="Table1" data-win-control="Infragistics.UI.Grid"
  2.     data-win-options='{
  3.     autoGenerateColumns: false,                    
  4.     columns: [{headerText:"OrderID", key:"OrderID"},{headerText:"UnitPrice",key:"UnitPrice"},
  5.               {headerText:"Quantity",key:"Quantity"} ,{headerText:"Discount",key:"Discount"}                 
  6.     ],
  7.     width: "100%",
  8.     height:"350px",
  9.     features: [
  10.         {
  11.             name: "Sorting",
  12.             enabled: true,
  13.             type: "local"  
  14.         },
  15.         {
  16.             name: "Paging",
  17.             type:"local",
  18.             enabled: true,
  19.             pageSize: 5,
  20.             defaultDropDownWidth: 90
  21.         }
  22.     ]
  23. }'>
  24. table>



It wasn’t so hard, right? So now that we have all elements needed for our dashboard let’s make it work. We want the user to be able to select a particular product and in result of that action we want the detail table to display more information about that product and additionally we want the chart to display updated data. In our case the chart will display the quantity of the product for each order. The grid control  has a lot of events, but the one we need is rowSelectionChanged. As you can see from the documentation the initialization of that event take place in the feature option.

  1. features: [
  2. {
  3.         name: "Sorting",
  4.         enabled: true,
  5.         type: "local"  
  6.     },
  7.     {
  8.         name: "Paging",
  9.         type:"local",
  10.         enabled: true,
  11.         pageSize: 5,
  12.         defaultDropDownWidth: 90
  13.     },{
  14.         name: "Selection",
  15.         mode: "row",
  16.         rowSelectionChanged: IgUtility.rowClick
  17.     }
  18. ]

In the default.js file we are going to define a namespace called for example “IgUtility” in which we are going to initialize the data source and the event handler. This part is tricky because we want both of the controls to share the same data source, that is why when we create a standalone data source, we use data bind function and apply the received data to both controls. We also define a function called setData for changing the data source.

As you saw earlier in the rowSelectionChanged event we use a function called rowClick. This function is used for data-win-options so it must be marked manually by calling theWinJS.Utilities.markSupportedForProcessing()  which returns the given function marked as compatible with declarative processing.  In our handler we need the grid and the chart to load the information about the selected item that is why we call setData with the appropriate value as shown:

  1. WinJS.Namespace.define("IgUtility", {
  2.     newDataSource: {},
  3.     dataSource: new $.ig.DataSource({
  4.         dataSource: "$format=json",
  5.         responseDataKey: "value",
  6.         callback: function (e) {
  7.             $("#chart").igDataChart("option", "dataSource", IgUtility.dataSource.dataView());
  8.             $("#Table1").igGrid("option", "dataSource", IgUtility.dataSource.dataView());
  9.         }
  10.     }).dataBind(),
  12.     setData: function (ProductID) {
  13.         if (!ProductID) ProductID = 1;
  14.         IgUtility.dataSource.dataSource("" + ProductID + ")/Order_Details?$format=json");
  15.         IgUtility.dataSource.dataBind();
  16.     },
  17.     rowClick: WinJS.Utilities.markSupportedForProcessing(function (evt, ui) {
  18.         var items = ui.row.element;
  19.         var selected = $(items).children().first().text();
  20.         IgUtility.setData(selected);
  21.     })
  22. });

If you follow the preceding steps you will be able to create a simple basic WinJS dashboard. You can see more about the options and features of data chart control and grid control in the API documentation

Sample for the WinJS dashboard.


You can follow us on Twitter @Infragistics and stay in touch on Facebook, Google+ and LinkedIn!