Reader small image

You're reading from  jQuery for Designers Beginner's Guide Second Edition

Product typeBook
Published inJul 2014
Reading LevelBeginner
Publisher
ISBN-139781783284535
Edition1st Edition
Languages
Tools
Concepts
Right arrow
Author (1)
Natalie Maclees
Natalie Maclees
author image
Natalie Maclees

contacted on 10 may '16 _______________ Natalie MacLees is the founder of Purple Pen Productions (purplepen.com), an interactive agency based in Los Angeles, California. She has been designing websites since 1997 and is a passionate advocate of both accessibility and usability. She loves teaching and sharing her knowledge, both in seminars and workshops and also with her clients. She discovered WordPress a few years ago as a flexible, extendable, and quick way to build robust websites that clients could manage on their own. She is the organizer of the Southern California WordPress Meetup Group. She is also a Google Analytics Qualified Individual.
Read more about Natalie Maclees

Right arrow

Chapter 10. Displaying Data Beautifully

While you might not consider displaying data to be all that exciting, it is often crucial to present large amounts of data to site visitors in a way that makes it easy for them to understand, explore, and interact with in new ways. Finding new and better ways to display data helps to communicate complex principles effectively. Allowing site visitors to interact with data enables them to make their own discoveries. As we are presented with more and more data everyday, the field of data visualization grows. Let's take a look at some simple things we can do when working with large amounts of data to make it easier for our site visitors to consume and understand.

In this chapter, we'll learn:

  • How to turn an ordinary table into an interactive data grid using the DataTables jQuery plugin by Allan Jardine

  • How to customize the appearance and behavior of the data grid using the jQuery UI ThemeRoller plugin

  • How to use the jQuery Visualize plugin to use tables...

A basic data grid


We'll get started by using the DataTables plugin to create a basic data grid, keeping the default settings and the styles provided with the data grid. Data grids are most helpful when we have large amounts of data to present, and the site visitors might want to filter and sort the data in different ways to find the information they are looking for. Think, for example, of a list of flights; one site visitor might be interested in sorting the flights by the departure time to find the earliest possible departure, while another site visitor might want to sort the flights by duration of the flight to find the shortest possible flight. Presenting the data in an interactive data grid allows each site visitor to quickly and easily find just the information they're looking for in a sea of information. For site visitors with JavaScript disabled, they'll simply see a large table of data and will never know that they're missing out on the interactive features. All of the information...

Time for action – creating a basic data grid


Let's take a look at how to turn a basic HTML table into an interactive data grid, as follows:

  1. We'll get started as usual with our basic HTML file and associated files and folders, just like we did in Chapter 1, Designer, Meet jQuery. We'll fill the <body> element of our HTML document with the HTML markup for a large table of data. The DataTables plugin requires us to be careful and correct with our table markup, otherwise the DataTables features may not work as expected. We'll need to ensure that we use a <thead> element for the table's header, and a <tbody> element for the table's body. A <tfoot> element for the table's footer is optional. The following code is an abbreviated sample of the HTML markup for a table of the all-time best-selling books:

    <table id="book-grid">
      <thead>
        <tr>
          <th>Title</th>
          <th>Author(s)</th>
          <th>Original Language</th>...

A customized data grid


The DataTables plugin is the first plugin we've used that has support for the jQuery UI ThemeRoller plugin. jQuery UI is a collection of widgets and interactions that make building complex applications easier and faster. Learning jQuery UI itself is beyond the scope of this book, but we'll take a look at how to use the jQuery UI ThemeRoller to create a custom theme for our data table. This same theme would apply to any jQuery UI widgets used on our page, as well as any jQuery plugins being used that include support for the jQuery UI ThemeRoller.

Time for action – customizing the data grid


We'll pick up right from where we left off with our data table. If you'd like to save your basic data grid example, just save a copy of the files we created. Then, perform the following steps to customize the appearance of your data grid:

  1. Head over to http://jqueryui.com/themeroller where we'll take a look at the ThemeRoller plugin. Take a look at the following screenshot to see the page. In the left-hand side column, you'll find the controls for selecting a predefined theme or creating a custom theme, and the right-hand side wide column contains samples of several different types of widgets.

  2. Click on the Gallery tab in the left-hand side column, and you'll see that you have dozens of choices of prebuilt ThemeRoller themes to choose from. As you click on different samples, you'll see the sample widgets in the right-hand side column update to reflect that style. I usually like to get started by selecting a prebuilt theme that's reasonably close to...

Showing graphs and charts


In some cases, a table is the ideal way of presenting a set of data. At other times, it would be more helpful to see that data visualized as a chart or a graph. Unfortunately, charts and graphs can be challenging to present in HTML. Without the help of JavaScript, we are stuck using static images to present graphs, which can then be difficult to update when the data changes.

This is when jQuery comes to the rescue. In this section, we'll take a look at using the Visualize plugin to turn tables of data into graphs and charts. The best part is that site visitors without JavaScript enabled will still have access to the data in the form of an HTML table, so nobody misses out on what we're trying to share. The data also remains accessible for those who visit our page and who might have visual impairment or other disabilities that would prevent them from consuming the data if it were presented in a static image.

Time for action – showing data in graphs and charts


Perform the following steps to create graphs and charts from HTML tables:

  1. We'll get started as usual with our basic HTML file and associated files and folders, just like we did in Chapter 1, Designer, Meet jQuery. Inside the <body> element of the HTML document, we'll include a heading and a table with some numerical data, as follows:

    <div class="content">
      <h1>A Mad Tea-Party</h1>
    </div>
    
    
    <table id="menu">
      <caption>Menu Items</caption>
      <thead>
        <tr>
          <td>Title</td>
          <th scope="col">Total Items</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th scope="row">Scones</th>
          <td>23</td>
        </tr>
        <tr>
          <th scope="row">Tea Sandwiches</th>
          <td>18</td>
        </tr>
        <tr>
          <th scope="row">Pastries</th>
          <td...

Time for action – creating a pie chart


We'll keep working with the files we set up in the previous section. Perform the following steps to create a pie chart with the Visualize plugin:

  1. Inside the HTML file, add a second HTML table that contains some data, as shown in the following code:

    <table id="eaten">
      <caption>Who had what?</caption>
      <thead>
        <tr>
          <td>&nbsp;</td>
          <th scope="col">Scones</th>
          <th scope="col">Tea Sandwiches</th>
          <th scope="col">Pastries</th>
          <th scope="col">Tea</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th scope="row">Alice</th>
          <td>1</td>
          <td>3</td>
          <td>1</td>
          <td>2</td>
        </tr>
        <tr>
          <th scope="row">Mad Hatter</th>
          <td>0</td>
          <td>6</td>
          <td>3<...

Time for action – calculating the ideal size for charts and graphs


We'll keep working with the files we created in the previous section. Perform the following steps to dynamically set the width and height of our charts and graphs according to the width of the browser window:

  1. Open your scripts.js file. We'll want to add a few quick calculations. First up, let's set a preferred width for our graphs and charts, as follows:

    $(document).ready(function(){
      var preferredWidth = 450;
    
    
      $('#menu').visualize({
        ...
      });
    
      $('#eaten').addClass('accessHide').visualize({
        ...
      }).appendTo('#pie-container').trigger('visualizeRefresh');
    
    });

    We created a variable named preferredWidth. Recall that a variable is just a container—in this case, the variable contains the size in pixels that we'd like our charts and graphs to appear by default.

  2. Next, we need to get the actual width of the available content area on our page. As our CSS code is fluid, this will change depending on the width of the browser...

Summary


In this chapter, we learned how to turn an ordinary HTML table into an interactive data grid. Our site visitors can now take advantage of sorting different columns of the table to view the data in different ways. Site visitors with JavaScript disabled simply see an ordinary HTML table that contains all of the data. Data grids aren't terribly exciting, but they can make dealing with large amounts of data worlds easier for your site visitors. We also took a look at how to display numerical tabular data in charts and graphs, adding visual interest to our pages. Next up, we'll take a look at using scrolling effects in our pages, including parallax effects.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
jQuery for Designers Beginner's Guide Second Edition
Published in: Jul 2014Publisher: ISBN-13: 9781783284535
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Natalie Maclees

contacted on 10 may '16 _______________ Natalie MacLees is the founder of Purple Pen Productions (purplepen.com), an interactive agency based in Los Angeles, California. She has been designing websites since 1997 and is a passionate advocate of both accessibility and usability. She loves teaching and sharing her knowledge, both in seminars and workshops and also with her clients. She discovered WordPress a few years ago as a flexible, extendable, and quick way to build robust websites that clients could manage on their own. She is the organizer of the Southern California WordPress Meetup Group. She is also a Google Analytics Qualified Individual.
Read more about Natalie Maclees