How to Visualize Real-Time Data
How to create real-time web visualization of real-time data and traffic to your website.
Join the DZone community and get the full member experience.
Join For Freeso you are on dzone right now and you have decided to read my article. that means there is a good chance that either you are into the business of creating websites or you own a website yourself. and if you are serious about your websites, then you must be using some kind of analytics. right?
well, my favorite analytics tool is google analytics. and one of the best features it offers is the 'real-time' view. i can see all kinds of crazy details — geographic location, current page, referral source etc. — about people who are on my website at this very moment. ability to see things in real-time can be a great feature in any product. and that's why i decided to write about it today.
in this tutorial, i will use fusioncharts ' real-time line chart to display processor load of two processors ('a' and 'b') in real-time.
this is what we are going to make (you can see live version here ):
3 steps to making a real-time chart
to make it easier to understand, i have divided this tutorial into following 3 steps:
-
fetching data from a data stream.
-
including a javascript dependency and creating an html container.
-
defining chart instance and rendering the chart.
step 1: fetching data from a data stream
reliably fetching data from a feed will is probably the most important step in creating real-time charts. once you have it, it's easy to visualize it by plugging it into any of the various charting libraries available in the market. for this tutorial i am going to use fusioncharts' javascript chart library .
fusioncharts provides six different types of real-time charts: area, line, column, stacked-area, stacked-column and line with dual y-axis. as i mentioned above, for this tutorial i am going to use a line chart as it is best suited for our purpose.
data format for real-time charts is slightly different than other charts. it depends upon various factors as explained below:
-
number of datasets : when it comes to real-time charts, usually we usually use them to compare 2 or more different datasets. for example, stock price of 2 competitors or performance of 2 pages in an a/b test. in my demo i have compared load on 2 hypothetical processors.
if there are two datasets, then there will be two values:&value=vala|valb
and if there are three datasets, there will be three values:&value=vala|valb|valc
where"|"
is data value separator. -
updating labels in real-time : static labels won't make sense in a real-time chart. we need to update labels as well along with the data. you need to use following data stream format to achieve that:
&label=demolabel&value=vala|valb|---|valn
-
attributes to be updated in the chart : data stream pattern also changes if attributes like division line thickness, line color, or cosmetics attributes are added to the chart. data stream with this information will look like this:
&value=vala|valb&vline=1&vlinethickness=2&vlinecolor=#ff0000&vlinelabel=demolabel
other constraints such as hiding labels, adding tool text, adding links, updating color, passing empty data can also be part of this. -
chart commands : the data stream format also changes if chart commands are supplied, like the
clear
command will clear the chart canvas, andstopupdate
will stop updating the chart. the syntax for both of them are as follows:
&clear=1
&stopupdate=1
for this tutorial, i have created a demo data stream using php to generate two random numbers between 15 and 95 to represent processor load. here is how i did it:
<?php
date_default_timezone_set("utc");
$now = date("h:i:s", time());
//get random numbers
$randomvaluesysa = floor(rand(15,95));
$randomvaluesysb = floor(rand(20,90));
//output
echo "&label=".$now."&value=".$randomvaluesysa."|".$randomvaluesysb;
?>
here is what a sample output from the above code looks like:
&label=11:00:37&value=39|55
step 2: including javascript dependency & creating html container
in this step we will include fusioncharts
core javascript file
and a
<div>
element as a container for the chart.
here is the code:
<html>
<head>
<!-- fusioncharts core javascript file -->
<script type="text/javascript" src="path/to/fusioncharts.js"></script>
</head>
<body>
<!-- chart container -->
<div id="chart-container">an awesome chart is on its way!</div>
</body>
</html>
step 3: defining chart instance and rendering the chart
in this step we will define fusioncharts constructor instance for the chart. here is the code: (please see the explanation in the comment)
fusioncharts.ready(function() { //fusioncharts constructor
var processorchart = new fusioncharts({
//chart instance
// we have defined type, dom container, dimensions, data format and data source here
type: "realtimeline",
renderat: "chart-container",
width: "100%",
height: "450",
dataformat: "json",
datasource: {
"chart": {
//data stream url will feed data to the chart
"datastreamurl": "http://fc.gagansikri.in/workspace/realtimechartsdemo/datastream.php",
// frequency of fetching new data
"refreshinterval": "2",
//other chart configurations
},
"categories": [{
"category": [{
"label": "start"
}]
}],
"dataset": [{
//for processor a
"color": "00dd00",
"seriesname": "processor a",
"data": [{
"value": "0"
}]
}, {
//similar declaration for processor b
...
}]
}
}).render(); //render method
});
next steps
-
you can customize both functionality and cosmetics of a real-time chart in great detail. for managing functionality, you can define refresh interval, update interval, decimal precisions, canvas and chart margins, etc. for controlling cosmetics you can add trend-lines and trend-zones, increasing the number of data sets, etc. to learn more about customizing, you can refer this documentation page .
-
fusioncharts provides ability to log messages depending on predefined conditions. if you want to add that to your chart, you can visit this page to learn more .
-
if you don't have a data stream, you can also feed data to the chart using
feeddata
method provided by fusioncharts. -
for some inspiration, check out more examples of real-time charts .
Opinions expressed by DZone contributors are their own.
Comments