Real-time Thermometer Gauge
The thermometer gauge is a real-time chart, which can continuously request new data from the server and display the same, without involving any page refreshes. The chart initializes itself, loads new data periodically, and automatically updates to reflect the current state of data. There are two different ways to update the thermometer gauge; one method directly polls data from the server while the other retrieves data using JavaScript API methods.
In this section, you will be shown how you can,
Configuring Real-time Updates Using JavaScript API
Real-time Data Format
The data provider page on the server’s end, when invoked by the thermometer gauge, will output the new data in the real-time data format.
The real-time data format for gauges depends on:
-
the value to be passed
-
whether you are using the message logger for the gauge
-
the use of commands for the gauge - like stop update etc.
In the simplest form, if you’re looking to update the thermometer gauge, you need to output the data in following format:
Here, the output is a single value, -5. So, when the gauge will read this value, it will update the chart by setting its value to -5 (if a data range is provided by the gauge, the value will first be checked to verify if it falls within the defined range).
A thermometer gauge configured for real-time updates using JavaScript API looks like this:
{
"chart": {
"caption": "Temperature Monitor",
"subcaption": " Central cold store",
"lowerLimit": "-10",
"upperLimit": "0",
"numberSuffix": "°C",
"decimals": "1",
"showhovereffect": "1",
"thmFillColor": "#008ee4",
"showGaugeBorder": "1",
"gaugeBorderColor": "#008ee4",
"gaugeBorderThickness": "2",
"gaugeBorderAlpha": "30",
"thmOriginX": "100",
"theme": "fint"
},
"value": "-6"
}
<html>
<head>
<title>My first chart using FusionCharts Suite XT</title>
<script type="text/javascript" src="http://static.fusioncharts.com/code/latest/fusioncharts.js"></script>
<script type="text/javascript" src="http://static.fusioncharts.com/code/latest/themes/fusioncharts.theme.fint.js?cacheBust=56"></script>
<script type="text/javascript">
FusionCharts.ready(function(){
var fusioncharts = new FusionCharts({
type: 'thermometer',
renderAt: 'chart-container',
id: 'myThm-1',
width: '240',
height: '300',
dataFormat: 'json',
dataSource: {
"chart": {
"caption": "Temperature Monitor",
"subcaption": " Central cold store",
"lowerLimit": "-10",
"upperLimit": "0",
"numberSuffix": "°C",
"decimals": "1",
"showhovereffect": "1",
"thmFillColor": "#008ee4",
"showGaugeBorder": "1",
"gaugeBorderColor": "#008ee4",
"gaugeBorderThickness": "2",
"gaugeBorderAlpha": "30",
"thmOriginX": "100",
"theme": "fint"
},
"value": "-6"
},
"events": {
"rendered": function(evt, arg) {
evt.sender.dataUpdate = setInterval(function() {
var value,
prevTemp = evt.sender.getData(),
mainTemp = (Math.random() * 10) * (-1),
diff = Math.abs(prevTemp - mainTemp);
diff = diff > 1 ? (Math.random() * 1) : diff;
if (mainTemp > prevTemp) {
value = prevTemp + diff;
} else {
value = prevTemp - diff;
}
evt.sender.feedData("&value=" + value);
}, 3000);
},
"disposed": function(evt, arg) {
clearInterval(evt.sender.dataUpdate);
}
}
}
);
fusioncharts.render();
});
</script>
</head>
<body>
<div id="chart-container">FusionCharts XT will load here!</div>
</body>
</html>
Given below is a brief description of the JavaScript API used to configure real-time updates:
Function Name | Parameter | Description |
---|---|---|
|
strData |
This method is used to feed real-time data to the gauge using JavaScript. The data has to be in the same format as provided by the real-time data provider page. |
|
None |
This method is used to return the data currently being shown by the gauge. |
|
value |
This method is used to set the data value for the gauge. The value should be within the limits of the gauge. |
Configuring Real-time Updates Using Server-side Script
A thermometer gauge configured for real-time updates using server-side script looks like this:
{
"chart": {
"caption": "Temperature Monitor",
"subcaption": " Central cold store",
"lowerLimit": "-10",
"upperLimit": "0",
"numberSuffix": "°C",
"decimals": "1",
"showhovereffect": "1",
"thmFillColor": "#008ee4",
"showGaugeBorder": "1",
"gaugeBorderColor": "#008ee4",
"gaugeBorderThickness": "2",
"gaugeBorderAlpha": "30",
"thmOriginX": "100",
"dataStreamURL": "../../../resources/php/gauge-and-widgets-guide-thermometer-gauge-real-time-gauges-php-1.php",
"refreshInterval": "5",
"theme": "fint"
},
"value": "-6"
}
<html>
<head>
<title>My first chart using FusionCharts Suite XT</title>
<script type="text/javascript" src="http://static.fusioncharts.com/code/latest/fusioncharts.js"></script>
<script type="text/javascript" src="http://static.fusioncharts.com/code/latest/themes/fusioncharts.theme.fint.js?cacheBust=56"></script>
<script type="text/javascript">
FusionCharts.ready(function(){
var fusioncharts = new FusionCharts({
type: 'thermometer',
renderAt: 'chart-container',
id: 'myThm-2',
width: '240',
height: '300',
dataFormat: 'json',
dataSource: {
"chart": {
"caption": "Temperature Monitor",
"subcaption": " Central cold store",
"lowerLimit": "-10",
"upperLimit": "0",
"numberSuffix": "°C",
"decimals": "1",
"showhovereffect": "1",
"thmFillColor": "#008ee4",
"showGaugeBorder": "1",
"gaugeBorderColor": "#008ee4",
"gaugeBorderThickness": "2",
"gaugeBorderAlpha": "30",
"thmOriginX": "100",
"dataStreamURL": "../../../resources/php/gauge-and-widgets-guide-thermometer-gauge-real-time-gauges-php-1.php",
"refreshInterval": "5",
"theme": "fint"
},
"value": "-6"
}
}
);
fusioncharts.render();
});
</script>
</head>
<body>
<div id="chart-container">FusionCharts XT will load here!</div>
</body>
</html>
Given below is a brief description of the attributes needed to configure real-time updates from the server:
Attribute Name | Description | ||
---|---|---|---|
|
This parameter sets the path of the page which is supposed to relay real-time data to the chart. If you have special characters as a part of the data stream URL, like ? or &, you will have to URL Encode the entire |
||
|
For this parameter, we can specify the number of seconds after which the chart will look for new data. This process will happen continuously - i.e., if we specify 5 seconds here, the chart will look for new data after every 5 seconds. |
||
|
|
Stopping/Restarting Updates
A thermometer gauge configured to stop and restart receiving real-time updates looks like this:
{
"chart": {
"caption": "Temperature Monitor",
"subcaption": " Central cold store",
"lowerLimit": "-10",
"upperLimit": "0",
"numberSuffix": "°C",
"decimals": "1",
"showhovereffect": "1",
"thmFillColor": "#008ee4",
"showGaugeBorder": "1",
"gaugeBorderColor": "#008ee4",
"gaugeBorderThickness": "2",
"gaugeBorderAlpha": "30",
"thmOriginX": "100",
"dataStreamURL": "../../../resources/php/gauge-and-widgets-guide-thermometer-gauge-real-time-gauges-php-2.php",
"refreshInterval": "5",
"theme": "fint"
},
"value": "-6"
}
<html>
<head>
<title>My first chart using FusionCharts Suite XT</title>
<script type="text/javascript" src="http://static.fusioncharts.com/code/latest/fusioncharts.js"></script>
<script type="text/javascript" src="http://static.fusioncharts.com/code/latest/themes/fusioncharts.theme.fint.js?cacheBust=56"></script>
<script type="text/javascript">
FusionCharts.ready(function(){
var fusioncharts = new FusionCharts({
type: 'thermometer',
renderAt: 'chart-container',
id: 'myThm-3',
width: '240',
height: '300',
dataFormat: 'json',
dataSource: {
"chart": {
"caption": "Temperature Monitor",
"subcaption": " Central cold store",
"lowerLimit": "-10",
"upperLimit": "0",
"numberSuffix": "°C",
"decimals": "1",
"showhovereffect": "1",
"thmFillColor": "#008ee4",
"showGaugeBorder": "1",
"gaugeBorderColor": "#008ee4",
"gaugeBorderThickness": "2",
"gaugeBorderAlpha": "30",
"thmOriginX": "100",
"dataStreamURL": "../../../resources/php/gauge-and-widgets-guide-thermometer-gauge-real-time-gauges-php-2.php",
"refreshInterval": "5",
"theme": "fint"
},
"value": "-6"
},
"events": {
'beforeRender': function(evt, args) {
// creating div for controllers
var controllers = document.createElement('div');
// Create button inside div
controllers.innerHTML = '<input id="btnSU" type="submit" name="Submit" value="Stop Update" >';
args.container.parentNode.insertBefore(controllers, args.container.nextSibling);
controllers.setAttribute('id', 'controllers');
},
"renderComplete": function(evtObj, argObj) {
var flag = 0,
btn = document.getElementById("btnSU"),
startStopUpdate = function() {
if (flag === 0) {
btn.value = "Restart Update";
evtObj.sender.stopUpdate();
flag = 1;
} else {
btn.value = "Stop Update";
evtObj.sender.restartUpdate();
flag = 0;
}
};
btn.addEventListener && btn.addEventListener("click", startStopUpdate);
}
}
}
);
fusioncharts.render();
});
</script>
</head>
<body>
<div id="chart-container">FusionCharts XT will load here!</div>
</body>
</html>
Given below is a brief description of the JavaScript API used to stop and restart receiving real-time updates:
Function Name | Parameter | Description |
---|---|---|
|
|
This method stops the chart from self-updating. |
|
|
If you’ve stopped the real-time update of the chart, you can resume the update using this method. |
Configuring Real-time Events
FusionCharts Suite XT introduces two events, realTimeUpdateComplete
and realTimeUpdateError
, to track real-time updates on gauges.
A real-time thermometer gauge configured to listen to the realTimeUpdateComplete
event looks like this:
{
"chart": {
"caption": "Temperature Monitor",
"subcaption": " Central cold store",
"lowerLimit": "-10",
"upperLimit": "0",
"decimals": "1",
"numberSuffix": "°C",
"showhovereffect": "1",
"thmFillColor": "#008ee4",
"showGaugeBorder": "1",
"gaugeBorderColor": "#008ee4",
"gaugeBorderThickness": "2",
"gaugeBorderAlpha": "30",
"thmOriginX": "100",
"chartBottomMargin": "20",
"valueFontColor": "#000000",
"theme": "fint"
},
"value": "-6",
"annotations": {
"showbelow": "0",
"groups": [
{
"id": "indicator",
"items": [
{
"id": "background",
"type": "rectangle",
"alpha": "50",
"fillColor": "#AABBCC",
"x": "$gaugeEndX-40",
"tox": "$gaugeEndX",
"y": "$gaugeEndY+54",
"toy": "$gaugeEndY+72"
}
]
}
]
}
}
<html>
<head>
<title>My first chart using FusionCharts Suite XT</title>
<script type="text/javascript" src="http://static.fusioncharts.com/code/latest/fusioncharts.js"></script>
<script type="text/javascript" src="http://static.fusioncharts.com/code/latest/themes/fusioncharts.theme.fint.js?cacheBust=56"></script>
<script type="text/javascript">
FusionCharts.ready(function(){
var fusioncharts = new FusionCharts({
type: 'thermometer',
renderAt: 'chart-container',
id: 'myThm-4',
width: '240',
height: '310',
dataFormat: 'json',
dataSource: {
"chart": {
"caption": "Temperature Monitor",
"subcaption": " Central cold store",
"lowerLimit": "-10",
"upperLimit": "0",
"decimals": "1",
"numberSuffix": "°C",
"showhovereffect": "1",
"thmFillColor": "#008ee4",
"showGaugeBorder": "1",
"gaugeBorderColor": "#008ee4",
"gaugeBorderThickness": "2",
"gaugeBorderAlpha": "30",
"thmOriginX": "100",
"chartBottomMargin": "20",
"valueFontColor": "#000000",
"theme": "fint"
},
"value": "-6",
//All annotations are grouped under this element
"annotations": {
"showbelow": "0",
"groups": [{
//Each group needs a unique ID
"id": "indicator",
"items": [
//Showing Annotation
{
"id": "background",
//Rectangle item
"type": "rectangle",
"alpha": "50",
"fillColor": "#AABBCC",
"x": "$gaugeEndX-40",
"tox": "$gaugeEndX",
"y": "$gaugeEndY+54",
"toy": "$gaugeEndY+72"
}
]
}]
},
},
"events": {
"rendered": function(evt, arg) {
evt.sender.dataUpdate = setInterval(function() {
var value,
prevTemp = evt.sender.getData(),
mainTemp = (Math.random() * 10) * (-1),
diff = Math.abs(prevTemp - mainTemp);
diff = diff > 1 ? (Math.random() * 1) : diff;
if (mainTemp > prevTemp) {
value = prevTemp + diff;
} else {
value = prevTemp - diff;
}
evt.sender.feedData("&value=" + value);
}, 3000);
updateAnnotation = function(evtObj, argObj) {
var code,
chartObj = evtObj.sender,
val = chartObj.getData(),
annotations = chartObj.annotations;
if (val >= -4.5) {
code = "#00FF00";
} else if (val < -4.5 && val > -6) {
code = "#ff9900";
} else {
code = "#ff0000";
}
annotations.update("background", {
"fillColor": code
});
};
},
"renderComplete": function(evt, arg) {
updateAnnotation(evt, arg);
},
"realtimeUpdateComplete": function(evt, arg) {
updateAnnotation(evt, arg);
},
"disposed": function(evt, arg) {
clearInterval(evt.sender.dataUpdate);
}
}
}
);
fusioncharts.render();
});
</script>
</head>
<body>
<div id="chart-container">FusionCharts XT will load here!</div>
</body>
</html>
Given below is a brief description of the realTimeUpdateComplete
event:
Event Name | Description |
---|---|
|
This event is raised every time the real-time gauge updates itself with new data. |
A thermometer gauge configured to listen to the realTimeUpdateError
event looks like this:
{
"chart": {
"caption": "Temperature Monitor",
"subcaption": " Central cold store",
"lowerLimit": "-10",
"upperLimit": "0",
"numberSuffix": "°C",
"decimals": "1",
"showhovereffect": "1",
"thmFillColor": "#008ee4",
"showGaugeBorder": "1",
"gaugeBorderColor": "#008ee4",
"gaugeBorderThickness": "2",
"gaugeBorderAlpha": "30",
"thmOriginX": "100",
"dataStreamURL": "resources/php/dummyUrl.php",
"refreshInterval": "5",
"theme": "fint"
},
"value": "-6"
}
<html>
<head>
<title>My first chart using FusionCharts Suite XT</title>
<script type="text/javascript" src="http://static.fusioncharts.com/code/latest/fusioncharts.js"></script>
<script type="text/javascript" src="http://static.fusioncharts.com/code/latest/themes/fusioncharts.theme.fint.js?cacheBust=56"></script>
<script type="text/javascript">
FusionCharts.ready(function(){
var fusioncharts = new FusionCharts({
type: 'thermometer',
renderAt: 'chart-container',
id: 'myThm-5',
width: '240',
height: '300',
dataFormat: 'json',
dataSource: {
"chart": {
"caption": "Temperature Monitor",
"subcaption": " Central cold store",
"lowerLimit": "-10",
"upperLimit": "0",
"numberSuffix": "°C",
"decimals": "1",
"showhovereffect": "1",
"thmFillColor": "#008ee4",
"showGaugeBorder": "1",
"gaugeBorderColor": "#008ee4",
"gaugeBorderThickness": "2",
"gaugeBorderAlpha": "30",
"thmOriginX": "100",
"dataStreamURL": "resources/php/dummyUrl.php",
"refreshInterval": "5",
"theme": "fint"
},
"value": "-6"
},
"events": {
'beforeRender': function(evt, args) {
// creating div for controllers
var controllers = document.createElement('div'),
container = document.getElementById('chart-container');
// Create checkbox inside div
controllers.innerHTML = '<div id="chart-message"></div>';
controllers.setAttribute('id', 'controllers');
// setting css styles for controllers div
controllers.style.cssText = "min-height: 50px;color : #cc0000;font-family : Arial, Helvetica, sans-serif;font-size : 14px;margin-top : 10px;text-align: center;";
args.container.parentNode.insertBefore(controllers, args.container.nextSibling);
},
"realtimeUpdateError": function(evtObj, argObj) {
document.getElementById('chart-message').innerHTML = "<b>Error Occured !</b><br> Status Code : " + argObj.httpStatus;
}
}
}
);
fusioncharts.render();
});
</script>
</head>
<body>
<div id="chart-container">FusionCharts XT will load here!</div>
</body>
</html>
Given below is a brief description of the realTimeUpdateError
event:
Event Name | Description |
---|---|
|
This event is raised when an error occurs while performing real-time update using |
Troubleshooting Real-time Gauges
While accessing any of the JavaScript methods listed above, if you get an error like "... is not a function of ..."
, make sure that you are NOT running the chart from the local file system (C:\ , D:\). Instead, run the chart from behind a server (localhost - IIS, Apache etc.). This is because the default security settings do not allow the chart to perform JavaScript interactions on the local file system, unless otherwise specifically set.