I have been experimenting for a while with Google Chart Tools [01] and they are a powerful tool to apply on many situations when we might want to present data in a nicely and more readable manner.
It causes a much better impact to present a chart rather than the raw data. Therefore the task of adding graphics might be a repeatable task, but of course, we would like to perform this in an easier way. This is why the fact of having a tool on the toolkit comes as a great thing, and besides the particular technology you use (Google charts are not the only technology for this), what is important (from my point of view) is to have something located so you can call it when you need it.
This particular set of tools are very powerful (because most of the reasons I explained on the previous paragraph), but it is also very simple to use (it just requires a few examples and an understanding of JavaScript, but after identifying a pattern, it becomes easier).
It displays the data based on sources which could be data tables or data sources (bases, etc.). Regardless the underlying data source, I would like to present how is the process of displaying the graphics and perhaps let the data sources for a further post.
Getting started with a simple example
The steps for achieving this are actually simple. It requires to add the Google-chart libraries and after they are linked, create the structure the chart will have, fill in the data, and then define the HTML section (div) on where the graphic is going to be displayed (the part on the DOM identified by an id, that will be referenced within the JavaScript code).
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data_table =
[ ['col1' , 'col2' ]
, [ x11 , x12 ]
, [ x21 , x22 ]
, [ x31 , x32 ]
//......
];
var data = google.visualization.arrayToDataTable(
data_table
);
var options = {
title: 'Function Graphic',
curveType: 'function'
};
var chart =
new google.visualization.LineChart(
document.getElementById('chart_div')
);
chart.draw(data, options);
}
Then with this generic code skeleton (along with other HTML elements that are required) we just create an element on the DOM with the id “chart_div” (for this case, as it was references on the chart function), and the graphic will be displayed.
One important thing is the way we define the data values on the variable, is that the function expects a list of list where the first element refers to the header and the rest of them are the pair-values that will be displayed. For all the kind of graphic that this type of information applies, we could change the type of chart (instead of LineChart, for a different one, if applies).
For example, by following a structure like this one (it’s not the only one, different kind of graphics might require a different structure, but it is a common programming idiom for cases like this one), we could create a representation of a mathematical function (x² for example).
The code would be like the following one:
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data_results = generate_values( f , -10 , 10 , 0.5 ) ;
console.log(data_results);
var axis = ['X' , 'Y=f(x)'];
var table = data_results;
data_results.unshift(axis); //add the header at the beginning of the table
var data = google.visualization.arrayToDataTable(
data_results
);
var options = {
title: 'Function Graphic',
curveType: 'function'
};
var chart =
new google.visualization.LineChart(
document.getElementById('chart_div')
);
chart.draw(data, options);
}
function f(x){
return Math.pow( x , 2 );
}
function generate_values( fn , startValue , endValue , pace ){
var results = [];
for(var i = startValue ; i <= endValue ; i += pace ){
results.push([ i , fn(i) ]);
}
return results;
}
The only difference here is that an auxiliary function is used for the generation of the values to represent, and it works for any kind of function passed by parameter (courtesy of JavaScript), but all in all this function returns an Array with the required structure, and the headers are added at the beginning of this array. Therefore, the resulting object is a valid structure for what the library needs, and there is no issue with the rendering.
The options parameter is an object that handles and owns the properties of the chart to draw. All type of charts share some generic options, but they also have particular private ones depending on the type of graphic. The details and explanation of each one are perfectly documented on the API page.
Although there are many resources within this library, explaining all of them would demand much time. Therefore is a good point to make on saying that probably one of the greatest things this has is a common structure, or idiom that works as a starting point and allows to initially know how to work with the tool.
References