Use of Intervals in Google Charts

Introduction

We know charts play a very important role to represent any data. I think the pictorial representation of data is a hundred times better compared to the showing of data in text format or any other format because if we represent the data in a pictorial format then anyone can easily see what has happened. Google Charts are a very effective and beautiful tool to represent data and compare data. Here we will discuss the intervals. Intervals are nothing new, it is just a type of Google Chart that also represents data. In simple words I will say that intervals play a major role if we have a series of data and want a pictorial representation of that data. In other words if we have data and with a minimum and maximum value, then we can apply interval charts to those values with a series of data. Here the minimum and maximum values might be anything, it may be numbers, may be percentiles or many other thins.

So intervals are nothing, they are just a simple representation of data for specific periods. There are mainly six types of intervals available in Google Charts. Those intervals are given below and I describe them one by one for you.

  • Line Interval
  • Bar Interval
  • Box Interval
  • Stick Interval
  • Point Interval
  • Area Interval
  • Combination of intervals
Line Interval

In line intervals we represent some points on the line and when we click on the line then there is a tool tip that occurs that shows the data depending on our needs or our representation. In the line Intervals we have a series of intervals on a line and when we go to these intervals then a pop-up occurs and that shows the data.

Example of Line Interval

  1. <htmL>  
  2. <head>  
  3. <script type="text/javascript" src="https://www.google.com/jsapi"></script>  
  4. <script type="text/javascript">  
  5. google.load("visualization""1", {packages:["corechart"]});  
  6. google.setOnLoadCallback(drawChart);  
  7. function drawChart() {  
  8. var data = new google.visualization.DataTable();  
  9. data.addColumn('number''x');  
  10. data.addColumn('number''values');  
  11. data.addColumn({id:'i0', type:'number', role:'interval','color':'#D3362D'});  
  12. data.addColumn({id:'i1', type:'number', role:'interval','color':'#D3362D'});  
  13. data.addColumn({id:'i2', type:'number', role:'interval','color':'#D3362D'});  
  14. data.addColumn({id:'i2', type:'number', role:'interval','color':'#D3362D'});  
  15. data.addColumn({id:'i2', type:'number', role:'interval','color':'#D3362D'});  
  16. data.addColumn({id:'i2', type:'number', role:'interval','color':'#D3362D'});  
  17.   
  18. data.addRows([  
  19. [1, 200, 190, 110, 185, 196, 204, 220],  
  20. [2, 120, 95, 130, 90, 113, 124, 140],  
  21. [3, 80, 60, 80, 40, 77, 97, 93],  
  22. [4, 90, 85, 95, 85, 88, 92, 95],  
  23. [5, 70, 74, 63, 67, 69, 70, 72],  
  24. [6, 330, 339, 322, 321, 328, 334, 340],  
  25. [7, 80, 77, 83, 70, 77, 85, 90],  
  26. [8, 280, 290, 310, 285, 295, 302, 310]]);  
  27.   
  28.   
  29.  var options_lines = {  
  30.         title: 'Line intervals, tailored',  
  31.         lineWidth: 4,  
  32.         curveType:'function',  
  33.         interval: {  
  34.             'i0': { 'style':'line''color':'#D3362D''lineWidth': 0.5 },  
  35.             'i1': { 'style':'line''color':'#F1CA3A''lineWidth': 1 },  
  36.             'i2': { 'style':'line''color':'#5F9654''lineWidth': 2 },  
  37.         },  
  38.         legend: 'none',  
  39.     };  
  40.   
  41. var chart_lines = new google.visualization.LineChart(document.getElementById('chart_lines'));  
  42. chart_lines.draw(data, options_lines);  
  43. }  
  44. </script>  
  45. </head>  
  46. <body>  
  47. <div id="chart_lines" style="width: 600px; height: 500px;"></div>  
  48. </body>  
  49. </html>  
Output
 


Bar Interval

Bar intervals are simple, like the bar in the bar interval we can create an error bar around our data. We show the two bars, one is in the starting point of our data and the other is used to show the end point of the our data. We use the intervals.barwidth to main the width of the bars with the compare of the first and last bars. The first and last intervals are as wide as the domain axis but the internal intervals are drawn with the ticks.

Example of Bar Interval
  1. <html>  
  2.   <head>  
  3.     <script type="text/javascript" src="https://www.google.com/jsapi"></script>  
  4.     <script type="text/javascript">  
  5.       google.load("visualization""1", {packages:["corechart"]});  
  6.       google.setOnLoadCallback(drawChart);  
  7.       function drawChart() {  
  8.         var data = new google.visualization.DataTable();  
  9.         data.addColumn('number''x');  
  10.         data.addColumn('number''values');  
  11.         data.addColumn({id:'i0', type:'number', role:'interval'});  
  12.         data.addColumn({id:'i1', type:'number', role:'interval'});  
  13.         data.addColumn({id:'i2', type:'number', role:'interval'});  
  14.         data.addColumn({id:'i2', type:'number', role:'interval'});  
  15.         data.addColumn({id:'i2', type:'number', role:'interval'});  
  16.         data.addColumn({id:'i2', type:'number', role:'interval'});  
  17.     
  18.         data.addRows([  
  19.             [1, 100, 90, 110, 85, 96, 104, 120],  
  20.             [2, 120, 95, 130, 90, 113, 124, 140],  
  21.             [3, 130, 105, 140, 100, 117, 133, 139],  
  22.             [4, 90, 85, 95, 85, 88, 92, 95],  
  23.             [5, 70, 74, 63, 67, 69, 70, 72],  
  24.             [6, 30, 39, 22, 21, 28, 34, 40],  
  25.             [7, 80, 77, 83, 70, 77, 85, 90],  
  26.             [8, 100, 90, 110, 85, 95, 102, 110]]);  
  27.   
  28.     var options_bars = {  
  29.         title: 'Bars, Interval',  
  30.         curveType: 'function',  
  31.         series: [{'color''Orange'}],  
  32.         intervals: { style: 'bars' },  
  33.         legend: 'none',  
  34.     };  
  35.         var chart_lines = new google.visualization.LineChart(document.getElementById('chart_lines'));  
  36.         chart_lines.draw(data, options_bars);  
  37.       }  
  38.     </script>  
  39.   </head>  
  40.   <body>  
  41.     <div id="chart_lines" style="width: 600px; height: 500px;"></div>  
  42.   </body>  
  43. </html>  
Output



Box interval

Box intervals create some boxes at your specific intervals. These boxes are present in the form of a rectangle. The color of the boxes at the outermost rectangle is dark Green and the color of the innermost rectangle will be light. To make the box interval more efficient and effective we use the intervals.lineWidth and intervals.barWidth tags.

Example of Box interval
  1. <html>    
  2.   <head>    
  3.     <script type="text/javascript" src="https://www.google.com/jsapi"></script>    
  4.     <script type="text/javascript">    
  5.       google.load("visualization""1", {packages:["corechart"]});    
  6.       google.setOnLoadCallback(drawChart);    
  7.       function drawChart() {    
  8.         var data = new google.visualization.DataTable();    
  9.         data.addColumn('number''x');    
  10.         data.addColumn('number''values');    
  11.         data.addColumn({id:'i0', type:'number', role:'interval'});    
  12.         data.addColumn({id:'i1', type:'number', role:'interval'});    
  13.         data.addColumn({id:'i2', type:'number', role:'interval'});    
  14.         data.addColumn({id:'i2', type:'number', role:'interval'});    
  15.         data.addColumn({id:'i2', type:'number', role:'interval'});    
  16.         data.addColumn({id:'i2', type:'number', role:'interval'});    
  17.       
  18.         data.addRows([    
  19.             [1, 100, 90, 110, 85, 96, 104, 120],    
  20.             [2, 120, 95, 130, 90, 113, 124, 140],    
  21.             [3, 130, 105, 140, 100, 117, 133, 139],    
  22.             [4, 90, 85, 95, 85, 88, 92, 95],    
  23.             [5, 70, 74, 63, 67, 69, 70, 72],    
  24.             [6, 30, 39, 22, 21, 28, 34, 40],    
  25.             [7, 80, 77, 83, 70, 77, 85, 90],    
  26.             [8, 100, 90, 110, 85, 95, 102, 110]]);    
  27.     
  28.     var options = {    
  29.         title:'Boxes, Interval',    
  30.         curveType:'function',    
  31.         lineWidth: 4,    
  32.         series: [{'color''purple'}],    
  33.         intervals: { 'lineWidth':2, 'barWidth': 0.5, style: 'boxes' },    
  34.         legend: 'none',    
  35.     };    
  36.         var chart_lines = new google.visualization.LineChart(document.getElementById('chart_lines'));    
  37.         chart_lines.draw(data, options);    
  38.       }    
  39.     </script>    
  40.   </head>    
  41.   <body>    
  42.     <div id="chart_lines" style="width: 600px; height: 500px;"></div>    
  43.   </body>    
  44. </html>     
Output


Stick Interval

A Stick interval creates some sticks wherever you specify the intervals. These sticks look like Vertical lines. A stick that has a zero height looks like a point. So we can handle this with the pointSize property.

Example of stick interval
  1. <html>    
  2.   <head>    
  3.     <script type="text/javascript" src="https://www.google.com/jsapi"></script>    
  4.     <script type="text/javascript">    
  5.       google.load("visualization""1", {packages:["corechart"]});    
  6.       google.setOnLoadCallback(drawChart);    
  7.       function drawChart() {    
  8.         var data = new google.visualization.DataTable();    
  9.         data.addColumn('number''x');    
  10.         data.addColumn('number''values');    
  11.         data.addColumn({id:'i0', type:'number', role:'interval'});    
  12.         data.addColumn({id:'i1', type:'number', role:'interval'});    
  13.         data.addColumn({id:'i2', type:'number', role:'interval'});    
  14.         data.addColumn({id:'i2', type:'number', role:'interval'});    
  15.         data.addColumn({id:'i2', type:'number', role:'interval'});    
  16.         data.addColumn({id:'i2', type:'number', role:'interval'});    
  17.       
  18.         data.addRows([    
  19.             [1, 100, 90, 110, 85, 96, 104, 120],    
  20.             [2, 120, 95, 130, 90, 113, 124, 140],    
  21.             [3, 130, 105, 140, 100, 117, 133, 139],    
  22.             [4, 90, 85, 95, 85, 88, 92, 95],    
  23.             [5, 70, 74, 63, 67, 69, 70, 72],    
  24.             [6, 30, 39, 22, 21, 28, 34, 40],    
  25.             [7, 80, 77, 83, 70, 77, 85, 90],    
  26.             [8, 100, 90, 110, 85, 95, 102, 110]]);    
  27.     
  28.     var options_sticks = {    
  29.         title:'Sticks, Interval',    
  30.         curveType:'function',    
  31.         series: [{'color''Blue'}],    
  32.         intervals: { style: 'sticks' },    
  33.         legend: 'none',    
  34.     };    
  35.         var chart_lines = new google.visualization.LineChart(document.getElementById('chart_lines'));    
  36.         chart_lines.draw(data, options_sticks);    
  37.       }    
  38.     </script>    
  39.   </head>    
  40.   <body>    
  41.     <div id="chart_lines" style="width: 600px; height: 500px;"></div>    
  42.   </body>    
  43. </html>    
Output




Point interval

As its name indicates, a point interval creates a point at a specific interval present in you chart. When you see these points it looks like circles. We use the intervals.pointsize property to increase or decrease the size of the points in the point intervals.

Example of points interval
  1. <html>    
  2.   <head>    
  3.     <script type="text/javascript" src="https://www.google.com/jsapi"></script>    
  4.     <script type="text/javascript">    
  5.       google.load("visualization""1", {packages:["corechart"]});    
  6.       google.setOnLoadCallback(drawChart);    
  7.       function drawChart() {    
  8.         var data = new google.visualization.DataTable();    
  9.         data.addColumn('number''x');    
  10.         data.addColumn('number''values');    
  11.         data.addColumn({id:'i0', type:'number', role:'interval'});    
  12.         data.addColumn({id:'i1', type:'number', role:'interval'});    
  13.         data.addColumn({id:'i2', type:'number', role:'interval'});    
  14.         data.addColumn({id:'i2', type:'number', role:'interval'});    
  15.         data.addColumn({id:'i2', type:'number', role:'interval'});    
  16.         data.addColumn({id:'i2', type:'number', role:'interval'});    
  17.       
  18.         data.addRows([    
  19.             [1, 100, 90, 110, 85, 96, 104, 120],    
  20.             [2, 120, 95, 130, 90, 113, 124, 140],    
  21.             [3, 130, 105, 140, 100, 117, 133, 139],    
  22.             [4, 90, 85, 95, 85, 88, 92, 95],    
  23.             [5, 70, 74, 63, 67, 69, 70, 72],    
  24.             [6, 30, 39, 22, 21, 28, 34, 40],    
  25.             [7, 80, 77, 83, 70, 77, 85, 90],    
  26.             [8, 100, 90, 110, 85, 95, 102, 110]]);    
  27.     
  28.    var options_points = {    
  29.         title:'Points, default',    
  30.         curveType:'function',    
  31.         lineWidth: 4,    
  32.         series: [{'color''brown'}],    
  33.         intervals: { 'style':'points', pointSize: 10 },    
  34.         legend: 'none',    
  35.     };    
  36.         var chart_lines = new google.visualization.LineChart(document.getElementById('chart_lines'));    
  37.         chart_lines.draw(data, options_points);    
  38.       }    
  39.     </script>    
  40.   </head>    
  41.   <body>    
  42.     <div id="chart_lines" style="width: 600px; height: 500px;"></div>    
  43.   </body>    
  44. </html>  
Output



Area Interval

An Area interval shows a series of areas and when we click on the specific area in the area interval there is some shaded area and in that area is presented the intervals.

Example of Area Interval
  1. <html>  
  2.   <head>  
  3.     <script type="text/javascript" src="https://www.google.com/jsapi"></script>  
  4.     <script type="text/javascript">  
  5.       google.load("visualization""1", {packages:["corechart"]});  
  6.       google.setOnLoadCallback(drawChart);  
  7.       function drawChart() {  
  8.         var data = new google.visualization.DataTable();  
  9.         data.addColumn('number''x');  
  10.         data.addColumn('number''values');  
  11.         data.addColumn({id:'i0', type:'number', role:'interval'});  
  12.         data.addColumn({id:'i1', type:'number', role:'interval'});  
  13.         data.addColumn({id:'i2', type:'number', role:'interval'});  
  14.         data.addColumn({id:'i2', type:'number', role:'interval'});  
  15.         data.addColumn({id:'i2', type:'number', role:'interval'});  
  16.         data.addColumn({id:'i2', type:'number', role:'interval'});  
  17.     
  18.         data.addRows([  
  19.             [1, 100, 90, 110, 85, 96, 104, 120],  
  20.             [2, 120, 95, 130, 90, 113, 124, 140],  
  21.             [3, 130, 105, 140, 100, 117, 133, 139],  
  22.             [4, 90, 85, 95, 85, 88, 92, 95],  
  23.             [5, 70, 74, 63, 67, 69, 70, 72],  
  24.             [6, 30, 39, 22, 21, 28, 34, 40],  
  25.             [7, 80, 77, 83, 70, 77, 85, 90],  
  26.             [8, 100, 90, 110, 85, 95, 102, 110]]);  
  27.   
  28.     var options = {  
  29.         title:'Area, Interval',  
  30.         curveType:'function',  
  31.         series: [{'color''#F1CA3A'}],  
  32.         intervals: { 'style':'area' },  
  33.         legend: 'none',  
  34.     };  
  35.         var chart_lines = new google.visualization.LineChart(document.getElementById('chart_lines'));  
  36.         chart_lines.draw(data, options);  
  37.       }  
  38.     </script>  
  39.   </head>  
  40.   <body>  
  41.     <div id="chart_lines" style="width: 600px; height: 500px;"></div>  
  42.   </body>  
  43. </html>  
Output



So these are the six basic Intervals provided by Google Charts to represent intervals between the showing of the data in the pictorial form. Above I decribed that the six intervals. These six intervals play their roles according to the user requirements. Sometimes when we see a Cricket match the representation of the falling of the wickets at the specific time interval, or chasing a run graph, or sometimes we see the comparison of the run rates of boths teams with many wickets as in the example of the Interval charts.

We can also create some interval graphs with the combination of the two or more then the two graphs for understanding it. Please see the example given below in this example. I will tell you how to make an interval using two or more intervals.

Combination of Area and bars intervals

In this example we use the area interval with the bars interval as I discussed above. The bar interval shows the vertical lines at a specific interval and using the Area interval we can select an area and make our intervals within this area. So here I make it in which I inherit both properties.

Example of bar with Area intervals
  1. <html>  
  2.   <head>  
  3.     <script type="text/javascript" src="https://www.google.com/jsapi"></script>  
  4.     <script type="text/javascript">  
  5.       google.load("visualization""1", {packages:["corechart"]});  
  6.       google.setOnLoadCallback(drawChart);  
  7.       function drawChart() {  
  8.         var data = new google.visualization.DataTable();  
  9.         data.addColumn('number''x');  
  10.         data.addColumn('number''values');  
  11.         data.addColumn({id:'i0', type:'number', role:'interval'});  
  12.         data.addColumn({id:'i1', type:'number', role:'interval'});  
  13.         data.addColumn({id:'i2', type:'number', role:'interval'});  
  14.         data.addColumn({id:'i2', type:'number', role:'interval'});  
  15.         data.addColumn({id:'i2', type:'number', role:'interval'});  
  16.         data.addColumn({id:'i2', type:'number', role:'interval'});  
  17.     
  18.         data.addRows([  
  19.             [1, 100, 90, 110, 85, 96, 104, 120],  
  20.             [2, 120, 95, 130, 90, 113, 124, 140],  
  21.             [3, 130, 105, 140, 100, 117, 133, 139],  
  22.             [4, 90, 85, 95, 85, 88, 92, 95],  
  23.             [5, 70, 74, 63, 67, 69, 70, 72],  
  24.             [6, 30, 39, 22, 21, 28, 34, 40],  
  25.             [7, 80, 77, 83, 70, 77, 85, 90],  
  26.             [8, 100, 90, 110, 85, 95, 102, 110]]);  
  27.   
  28.     var options = {  
  29.         title:'Bar and area interval chart',  
  30.         curveType:'function',  
  31.         intervals: { 'color':'series-color' },  
  32.         interval: {  
  33.             'i0': { 'color''red''style':'bars''barWidth':0, 'lineWidth':4, 'pointSize':10, 'fillOpacity':1 },  
  34.             'i1': { 'color''#D3362D''style':'bars''barWidth':0, 'lineWidth':4, 'pointSize':10, 'fillOpacity':1 },  
  35.             'i2': { 'style':'area','color''#F1CA3A''curveType':'function''fillOpacity':0.3 }},  
  36.         legend: 'none',  
  37.     };  
  38.         var chart_lines = new google.visualization.LineChart(document.getElementById('chart_lines'));  
  39.         chart_lines.draw(data, options);  
  40.       }  
  41.     </script>  
  42.   </head>  
  43.   <body>  
  44.     <div id="chart_lines" style="width: 700px; height: 500px;"></div>  
  45.   </body>  
  46. </html>  
Output



Combination of line interval with box interval

In this example we make the vertical line interval and in the background we make the box interval with this combination at every interval of line interval. In the background there is a box. So this is a box interval with a line interval. Please follow the given example.

Example of line interval with box interval
  1. <html>    
  2.   <head>    
  3.     <script type="text/javascript" src="https://www.google.com/jsapi"></script>    
  4.     <script type="text/javascript">    
  5.       google.load("visualization""1", {packages:["corechart"]});    
  6.       google.setOnLoadCallback(drawChart);    
  7.       function drawChart() {    
  8.         var data = new google.visualization.DataTable();    
  9.         data.addColumn('number''x');    
  10.         data.addColumn('number''values');    
  11.         data.addColumn({id:'i0', type:'number', role:'interval'});    
  12.         data.addColumn({id:'i1', type:'number', role:'interval'});    
  13.         data.addColumn({id:'i2', type:'number', role:'interval'});    
  14.         data.addColumn({id:'i2', type:'number', role:'interval'});    
  15.         data.addColumn({id:'i2', type:'number', role:'interval'});    
  16.         data.addColumn({id:'i2', type:'number', role:'interval'});    
  17.       
  18.         data.addRows([    
  19.             [1, 100, 90, 110, 85, 96, 104, 120],    
  20.             [2, 120, 95, 130, 90, 113, 124, 140],    
  21.             [3, 130, 105, 140, 100, 117, 133, 139],    
  22.             [4, 90, 85, 95, 85, 88, 92, 95],    
  23.             [5, 70, 74, 63, 67, 69, 70, 72],    
  24.             [6, 30, 39, 22, 21, 28, 34, 40],    
  25.             [7, 80, 77, 83, 70, 77, 85, 90],    
  26.             [8, 100, 90, 110, 85, 95, 102, 110]]);    
  27.     
  28.         
  29.     var options_boxes_background = {    
  30.         title:'Background boxes',    
  31.         curveType:'function',    
  32.         lineWidth: 4,    
  33.         series: [{'color''Red'}],    
  34.         intervals: { 'lineWidth':2, 'barWidth': 0.5 },    
  35.         interval: {    
  36.             'i2': { 'style':'boxes''color':'Green''boxWidth': 2.5,    
  37.             'lineWidth': 0, 'fillOpacity': 0.2 }    
  38.         },    
  39.         legend: 'none',    
  40.     };    
  41.         var chart_lines = new google.visualization.LineChart(document.getElementById('chart_lines'));    
  42.         chart_lines.draw(data, options_boxes_background);    
  43.       }    
  44.     </script>    
  45.   </head>    
  46.   <body>    
  47.     <div id="chart_lines" style="width: 900px; height: 500px;"></div>    
  48.   </body>    
  49. </html>  
Output



So in this article I showed the use of intervals. Chart intervals play a very important role in the pictorial representation of data. Intervals tell us the data information at specific positions. There are six main ways to show the data using intervals, but if we want to use more than two intervals in our chart then we can use them. I provided two examples.

Summary

Intervals make intervals in the data, we create some intervals between the first point to the end point and represent the data on that specific intervals so we can say that intervals are used to show the data in the chart at a specific position. It is dependent on you which intervals you prefer to use because there are six main intervals given by the Google Chart control. When we click on the intervals it shows the data in the pop-up forms so it looks very interesting when you play with it. I hope this article will be useful for everyone that wants to become familiar with intervals in charts.

Up Next
    Ebook Download
    View all
    Learn
    View all