RealTime Pulse Monitor Using SignalR And Ignite UI igSparkline

Hey folks. Here I’m sharing a real time heart rate monitor app that I’ve created using SignalR and Ignite UI igSparkline chart. Ignite UI is so cool and comes in very handy when building adaptable UI for Line of Business applications.
Here’s an article, on Getting Started with Ignite UI igGrid from my recent posts:

Download the sample code for this demo from my GitHub Repo:

HeartRate Monitor Using SignalR & igSparkline.

Below is a snapshot of what I’ve built:

snapshot

Prerequisite: Install Infragistics Ignite UI, if you haven’t installed it yet. (Alternatively, you can use CDN versions of JS scripts and CSS files referred). Here, I assume that you have an idea on how SignalR works.

Create an empty ASP.NET project.

empty ASP.NET project
Add SignalR using Nuget.

Add SignalR using Nuget

Install jQuery and jQuery UI the same way. Please note that I’m using version 1.6.4 of jQuery due to compatibility reasons with SignalR. You can find all the libraries in my sample code.

Add Startup.cs file (used to plug-in SignalR middleware) and add the following code.code
Add a class HeartRateHub.cs and add the following code.
code
A SignalR hub inherits from Hub class. This way, it knows it is a hub and creates client proxies accordingly. The class contains two methods, PulseSimulator and GeneratePulse.

Since I do not have a device to measure heart rate, I’m trying to simulat it (yet it is not so accurate) using a timer in SimulatePulse method.

GeneratePulse method generated a random number as current pulse rate and broadcast to all connected clients.

Add an HTML page and add references to JS / CSS files
code
There’s a special reference “/signalr/hubs” which SignalR uses to render client proxies of the methods defined on server, so that client would know what functions are available on server.

Here is how you create an object of the proxy connection in JavaScript.
code
Add the method to receive data from server and update the chart.

hub.client is the container of all client side functions that a hub at server can ask for.
code
What it does is, whenever the data is received, it appends the new item at the end and removes an item from start.

Next is to start the connection and Invoke PulseSimulator method on server as soon as the connection to the hub is made successfully.
code
I have added some dummy data to initialize the igSparkline charts. The igSparkline charts accepts an Array of Objects having properties.
code
Add HTML containers for charts. I’m using three different display modes of igSparkline chart control - Line, Area and Column. It has one more “WinLoss” that I will skip for now.
code
Add scripts to initialize the igSparkline charts:
code
And that’s all it took from me to get it done.

After getting all the things, my Solution Explorer looked like this:

solution explorer

Here’s the complete code for each file separately.

Startup.cs

  1. using Owin;  
  2.   
  3. namespace HeartRateMonitor_Using_IgniteUI_Sparkline_Chart  
  4. {  
  5.     public class Startup  
  6.     {  
  7.         public void Configuration(IAppBuilder appBuilder)  
  8.         {  
  9.             appBuilder.MapSignalR();  
  10.         }  
  11.     }  
  12. }  
Index.html
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Pulse Monitor using SignalR and IgniteUI igSparkline</title>  
  5.     <meta charset="utf-8" />  
  6.   
  7.     <link href="css/structure/infragistics.css" rel="stylesheet" />  
  8.     <link href="css/themes/bootstrap4/infragistics.theme.css" rel="stylesheet" />  
  9.     <script src="Scripts/jquery-1.6.4.js"></script>  
  10.     <script src="Scripts/jquery-ui-1.11.4.js"></script>  
  11.     <script src="js/infragistics.core.js"></script>  
  12.     <script src="js/infragistics.dv.js"></script>  
  13.     <script src="Scripts/jquery.signalR-2.2.0.min.js"></script>  
  14.   
  15.     <!--Reference the autogenerated SignalR hub script. -->  
  16.     <script src="/signalr/hubs"></script>  
  17.   
  18.   
  19.     <script type="text/javascript">  
  20.   
  21.         var pulseData = [  
  22.             { "pulseRate": "98" },  
  23.             { "pulseRate": "50" },  
  24.             { "pulseRate": "40" },  
  25.             { "pulseRate": "70" },  
  26.             { "pulseRate": "90" },  
  27.             { "pulseRate": "30" },  
  28.             { "pulseRate": "70" },  
  29.             { "pulseRate": "20" },  
  30.             { "pulseRate": "30" },  
  31.             { "pulseRate": "57" },  
  32.             { "pulseRate": "80" },  
  33.             { "pulseRate": "70" },  
  34.             { "pulseRate": "90" },  
  35.             { "pulseRate": "30" },  
  36.             { "pulseRate": "70" },  
  37.             { "pulseRate": "75" },  
  38.             { "pulseRate": "63" },  
  39.             { "pulseRate": "70" },  
  40.             { "pulseRate": "65" }  
  41.         ];  
  42.   
  43.         $(function () {  
  44.   
  45.             // Declare a proxy to reference the signalr hub.  
  46.             var hub = $.connection.heartRateHub;  
  47.   
  48.             hub.client.updatePulse = function (pulseData) {  
  49.   
  50.                 $("#sparkline_LineChart,#sparkline_AreaChart,#sparkline_ColumnChart")  
  51.                     .igSparkline("insertItem", JSON.parse(pulseData))  
  52.                     .igSparkline("removeItem", 0);  
  53.   
  54.             }  
  55.   
  56.             // Start the connection.  
  57.             $.connection.hub.start()  
  58.                 .done(function () {  
  59.                     hub.invoke("PulseSimulator");  
  60.                 });  
  61.   
  62.   
  63.             // Initialize igSparkline charts  
  64.             $("#sparkline_LineChart")  
  65.                 .igSparkline({  
  66.                     dataSource: pulseData,  
  67.                     valueMemberPath: 'pulseRate'  
  68.                 });  
  69.   
  70.             $("#sparkline_AreaChart")  
  71.                 .igSparkline({  
  72.                     dataSource: pulseData,  
  73.                     valueMemberPath: 'pulseRate',  
  74.                     displayType: 'area'  
  75.                 });  
  76.   
  77.             $("#sparkline_ColumnChart")  
  78.                 .igSparkline({  
  79.                     dataSource: pulseData,  
  80.                     valueMemberPath: 'pulseRate',  
  81.                     displayType: 'column'  
  82.                 });  
  83.   
  84.         });  
  85.   
  86.   
  87.     </script>  
  88.   
  89.     <style>  
  90.         .chart {  
  91.             width: 40%;  
  92.             height: 200px;  
  93.             float: left;  
  94.             margin-left: 20px;  
  95.             padding-bottom: 60px;  
  96.             margin-bottom: 10px;  
  97.             border: 1px dashed blue;  
  98.         }  
  99.     </style>  
  100.   
  101. </head>  
  102.   
  103. <body>  
  104.   
  105.     <div id="container">  
  106.   
  107.         <div id="HeartRateMonitor">  
  108.   
  109.             <div id="sparkline_LineChart" class="chart">  
  110.                 <h4>Pulse Rate - Line Chart</h4>  
  111.             </div>  
  112.   
  113.             <div id="sparkline_AreaChart" class="chart">  
  114.                 <h4>Pulse Rate - Area Chart</h4>  
  115.             </div>  
  116.   
  117.             <div id="sparkline_ColumnChart" class="chart">  
  118.                 <h4>Pulse Rate - Column Chart</h4>  
  119.             </div>  
  120.   
  121.         </div>  
  122.     </div>  
  123.   
  124. </body>  
  125. </html>  
HeartRateHub.cs
  1. using System;  
  2. using System.Threading;  
  3. using Microsoft.AspNet.SignalR;  
  4. using Newtonsoft.Json;  
  5.   
  6. namespace HeartRateMonitor_Using_IgniteUI_Sparkline_Chart  
  7. {  
  8.     public class HeartRateHub : Hub  
  9.     {  
  10.         private static Timer _timer;  
  11.   
  12.         private readonly Random _rnd = new Random();  
  13.   
  14.   
  15.         public void PulseSimulator()  
  16.         {  
  17.             if (_timer == null)  
  18.             {  
  19.                 _timer = new Timer(GeneratePulse, null, 3000, 1000);  
  20.             }  
  21.         }  
  22.   
  23.   
  24.         public void GeneratePulse(object state)  
  25.         {  
  26.             var pulseRate = new {pulseRate = _rnd.Next(60, 100)};  
  27.   
  28.             Clients.All.updatePulse(JsonConvert.SerializeObject(pulseRate));  
  29.         }  
  30.     }  
  31. }  
The rest of everything comes with the infrastructure. And, that wraps up this post. I will be back with more on Ignite UI controls, soon. Launch the app to see the data coming from the server in real time, and the charts being updated with the same.

Explore more on igSparkline here.
Start with Ignite UI controls by downloading a free trial here.
Learn more about SignalR.

I hope you enjoyed reading. Please share your valuable feedback in the comments section.

 

Up Next
    Ebook Download
    View all
    Learn
    View all