Book Image

Instant jQuery Flot Visual Data Analysis

By : Brian Peiris
Book Image

Instant jQuery Flot Visual Data Analysis

By: Brian Peiris

Overview of this book

Data visualization and analysis is a crucial skill in many software projects. Flot uses jQuery and HTML5 to add easy and powerful visualization capabilities to any web application. Flot produces beautiful visualizations with a minimal amount of code. It is also highly configurable, extensible, and includes many plugins out of the box. A practical guide to take you through the basics of using Flot to visualize your data; the book describes Flot's functionality in dedicated sections that include code, screenshots, and detailed explanations so that you can learn to produce charts in minutes. As you progress though this book, it will guide you through real-world examples to show you how to use statistical techniques with Flot for interactive data visualization and analysis. Starting with the very basics, you will learn exactly what you need to do to display the simplest chart using Flot. This step-by-step guide takes you through Flot's many features and settings until you can finally master the techniques you'll need to apply Flot to your application's data. You'll learn to create basic point, line, and bar charts and to use Flot's stack, pie, and time plugins to create specialized chart types. Along with learning to display complex data with multiple customizable axes you will learn to make your charts interactive with Flot's crosshair plugin. Finally, this book will guide you through learning statistical techniques via the jStat JavaScript library to analyse data; along with Flot's errorbars and fillbetween plugins to display error margins and data percentiles. Instant jQuery Flot Visual Data Analysis will give you a head start so that you can add data visualization features to your applications with ease.
Table of Contents (7 chapters)

Plotting time series


Flot includes a plugin that handles time-based data. The plugin takes care of displaying the X axis accordingly, including date formatting, time zone calculations, and unit conversions.

Getting ready

Again, we can use the same boilerplate along with the time plugin:

...
  <script src="jquery.js"></script>
  <script src="jquery.flot.js"></script>
  <script src="jquery.flot.time.js"></script>
...

How to do it…

We enable the time plugin by setting mode to time on the xaxis setting. The time plugin also expects a specific data type for the x coordinates of our dataset as follows:

...
    var data = [ 
      [1357016400000, 0], 
      [1359694800000, 4], 
      [1362114000000, 2] 
    ];

    $('#sampleChart').plot(
      [ data ],
      { xaxis: { mode: 'time' } }
    );
...

The time plugin automatically draws the points in their appropriate positions on the chart and also changes the labels on the X axis to display the corresponding dates:

How it works…

The time plugin expects the x coordinates of our data set to be standard JavaScript timestamps. They represent the number of milliseconds since January 1, 1970 00:00:00 UTC. You can obtain these timestamps in JavaScript by using the getTime method on a Date object.

The plugin also assumes that the timestamps are in the UTC time zone. You may set the timezone setting under the xaxis setting to browser, if your data is in the user's time zone already. Other time zones are supported through a third-party timezoneJS plugin that you can find at https://github.com/mde/timezone-js.

The time plugin also includes settings to control the number of ticks displayed on the axis to use a twelve hour clock format and to change the text displayed for months and days. You can find more information in the Flot documentation available at https://github.com/flot/flot/blob/0.8.1/API.md#time-series-data.

There's more…

The time plugin also supports custom date and time formats using a formatting string that is a subset of the strftime standard from the C programming language. You can find more details in the preceding linked documentation.

Using custom date and time formats

Here we use a custom formatting string to force the time plugin to show the full date and time on the X axis labels as follows:

...
    var data = [
      [new Date('2013-02-28T22:30:00Z').getTime(), 0],
      [new Date('2013-02-28T23:45:00Z').getTime(), 4],
      [new Date('2013-03-01T01:15:00Z').getTime(), 2]
    ];

    $('#sampleChart').plot(
      [ data ],
      { xaxis: { 
        mode: 'time', 
        timeformat: '%Y-%m-%d %H:%M'
      } }
    );
...

The labels on the X axis are replaced with text in the format we've specified: