BIG-IP CPU Visualization with the Google Visualization API

In previous articles, we have explored using the Google Chart APIs to integrate static charts and graphs with iControl and iRules to build monitoring applications.

In this article, I’m going to move beyond the static charting APIs and take a look at the more complex Google Visualization API.  It is a JavaScript library that allows you to embed interactive charts, graphs, or other graphics onto a webpage.

For this application, I will take a look at the CPU metrics on a BIG-IP and build a dynamic gauge based dashboard.

The Application

image_282698.jpg

There are several approaches to building a dashboard and for this example, I’ve build a Windows Form application in C#.  The core of the app is the WebBrowser control which essentially wraps the Internet Explorer COM control up in a nice little package.  I then use the iControl Assembly to get the metrics from the BIG-IP, and then dynamically execute JavaScript functions from the C# code to manipulate the gauges.

The main components to the application are described in the following sections.

Connecting to the BIG-IP

image_282698.jpg
I use a iControl.Dialogs.ConnectionDialog, available from the iControl Assembly, to connect to the BIG-IP.  If you use the iRule Editor, this may look familiar…  The DoConnect() method will attempt to connect to the BIG-IP with the ConnectionDialog and, if it succeeds, then build the initial browser content for the WebBrowser control.  It then updates the charts with the latest data and then triggers the timer to start a update cycle.  Each time the timer triggers, another data collection is made and the charts are updated with the updateCharts() method.

<span id="lnum1" style="color: rgb(96, 96, 96);">   1:</span> <span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 255);">void</span> DoConnect()
<span id="lnum2" style="color: rgb(96, 96, 96);">   2:</span> {
<span id="lnum3" style="color: rgb(96, 96, 96);">   3:</span>     <span style="color: rgb(0, 0, 255);">if</span> (timer1.Enabled)
<span id="lnum4" style="color: rgb(96, 96, 96);">   4:</span>     {
<span id="lnum5" style="color: rgb(96, 96, 96);">   5:</span>         timer1.Enabled = <span style="color: rgb(0, 0, 255);">false</span>;
<span id="lnum6" style="color: rgb(96, 96, 96);">   6:</span>     }
<span id="lnum7" style="color: rgb(96, 96, 96);">   7:</span>     <span style="color: rgb(0, 0, 255);">else</span>
<span id="lnum8" style="color: rgb(96, 96, 96);">   8:</span>     {
<span id="lnum9" style="color: rgb(96, 96, 96);">   9:</span>         DialogResult dr = _cd.ShowDialog();
<span id="lnum10" style="color: rgb(96, 96, 96);">  10:</span>         <span style="color: rgb(0, 0, 255);">if</span> (dr == DialogResult.OK)
<span id="lnum11" style="color: rgb(96, 96, 96);">  11:</span>         {
<span id="lnum12" style="color: rgb(96, 96, 96);">  12:</span>             loadBrowserContents(buildHTMLContent());
<span id="lnum13" style="color: rgb(96, 96, 96);">  13:</span>             updateCharts();
<span id="lnum14" style="color: rgb(96, 96, 96);">  14:</span>             timer1.Enabled = <span style="color: rgb(0, 0, 255);">true</span>;
<span id="lnum15" style="color: rgb(96, 96, 96);">  15:</span>         }
<span id="lnum16" style="color: rgb(96, 96, 96);">  16:</span>     }
<span id="lnum17" style="color: rgb(96, 96, 96);">  17:</span>     updateMenus();
<span id="lnum18" style="color: rgb(96, 96, 96);">  18:</span> }

Creating HTML Page

The first time a connection is made to the LTM, the buildHTMLContent() method is called which makes a call to the get_cpu_usage_extended_information() iControl method to extract the CPU metrics for the given BIG-IP.  In this example, I’m only using the first host.  With future versions of BIG-IP, you will be able to manage multiple “hosts” within a single BIG-IP system.

An HTML page is created with DIV elements created and named for each CPU.  A row in the output table is created for each CPU with 3 gauges for Usage, Speed, and Temperature.

<span id="lnum1" style="color: rgb(96, 96, 96);">   1:</span> <span style="color: rgb(0, 0, 255);">private</span> String buildHTMLContent()
<span id="lnum2" style="color: rgb(96, 96, 96);">   2:</span> {
<span id="lnum3" style="color: rgb(96, 96, 96);">   3:</span>     String page_data = <span style="color: rgb(0, 96, 128);">""</span>;
<span id="lnum4" style="color: rgb(96, 96, 96);">   4:</span> 
<span id="lnum5" style="color: rgb(96, 96, 96);">   5:</span>     <span style="color: rgb(0, 0, 255);">if</span> (_cd.m_interfaces.initialized)
<span id="lnum6" style="color: rgb(96, 96, 96);">   6:</span>     {
<span id="lnum7" style="color: rgb(96, 96, 96);">   7:</span>         iControl.SystemCPUUsageExtendedInformation cpu_info =
<span id="lnum8" style="color: rgb(96, 96, 96);">   8:</span>             _cd.m_interfaces.SystemSystemInfo.get_cpu_usage_extended_information(<span style="color: rgb(0, 0, 255);">new</span> String[] { <span style="color: rgb(0, 96, 128);">"0"</span> });
<span id="lnum9" style="color: rgb(96, 96, 96);">   9:</span> 
<span id="lnum10" style="color: rgb(96, 96, 96);">  10:</span>         iControl.SystemCPUUsageExtended host_info = cpu_info.hosts[0];
<span id="lnum11" style="color: rgb(96, 96, 96);">  11:</span>         String host_id = host_info.host_id;
<span id="lnum12" style="color: rgb(96, 96, 96);">  12:</span>         iControl.CommonStatistic[][] statisticAofA = host_info.statistics;
<span id="lnum13" style="color: rgb(96, 96, 96);">  13:</span> 
<span id="lnum14" style="color: rgb(96, 96, 96);">  14:</span>         String table_rows = <span style="color: rgb(0, 96, 128);">""</span>;
<span id="lnum15" style="color: rgb(96, 96, 96);">  15:</span> 
<span id="lnum16" style="color: rgb(96, 96, 96);">  16:</span>         <span style="color: rgb(0, 128, 0);">// Loop over CPUs</span>
<span id="lnum17" style="color: rgb(96, 96, 96);">  17:</span>         <span style="color: rgb(0, 0, 255);">for</span> (<span style="color: rgb(0, 0, 255);">int</span> i = 0; i < statisticAofA.Length; i++)
<span id="lnum18" style="color: rgb(96, 96, 96);">  18:</span>         {
<span id="lnum19" style="color: rgb(96, 96, 96);">  19:</span>             <span style="color: rgb(0, 0, 255);">long</span> cpu_id = extractStatistic(statisticAofA[i], iControl.CommonStatisticType.STATISTIC_CPU_INFO_CPU_ID);
<span id="lnum20" style="color: rgb(96, 96, 96);">  20:</span> 
<span id="lnum21" style="color: rgb(96, 96, 96);">  21:</span>             table_rows += <span style="color: rgb(0, 96, 128);">"<tr><td>C<br/>P<br/>U<br/>"</span> + cpu_id + <span style="color: rgb(0, 96, 128);">"</td>"</span>;
<span id="lnum22" style="color: rgb(96, 96, 96);">  22:</span>             table_rows += <span style="color: rgb(0, 96, 128);">"<td class='gauge'><div id='gauge_"</span> + cpu_id + <span style="color: rgb(0, 96, 128);">"_0'></div></td>"</span>;
<span id="lnum23" style="color: rgb(96, 96, 96);">  23:</span>             table_rows += <span style="color: rgb(0, 96, 128);">"<td class='gauge'><div id='gauge_"</span> + cpu_id + <span style="color: rgb(0, 96, 128);">"_1'></div></td>"</span>;
<span id="lnum24" style="color: rgb(96, 96, 96);">  24:</span>             table_rows += <span style="color: rgb(0, 96, 128);">"<td class='gauge'><div id='gauge_"</span> + cpu_id + <span style="color: rgb(0, 96, 128);">"_2'></div></td></tr>"</span>;
<span id="lnum25" style="color: rgb(96, 96, 96);">  25:</span> 
<span id="lnum26" style="color: rgb(96, 96, 96);">  26:</span>         }
<span id="lnum27" style="color: rgb(96, 96, 96);">  27:</span>         updateTimestamp(<span style="color: rgb(0, 0, 255);">true</span>);
<span id="lnum28" style="color: rgb(96, 96, 96);">  28:</span>         page_data = <span style="color: rgb(0, 96, 128);">@"<html><head><title>iControl CPU Gauges</title></span>
<span id="lnum29" style="color: rgb(96, 96, 96);">  29:</span>             <style type='text/css'>
<span id="lnum30" style="color: rgb(96, 96, 96);">  30:</span>             body,td,th { font-family: Tahoma; font-size: 10pt; }
<span id="lnum31" style="color: rgb(96, 96, 96);">  31:</span>             .gaugex { width:200px;height:200px; }
<span id="lnum32" style="color: rgb(96, 96, 96);">  32:</span>             </style>
<span id="lnum33" style="color: rgb(96, 96, 96);">  33:</span>             <script type='text/javascript' src='https://www.google.com/jsapi'></script>
<span id="lnum34" style="color: rgb(96, 96, 96);">  34:</span>             <script type='text/javascript'>
<span id="lnum35" style="color: rgb(96, 96, 96);">  35:</span>             google.load('visualization', '1', {packages:['gauge']});
<span id="lnum36" style="color: rgb(96, 96, 96);">  36:</span>             </script>
<span id="lnum37" style="color: rgb(96, 96, 96);">  37:</span>             </head>";
<span id="lnum38" style="color: rgb(96, 96, 96);">  38:</span> 
<span id="lnum39" style="color: rgb(96, 96, 96);">  39:</span>         page_data += <span style="color: rgb(0, 96, 128);">"<body><center><table border='0'>"</span>;
<span id="lnum40" style="color: rgb(96, 96, 96);">  40:</span>         page_data += table_rows;
<span id="lnum41" style="color: rgb(96, 96, 96);">  41:</span>         page_data += <span style="color: rgb(0, 96, 128);">"</table></center></body>"</span>;
<span id="lnum42" style="color: rgb(96, 96, 96);">  42:</span>         page_data += <span style="color: rgb(0, 96, 128);">@"<script type='text/javascript'></span>
<span id="lnum43" style="color: rgb(96, 96, 96);">  43:</span>             var chartWidth = 200;
<span id="lnum44" style="color: rgb(96, 96, 96);">  44:</span>             var chartHeight = 200;
<span id="lnum45" style="color: rgb(96, 96, 96);">  45:</span> 
<span id="lnum46" style="color: rgb(96, 96, 96);">  46:</span>             function setChartSize(w, h) {
<span id="lnum47" style="color: rgb(96, 96, 96);">  47:</span>                 chartWidth=w;
<span id="lnum48" style="color: rgb(96, 96, 96);">  48:</span>                 chartHeight=h;
<span id="lnum49" style="color: rgb(96, 96, 96);">  49:</span>             }
<span id="lnum50" style="color: rgb(96, 96, 96);">  50:</span>             function drawChart(id, chart_num, label, value) {
<span id="lnum51" style="color: rgb(96, 96, 96);">  51:</span>                 var data = new google.visualization.DataTable();
<span id="lnum52" style="color: rgb(96, 96, 96);">  52:</span>                 data.addColumn('string', 'Label');
<span id="lnum53" style="color: rgb(96, 96, 96);">  53:</span>                 data.addColumn('number', 'Value');
<span id="lnum54" style="color: rgb(96, 96, 96);">  54:</span>                 data.addRows(1);
<span id="lnum55" style="color: rgb(96, 96, 96);">  55:</span>                 data.setValue(0, 0, label);
<span id="lnum56" style="color: rgb(96, 96, 96);">  56:</span>                 data.setValue(0, 1, parseInt(value));
<span id="lnum57" style="color: rgb(96, 96, 96);">  57:</span> 
<span id="lnum58" style="color: rgb(96, 96, 96);">  58:</span>                 var elem = document.getElementById(id);
<span id="lnum59" style="color: rgb(96, 96, 96);">  59:</span>                 var chart = new google.visualization.Gauge(elem);
<span id="lnum60" style="color: rgb(96, 96, 96);">  60:</span> 
<span id="lnum61" style="color: rgb(96, 96, 96);">  61:</span>                 var usage_options = {
<span id="lnum62" style="color: rgb(96, 96, 96);">  62:</span>                     width:chartWidth,height:chartHeight,
<span id="lnum63" style="color: rgb(96, 96, 96);">  63:</span>                     min:0,max:100,greenFrom:70,greenTo:75,yellowFrom:75,yellowTo:90,redFrom:90,redTo:100,
<span id="lnum64" style="color: rgb(96, 96, 96);">  64:</span>                     minorTicks:5,
<span id="lnum65" style="color: rgb(96, 96, 96);">  65:</span>                     majorTicks:['0','10','20','30','40','50','60','70','80','90','100']
<span id="lnum66" style="color: rgb(96, 96, 96);">  66:</span>                     };
<span id="lnum67" style="color: rgb(96, 96, 96);">  67:</span>                 var fanspeed_options = {
<span id="lnum68" style="color: rgb(96, 96, 96);">  68:</span>                     width:chartWidth,height:chartHeight,
<span id="lnum69" style="color: rgb(96, 96, 96);">  69:</span>                     min:0,max:6000,
<span id="lnum70" style="color: rgb(96, 96, 96);">  70:</span>                     greenFrom:4000,greenTo:4500,yellowFrom:4500,yellowTo:5500,redFrom:5500,redTo:6000,
<span id="lnum71" style="color: rgb(96, 96, 96);">  71:</span>                     minorTicks:5,
<span id="lnum72" style="color: rgb(96, 96, 96);">  72:</span>                     majorTicks:['0','1000','2000','3000','4000','5000','6000']
<span id="lnum73" style="color: rgb(96, 96, 96);">  73:</span>                    };
<span id="lnum74" style="color: rgb(96, 96, 96);">  74:</span>                 var temp_options = {
<span id="lnum75" style="color: rgb(96, 96, 96);">  75:</span>                     width:chartWidth,height:chartHeight,
<span id="lnum76" style="color: rgb(96, 96, 96);">  76:</span>                     min:0,max:100,greenFrom:70,greenTo:75,yellowFrom:75,yellowTo:90,redFrom:90,redTo:100,
<span id="lnum77" style="color: rgb(96, 96, 96);">  77:</span>                     minorTicks:5,
<span id="lnum78" style="color: rgb(96, 96, 96);">  78:</span>                     majorTicks:['0','10','20','30','40','50','60','70','80','90','100']
<span id="lnum79" style="color: rgb(96, 96, 96);">  79:</span>                     };
<span id="lnum80" style="color: rgb(96, 96, 96);">  80:</span>                 var options = '';
<span id="lnum81" style="color: rgb(96, 96, 96);">  81:</span>                 if ( chart_num == 0 ) { options = usage_options; }
<span id="lnum82" style="color: rgb(96, 96, 96);">  82:</span>                 else if ( chart_num == 1 ) { options = fanspeed_options; }
<span id="lnum83" style="color: rgb(96, 96, 96);">  83:</span>                 else if ( chart_num == 2 ) { options = temp_options; }
<span id="lnum84" style="color: rgb(96, 96, 96);">  84:</span> 
<span id="lnum85" style="color: rgb(96, 96, 96);">  85:</span>                 chart.draw(data, options);
<span id="lnum86" style="color: rgb(96, 96, 96);">  86:</span>             }
<span id="lnum87" style="color: rgb(96, 96, 96);">  87:</span>             </script></html>";
<span id="lnum88" style="color: rgb(96, 96, 96);">  88:</span>     }
<span id="lnum89" style="color: rgb(96, 96, 96);">  89:</span> 
<span id="lnum90" style="color: rgb(96, 96, 96);">  90:</span>     <span style="color: rgb(0, 0, 255);">return</span> page_data;
<span id="lnum91" style="color: rgb(96, 96, 96);">  91:</span> }

Inserting HTML Page in Browser Control

Injecting the HTML contents into the WebBrowser control is as simple as assigning it’s DocumentText property with the HTML contents.

<span id="lnum1" style="color: rgb(96, 96, 96);">   1:</span> <span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 255);">void</span> loadBrowserContents(String s)
<span id="lnum2" style="color: rgb(96, 96, 96);">   2:</span> {
<span id="lnum3" style="color: rgb(96, 96, 96);">   3:</span>     <span style="color: rgb(0, 0, 255);">if</span> ( <span style="color: rgb(0, 0, 255);">null</span> != s )
<span id="lnum4" style="color: rgb(96, 96, 96);">   4:</span>     {
<span id="lnum5" style="color: rgb(96, 96, 96);">   5:</span>         webBrowser1.DocumentText = s;
<span id="lnum6" style="color: rgb(96, 96, 96);">   6:</span>     }
<span id="lnum7" style="color: rgb(96, 96, 96);">   7:</span> }

Refreshing Charts

Once the WebBrowser control contains the contents of the page with the embedded functions to update the gauges, the updateCharts() method in this example will query the cpu metrics and make calls into the embedded JavaScript on the page with the WebBrowser’s Document.InvokeScript() method.

The updateChart() method takes as input the id, cpu id, title, and value for the chart.  These values are then bundled up into an Object array and passed to the drawChart() JavaScript function with the parameters.  The drawChart() JavaScript function then calls the Google Visualization chart object’s draw() method with the supplied options and the chart is then refreshed.

<span id="lnum1" style="color: rgb(96, 96, 96);">   1:</span> <span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 255);">void</span> updateCharts()
<span id="lnum2" style="color: rgb(96, 96, 96);">   2:</span> {
<span id="lnum3" style="color: rgb(96, 96, 96);">   3:</span>     <span style="color: rgb(0, 0, 255);">if</span> (_cd.m_interfaces.initialized)
<span id="lnum4" style="color: rgb(96, 96, 96);">   4:</span>     {
<span id="lnum5" style="color: rgb(96, 96, 96);">   5:</span>         DoResize();
<span id="lnum6" style="color: rgb(96, 96, 96);">   6:</span> 
<span id="lnum7" style="color: rgb(96, 96, 96);">   7:</span>         iControl.SystemCPUUsageExtendedInformation cpu_info =
<span id="lnum8" style="color: rgb(96, 96, 96);">   8:</span>             _cd.m_interfaces.SystemSystemInfo.get_cpu_usage_extended_information(<span style="color: rgb(0, 0, 255);">new</span> String[] { <span style="color: rgb(0, 96, 128);">"0"</span> });
<span id="lnum9" style="color: rgb(96, 96, 96);">   9:</span> 
<span id="lnum10" style="color: rgb(96, 96, 96);">  10:</span>         iControl.SystemCPUUsageExtended host_info = cpu_info.hosts[0];
<span id="lnum11" style="color: rgb(96, 96, 96);">  11:</span>         String host_id = host_info.host_id;
<span id="lnum12" style="color: rgb(96, 96, 96);">  12:</span>         iControl.CommonStatistic[][] statisticAofA = host_info.statistics;
<span id="lnum13" style="color: rgb(96, 96, 96);">  13:</span> 
<span id="lnum14" style="color: rgb(96, 96, 96);">  14:</span>         iControl.SystemPlatformCPUs PlatformCPUs =
<span id="lnum15" style="color: rgb(96, 96, 96);">  15:</span>             _cd.m_interfaces.SystemSystemInfo.get_cpu_metrics();
<span id="lnum16" style="color: rgb(96, 96, 96);">  16:</span> 
<span id="lnum17" style="color: rgb(96, 96, 96);">  17:</span>         <span style="color: rgb(0, 128, 0);">// Loop over CPUs</span>
<span id="lnum18" style="color: rgb(96, 96, 96);">  18:</span>         <span style="color: rgb(0, 0, 255);">for</span> (<span style="color: rgb(0, 0, 255);">int</span> i = 0; i < statisticAofA.Length; i++)
<span id="lnum19" style="color: rgb(96, 96, 96);">  19:</span>         {
<span id="lnum20" style="color: rgb(96, 96, 96);">  20:</span>             iControl.SystemCPUMetric[] CPUMetricA = PlatformCPUs.cpus[i];
<span id="lnum21" style="color: rgb(96, 96, 96);">  21:</span> 
<span id="lnum22" style="color: rgb(96, 96, 96);">  22:</span>             <span style="color: rgb(0, 0, 255);">long</span> cpu_index = extractMetric(CPUMetricA, iControl.SystemCPUMetricType.CPU_INDEX);
<span id="lnum23" style="color: rgb(96, 96, 96);">  23:</span>             <span style="color: rgb(0, 0, 255);">long</span> cpu_temp = extractMetric(CPUMetricA, iControl.SystemCPUMetricType.CPU_TEMPERATURE);
<span id="lnum24" style="color: rgb(96, 96, 96);">  24:</span>             <span style="color: rgb(0, 0, 255);">long</span> cpu_fan_speed = extractMetric(CPUMetricA, iControl.SystemCPUMetricType.CPU_FAN_SPEED);
<span id="lnum25" style="color: rgb(96, 96, 96);">  25:</span> 
<span id="lnum26" style="color: rgb(96, 96, 96);">  26:</span>             <span style="color: rgb(0, 0, 255);">long</span> cpu_id = extractStatistic(statisticAofA[i], iControl.CommonStatisticType.STATISTIC_CPU_INFO_CPU_ID);
<span id="lnum27" style="color: rgb(96, 96, 96);">  27:</span>             <span style="color: rgb(0, 0, 255);">long</span> cpu_usage_ratio = extractStatistic(statisticAofA[i], iControl.CommonStatisticType.STATISTIC_CPU_INFO_USAGE_RATIO);
<span id="lnum28" style="color: rgb(96, 96, 96);">  28:</span> 
<span id="lnum29" style="color: rgb(96, 96, 96);">  29:</span>             updateChart(<span style="color: rgb(0, 96, 128);">"gauge_"</span> + cpu_id + <span style="color: rgb(0, 96, 128);">"_0"</span>, 0, <span style="color: rgb(0, 96, 128);">"Usage"</span>, cpu_usage_ratio);
<span id="lnum30" style="color: rgb(96, 96, 96);">  30:</span>             updateChart(<span style="color: rgb(0, 96, 128);">"gauge_"</span> + cpu_id + <span style="color: rgb(0, 96, 128);">"_1"</span>, 1, <span style="color: rgb(0, 96, 128);">"Speed"</span>, cpu_fan_speed);
<span id="lnum31" style="color: rgb(96, 96, 96);">  31:</span>             updateChart(<span style="color: rgb(0, 96, 128);">"gauge_"</span> + cpu_id + <span style="color: rgb(0, 96, 128);">"_2"</span>, 2, <span style="color: rgb(0, 96, 128);">"Temp"</span>, cpu_temp);
<span id="lnum32" style="color: rgb(96, 96, 96);">  32:</span> 
<span id="lnum33" style="color: rgb(96, 96, 96);">  33:</span>         }
<span id="lnum34" style="color: rgb(96, 96, 96);">  34:</span>         updateTimestamp(<span style="color: rgb(0, 0, 255);">true</span>);
<span id="lnum35" style="color: rgb(96, 96, 96);">  35:</span>     }
<span id="lnum36" style="color: rgb(96, 96, 96);">  36:</span> }
<span id="lnum37" style="color: rgb(96, 96, 96);">  37:</span> 
<span id="lnum38" style="color: rgb(96, 96, 96);">  38:</span> <span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 255);">void</span> updateChart(String id, <span style="color: rgb(0, 0, 255);">long</span> cpuid, String title, <span style="color: rgb(0, 0, 255);">long</span> <span style="color: rgb(0, 0, 255);">value</span>)
<span id="lnum39" style="color: rgb(96, 96, 96);">  39:</span> {
<span id="lnum40" style="color: rgb(96, 96, 96);">  40:</span>     <span style="color: rgb(0, 0, 255);">if</span> (<span style="color: rgb(0, 0, 255);">null</span> != webBrowser1.Document)
<span id="lnum41" style="color: rgb(96, 96, 96);">  41:</span>     {
<span id="lnum42" style="color: rgb(96, 96, 96);">  42:</span>         String[] args = <span style="color: rgb(0, 0, 255);">new</span> String[] { id, cpuid.ToString(), title, <span style="color: rgb(0, 0, 255);">value</span>.ToString() };
<span id="lnum43" style="color: rgb(96, 96, 96);">  43:</span>         webBrowser1.Document.InvokeScript(<span style="color: rgb(0, 96, 128);">"drawChart"</span>, args);
<span id="lnum44" style="color: rgb(96, 96, 96);">  44:</span>     }
<span id="lnum45" style="color: rgb(96, 96, 96);">  45:</span> }

Resizing Charts

Resizing the charts in the page was rather trivial.  I created another embedded JavaScript function setChartSize() which takes as parameters the chart’s width and height.  The next time the drawChart() method is called, it uses these values for the creation of the charts.

<span id="lnum1" style="color: rgb(96, 96, 96);">   1:</span> <span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 255);">void</span> DoResize()
<span id="lnum2" style="color: rgb(96, 96, 96);">   2:</span> {
<span id="lnum3" style="color: rgb(96, 96, 96);">   3:</span>     <span style="color: rgb(0, 0, 255);">if</span> (timer1.Enabled)
<span id="lnum4" style="color: rgb(96, 96, 96);">   4:</span>     {
<span id="lnum5" style="color: rgb(96, 96, 96);">   5:</span>         <span style="color: rgb(0, 128, 0);">// Calculate the new chart sizes;</span>
<span id="lnum6" style="color: rgb(96, 96, 96);">   6:</span>         <span style="color: rgb(0, 0, 255);">long</span> width = webBrowser1.Width;
<span id="lnum7" style="color: rgb(96, 96, 96);">   7:</span>         <span style="color: rgb(0, 0, 255);">long</span> height = webBrowser1.Height;
<span id="lnum8" style="color: rgb(96, 96, 96);">   8:</span> 
<span id="lnum9" style="color: rgb(96, 96, 96);">   9:</span>         <span style="color: rgb(0, 0, 255);">long</span> newWidth = ((width) / 3) - 50;
<span id="lnum10" style="color: rgb(96, 96, 96);">  10:</span>         <span style="color: rgb(0, 0, 255);">long</span> newHeight = ((height) / 2) - 20;
<span id="lnum11" style="color: rgb(96, 96, 96);">  11:</span> 
<span id="lnum12" style="color: rgb(96, 96, 96);">  12:</span>         <span style="color: rgb(0, 0, 255);">if</span> (newHeight > newWidth) { newHeight = newWidth; }
<span id="lnum13" style="color: rgb(96, 96, 96);">  13:</span>         <span style="color: rgb(0, 0, 255);">if</span> (newWidth > newHeight) { newWidth = newHeight; }
<span id="lnum14" style="color: rgb(96, 96, 96);">  14:</span> 
<span id="lnum15" style="color: rgb(96, 96, 96);">  15:</span>         resizeCharts(newWidth, newHeight);
<span id="lnum16" style="color: rgb(96, 96, 96);">  16:</span>     }
<span id="lnum17" style="color: rgb(96, 96, 96);">  17:</span> }
<span id="lnum18" style="color: rgb(96, 96, 96);">  18:</span> <span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 255);">void</span> resizeCharts(<span style="color: rgb(0, 0, 255);">long</span> width, <span style="color: rgb(0, 0, 255);">long</span> height)
<span id="lnum19" style="color: rgb(96, 96, 96);">  19:</span> {
<span id="lnum20" style="color: rgb(96, 96, 96);">  20:</span>     <span style="color: rgb(0, 0, 255);">if</span> (<span style="color: rgb(0, 0, 255);">null</span> != webBrowser1.Document)
<span id="lnum21" style="color: rgb(96, 96, 96);">  21:</span>     {
<span id="lnum22" style="color: rgb(96, 96, 96);">  22:</span>         String [] args = <span style="color: rgb(0, 0, 255);">new</span> String[] { width.ToString(), height.ToString() };
<span id="lnum23" style="color: rgb(96, 96, 96);">  23:</span>         webBrowser1.Document.InvokeScript(<span style="color: rgb(0, 96, 128);">"setChartSize"</span>, args);
<span id="lnum24" style="color: rgb(96, 96, 96);">  24:</span>     }
<span id="lnum25" style="color: rgb(96, 96, 96);">  25:</span> }

Video Walkthrough

CPU Monitoring witht the Google Visualization APIs

Get The Source

The source code for this application can be downloaded from the following link: CPUVisualization.zip

Related Articles on DevCentral

Technorati Tags: iControl, Visualization, Reporting, CPU, C#, Visual Studio, Joe Pruitt