Book Image

Data Visualization with D3 and AngularJS

By : Erik Hanchett, Christoph Körner
Book Image

Data Visualization with D3 and AngularJS

By: Erik Hanchett, Christoph Körner

Overview of this book

Table of Contents (16 chapters)
Data Visualization with D3 and AngularJS
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

The question of how I could better visualize my application server's log files has always hovered around in my mind. In this book, we'll materialize this idea. After reading the book, you'll be able to analyze the log files of your application server and display them as a time series chart on a web page—a real-time autoupdating chart. I picked my two favorite JavaScript libraries—AngularJS and D3.js—to develop such a visualization.

AngularJS is a web application framework that facilitates the development of modern web applications with concepts such as the MVC/MVVM pattern, two-way data binding, testable modules, and dependency injection. I especially like the idea that one can declare templates to extend the HTML language; this enables a whole new world of applications and the sharing of work between designers and frontend engineers.

D3.js is a visualization toolkit that facilitates the generation and manipulation of web-based vector graphics with full access to the underlying SVG standard. Moreover, animations and interactive visualizations change how users perceive web applications; D3.js offers everything you need to make a visualization interactive out of the box.

In this book, I will teach you how to create an interactive real-time visualization for the Web with AngularJS as the application framework and D3.js as the visualization toolkit. I will explain the basic concepts of D3.js, when and why we use it, and why it's so much better than other DOM-transforming libraries, with a focus on its data-driven approach. Also, I will show you best practices to manipulate datasets and preprocess data—an everyday job if you want to display data from different sources. Then, we will spend more time on seeing how a visualization library can be best integrated into an AngularJS application using modules, directives, and services to make the components testable and maintainable. Later, we will focus on developing the visualization.

First, we need to load real data in the application and parse the text data to a JavaScript array of objects. Then, we need to preprocess and group the data according to the way we want to display it. Now, we can generate a curve or shape in the chart that represents our data. Next, we want to animate this curve—to show a transition between the old and the new state—whenever we update the data of the chart. We also want to zoom and pan inside the charts and create filters in order to clamp the chart to a certain range, for example, a starting time and an ending time for the time series data. Finally, we want to put everything together on to a dashboard and feed it with real server logs of a web server.

Displaying data on the web is a common task for developers. This books walks you through an exciting project, explaining best practices and common pitfalls in this domain using the two most amazing JavaScript libraries: AngularJS and D3.js.

What this book covers

Chapter 1, The Magic of SVG, D3.js, and AngularJS, introduces you to the world of vector graphics on the web and points out the advantages of D3.js.

Chapter 2, Getting Started with D3.js, explains how to draw vector graphics with D3.js and its data-driven approach.

Chapter 3, Manipulating Data, walks you through the important task of manipulating and preprocessing datasets in JavaScript and D3.js.

Chapter 4, Building a Chart Directive, lets you implement a first scatter chart directive in order to integrate the visualization into a simple AngularJS application with a focus on modularization and testability.

Chapter 5, Loading and Parsing Data, explains how to load files and parse real data using AngularJS and D3.js. It also explains how to aggregate information for the visualization.

Chapter 6, Drawing Curves and Shapes, helps you understand lines, curves, and shapes in SVG and D3.js in order to create different chart types, such as line charts, area charts, and bar charts.

Chapter 7, Controlling Transitions and Animations, shows how animations can be used to better visualize changes in the dataset and the visualization. You will learn about animations in D3.js and how to create plausible transitions to update chart directives.

Chapter 8, Bringing the Chart to Life with Interactions, explains how to add interactions to the chart directive, which is a big advantage of web-based visualizations. You will also see how to combine and encapsulate the interactions of the D3 chart and the AngularJS application.

Chapter 9, Building a Real-time Visualization to Monitor Server Logs, walks you through an exciting example where you will use all the acquired knowledge of the book to build a visualization dashboard to analyze server logs.

What you need for this book

To get started, all you need is a web browser and a text editor, such as Sublime Text. However, I recommend that you install Node.js (http://nodejs.org/) that includes the npm package manager and the Bower (http://bower.io/) module in order to run the tests. I will walk you through the installation of the required packages in the corresponding chapters.

Starting from Chapter 4, Building a Chart Directive, we will use the http-server module (https://www.npmjs.com/package/http-server) to run the examples from a local web server. An internet connection is only required for installing tools and libraries, but not for running the examples.

Who this book is for

Data Visualization with D3 and AngularJS is intended for web developers with experience in AngularJS who want to implement fully customizable interactive vector graphics for all platforms. Whether you already know a bit about SVG and vector graphics in the browser or have never used any visualization library in JavaScript before, you will be able to master the data-driven techniques of D3. In either case, this book will get you up and running quickly and also challenges you if you have already worked with D3.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The .exit() function will return all elements of the Selection because the dataset contains no elements."

A block of code is set as follows:

<html>
  <head>
    <title>My App</title>
  </head>
  <body>
    <h1 class="header>My Application</h1>
    <p class="content">
      Lorem ipsum dolor sit amet, ...
    </p>
  </body>
</html>

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

<html>
  ...
  <body>
  <object id="vis" width="50" height="30">
    <param name="movie" value="vis.swf">
    <embed src="vis.swf" type="application/x-shockwave-flash">
  </object>
  </body>
</html>

Any command-line input or output is written as follows:

npm install --save express
npm install --save serve-static

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "The t parameter in the range 0 to 1 defines the position on the curve between the starting point P0 (in the figure P1) and the end point P3 (in the figure P2); P1 (in the figure C1) and P2 (in the figure C2) are called control points and are used to model the shape between these points."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from: https://www.packtpub.com/sites/default/files/downloads/8484OS_Graphics.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.