Building LOGD Visualizations

LOGD 101
This tutorial presents a simple example that visualizes the LOGD data mashups.

What to Expect

By the end of this tutorial you should be able to generate a Javascript-based visualization, through use of the Google Visualization API.

What You Need to Know

This tutorial assumes you are familiar with concepts found in the following resources:
  • Javascript is a Web programming language. See [1]

The LOGD Mashup

The code described in the tutorial uses Dataset 353:State Library Agency Survey: Fiscal Year 2006 and Dataset 1356:Tax Year 2007 County Income Data from The expected output is a map of "Adjusted Gross Income(AGI) per Capita": a US map where each state is colored according to the average AGI per person living in that state. We obtain a state's AGI data from Dataset 1356 and a state's population data from Dataset 353. We also assume that state's population remain the same from fiscal year 2006 to fiscal year 2007. The LOGD data mashup is enabled by the following SPARQL query

Static Visualization

Defining the HTML Layout

To make a visualization web-accessible, an accompanying HTML layout is necessary. HTML layouts can be managed through use of division (div) elements, which define different sections of a page. The HTML layout for our LOGD demo is given below, with a div element for representing the demo description, and another for defining where the visualization will be placed (with id='map_canvas').
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="">
  	<title>AGI per Capita Map</title>
      <div>AGI per Capita Map: average adjusted gross income per person in dollar amount in US states.</div>
      <div id='map_canvas'>Loading Map ...</div>

Inserting Visualization Code

Following the creation of an HTML layout, javascript-based visualization code may be inserted in the HEAD section. This code has the following objectives in our LOGD demo:
  1. Load the appropriate Google Visualization API packages (in this case, the GeoMap package).
  2. Define a callback function for loading visualization code, which is called upon the loading of the HTML page.
  3. Obtain data from a given source to pass to our GeoMap instance. The Google Visualization API is designed to accept data in the form of specially-formatted JSON (represented by the URI , which can then be fed to a JSON processing function.
  4. Following a call to the JSON processor, verify that it successfully processed the passed file.
  5. Get back a response from the query processor, containing the data from the JSON file.
  6. Define a data table to store the response data in. This process starts by defining header entries of the form TABLE.addColumn(DATATYPE, NAME).
  7. For each entry in the response, create a new data table row for the corresponding data.
  8. Define a configuration for the GeoMap instance to be visualized, containing information such as resolution.
  9. Define the GeoMap instance in the HTML div with id='map_canvas', using the configuration from Step 8 and data table from Step 7.
    <!--   import Google visualization  API -->
    <script type="text/javascript" src=""></script>

    <!--   customize function -->
    <script type="text/javascript">
    /* <![CDATA[ */

      // load google visualization packages - STEP 1
      google.load('visualization', '1', {'packages': ['geomap']});
      // set callback function for drawing visualizations - STEP 2
      function drawMap() {
      	//load static data - STEP 3
      	var queryurl = "";
      	var query = new google.visualization.Query(queryurl); // Send the query.
      function  handleQueryResponse(response){
      	// Check for query response errors. - STEP 4
      	if (response.isError()) {
           alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());

      	// read data  - STEP 5
      	var data = response.getDataTable();
      	// create new data - STEP 6
      	var newdata = new google.visualization.DataTable();
      	newdata.addColumn('string', 'State');
      	newdata.addColumn('number', 'AGI per Capita');

        // populate each row - STEP 7
      	var rows = data.getNumberOfRows();
      	for (var i = 0; i < rows; i++ )
      	  var state = 'US-' + data.getValue(i, 0);
      	  var value =  Math.round(data.getValue(i, 1)*1000/data.getValue(i, 2)); // AGI figure uses thousand-dollar unit
      	  newdata.addRow([state, value]);
      	// configure map options - STEP 8
      	var options = {};
      	options['region'] = 'US';	// show US map
      	options['dataMode'] = 'regions';
      	options['width'] = 900;
      	options['height'] = 550;

        // define geomap instance - STEP 9
        var viz = document.getElementById('map_canvas');
        new google.visualization.GeoMap(viz).draw(newdata, options );    
    /* ]]> */
Once this code is inserted in the HEAD section, the visualization will appear: . NOTE: View in Firefox!

Build a SPARQL-based Dynamic Visualization

Our SPARQL endpoint ( is designed to be capable of formatting results in JSON compatible with the Google Visualization API. In the above code, the section:
  var queryurl = "";
corresponding to Step 3 above can be replaced with:
  //load data using SPARQL query
  var sparqlproxy = "";
  var queryloc = "";    
  var service = "";
  var queryurl = sparqlproxy 
                + "?" + "output=gvds"
                + "&service-uri=" + encodeURIComponent(service)
                + "&query-uri=" + encodeURIComponent(queryloc) ;
The above code passes a SPARQL query to our endpoint, returning the same information that is contained in the static JSON file. The modified coded is located at: NOTE: View in Firefox!

Extending this demo

The above code may be used as a starting point for generating your own GeoMap-based visualization. Doing this will require the following steps:
  1. Specifying a different SPARQL query (queryloc) in Step 3
  2. Modifying the column definitions in Step 6, to correspond to the new SPARQL query.
  3. Modifying the response processing code in Step 7.
Your rating: None Average: 5 (2 votes)

Warning: Table './drupal/watchdog' is marked as crashed and last (automatic?) repair failed query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:12:\"user warning\";s:8:\"%message\";s:351:\"Table &#039;./drupal/accesslog&#039; is marked as crashed and last (automatic?) repair failed\nquery: INSERT INTO accesslog (title, path, url, hostname, uid, sid, timer, timestamp) values(&#039;Building LOGD Visualizations&#039;, &#039;node/3918&#039;, &#039;&#039;, &#039;;, 0, &#039;ac04bmf1lkhvdgfn10dosoqiq1&#039;, 31, 1618680302)\";s:5:\"%file\";s:58:\"/data/www/html/drupal/modules/statistics/statistics.module\";s:5:\"%line\";i:63;}', 3, '', ' in /data/www/html/drupal/includes/ on line 134