The Ignite UI Grid control is a jQuery-based grid that presents data in tabular form. I have already written about this control and it’s column fixing feature, but now we are going to consider it in a different light. The coolest thing about the grid is that it visualize the information you are setting in a structured way and gives the end-user the ability to play with it and manipulate it. Imagine combining that data with a heat map - the users will be able to trace it even faster. Heat maps represent data as colors and those colors change respectively to the intensity of the data. In the current blog we will see how to build such grid by using jsRender templates.
Setting the basis
To build the grid you will need the required Ignite UI JavaScript and CSS files. As I said we are going to use jsRender template to define a structure and reuse it to generate HTML dynamically, that is why you will need a jsRender library referenced as well. We are going to use a table tag to host our grid.
- <tableid="grid"></table>
By default, the igGrid control uses the Infragistics template engine, so in order to use jsRender you need to configure it using the templatingEngine option and setting it to a ”jsRender” value.
I’ve mentioned few times already jsRender so lets take a step back and see what exactly is this. JsRender is a JavaScript library, which brings a new templating library to HTML5 development that has a codeless tag syntax and high performance, has no dependency on jQuery nor on the Document Object Model (DOM), supports creating custom functions and uses pure string-based rendering. You can read more about jsRender in MSDN 'Client Insight' articles on JsRender part one and part two or check out the documentation.
Back to our implementation of the grid we should create a custom row template if we want to use jsRender. To do that we need to follow few steps. First we have to configure the igGrid to use the jsRender templating engine, as we explained earlier, then we will have to define a helper function specific to the template.
- $.views.helpers(
- {
- //define function here
- });
The third step is to create the jsRender template and finally we should define the rowTemplate option to use this template.
- <scriptid="template"type="text/x-jsrender">
- <tr>@* You can use <td> tags and place your data here. *@
- <td>{{>ID}}</td>
- </tr>
- </script>
- $("#grid").igGrid({
- width: "600px",
- height: "600px",
- dataSource: busy,
- rowTemplate: $("#template").html(),
- columns: [
- { headerText: "Id", key: "ID" },
- { headerText: "Name", key: "Name" },
- { headerText: "Mon", key: "Mon" },
- { headerText: "Tue", key: "Tue" },
- { headerText: "Wed", key: "Wed" },
- { headerText: "Thu", key: "Thu" },
- { headerText: "Fri", key: "Fri" }
- ],
- templatingEngine: "jsrender"
- });
Heatmap Grid
Creating a heatmap means we need to represent the values in the grid’s row not only by their numbers and labels but with colors as well. The data we are going to use represents how busy are the employees in one organization each day of the week and their business is represented by percentage – 0% means the employee has no work to do and 100% means he is occupied the whole day. Respectively to those percentage we want to paint the row cells in colors from green to red. We are going to make that in the jsRender helper function. What we need to do is define min and max values for the entire data. In a real case scenario you may have a remote data and you may not be able to calculate the minimum and maximum values on the client side, then you should take them as prepared values from the server side. For our sample as we already know the max and min values so we are going to use them as is to keep it simple. Then on every step we will call that helper giving him the current value of the cell and using that value we will generate an appropriate color for the cell.
- $.views.helpers({
- colorChange: function (val) {
- var perRed;
- var perGreen;
- var maxValue = 100;
- var minValue = 0;
- var ratio = 1 / (maxValue - minValue);
- var finalValue = (val - minValue) * ratio;
- if (finalValue == 0.5) {
- perRed = 1;
- perGreen = 1;
- }
- elseif (finalValue > 0.5) {
- perRed = 1;
- perGreen =( 1 - finalValue)*2;
- }
- else {
- perGreen = 1;
- perRed = finalValue*2;
- }
- var red = Math.round(255 * perRed);
- var green = Math.round(255 * perGreen);
- var gString = green.toString(16);
- var rString = red.toString(16);
- if (gString.length == 1) {
- gString = '0' + gString;
- }
- if (rString.length == 1) {
- rString = rString + '0';
- }
- var color = '#' + rString + gString + '00';
- return color;
- }
- });
How do we call the function and when? Well this is a jsRender helper which means that we need it in our template. In the template we create <td> tags for all of the columns in our grid and set a background style to those tags, by calling the function.
- <scriptid="template"type="text/x-jsrender">
- <tr>
- <td>{{>ID}}</td>
- <td>{{>Name}}</td>
- <tdstyle='background: {{:~colorChange(Mon)}};'>
- <b>{{>Mon}}%</b>
- </td>
- <tdstyle='background: {{:~colorChange(Tue)}};'>
- <b>{{>Tue}}%</b>
- </td>
- <tdstyle='background: {{:~colorChange(Wed)}};'>
- <b>{{>Wed}}%</b>
- </td>
- <tdstyle='background: {{:~colorChange(Thu)}};'>
- <b>{{>Thu}}%</b>
- </td>
- <tdstyle='background: {{:~colorChange(Fri)}};'>
- <b>{{>Fri}}%</b>
- </td>
- </tr>
- </script>
The example image demonstrates the final view of the grid.
You can see the jsRender Integration sample for more information about how to use it in the grid’s row template.
Conclusion
Basically a heatmap visualize a table of numbers with corresponding colors. This is a useful way for finding highs and lows or sometimes patterns. Although usually the numbers are substituted by colors, using the igGrid you can have them both for even better user experience. JsRender templates are a powerful way of creating and loading HTML tags dynamically and thanks to it’s helper functions we can manipulate those templates as we wish.
You can see a live demo on jsFiddle or download the An ASP.Net MVC Heatmap with Grid sample.
You can follow us on Twitter @Infragistics and stay in touch on Facebook, Google+ and LinkedIn!