SlideShare ist ein Scribd-Unternehmen logo
1 von 15
Downloaden Sie, um offline zu lesen
d3 is cool
                     maps data to DOM




Sunday, 7 April 13
# jQuery-esque API
            d3
               .select("body")
               .append("p")
               .text("New paragraph!")
               .attr("class", "yep")
               .style("height", "10px")




Sunday, 7 April 13
data = []
            for num in [0..25]
              data.push Math.random() * 30

            d3.select("#bar") # get a wrapper element
              .selectAll("div") # get an array
              .data(data) # get a data-bound array
              .enter() # creates new nodes
              .append("div") # adds to DOM
              .style("height", (d)-> d * 5 + "px")




Sunday, 7 April 13
maps data to DOM




Sunday, 7 April 13
svg is cool
                     DOM for vector graphics & text




Sunday, 7 April 13
<svg height="200" width="700">
              <rect x="0" y="0" width="500" height="50">
              <circle cx="10" cy="100" r="50" fill="red">
              <text x="250" y="50">Easy-peasy</text>
              <path d="M 100 100 L 300 100 L 200 300 z" stroke-width="3">
              <image>
              <font> # fek yeah
            </svg>




Sunday, 7 April 13
svg = d3.select("body")
              .append("svg")
              .attr("width", w)
              .attr("height", h)
            circles = svg.selectAll()
              .data(dataset)
              .enter()
              .append("circle")
              .attr("cx", (d, i)-> i * 22)
              .attr("cy", 50)
              .attr("r", (d)-> d / 2)
              .attr("fill", (d)-> "rgb(" + Math.floor(d * 6) + ",0,150)")




Sunday, 7 April 13
data = [
              [5, 20],
              ...
              [220, 88]
            ]
            svg = getSVG()
            svg.selectAll()
               .data(data)
               .enter()
               .append("circle")
               .attr("cx", (d)-> d[0])
               .attr("cy", (d)-> d[1])
               .attr("r", 4)




Sunday, 7 April 13
# scales - d3's best feature
            # functions that map an input range to an output range
            xScale = d3.time.scale()
              .domain([xMin, xMax])
              .range([0, w]);

            yScale = d3.scale.linear()
              .domain([yMin, yMax])
              .range([h, 0]);
              # y output reversed because elements are positioned top/left




Sunday, 7 April 13
readings = [{
              read_at: '2013-03-27T00:04:00Z'
              wind_speed: 28,
              gust: 34,
              wind_direction: 287.3
            }...]

            xPoint = (reading)-> xScale(new Date(reading.read_at))
            yPointBySpeed = (reading)-> yScale(reading.wind_speed)
            yPointByGust = (reading)-> yScale(reading.gust)

            speedLine = d3.svg.line()
              .x(xPoint)
              .y(yPointBySpeed)

            # dark blue line
            svg.append("svg:path")
              .attr("d", speedLine(readings))
              .attr("stroke-width", 2)
              .attr("stroke", "#04C8FF")
              .attr("fill", "none")

Sunday, 7 April 13
speeds = []
            gusts = []
            for reading in data
              x = xPoint reading
              speeds.push [x, yPointBySpeed(reading)]
              gusts.push [x, yPointByGust(reading)]

            # light blue area
            # array of speed(x,y) followed by array of gust(x,y) reversed
            svg.selectAll("path.area")
              .data([speeds.concat(gusts.reverse())])
              .enter()
              .append("path")
              .attr("fill", "#04C8FF")
              .attr("fill-opacity", .2)
              .attr("d", d3.svg.area())




Sunday, 7 April 13
# arrow heads
            arrowTransform = (reading)->
              "translate(#{xPoint(reading)}, #{yPointBySpeed(reading)}),
               rotate(#{reading.wind_direction})"

            svg.selectAll("polygon")
              .data(data)
              .enter()
              .append("polygon")
              .attr("transform", arrowTransform)
              .attr("points", "0,-7.8 -5.1,7.8 0,5.4 5.1,7.8")
              .attr("stroke-width", 1)
              .attr("stroke", "#fff")
              .attr("fill", "#04C8FF")




Sunday, 7 April 13
yPointBySwellPeriod = (reading)->
              yScale(reading.swell_period)

            swellLine = d3.svg.line()
              .x(xPoint)
              .y(yPointBySwellPeriod)
              .interpolate("cardinal")
              # Many ways for svg paths to be drawn around the points

            svg.append("svg:path")
              .attr("d", swellLine(data))
              .attr("stroke-width", 2)
              .attr("stroke", "#28d7bd")
              .attr("fill", "none")




Sunday, 7 April 13
Lots of things I’m yet to play with..

                                      Transitions
                                Events - mouse, touches
                                          Ajax
                                         Colors
                                          Axes
                                   Other scale types
                                     Other shapes
                                    Time Intervals

                Cluster, Force, Hierarchy, Histogram, Pack, Partition, Pie,
                Stack, Tree, Treemap, Geography, Projections, Zoom etc..


Sunday, 7 April 13
d3 is cool
                                  Thanks!

                     http://alignedleft.com/tutorials/d3/

                              @markbrown4




Sunday, 7 April 13

Weitere ähnliche Inhalte

Was ist angesagt?

ARTDM 170, Week 13: Text Elements + Arrays
ARTDM 170, Week 13: Text Elements + ArraysARTDM 170, Week 13: Text Elements + Arrays
ARTDM 170, Week 13: Text Elements + Arrays
Gilbert Guerrero
 
Pointer Events in Canvas
Pointer Events in CanvasPointer Events in Canvas
Pointer Events in Canvas
deanhudson
 

Was ist angesagt? (20)

Functional programming in scala
Functional programming in scalaFunctional programming in scala
Functional programming in scala
 
Advanced Data Visualization Examples with R-Part II
Advanced Data Visualization Examples with R-Part IIAdvanced Data Visualization Examples with R-Part II
Advanced Data Visualization Examples with R-Part II
 
Effector: we need to go deeper
Effector: we need to go deeperEffector: we need to go deeper
Effector: we need to go deeper
 
Introduction to D3
Introduction to D3 Introduction to D3
Introduction to D3
 
Intro to HTML5 Canvas
Intro to HTML5 CanvasIntro to HTML5 Canvas
Intro to HTML5 Canvas
 
Html5 canvas
Html5 canvasHtml5 canvas
Html5 canvas
 
Fuzzy Data Leaks
Fuzzy Data LeaksFuzzy Data Leaks
Fuzzy Data Leaks
 
Drawing with the HTML5 Canvas
Drawing with the HTML5 CanvasDrawing with the HTML5 Canvas
Drawing with the HTML5 Canvas
 
Ml all programs
Ml all programsMl all programs
Ml all programs
 
ARTDM 170, Week 13: Text Elements + Arrays
ARTDM 170, Week 13: Text Elements + ArraysARTDM 170, Week 13: Text Elements + Arrays
ARTDM 170, Week 13: Text Elements + Arrays
 
Supstat nyc subway
Supstat nyc subwaySupstat nyc subway
Supstat nyc subway
 
Advanced Data Visualization in R- Somes Examples.
Advanced Data Visualization in R- Somes Examples.Advanced Data Visualization in R- Somes Examples.
Advanced Data Visualization in R- Somes Examples.
 
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSONFun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
 
Beyond Scala Lens
Beyond Scala LensBeyond Scala Lens
Beyond Scala Lens
 
λ | Lenses
λ | Lensesλ | Lenses
λ | Lenses
 
Visualization of Big Data in Web Apps
Visualization of Big Data in Web AppsVisualization of Big Data in Web Apps
Visualization of Big Data in Web Apps
 
Micro and moblile: Java on the Raspberry Pi
Micro and moblile: Java on the Raspberry PiMicro and moblile: Java on the Raspberry Pi
Micro and moblile: Java on the Raspberry Pi
 
Pointer Events in Canvas
Pointer Events in CanvasPointer Events in Canvas
Pointer Events in Canvas
 
Expand/Collapse animation on Android
Expand/Collapse animation on AndroidExpand/Collapse animation on Android
Expand/Collapse animation on Android
 
Will it Blend? - ScalaSyd February 2015
Will it Blend? - ScalaSyd February 2015Will it Blend? - ScalaSyd February 2015
Will it Blend? - ScalaSyd February 2015
 

Andere mochten auch (9)

7 tips for great presentations
7 tips for great presentations7 tips for great presentations
7 tips for great presentations
 
Bhagavat Gita Simplified[1]
Bhagavat Gita Simplified[1]Bhagavat Gita Simplified[1]
Bhagavat Gita Simplified[1]
 
Project
ProjectProject
Project
 
FM&amp;Property Summit Oct2011
FM&amp;Property Summit Oct2011FM&amp;Property Summit Oct2011
FM&amp;Property Summit Oct2011
 
Why Does God Exist
Why Does God ExistWhy Does God Exist
Why Does God Exist
 
Lychnari 2005 01 Zigeuners
Lychnari 2005 01 ZigeunersLychnari 2005 01 Zigeuners
Lychnari 2005 01 Zigeuners
 
Don\'t Sprint the Marathon
Don\'t Sprint the MarathonDon\'t Sprint the Marathon
Don\'t Sprint the Marathon
 
Fixtures
FixturesFixtures
Fixtures
 
Child Development
Child DevelopmentChild Development
Child Development
 

Ähnlich wie d3 is cool

Useful javascript
Useful javascriptUseful javascript
Useful javascript
Lei Kang
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
Dmitry Buzdin
 

Ähnlich wie d3 is cool (20)

Scrollytelling
ScrollytellingScrollytelling
Scrollytelling
 
SVGo workshop
SVGo workshopSVGo workshop
SVGo workshop
 
D3.js 30-minute intro
D3.js   30-minute introD3.js   30-minute intro
D3.js 30-minute intro
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Better d3 charts with tdd
Better d3 charts with tddBetter d3 charts with tdd
Better d3 charts with tdd
 
D3 svg & angular
D3 svg & angularD3 svg & angular
D3 svg & angular
 
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...
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
Useful javascript
Useful javascriptUseful javascript
Useful javascript
 
Svcc 2013-d3
Svcc 2013-d3Svcc 2013-d3
Svcc 2013-d3
 
SVCC 2013 D3.js Presentation (10/05/2013)
SVCC 2013 D3.js Presentation (10/05/2013)SVCC 2013 D3.js Presentation (10/05/2013)
SVCC 2013 D3.js Presentation (10/05/2013)
 
Joclad 2010 d
Joclad 2010 dJoclad 2010 d
Joclad 2010 d
 
Chapter3_Visualizations2.pdf
Chapter3_Visualizations2.pdfChapter3_Visualizations2.pdf
Chapter3_Visualizations2.pdf
 
JQuery Flot
JQuery FlotJQuery Flot
JQuery Flot
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and Maps
 
imager package in R and examples..
imager package in R and examples..imager package in R and examples..
imager package in R and examples..
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
 
D3
D3D3
D3
 

Kürzlich hochgeladen

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 

d3 is cool

  • 1. d3 is cool maps data to DOM Sunday, 7 April 13
  • 2. # jQuery-esque API d3 .select("body") .append("p") .text("New paragraph!") .attr("class", "yep") .style("height", "10px") Sunday, 7 April 13
  • 3. data = [] for num in [0..25] data.push Math.random() * 30 d3.select("#bar") # get a wrapper element .selectAll("div") # get an array .data(data) # get a data-bound array .enter() # creates new nodes .append("div") # adds to DOM .style("height", (d)-> d * 5 + "px") Sunday, 7 April 13
  • 4. maps data to DOM Sunday, 7 April 13
  • 5. svg is cool DOM for vector graphics & text Sunday, 7 April 13
  • 6. <svg height="200" width="700"> <rect x="0" y="0" width="500" height="50"> <circle cx="10" cy="100" r="50" fill="red"> <text x="250" y="50">Easy-peasy</text> <path d="M 100 100 L 300 100 L 200 300 z" stroke-width="3"> <image> <font> # fek yeah </svg> Sunday, 7 April 13
  • 7. svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h) circles = svg.selectAll() .data(dataset) .enter() .append("circle") .attr("cx", (d, i)-> i * 22) .attr("cy", 50) .attr("r", (d)-> d / 2) .attr("fill", (d)-> "rgb(" + Math.floor(d * 6) + ",0,150)") Sunday, 7 April 13
  • 8. data = [ [5, 20], ... [220, 88] ] svg = getSVG() svg.selectAll() .data(data) .enter() .append("circle") .attr("cx", (d)-> d[0]) .attr("cy", (d)-> d[1]) .attr("r", 4) Sunday, 7 April 13
  • 9. # scales - d3's best feature # functions that map an input range to an output range xScale = d3.time.scale() .domain([xMin, xMax]) .range([0, w]); yScale = d3.scale.linear() .domain([yMin, yMax]) .range([h, 0]); # y output reversed because elements are positioned top/left Sunday, 7 April 13
  • 10. readings = [{ read_at: '2013-03-27T00:04:00Z' wind_speed: 28, gust: 34, wind_direction: 287.3 }...] xPoint = (reading)-> xScale(new Date(reading.read_at)) yPointBySpeed = (reading)-> yScale(reading.wind_speed) yPointByGust = (reading)-> yScale(reading.gust) speedLine = d3.svg.line() .x(xPoint) .y(yPointBySpeed) # dark blue line svg.append("svg:path") .attr("d", speedLine(readings)) .attr("stroke-width", 2) .attr("stroke", "#04C8FF") .attr("fill", "none") Sunday, 7 April 13
  • 11. speeds = [] gusts = [] for reading in data x = xPoint reading speeds.push [x, yPointBySpeed(reading)] gusts.push [x, yPointByGust(reading)] # light blue area # array of speed(x,y) followed by array of gust(x,y) reversed svg.selectAll("path.area") .data([speeds.concat(gusts.reverse())]) .enter() .append("path") .attr("fill", "#04C8FF") .attr("fill-opacity", .2) .attr("d", d3.svg.area()) Sunday, 7 April 13
  • 12. # arrow heads arrowTransform = (reading)-> "translate(#{xPoint(reading)}, #{yPointBySpeed(reading)}), rotate(#{reading.wind_direction})" svg.selectAll("polygon") .data(data) .enter() .append("polygon") .attr("transform", arrowTransform) .attr("points", "0,-7.8 -5.1,7.8 0,5.4 5.1,7.8") .attr("stroke-width", 1) .attr("stroke", "#fff") .attr("fill", "#04C8FF") Sunday, 7 April 13
  • 13. yPointBySwellPeriod = (reading)-> yScale(reading.swell_period) swellLine = d3.svg.line() .x(xPoint) .y(yPointBySwellPeriod) .interpolate("cardinal") # Many ways for svg paths to be drawn around the points svg.append("svg:path") .attr("d", swellLine(data)) .attr("stroke-width", 2) .attr("stroke", "#28d7bd") .attr("fill", "none") Sunday, 7 April 13
  • 14. Lots of things I’m yet to play with.. Transitions Events - mouse, touches Ajax Colors Axes Other scale types Other shapes Time Intervals Cluster, Force, Hierarchy, Histogram, Pack, Partition, Pie, Stack, Tree, Treemap, Geography, Projections, Zoom etc.. Sunday, 7 April 13
  • 15. d3 is cool Thanks! http://alignedleft.com/tutorials/d3/ @markbrown4 Sunday, 7 April 13