Chapter 7. Dashboards, Controls, and Events

The previous chapter was dedicated to increasing the data capabilities of a visualization. This chapter discusses how to make customizable viewing options, allowing viewers to explore the visualization of data. Given that visualizations are such powerful tools, an increasing amount of scientific discovery is being attributed to data presented in exploratory visual environments. Even though the examples presented in this chapter are basic, it is feasible to extend Visualization API capabilities to more robust and complicated data models.

In this chapter we will cover:

  • Architecture of a Visualization with a Dashboard
  • Dashboard controls
  • Event-based visualizations
  • Transitioning between Visualizations with animation
  • Chart editor for users

Architecture

The overall structure of a visualization with controls has slight but definitive differences from a chart without controls. For charts with controls, the HTML framework encasing the visualization is slightly modified, as is the method of drawing the visualization with the API. The modified HTML framework is a good starting point to describe the architecture of a visualization with control components.

HTML framework

The HTML structure of a visualization with controls is similar to the standard HTML visualization framework, with a few distinct alterations. Dashboards require a few small changes to the HTML <body> section of the page. As with visualizations themselves, a <div id> must be designated for not only the chart, but also each of the Dashboard and control elements. An advantage to separately assigned <div id> tags is the Dashboard, control, and chart components that can be placed as desired in a page stylized with conventional HTML methods.

To start with, in the <head> section, a small change must be made to the line indicating which chart library is to be loaded. In any standard visualization using the chart.draw() method, a Google library must always be identified in the code in order for the appropriate visualization components to be loaded. The key difference for a chart with controls is that the visualization uses the ChartWrapper class, rather than the chart.draw() method, to package the controls and chart together. This means the library declaration that is used not only loads the mechanisms to create and link control elements, but also loads the mechanisms to draw the charts themselves. The controls library packaged is loaded using the google.load command.

google.load('visualization', '1.0',{'packages':['controls']});

API framework

Along with an alternative library load requirement, there are also several changes in the API execution of the visualization with chart controls. When creating one or more controls for a chart, the controllers and data must be logically linked together in order to alter the view of the chart on the fly. This bundling function is accomplished through the use of the Dashboard component. Dashboards are the encapsulation that holds the controls, data, and chart together as a single visualization. The encapsulation of all of these elements requires a slightly different method of chart creation as well. The ChartWrapper class, introduced briefly in Chapter 2, Anatomy of a Visualization, is the prescribed method when drawing charts with controls. The ChartWrapper class is analogous to chart.draw(), but is intended as a helper or bundling method to simplify drawing chart components. It packages together the handling of the data source, chart libraries, and drawing for the visualization. This simplification does potentially limit chart drawing customization options when compared to chart.draw(), but is the Google recommended method for including Dashboards and controls in a visualization, given the increased simplicity of deployment.

Additionally, to perform the bundling task for the Dashboard, the bind function is used to designate the link from charts and controls to the Dashboard. Once the controls and chart visualization variables have been created, bind is used to bundle each of the objects together prior to the Dashboard's draw process.

API framework

ControlWrapper

In the preceding figure, the ChartWrapper and ControlWrapper classes both have their own declaration in the Visualization API environment. The result of ChartWrapper is, of course, a chart. In a similar manner a ControlWrapper class equates to a handful of possible user controls: StringFilter, NumberRangeFilter, ChartRangeFilter, and CategoryFilter.

ControlWrapper

Details regarding functionality for each of these control filter options are discussed in the Dashboard—controls section of this chapter.

ChartWrapper

ChartWrapper handles tasks associated with the chart creation itself. These tasks are as follows:

  • Load
  • Draw
  • Data source
  • Events

In particular, the ChartWrapper class has its own methods for setting chart attributes and values. It also does not require a callback function to be declared, as would be the case with chart.draw. Instead ChartWrapper handles the callback as part of the data source query. For larger visualizations with a variety of charts, the ChartWrapper helper class dynamically loads chart libraries, rather than requiring each individual library to be declared in the code.

Load

As mentioned in the HTML framework section of this chapter, the google.load command with the 'controls' package is required in the HTML portion of the visualization code. The following two lines load the JavaScript API into the visualization. The next line is the google.load declaration for the Visualization controls library. Both the JavaScript API library and subsequent controls library must be present in order for visualizations with controls to function.

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
    
<script type="text/javascript">
google.load('visualization', '1.1', {packages: ['controls']});

Tip

Important

In general, an alternative to loading the appropriate libraries manually for ChartWrapper is to load them dynamically. The dynamic method does NOT replace the loading of the control library for charts with controls and dashboards. This method is intended for ChartWrapper charts that do not include dashboards or controls at this time.

For reference only, to dynamically load chart libraries for a visualization with no controls or dashboard, use the following code:

<script type="text/javascript"src='https://www.google.com/jsapi?autoload={"modules":[{"name":"visualization","version":"1"}]}'>
</script>

Use the previous code in replacement of the jsapi and google.load lines of a ChartWrapper chart without controls. Once again note this information has been provided for reference only in this section, and will not work properly with charts using dashboards and controls.

Data source

As mentioned in Chapter 6, Data Manipulation and Sources, Google Visualizations can accept a variety of data sources. The tradeoff to this flexibility is a translation step that is required when loading data from a data source to a chart. Sometimes the translation is easy yet some must be built, as is the case with using the API DataTable object. Other cases require some additional programming to properly align the data source and the visualization's formatting expectations. A benefit of the ChartWrapper helper function is the built-in streamlined access to several data sources. In particular, ChartWrapper has the ability to handle JSON strings directly. There is also a small assortment of data-related commands in ChartWrapper that can simplify initiating and then querying a data source. The most frequently used commands are as follows:

  • setQuery(query_string)
  • setDataSourceUrl(url)
  • getDataTable()
  • setDataTable(table)

Draw

The declaration and drawing method for ChartWrapper is very similar to the chart.draw() function. In fact, on the surface, the primary difference between the two is simply the organization of the declaration. For example, both chart.draw() and ChartWrapper are set to a variable such that additional methods may be invoked using the variable. In more detail, the difference becomes apparent in the execution of the two methods. The chart.draw() method returns a chart and uses the variable as a reference, where ChartWrapper only uses the variable as a reference to the Chart Wrapper function.

Using chart.draw():

varmychart = new google.visualization.LineChart(document.getElementByID('visualization').
      draw(data, {curveType: "function",
                  width: 500, height: 400}

or, using ChartWrapper:

   var wrapper = new google.visualization.ChartWrapper({
    chartType: 'LineChart',
    options: {'curveType': 'function', 
    'width': 500, 'height': 400},
    containerId: 'visualization'
  });
  wrapper.draw();

Both methods produce identical charts. To break down the ChartWrapper declaration even further, the key components of the ChartWrapper declaration are listed as follows:

The setup process for the draw function is:

  • setChartType(type)
    • set a chart type (example: line, bar, or area)
  • setOption(key, value)
    • set chart options that are specific to the type of chart chosen
  • getContainerId(id)
    • tell ChartWrapper where to draw the chart through retrieving the ContainerId
  • draw(opt_container_ref)
    • draw the chart using the draw() attribute

Events

As an added feature of the ChartWrapper class method, developers are given in-built control to a standard set of messages outputted from ChartWrapper. The message availability is provided in order to enable additional functionality to a visualization application. When working with events, it is important to keep in mind that, in order for any event to occur, the draw() function must have occurred first, as it generates the event messages.

The three types of messages generated by ChartWrapper are:

  • Error
  • Ready
  • Select
Error

An error event is a message that results from an issue occurring during the rendering of the chart visualization itself.

Ready

The ready event primarily functions as a method for acknowledging user input from a visualized chart. When an input function occurs, such as a mouse click, the application reacts to the click and performs the designated section of code. This type of event must be declared prior to calling the ChartWrapperdraw() function.

Select

Using the select event is not completely intuitive as its purpose is to return the location of the user's selection in the visualization chart itself. That is, HTML selection events outside the visualization are not detected and returned as a select event. Instead, the purpose of a select event is to return a notification to the application when a user selects a bar or legend of the visualization.

Tip

Troubleshooting

The ready event must be declared prior to the execution of draw().

All events (error/ready/select) use the same method for declaration. The following is the function that sets up a specific event. The chart being drawn, type of event (error/ready/select), and subsequent action to take, are defined as options in the declaration. The following code creates an event that listens for event_type to occur. When the event occurs, the code then instructs to execute the run_on_event function.

Google.visualization.events.addListener(wrapper, 'event_type', run_on_event);

The anatomy of the function run when the event is triggered, run_on_event, is strikingly similar to the general anatomy of a visualization function, or any other JavaScript function. Just as with a visualization, the event handling code is encapsulated entirely in a function of its own. Event functions are not included in the visualization function, but are standalone entities between the <script> tags in the application code. The run_on_event function here sends an alert to the user that the chart failed to draw.

// Run this when the 'error' event occurs! 
 
function run_on_event() { 
alert("Chart failed to draw!");  } 
}

It is helpful to think of functions that call other functions as a call and response process. The purpose of the event listener inside the visualization function is to literally listen for the specified event to occur. When it does occur, the listener calls to the responder function outside of the visualization code section. The responder function then executes its code, which concludes the call and then the response. Events are a useful tool in particular when designing visualizations to respond to user selections.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset