SlideShare une entreprise Scribd logo
1  sur  23
Télécharger pour lire hors ligne
D3 BASIC TUTORIAL
Introduction, installation and simple examples.
2016.10@hijiangtao
MENU
D3 Introduction and demo
Installing, IDE, etc
Coding: visualize your rst
chart
More
D3 INTRODUCTION AND DEMO
D3 INTRODUCTION
D3 (or D3.js) is a JavaScript library for visualizing data
using web standards.
D3 helps you bring data to life using SVG, Canvas and
HTML.
D3 combines powerful visualization and interaction
techniques with a data-driven approach to DOM
manipulation, giving you the full capabilities of modern
browsers and the freedom to design the right visual
interface for your data.
D3JS.ORG
ONLINE GALLERY
Charts:
Tree, Chord, and Sankey:
Networks:
Maps:
Trending:
Population Pyramid
Sankey Diagrams
Collapsible Force Layout
Tokyo Wind Map
World Bank Global Development
Sprint
HTML, SVG AND CANVAS
HTML: Hyper Text Markup Language, is a markup language
for describing web documents (web pages)
SVG: Scalable Vector Graphics. SVG de nes vector-based
graphics in XML format
Canvas: The <canvas>element is only a container for
graphics. You must use a JavaScript to actually draw the
graphics. Canvas has several methods for drawing paths,
boxes, circles, text, and adding images
INSTALLING, IDE, ETC
INSTALLING
// Use a script tag to include library
// d3 4.0 standard version 
<script src="https://d3js.org/d3.v4.js"></script>
// d3 4.0 minified version
<script src="https://d3js.org/d3.v4.min.js"></script>
// d3 3.x version
<script src="//d3js.org/d3.v3.js"></script>
// Import D3 into an application
npm install d3 // installing command
import * as d3 from "d3"; // use in application
// include standalone D3 microlibraries
<script src="https://d3js.org/d3­selection.v1.js"></script>
// or
import {scaleLinear} from "d3­scale";
INSTALLING
API: ( , , ,
), , , , ( ,
...
After include the library into your workspace, you can use
namespace d3 to visualize your data, such as:
Arrays Statistics Search Transformations
Histograms Axes Brushes Chords Collections Objects
Maps
d3.select("body").append("p").text("New paragraph!");
IDE
Microsoft Notepad
: A sophisticated text editor for code, markup
and prose
: The smartest JavaScript IDE
: Capable and Ergonomic Java * IDE
: Eclipse IDE for JavaScript Web Developers
: A hackable text editor
: A free source code editor
Sublime Text
Webstorm
IntelliJ IDEA
Eclipse
Atom
Notepad++
SERVER CONFIGURATION
: WAMP, LAMP, MAMP and XAMPP
: A JavaScript runtime built on Chrome's V8
JavaScript engine
: A platform-independent, Java-centric environment
: SimpleHTTPServer, Simple HTTP request handler
in python 2
Others
Apache
Node.js
J2EE
Python
4 STEPS TO VISUALIZE YOUR FIRST CHART
Construct a simple bar chart from TSV le
0. DATA
1. A HTML TEMPLATE
<html>
<head>
<meta charset="utf­8">
<title>D3 Tutorial</title>
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
.bar {
  fill: steelblue;
}
.bar:hover {
  fill: brown;
}
.axis­­x path {
  display: none;
}
</style>
</head>
2. INITIAL SVG AND SCALE
// append a svg element and define margins
var svg = d3.select("#barchart")
.append("svg")
.attr("width", 960)
.attr("height", 500),
    margin = {top: 20, right: 20, bottom: 30, left: 40},
    width = +svg.attr("width") ­ margin.left ­ margin.right,
    height = +svg.attr("height") ­ margin.top ­ margin.bottom;
// Constructs a new band scale (x) and a new continuous scale (y)
var x = d3.scaleBand().rangeRound([0, width]).padding(0.1),
    y = d3.scaleLinear().rangeRound([height, 0]);
// append g element
var transStr = "translate(" + margin.left + "," + margin.top + ")";
var g = svg.append("g")
    .attr("transform", transStr);
3. LOAD AND FORMAT DATA
// d3 tsv Function
// Creates a request for the TSV file at the specified url 
// with the default mime type text/tab­separated­values
d3.tsv(url, row, callback);
d3.tsv("d3­tutorial­tsv­data.tsv", function(d) {
  d.frequency = +d.frequency;
  return d;
}, barchartCallback);
Requests (d3-request)
- get a comma-separated values (CSV) le.
- get an HTML le.
- get a JSON le.
- get a plain text le.
- get a tab-separated values (TSV) le.
- get an XML le.
d3.csv
d3.html
d3.json
d3.text
d3.tsv
d3.xml
4. BIND DATA AND UPDATE ELEMENT
function barchartCallback(error, data) {
  if (error) throw error;
  // Given a value from the domain
  // returns the corresponding value from the range.
  x.domain(data.map(function(d) { 
   return d.letter; 
  }));
  y.domain([0, d3.max(data, function(d) { 
   return d.frequency; 
  })]);
  // x axis
  g.append("g")
      .attr("class", "axis axis­­x")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x));
  
5. COMPLETE CODES
<html>
<head>
  <meta charset="utf­8">
  <title>D3 Tutorial</title>
  <script src="https://d3js.org/d3.v4.min.js"></script>
  <style>
  .bar {
    fill: steelblue;
  }
  .bar:hover {
    fill: brown;
  }
  .axis­­x path {
    display: none;
  }
  </style>
</head>
6. RESULT
MORE
Be careful about the API's differences between 4.0 and 3.x
Be careful about async loading in JavaScript
Make good use of Google, Stack Over ow, etc.
MORE
, Mike Dewar, O'Reilly Media, June
2012
, 吕之华
, 陈为,沈则潜,陶煜波等著
/ English Version
D3 WIKI
D3 API Reference
D3 Gallery
Mike's Blog
Tutorials
Getting Started with D3
精通D3.js:交互式数据可视化高级编程
数据可视化
JavaScript高级程序设计(第3版)
Professional JavaScript for Web Developers 3rd Edition
THE END
@hijiangtao
-
-
-
d3js.org
VIS Course (2016 Fall) Wiki
Joe's Blog

Contenu connexe

Tendances

Mongo db washington dc 2014
Mongo db washington dc 2014Mongo db washington dc 2014
Mongo db washington dc 2014
ikanow
 
HTML5 and SVG
HTML5 and SVGHTML5 and SVG
HTML5 and SVG
yarcub
 
Android 2D Drawing and Animation Framework
Android 2D Drawing and Animation FrameworkAndroid 2D Drawing and Animation Framework
Android 2D Drawing and Animation Framework
Jussi Pohjolainen
 

Tendances (20)

D3.js: Data Visualization for the Web
D3.js: Data Visualization for the Web D3.js: Data Visualization for the Web
D3.js: Data Visualization for the Web
 
SVGD3Angular2React
SVGD3Angular2ReactSVGD3Angular2React
SVGD3Angular2React
 
Learning d3
Learning d3Learning d3
Learning d3
 
Introduction to data visualisation with D3
Introduction to data visualisation with D3Introduction to data visualisation with D3
Introduction to data visualisation with D3
 
Mongo db washington dc 2014
Mongo db washington dc 2014Mongo db washington dc 2014
Mongo db washington dc 2014
 
Utahjs D3
Utahjs D3Utahjs D3
Utahjs D3
 
SVG - Scalable Vector Graphics
SVG - Scalable Vector GraphicsSVG - Scalable Vector Graphics
SVG - Scalable Vector Graphics
 
D3 js
D3 jsD3 js
D3 js
 
HTML5 and SVG
HTML5 and SVGHTML5 and SVG
HTML5 and SVG
 
SenchaCon 2016: Add Magic to Your Ext JS Apps with D3 Visualizations - Vitaly...
SenchaCon 2016: Add Magic to Your Ext JS Apps with D3 Visualizations - Vitaly...SenchaCon 2016: Add Magic to Your Ext JS Apps with D3 Visualizations - Vitaly...
SenchaCon 2016: Add Magic to Your Ext JS Apps with D3 Visualizations - Vitaly...
 
Android 2D Drawing and Animation Framework
Android 2D Drawing and Animation FrameworkAndroid 2D Drawing and Animation Framework
Android 2D Drawing and Animation Framework
 
Dive into HTML5: SVG and Canvas
Dive into HTML5: SVG and CanvasDive into HTML5: SVG and Canvas
Dive into HTML5: SVG and Canvas
 
Svg
SvgSvg
Svg
 
Html5
Html5Html5
Html5
 
SenchaCon 2016: Handle Real-World Data with Confidence - Fredric Berling
SenchaCon 2016: Handle Real-World Data with Confidence - Fredric Berling SenchaCon 2016: Handle Real-World Data with Confidence - Fredric Berling
SenchaCon 2016: Handle Real-World Data with Confidence - Fredric Berling
 
SVG - Scalable Vector Graphic
SVG - Scalable Vector GraphicSVG - Scalable Vector Graphic
SVG - Scalable Vector Graphic
 
Apache doris (incubating) introduction
Apache doris (incubating) introductionApache doris (incubating) introduction
Apache doris (incubating) introduction
 
Html5
Html5Html5
Html5
 
Java/Scala Lab: Борис Трофимов - Обжигающая Big Data.
Java/Scala Lab: Борис Трофимов - Обжигающая Big Data.Java/Scala Lab: Борис Трофимов - Обжигающая Big Data.
Java/Scala Lab: Борис Трофимов - Обжигающая Big Data.
 
Introduction to D3
Introduction to D3 Introduction to D3
Introduction to D3
 

En vedette

Translating Big Raw Data Into Small Actionable Information
Translating Big Raw Data Into Small Actionable InformationTranslating Big Raw Data Into Small Actionable Information
Translating Big Raw Data Into Small Actionable Information
Alan McSweeney
 
Solution Architecture Concept Workshop
Solution Architecture Concept WorkshopSolution Architecture Concept Workshop
Solution Architecture Concept Workshop
Alan McSweeney
 
Forget Big Data. It's All About Smart Data
Forget Big Data. It's All About Smart DataForget Big Data. It's All About Smart Data
Forget Big Data. It's All About Smart Data
Alan McSweeney
 

En vedette (20)

D3.js mindblow
D3.js mindblowD3.js mindblow
D3.js mindblow
 
D3.js - A picture is worth a thousand words
D3.js - A picture is worth a thousand wordsD3.js - A picture is worth a thousand words
D3.js - A picture is worth a thousand words
 
D3
D3D3
D3
 
My last three projects - wins and failures
My last three projects - wins and failuresMy last three projects - wins and failures
My last three projects - wins and failures
 
Times Ten in-memory database when time counts - Laszlo Ludas
Times Ten in-memory database when time counts - Laszlo LudasTimes Ten in-memory database when time counts - Laszlo Ludas
Times Ten in-memory database when time counts - Laszlo Ludas
 
The Craftsman Developer In An Agile World
The Craftsman Developer In An Agile WorldThe Craftsman Developer In An Agile World
The Craftsman Developer In An Agile World
 
Intro to D3: Data-Driven Documents
Intro to D3: Data-Driven DocumentsIntro to D3: Data-Driven Documents
Intro to D3: Data-Driven Documents
 
ASP.NET Core: The best of the new bits
ASP.NET Core: The best of the new bitsASP.NET Core: The best of the new bits
ASP.NET Core: The best of the new bits
 
Python for Big Data Analytics
Python for Big Data AnalyticsPython for Big Data Analytics
Python for Big Data Analytics
 
.Net Core
.Net Core.Net Core
.Net Core
 
ASP.NET Core 1.0 Overview
ASP.NET Core 1.0 OverviewASP.NET Core 1.0 Overview
ASP.NET Core 1.0 Overview
 
The Myth Of Requirements
The Myth Of RequirementsThe Myth Of Requirements
The Myth Of Requirements
 
Python in the Hadoop Ecosystem (Rock Health presentation)
Python in the Hadoop Ecosystem (Rock Health presentation)Python in the Hadoop Ecosystem (Rock Health presentation)
Python in the Hadoop Ecosystem (Rock Health presentation)
 
Translating Big Raw Data Into Small Actionable Information
Translating Big Raw Data Into Small Actionable InformationTranslating Big Raw Data Into Small Actionable Information
Translating Big Raw Data Into Small Actionable Information
 
Real Time Data Strategy and Architecture
Real Time Data Strategy and ArchitectureReal Time Data Strategy and Architecture
Real Time Data Strategy and Architecture
 
Process Oriented Architecture
Process Oriented ArchitectureProcess Oriented Architecture
Process Oriented Architecture
 
Introduction To Business Architecture – Part 1
Introduction To Business Architecture – Part 1Introduction To Business Architecture – Part 1
Introduction To Business Architecture – Part 1
 
Solution Architecture Concept Workshop
Solution Architecture Concept WorkshopSolution Architecture Concept Workshop
Solution Architecture Concept Workshop
 
Complexity and Solution Architecture
Complexity and Solution ArchitectureComplexity and Solution Architecture
Complexity and Solution Architecture
 
Forget Big Data. It's All About Smart Data
Forget Big Data. It's All About Smart DataForget Big Data. It's All About Smart Data
Forget Big Data. It's All About Smart Data
 

Similaire à D3 Basic Tutorial

PPT with Flash ry
PPT with Flash ryPPT with Flash ry
PPT with Flash ry
marina2207
 
WAD - WaveMaker tutorial
WAD - WaveMaker tutorial WAD - WaveMaker tutorial
WAD - WaveMaker tutorial
marina2207
 
WaveMaker tutorial with Flash
WaveMaker tutorial with FlashWaveMaker tutorial with Flash
WaveMaker tutorial with Flash
marina2207
 
Modern Architectures with Spring and JavaScript
Modern Architectures with Spring and JavaScriptModern Architectures with Spring and JavaScript
Modern Architectures with Spring and JavaScript
martinlippert
 
Node.JS| Coffeescript Presentation
Node.JS| Coffeescript PresentationNode.JS| Coffeescript Presentation
Node.JS| Coffeescript Presentation
Sam Frons
 
JAX 2012: Moderne Architektur mit Spring und JavaScript
JAX 2012: Moderne Architektur mit Spring und JavaScriptJAX 2012: Moderne Architektur mit Spring und JavaScript
JAX 2012: Moderne Architektur mit Spring und JavaScript
martinlippert
 
Fundamentals of Web building
Fundamentals of Web buildingFundamentals of Web building
Fundamentals of Web building
RC Morales
 

Similaire à D3 Basic Tutorial (20)

Intro to mobile web application development
Intro to mobile web application developmentIntro to mobile web application development
Intro to mobile web application development
 
WaveMaker Presentation
WaveMaker PresentationWaveMaker Presentation
WaveMaker Presentation
 
PPT with Flash ry
PPT with Flash ryPPT with Flash ry
PPT with Flash ry
 
WAD - WaveMaker tutorial
WAD - WaveMaker tutorial WAD - WaveMaker tutorial
WAD - WaveMaker tutorial
 
WaveMaker tutorial with Flash
WaveMaker tutorial with FlashWaveMaker tutorial with Flash
WaveMaker tutorial with Flash
 
JavaONE 2012 Using Java with HTML5 and CSS3
JavaONE 2012 Using Java with HTML5 and CSS3JavaONE 2012 Using Java with HTML5 and CSS3
JavaONE 2012 Using Java with HTML5 and CSS3
 
Super billing asp.net
Super billing   asp.netSuper billing   asp.net
Super billing asp.net
 
Survive JavaScript - Strategies and Tricks
Survive JavaScript - Strategies and TricksSurvive JavaScript - Strategies and Tricks
Survive JavaScript - Strategies and Tricks
 
Modern Architectures with Spring and JavaScript
Modern Architectures with Spring and JavaScriptModern Architectures with Spring and JavaScript
Modern Architectures with Spring and JavaScript
 
Making Of PHP Based Web Application
Making Of PHP Based Web ApplicationMaking Of PHP Based Web Application
Making Of PHP Based Web Application
 
Intro to .NET for Government Developers
Intro to .NET for Government DevelopersIntro to .NET for Government Developers
Intro to .NET for Government Developers
 
Node.JS| Coffeescript Presentation
Node.JS| Coffeescript PresentationNode.JS| Coffeescript Presentation
Node.JS| Coffeescript Presentation
 
Programming languages asp.net
Programming languages asp.netProgramming languages asp.net
Programming languages asp.net
 
HTML, CSS And JAVASCRIPT!
HTML, CSS And JAVASCRIPT!HTML, CSS And JAVASCRIPT!
HTML, CSS And JAVASCRIPT!
 
[2015/2016] HTML5 and CSS3 Refresher
[2015/2016] HTML5 and CSS3 Refresher[2015/2016] HTML5 and CSS3 Refresher
[2015/2016] HTML5 and CSS3 Refresher
 
Crash Course HTML/Rails Slides
Crash Course HTML/Rails SlidesCrash Course HTML/Rails Slides
Crash Course HTML/Rails Slides
 
JAX 2012: Moderne Architektur mit Spring und JavaScript
JAX 2012: Moderne Architektur mit Spring und JavaScriptJAX 2012: Moderne Architektur mit Spring und JavaScript
JAX 2012: Moderne Architektur mit Spring und JavaScript
 
Fundamentals of Web building
Fundamentals of Web buildingFundamentals of Web building
Fundamentals of Web building
 
Web development concepts using microsoft technologies
Web development concepts using microsoft technologiesWeb development concepts using microsoft technologies
Web development concepts using microsoft technologies
 
Web 2.0
Web 2.0Web 2.0
Web 2.0
 

D3 Basic Tutorial