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)

Incorporating statistics with Flot (Should know)


Although Flot provides everything you need for visualization, it doesn't include the typical statistical calculations you might want to perform on your raw data. Here, we will learn how to use the jStat library to process your raw data so that it can be visualized with Flot.

jStat includes a large range of advanced statistical calculations. You can view its documentation online at http://jstat.github.io/index.html.

Getting ready

You can download jStat from its GitHub repository at https://github.com/jstat/jstat. We will be using some of the calculations in its vector and linearalgebra modules:

...
  <script src="jquery.js"></script>
  <script src="jquery.flot.js"></script>
  <script src="jstat/core.js"></script>
  <script src="jstat/linearalgebra.js"></script>
  <script src="jstat/vector.js"></script>
...

How to do it…

We use jStat's functions to analyze our data in various ways as follows:

...
    var yvalues = [3, 5, 2, 3, 6, 9, 3, 8, 7, 1];
    var n = yvalues.length;

    var mean = jStat.mean(yvalues);
    var median = jStat.median(yvalues);
    var mode = jStat.mode(yvalues);
    var stdev = jStat.stdev(yvalues);

    var xvalues = jStat(0, n - 1, n);
    var data = jStat([xvalues[0], yvalues]).transpose();
    var corrcoeff = jStat.corrcoeff(yvalues, xvalues[0]);

    var display = function (label, value) {
      $('#sample').
        before(label + ': ' + value.toFixed(3) + '<br />');
    };
    display('Standard deviation', stdev);
    display('Correlation coefficient', corrcoeff);

    var line = function (y) {
      return [ [0, y], [n - 1, y] ];
    };

    $.plot(
      $('#sample'),
      [
        { data: data, points: {show: true}, color: 'black' },
        { label: 'mean', data: line(mean) },
        { label: 'median', data: line(median) },
        { label: 'mode', data: line(mode) }
      ],
      { legend: { position: 'nw' } }
    );
  </script>
...

The data is displayed along with multiple types of analysis information:

How it works…

jStat, like most statistical and mathematical software, operates on vectors and matrices. So, we represent our data, yvalues, as a vector in the form of an array of y values. We then use jStat to calculate the mean, median, mode, and standard deviation (stdev) of those y values.

The main jStat function can be used to produce a sequence of values. We use this to create xvalues, an array that contains a sequence from 0 to 9 ([0, 1, 2, ..., 9]). The jStat function can also be used to create a matrix. We do this with our xvalues and yvalues vectors and use the transpose function to create a matrix which happens to be in the same structure that Flot can plot, that is transpose takes a matrix like the following matrix:

  • [ [0, 1, 2], [7, 8, 9] ]

It produces a matrix like the following matrix:

  • [ [0, 7], [1, 8], [2, 9] ]

Next, we use jStat's corrcoeff function to calculate the correlation coefficient between our data and xvalues.

Finally, we display all of this information. We display the standard deviation and correlation as text, and we use Flot to plot the data points as well as three horizontal lines that represent the mean, median, and mode.

There's more…

jStat also comes with a large variety of probability distribution calculations, which are typical tools in statistical analysis. These include the normal (Gaussian) distribution, Beta distribution, Chi-squared distribution, Poisson distribution, and much more.

Plotting a normal distribution curve

You can calculate the data points for a normal distribution curve in only a few lines. Simply input your mean and standard deviation values into the normal function and use the pdf function to retrieve the y values:

...
var numPoints = 50;

var mean = 10;
var stdev = 5;
var span = stdev * 4;

var xvalues = jStat(mean - span, mean + span, numPoints + 1);
var normalDistribution = xvalues.normal(mean, stdev).pdf();

var normalPlot = jStat([ 
  xvalues[0], 
  normalDistribution[0]
]).transpose();

$.plot( $('.chart'), [ normalPlot ] );
...

Flot draws the distribution curve centered at our mean value: