TUTORIAL: D3 (1) Basics. Christoph Kralj Manfred Klaffenböck

Similar documents
D3js Tutorial. Tom Torsney-Weir Michael Trosin

About the Tutorial. Audience. Prerequisites. Copyright and Disclaimer. D3.js

Scalable Vector Graphics (SVG) vector image World Wide Web Consortium (W3C) defined with XML searched indexed scripted compressed Mozilla Firefox

Introduction to WEB PROGRAMMING

D3 Introduction. Gracie Young and Vera Lin. Slides adapted from

The Structure of the Web. Jim and Matthew

Data Visualization (DSC 530/CIS )

IAT 355 : Lab 01. Web Basics

Generating Page Elements. CHAPTER 5 Data

Data Visualization (DSC 530/CIS )

HTML5 - SVG. SVG is mostly useful for vector type diagrams like Pie charts, Two-dimensional graphs in an X,Y coordinate system etc.

Default Parameters and Shapes. Lecture 18

Data Visualization on the Web with D3

1. More jquery Methods 2. JavaScript + SVG: Raphaël 3. About SVG 4. Working with SVG 5. Animating SVG

Learning to Code with SVG

Data Visualization (CIS 468)

HTML5. HTML5 Introduction. Form Input Types. Semantic Elements. Form Attributes. Form Elements. Month Number Range Search Tel Url Time Week

Week 8 Google Maps. This week you ll learn how to embed a Google Map into a web page and add custom markers with custom labels.

Lecture Topic Projects

Data Visualization (DSC 530/CIS )

Chapter 13 HTML5 Functions

HCDE 530: Computational Techniques for HCDE Data Visualization in Web, Part 2

Scalable Vector Graphics SVG

Adobe Dreamweaver CS5/6: Learning the Tools

Visualization Process

Data Visualization (CIS/DSC 468)

c122jan2714.notebook January 27, 2014

An Brief Introduction to a web browser's Document Object Model (DOM) Lecture 20 COMP110 Spring 2018

CS7026 CSS3. CSS3 Graphics Effects

COMSC-031 Web Site Development- Part 2

Scalable Vector Graphics commonly known as SVG is a XML based format to draw vector images. It is used to draw twodimentional vector images.

Client Side JavaScript and AJAX

Visualization Process

An Brief Introduction to a web browser's Document Object Model (DOM) Lecture 24

16. HTML5, HTML Graphics, & HTML Media 웹프로그래밍 2016 년 1 학기 충남대학교컴퓨터공학과

Anatomy of an HTML document

jquery Tutorial for Beginners: Nothing But the Goods

MTA EXAM HTML5 Application Development Fundamentals

WELCOME TO JQUERY PROGRAMMING LANGUAGE ONLINE TUTORIAL


Time series in html Canvas

Using Dreamweaver CC. Logo. 4 Creating a Template. Page Heading. Page content in this area. About Us Gallery Ordering Contact Us Links

This is the vector graphics "drawing" technology with its technical and creative beauty. SVG Inkscape vectors

Dreamweaver Domain 5: Organizing Content by Using Dreamweaver CS5

HTML5, CSS3, JQUERY SYLLABUS

Unit 5 Web Publishing Systems Page 1 of 13 Part 4 HTML Part 4

Frontend guide. Everything you need to know about HTML, CSS, JavaScript and DOM. Dejan V Čančarević

A Brief Introduction to a web browser's HTML. and the Document Object Model (DOM) Lecture 28

CS474 MULTIMEDIA TECHNOLOGY

How to set up a local root folder and site structure

Tutorial 1: HTML Tutorial (Windows Version) HTML (Hypertext Markup Language) Basics Objectives

Index. grouped (see Grouped bar chart) horizontal, jqplot library, 128 stacked bar charts (see Stacked bar charts) Bubble chart,

The figure below shows the Dreamweaver Interface.

Flash Image Enhancer Manual DMXzone.com Flash Image Enhancer Manual

c122sep814.notebook September 08, 2014 All assignments should be sent to Backup please send a cc to this address

PHP,HTML5, CSS3, JQUERY SYLLABUS

pysvg Tutorial Lets start with the typical "hello world" which I have already packed into a method.

Web Programming 1 Packet #5: Canvas and JavaScript

Lecture Topic Projects 1 Intro, schedule, and logistics 2 Applications of visual analytics, data types 3 Data sources and preparation Project 1 out 4

CISC 1600, Lab 2.1: Processing

How the Internet Works

It is possible to create webpages without knowing anything about the HTML source behind the page.

Using Dreamweaver CS6

Introducing Cascading Style Sheets. Cascading Style Sheet Basics Creating Styles Using Styles Manipulating Styles Text Formatting with CSS

UNIT 3 SECTION 1 Answer the following questions Q.1: What is an editor? editor editor Q.2: What do you understand by a web browser?

Downloads: Google Chrome Browser (Free) - Adobe Brackets (Free) -

JQuery WHY DIDN T WE LEARN THIS EARLIER??!

c122sep2914.notebook September 29, 2014

HTML/CSS Lesson Plans

HTML+ CSS PRINCIPLES. Getting started with web design the right way

How to lay out a web page with CSS

KillTest *KIJGT 3WCNKV[ $GVVGT 5GTXKEG Q&A NZZV ]]] QORRZKYZ IUS =K ULLKX LXKK [VJGZK YKX\OIK LUX UTK _KGX

Make a Website. A complex guide to building a website through continuing the fundamentals of HTML & CSS. Created by Michael Parekh 1

Best Practices Chapter 5

Welcome to Book Display Widgets

The World Wide Web is a technology beast. If you have read this book s

Data Visualization (DSC 530/CIS )

How to lay out a web page with CSS

Web Site Design and Development. CS 0134 Fall 2018 Tues and Thurs 1:00 2:15PM

LivingSlides. Extended Abstract. Joana Borges Pereira

Dreamweaver Basics. Planning your website Organize site structure Plan site design & navigation Gather your assets

CHAPTER 1 COPYRIGHTED MATERIAL. Finding Your Way in the Inventor Interface

Create web pages in HTML with a text editor, following the rules of XHTML syntax and using appropriate HTML tags Create a web page that includes

Study Guide 2 - HTML and CSS - Chap. 6,8,10,11,12 Name - Alexia Bernardo

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

CSCU9B2 Practical 1: Introduction to HTML 5

Animating Layers with Timelines

Part 1: jquery & History of DOM Scripting

Configuration Guide For The Online Converter

Web Development and HTML. Shan-Hung Wu CS, NTHU

WebKit ; FOR : DUMMIES. by Chris Minnick WILEY. John Wiley & Sons, Inc.

Creating your first website Part 4: Formatting your page with CSS

HTML & CSS. Rupayan Neogy

ITEC447 Web Projects CHAPTER 9 FORMS 1

UI Course HTML: (Html, CSS, JavaScript, JQuery, Bootstrap, AngularJS) Introduction. The World Wide Web (WWW) and history of HTML

Dreamweaver Basics Outline

Tutorial: Overview. CHAPTER 2 Tutorial

Announcements. 1. Class webpage: Have you been reading the announcements? Lecture slides and coding examples will be posted

Using Dreamweaver CC. 3 Basic Page Editing. Planning. Viewing Different Design Styles

Maurizio Tesconi 24 marzo 2015

Transcription:

TUTORIAL: D3 (1) Basics Christoph Kralj christoph.kralj@univie.ac.at Manfred Klaffenböck manfred.klaffenboeck@univie.ac.at

Overview Our goal is to create interactive visualizations viewable in your, or anyone s, web browser. To get content into the browser we will use HTML documents, to make them interactive we will include JavaScript in these documents. Finally, to tend to our graphical needs, we will use D3, a JavaScript library, for creating visualizations. Some examples of what is possible with D3 can be found here: http://bl.ocks.org/mbostock http://christopheviau.com/d3list/ Be aware: in 2016, d3.v4 came out, which introduced some changes which are not backwardscompatible towards d3.v3. Please keep that in mind when you browse for examples on the internet. In this tutorial, we are going to use D3-V4. Contents Overview... 1 HTML (Hypertext Markup Language)... 2 IDs and Classes... 3 SVG (Scalable Vector Graphics)... 4 D3... 6 select() and selectall()... 6 append()... 6 attr() and style()... 7 Excursus: Debugging... 7 data()... 8 enter()... 8 Anonymous functions... 9 Storing Elements... 9 csv() (loading data)... 10 Conversion... 10 Acknowledgements... 11 1

HTML (Hypertext Markup Language) Most of what we well be doing today will happen in a HTML document. HTML uses tags to structure content and define how they are presented. Usually they look somewhat like: <tagname> content </tagname> Tags affect the content written between the tag opening and the tag closing (identifiable by the /"). Common tags are for example <b></b> for bold text, <i></i> for italic text, or <p></p> for paragraphs. Every HTML documents needs to include some boilerplate code: <!DOCTYPE HTML> <html lang="en"> <head> <meta charset="utf-8"/> <title></title> </head> <body> </body> </html> Most things will happen within the body tag. Mini-Exercise Now create a HTML file including some text in the body utilizing at least one tag. To do so: Create a new file on your computer Give it the ending.html Paste the boilerplate code Fill in some content See if it works in your browser 2

IDs and Classes Later in this tutorial we want to be able to address specific elements or element groups. To achieve this we can assign attributes to tags that give them a certain identity. Attributes are defined in the first part of the tag, after its name. <div id="myid">this div has a unique ID. No other element can have the same. </div> <div class="myclass">this div has member of a class. The class can be shared by multiple elements. </div> The name of the class or ID is a string that can be decided by you, the developer. A list of tags and their effect can be found here: https://developer.mozilla.org/en-us/docs/web/html/element More information regarding HTML is available here: http://www.w3schools.com/html/ 3

SVG (Scalable Vector Graphics) SVGs are part of the HTML5 standard and provide us with the means of creating graphical elements that can be displayed in the browser. A main concept about SVGs is that we do not define our graphical elements pixel by pixel but rather what element we want, e.g. a circle, and what properties it should have, e.g. size, color, One advantage of this approach is that resizing of the elements does not decrease their quality. The coordinate system of SVGs has its origin at the top left corner and has its x axis to the right and its y axis downwards. x y SVG Another important thing to note is that the depth ordering of elements is defined by the order in which they are drawn, superimposing the new over the old ones. To use SVGs in we first have to define an SVG element, and give it a size: <svg width="400" height="200"> </svg> You can think of this as a canvas. Within the svg tag we can define graphical elements, e.g. circles, ellipses, rectangles, lines, text, For example: <svg width="400" height="200"> <rect x="0" y="0" width="75" height="75" fill="yellow" /> <line x1="0" y1="0" x2="400" y2="200" stroke="black" strokewidth="3" /> </svg> <ellipse cx="250" cy="150" rx="30" ry="50" fill="red" /> <text x="100" y="100" fill="green">sample text</text> 4

Results in: A list of SVG elements can be found here: https://developer.mozilla.org/en-us/docs/web/svg/element 5

D3 To dynamically add SVG shapes we will use JavaScript and D3. First we have to load the D3 library. To do that: Download d3: https://d3js.org/ Unzip the folder Take d3.min.js and put it in the same folder you.html file resides in. Now you can load the d3 library from within your HTML document by simply adding the following line: <script src="d3.min.js"></script> We can now use D3 from within a script tag. This library offers a wealth of methods to dynamically modify the HTML document, and provides the d3 object which is needed to call them. select() and selectall() To choose which objects to manipulate, D3 provides the select("myselector") method, which returns the first element in your document to match the selector, and the selectall("myselector") method that returns all elements matching the selector. You can select elements by various criteria, the most important ones being by tag, by class, and by id (we have discussed class and id earlier): d3.select("div") <!-- Targets the first "div" tag --> d3.select(".class1") <!-- Targets the first tag with the class "class1" --> d3.select("#myid") <!-- Targets the tag with the id "myid" --> More about select here: https://github.com/mbostock/d3/wiki/selections append() Another important method is append("elementtoappend"). This method can be called on the return value of the select() or selectall() and simply adds new elements as children to the selected nodes. The parameter we pass to the append() method is a string defining the type of element we want to add. E.g.: div, p, svg, a, h1, Some elements might be addressed via their namespace-prefix, e.g.: svg:circle More about append here: https://github.com/mbostock/d3/wiki/selections#append 6

We can now, for example, append a new div and fill it with text: <script> var body = d3.select("body"); var mydiv = body.append("div"); mydiv.text("hello World!"); </script> JavaScript allows us to do that in an even more compact way, via method chaining. This enables us to call multiple functions (on the same object) consecutively: d3.select("body").append("div").text("hello World!"); Method calls are connected with periods and the output of each method is used as the input for the next one. attr() and style() Each element you select can be modified with certain attributes, like class, id, etc., and CSS styles, like color, stroke, opacity, etc. To set these values D3 offers the.attr(name, value) and the.style(name, value): Fr example we can give all our divs the same class and change their text color: d3.selectall("div").attr("class","mydivclass"); d3.selectall(".mydivclass").style("color","orange"); More about.attr(): https://github.com/mbostock/d3/wiki/selections#attr More about.style(): https://github.com/mbostock/d3/wiki/selections#style A list of styles: http://www.w3schools.com/cssref/default.asp Excursus: Debugging Something very helpful when working with HTML and JavaScript are the inspection and debugging capabilities of your browser. Those differ from browser to browser, but the most popular ones all have the same basic tools to aid you. For example in Firefox: The Inspector (Extras -> Web-Developer -> Inspector) lets you look at the current elements on your page. 7

This is helpful as it allows you to see if elements have been successfully created and what attributes they have. If you cannot see an element that should be there, it might be that it has been created, but some attributes area not defined or set to values that make the object not appear on the screen. The Console (Extras -> Web-Developer -> Inspector) displays you error messages and warnings that might occur in your JavaScript part. If you prefer a different browser, it should be easy to find the pendants to these tools by googling. Exercise 1.1 In your HTML file: Include D3 Using HTML: Create an SVG Using d3: Add a blue circle of radius 60 to your SVG and make sure it s completely visible Using d3: Add a paragraph containing green text to your document all in one line. data() For data visualization it is, of course, necessary to be able to access data. For now we will work with static data we hard-code in our HTML file. Inside the script tag, define a JavaScript array: var data = [1, 5, 15, 20, 25]; We can bind this data to HTML elements using the.data(data) method after the selection. d3.select("body").selectall("p").data(data).text("sample text"); So now if we have the same number of items in our array as we have paragraphs, each of the paragraphs get its text set. enter() Usually we don t want to create our elements beforehand so that they match the number of data-items in our array. That s why d3 provides the.enter() method that tells the browser what to do if there are not enough HTML-elements for the number of data items. (.enter() will be explained more thoroughly in the next tutorial) 8

Consequently, we can use the following code to create paragraphs on the fly if we do not already have enough in our document. d3.select("body").selectall("p").data(data).enter().append("p").text("sample text"); This is an important concept in D3, as it lets us select elements which do not yet exist. Anonymous functions But we also want our changes to be dependent on the different data-items. For this we can use functions, a concept you should be familiar with from other languages. In D3 we often use anonymous functions that only differ from normal functions by not having a name, and which thus cannot be reused. d3.select("body").selectall("div").data(data).enter().append("div").text( function(d){ return d; }); The parameter d is provided as first argument to anonymous functions by D3 and holds the data elements. Optionally, you can also pass the index of the data-item as second parameter to your functions. Storing Elements Of course you can also store the elements you have created in variables so you can access them even easier than with selections. For your assignments, you will probably want to save the SVG in a variable. var mysvg = d3.select("body").append("svg"); Exercise 1.2 Create an array of at least 4 numbers Create an SVG element and store it in a variable For each data-item in your array create an SVG-shape with its position being a function of the index of the respective data-item and its size depending on the value of the data-item. Check your results in the browser and make sure that all circles are at least partially visible. 9

csv() (loading data) D3 also allows us to load real data in various formats; in this tutorial we will use a.csv file. D3 provides the.csv("datafile.csv", function(data){ }) method. It takes two parameters, a string with the path to the file, and an aonymous callback function. d3.csv("fruit.csv", function(data) { console.log(data); }); D3 loads data asynchronously, so that interaction with the browser is not hampered while loading (scripts not related to the data can still run in the meantime). To signal that the loading is complete, D3 calls the callback function. This is an important concept, as it means for developers that code which depends on the data should only exist in the callback function. Conversion D3 loads all our data as strings, so we have to convert it to numbers to work with it. This can be done with the "+" operator. data.price = +data.price; But as this has to be done for every item in the data set, you have to iterate over all items, either by using a loop or with the following statement: data.foreach(function(d){ d.price = +d.price }); Exercise 1.3 Download the data http://vda.univie.ac.at/teaching/vis/17w/data/fruit.csv Load the data Convert the price to numbers Create a simple barchart with the bar length corresponding to the price, each bar labelled by the fruit name and the bar color encoding the pit-type. In order to solve this problem, you will need the following two code-snippets: 10

//Get max price var datamax = d3.max(data, function(d){ return d.price; //We only evaluate the height. }); // Create scale to map price to bar length. var barscale = d3.scalelinear().domain([0, datamax]).range([0, barsheight]); They both contain concepts which we haven't talked about yet. They will be explained in further detail in the next tutorial. For now, please just take them for granted. Acknowledgements Many thanks to Michael Opperman for all the help and material he provided. 11