Lab 2: Visualization with d3.js



Similar documents
Interactive Data Visualization for the Web Scott Murray

How to Deploy Custom Visualizations Using D3 on MSTR 10. Version 1.0. Presented by: Felipe Vilela

Interactive HTML Reporting Using D3 Naushad Pasha Puliyambalath Ph.D., Nationwide Insurance, Columbus, OH

EXERCISE: Introduction to the D3 JavaScript Library for Interactive Graphics and Maps

Basic tutorial for Dreamweaver CS5

Table of Contents. What is ProSite? What is Behance? How do ProSite & Behance work together? Get Started in 6 Easy Steps.

Using Adobe Dreamweaver CS4 (10.0)

Introduction to D3.js Interactive Data Visualization in the Web Browser

How To Change Your Site On Drupal Cloud On A Pcode On A Microsoft Powerstone On A Macbook Or Ipad (For Free) On A Freebie (For A Free Download) On An Ipad Or Ipa (For

We automatically generate the HTML for this as seen below. Provide the above components for the teaser.txt file.

Custom Visualizations with D3.js

Team Members: Christopher Copper Philip Eittreim Jeremiah Jekich Andrew Reisdorph. Client: Brian Krzys

Adobe Illustrator CS6. Illustrating Innovative Web Design

Dashboard Skin Tutorial. For ETS2 HTML5 Mobile Dashboard v3.0.2

MetroBoston DataCommon Training

Hypercosm. Studio.

WP Popup Magic User Guide

Create interactive web graphics out of your SAS or R datasets

Microsoft Expression Web

PASTPERFECT-ONLINE DESIGN GUIDE

38 Essential Website Redesign Terms You Need to Know

Charts for SharePoint

LaGuardia Community College Thomson Ave, Long Island City, New York Created by ISMD s Dept. Training Team. Overview

CIS 467/602-01: Data Visualization

Adding 3rd-Party Visualizations to OBIEE Kevin McGinley

Recreate your Newsletter Content and Layout within Informz (Workshop) Monica Capogna and Dan Reade. Exercise: Creating two types of Story Layouts

D3.JS: Data-Driven Documents

Working with the Ektron Content Management System

Data Visualization. Scientific Principles, Design Choices and Implementation in LabKey. Cory Nathe Software Engineer, LabKey

Enhanced Formatting and Document Management. Word Unit 3 Module 3. Diocese of St. Petersburg Office of Training Training@dosp.

Citrix StoreFront. Customizing the Receiver for Web User Interface Citrix. All rights reserved.

Joomla! template Blendvision v 1.0 Customization Manual

Visualizing Data: Scalable Interactivity

Google Sites: Creating, editing, and sharing a site

Dreamweaver and Fireworks MX Integration Brian Hogan

DIRECTIONS FOR SETTING UP LABELS FOR MARCO S INSERT STOCK IN WORD PERFECT, MS WORD AND ACCESS

So you want to create an a Friend action

MicroStrategy Analytics Express User Guide

Step 1: Setting up the Document/Poster

Creating Personal Web Sites Using SharePoint Designer 2007

WEB DESIGN COURSE CONTENT

WebSphere Business Monitor V7.0 Business space dashboards

IAS Web Development using Dreamweaver CS4

CREATING EXCEL PIVOT TABLES AND PIVOT CHARTS FOR LIBRARY QUESTIONNAIRE RESULTS

Catalog Creator by On-site Custom Software

Excel -- Creating Charts

WP Popup Magic User Guide

Web Design for Programmers. Brian Hogan NAPCS Slides and content 2008 Brian P. Hogan Do not reproduce in any form without permission

TIBCO Spotfire Web Player Release Notes

jquery Tutorial for Beginners: Nothing But the Goods

Previewing & Publishing

BT CONTENT SHOWCASE. JOOMLA EXTENSION User guide Version 2.1. Copyright 2013 Bowthemes Inc.

Virtual Exhibit 5.0 requires that you have PastPerfect version 5.0 or higher with the MultiMedia and Virtual Exhibit Upgrades.

Adobe Dreamweaver CC 14 Tutorial

Quick Start Guide. Microsoft OneNote 2013 looks different from previous versions, so we created this guide to help you minimize the learning curve.

Introduction Course in SPSS - Evening 1

Microsoft Expression Web Quickstart Guide

Sample Table. Columns. Column 1 Column 2 Column 3 Row 1 Cell 1 Cell 2 Cell 3 Row 2 Cell 4 Cell 5 Cell 6 Row 3 Cell 7 Cell 8 Cell 9.

Plotting: Customizing the Graph

Microsoft FrontPage 2003

DOING MORE WITH WORD: MICROSOFT OFFICE 2010

Working with the new enudge responsive styles

Appendix 2.1 Tabular and Graphical Methods Using Excel

Course Project Lab 3 - Creating a Logo (Illustrator)

ADOBE MUSE. Building your first website

WEB DEVELOPMENT IA & IB (893 & 894)

Getting Started with Excel Table of Contents

TUTORIAL 4 Building a Navigation Bar with Fireworks

Quick Start Guide. Microsoft Publisher 2013 looks different from previous versions, so we created this guide to help you minimize the learning curve.

WebSphere Business Monitor V6.2 Business space dashboards

STATGRAPHICS Online. Statistical Analysis and Data Visualization System. Revised 6/21/2012. Copyright 2012 by StatPoint Technologies, Inc.

Spotfire v6 New Features. TIBCO Spotfire Delta Training Jumpstart

Mobile Web Design with HTML5, CSS3, JavaScript and JQuery Mobile Training BSP-2256 Length: 5 days Price: $ 2,895.00

introduces the subject matter, presents clear navigation, is easy to visually scan, and leads to more in-depth content. Additional Resources 10

Create Charts in Excel

MailChimp Instruction Manual

Getting Started with WebSite Tonight

Once you have gone through this document you will have a form that, when completed, will create an Account & Contact in Oracle.

Building a Horizontal Menu in Dreamweaver CS3 Using Spry R. Berdan

Quick Start Guide. Microsoft OneNote 2013 looks different from previous versions, so we created this guide to help you minimize the learning curve.

Kentico CMS 5.5 User s Guide

SmallBiz Dynamic Theme User Guide

PROMIS Tutorial SMS Data Collection System

Using HTML5 Pack for ADOBE ILLUSTRATOR CS5

Blueball Design Dynamic Content 2 Stack Readme Manual v1.0

MEAP Edition Manning Early Access Program D3.js in Action Version 5

HOUR 9. Formatting Worksheets to Look Great

Dreamweaver Domain 2: Planning Site Design and Page Layout

Figure 1. An embedded chart on a worksheet.

Plots, Curve-Fitting, and Data Modeling in Microsoft Excel

SPHOL326: Designing a SharePoint 2013 Site. Hands-On Lab. Lab Manual

Chapter 4: Website Basics

CONTENTM WEBSITE MANAGEMENT SYSTEM. Getting Started Guide

01/42. Lecture notes. html and css

MicroStrategy Desktop

Making a Web Page with Microsoft Publisher 2003

Advanced Presentation Features and Animation

Serif PagePlusX4. Group Listing

Web Design Basics. Cindy Royal, Ph.D. Associate Professor Texas State University

Transcription:

Lab 2: Visualization with d3.js SDS235: Visual Analytics 30 September 2015 Introduction & Setup In this lab, we will cover the basics of creating visualizations for the web using the d3.js library, which can be found at: http://www.d3js.org. First we ll build a bare-bones bar chart using only HTML elements. Next, we ll build a more flexible version using Scalable Vector Graphics (SVG). Finally, we ll read in some actual data and add axes, labels and interaction. 1 You have several options available for completing this lab: JSFiddle If you want to focus on the javascript, you might want to try https://jsfiddle.net, an online resource for testing and running JavaScript code right from your browser: Under Frameworks & Extensions, you ll want to select D3 3.0. to include the d3.js library. To test it out, simply enter some JS into the JavaScript window, and click the Run button! Get stuck? Try the JSHint button. On Your Own Machine If you d like to run the code from your local machine, you can download a template for this lab at: http://www.science.smith.edu/~jcrouser/sds235/demos/lab-02.zip. 1 This lab is based on the Let s Make a Bar Chart tutorial series by Mike Bostock, creator of d3.js. The original tutorial series can be accessed at http://bost.ocks.org/mike/bar/. 1

Upzip the archive, and navigate to the lab-02 directory. To be able to load external data, you ll need to set up a web server, which you can do using the following command: $ python -m SimpleHTTPServer 8888 & This starts up a simple web server running on port 8888 on your machine. To access the page, point your browser to http://localhost:8888/lab-02.html (which will be blank to start with). Whenever you edit lab-02.html, just refresh the page to see your changes; you don t need to restart the server every time. 1 Creating a simple Bar Chart Let s start off by creating a small array of numeric data: 1 var data = [, 8, 15, 16, 23, 2]; If we wanted to create a basic bar chart by hand, we could do something like this: 1 < div class= " chart "> 2 < div style= " width: 0 px; "></ div > 3 < div style= " width: 80 px; ">8</ div > < div style= " width: 150 px; ">15</ div > 5 < div style= " width: 160 px; ">16</ div > 6 < div style= " width: 230 px; ">23</ div > 7 < div style= " width: 20 px; ">2</ div > 8 </ div > And then style the chart using CSS like this: 1. chart div { 2 font: 10 px sans- serif; 3 background- color: steelblue; text- align: right; 5 padding: 3 px; 6 margin: 1 px; 7 color: white; 8 } Together, that produces: 2

Because we only have a handful of data points, we could feasibly create 6 individual <div> elements one at a time. For most datasets, this would be impractical; we d like to instead be able to create charts from data automatically. 1.1 Getting started with Selections So now let s try to create an identical chart using D3, starting with an empty page that contains only a <div> of class.chart: 1 < div class= " chart "></ div > And a JavaScript array containing the data: 1 var data = [, 8, 15, 16, 23, 2]; Rather than dealing with elements one at a time, D3 (along with jquery and other libraries) instead handles groups of related elements called selections. We can use a class selector to grab the chart container like this: 1 var chart = d3.select (". chart "); 1.2 Joining Data to a Selection Next, we ll want to initiate the data join by defining the selection to which we will join data: 1 var bar = chart.selectall (" div "); Notice that the chart doesn t currently contain any <div> elements. That s okay! We re about to create them. You can think of the initial selection as a declaration of the elements you want to exist. 2 Let s join the data array to the selection using the.data() function: 1 var barupdate = bar. data ( data ); 1.3 Creating missing elements on.enter() Since there original selection was empty, the returned update and exit selections are also empty. We need only handle the enter selection, which represents new data for which there was no existing element. We can create these missing elements by appending to the enter selection: 1 var barenter = barupdate.enter (). append (" div "); Because these elements were created using a data join, each bar is already bound to data. This means that if we want to set the width of each new bar proportional to the associated data value, we can reference the data for each element: 1 barenter.style (" width ", function (d) { return d * 10 + "px"; }); 2 For more detail, see Thinking with Joins by M. Bostock, available at http://bost.ocks.org/mike/join/. 3

We can use a similar function to set the text content of each bar and make a label: 1 barenter.text ( function (d) { return d; }); To clean this up, we can chain together all the methods from Sections 1.1 to 1.3, which would look something like this: 1 d3. select (". chart ") // Select the container 2. selectall (" div ") // Select the elements we want to bind to 3. data ( data ) // Bind the data to the selection. enter (). append (" div ") // Append / style new elements 5. style (" width ", function (d) { return d * 10 + "px"; }) 6. text ( function (d) { return d; }); 1. Using Scales One weakness of the code we ve written so far is its dependence on the magic number 10, which we used to scale each bar to the appropriate pixel width. But what if we wanted to make our chart a bit larger, say 30px? That would be a pain to calculate manually. We can eliminate the magic number problem by using scales, which allow us to specify a mapping from data space (domain) to display space (range): 1 var x = d3. scale. linear () 2. domain ([0, d3.max ( data )]) 3. range ([0, 30]) ; We can now use x as a function, which will return the appropriate value in the range for any given data value in the domain. For example, an input value of returns 0, and an input value of 16 returns 160. To use the new scale, just replace the hard-coded multiplication by calling the scale() function: 1 d3.select (". chart ") 2. selectall (" div ") 3. data ( data ). enter (). append (" div ") 5. style (" width ", function (d) { return x(d) + "px"; }) // Ta-da! 6. text ( function (d) { return d; }); Great! Now you should (again) have something that looks like this:

2 Working with SVGs One problem of working with pure HTML is that we re pretty much restricted to rectangular shapes. While this is sufficient for building a bar chart, there are other visualizations that will need more complex primitives like Bézier curves, gradients, clipping and masks. For that, we ll need to use Scalable Vector Graphics, or SVGs. 2.1 Getting started We can start with the same basic setup we had in pt. 1, replacing the empty <div> with an <svg>: 1 <svg class= " chart "></svg> And of course we ll still need our data array: 1 var data = [, 8, 15, 16, 23, 2]; In the style section, we ll want to break out the styling for the rect and text elements, as they will be defined separately when using SVG: 1. chart rect { 2 fill: steelblue; 3 } 5. chart text { 6 fill: white; 7 font: 10 px sans- serif; 8 text- anchor: end; 9 } Now let s set up the dimensions of the <svg>, and define our scale: 1 var width = 20, 2 barheight = 20; 3 var x = d3. scale. linear () 5. domain ([0, d3.max ( data )]) 6. range ([0, width ]); 7 8 var chart = d3.select (". chart ") 9. attr (" width ", width ) 10. attr (" height ", barheight * data.length ); 2.2 Joining data to SVG elements Now we d like to bind the data to SVG group or <g> objects, which will eventually contain the rect and text for each bar. While we re at it, let s put each bar in the right place as well: 5

1 var bar = chart.selectall ("g") 2. data ( data ) 3. enter (). append ("g"). attr (" transform ", function (d, i) { return " translate (0," + i * barheight + ")"; }); Remember that all the new <g> objects we just created are now accessible from bar. For each bar, we want to append an appropriately-sized rect and an appropriately-positioned text label: 1 bar.append (" rect ") 2. attr (" width ", x) 3. attr (" height ", barheight - 1); 5 bar.append (" text ") 6. attr ("x", function (d) { return x(d) - 3; }) 7. attr ("y", barheight / 2) 8. attr ("dy", ".35 em") 9. text ( function (d) { return d; }); If all goes well, we should be... right back to where we started: 3 Creating charts from real data Now that you ve built a basic bar chart in HTML and then in SVG, let s polish up the display by rotating the chart into columns and adding axes. We ll also switch to using a real dataset, showing the relative frequency of letters appearing in the English language. 3 The data in tabular form looks something like this: letter frequency A.08167 B.0192 C.02782 D.0253.. 3 If you re using JSFiddle, you ll need to copy/paste the the data into a JavaScript variable called data. The dataset can be found at: http://www.science.smith.edu/~jcrouser/sds235/demos/lab-02/data.json 6

3.1 Getting started Let s start again with just an empty <svg>: 1 <svg class= " chart "></svg> To give us room to some axes, we can add margins and move the container over a bit: 1 var margin = { top: 20, right: 20, bottom: 30, left: 0}, 2 width = 960 - margin. left - margin. right, 3 height = 500 - margin. top - margin. bottom; 5 var chart = d3.select (". chart ") 6. attr (" width ", width + margin. left + margin. right ) 7. attr (" height ", height + margin. top + margin. bottom ) 8. append ("g") 9. attr (" transform ", " translate (" + margin.left + "," + margin.top + ")"); We d like our chart to be oriented vertically this time, so we ll need a scale for our y values: 1 var y = d3. scale. linear () 2. range ([ height, 0]) ; // Remember: up/ down are flipped! The x axis is a little trickier, since we are now working with ordinal values. Luckily, D3 has ordinal scales as well, and we can use the.rangebands() function to split the range up into bins: 1 var x = d3. scale. ordinal () 2. rangebands ([0, width ],.1) ; // The last parameter adds padding 3.2 Loading external data You ll notice that we haven t included a domain on either or our scales. This is because we haven t loaded the data yet. Let s do that now using the.tsv() function: 1 d3.tsv (" data.tsv ", function ( error, data ) { 2 // We ' ll fill this in shortly 3 }); Every operation that depends on that data will go inside this function. Otherwise, because JavaScript is asynchronous, the operation might try to execute before the data is done loading. Once the data loads, we can add our domains: 1 d3.tsv (" data.tsv ", function ( error, data ) { 2 3 x.domain ( data.map ( function (d) { return d.letter; })); y.domain ([0, d3.max (data, function (d) { return d.frequency; }) ]); 5 6 // Remember to add all data- dependent calls in here! 7 8 }); 7

3.3 Adding axes Now we can build our axes: 1 var xaxis = d3. svg. axis () 2. scale (x) 3. orient (" bottom "); // Positions the labels under the xaxis 5 var yaxis = d3. svg. axis () 6. scale (y) 7. orient (" left "); // Positions the labels to the left of the yaxis We can add the axes to the chart using the.append() function. Let s start with the x axis, using the.translate() transform to move it to the bottom of the SVG.: 1 chart.append ("g") 2. attr (" class ", "x axis ") 3. attr (" transform ", " translate (0," + height + ")"). call ( xaxis ); For good measure, let s add in our CSS styling too: 1. bar { fill: steelblue; } 2 3. axis text { font: 10 px sans- serif; } 5. axis path, 6. axis line { 7 fill: none; 8 stroke: #000; 9 shape- rendering: crispedges; 10 } 11 12. x. axis path { display: none; } Great! That gave us: And now the y axis, which by default is positioned on the lefthand side of the SVG: 1 chart.append ("g") 2. attr (" class ", "y axis ") 3. call ( yaxis ); 8

Now we ve got: 3. Finishing touches Finally, we ll want to add in the bars for each letter: 1 chart.selectall (". bar ") 2. data ( data ) 3. enter (). append (" rect "). attr (" class ", " bar ") 5. attr ("x", function (d) { return x( d.letter ); }) 6. attr ("y", function (d) { return y( d.frequency ); }) 7. attr (" height ", function (d) { return height - y( d.frequency ); }) 8. attr (" width ", x.rangeband ()); And perhaps add a title, centered at the top of the chart: 1 chart.append (" text ") 2. attr ("x", ( width / 2)) 3. attr ("y", 0 + ( margin.top / 2)). attr (" text-anchor ", " middle ") 5. text (" Relative Frequency of Letters in the English Alphabet "); 9

All together, this should result in something that looks like this: And there you have it! You ve built your first data visualization using D3. Want to learn more? Check out: for documentation and tutorials. https://github.com/mbostock/d3/wiki Looking for examples of cool stuff other people have done with D3? Try the galleries at: https://github.com/mbostock/d3/wiki/gallery http://christopheviau.com/d3list/gallery.html http://c3js.org/examples.html 10