Skip to content

Instantly share code, notes, and snippets.

@codetricity
Last active August 27, 2018 14:17
Show Gist options
  • Select an option

  • Save codetricity/4c67b0e397f17b7a0ca9a3cf447d2940 to your computer and use it in GitHub Desktop.

Select an option

Save codetricity/4c67b0e397f17b7a0ca9a3cf447d2940 to your computer and use it in GitHub Desktop.

Create new HTML and JavaScript files

Create new HTML file with tag <body></body>

Download and save d3.js to your project.

https://raw.githubusercontent.com/alignedleft/d3-book/master/d3.js

In the HTML file, access the d3.js file using <script>

Create new JavaScript file.

In the HTML file, access the new JavaScript file you created.

Set Up SVG viewport

In the JavaScript file you created, set two new variables for width and height.

//Create SVG element
var svg = d3.select("body")
            .append("svg")
            .attr("width", w)
            .attr("height", h);

Practice with Single Rectangle

use inspector in browser developer tools. use split console to interactively append "rect" to "svg" element.

svg.append("rect").attr("x", "num")

You need to chain these four attributes:

  • x
  • y
  • width
  • height

change color create circle

add rectangle code to JavaScript file

add data

Start with this data.

var dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 13,
              11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ];

bind data to rect elements

prepend the following code to the rectangle

svg.selectAll("rect")
   .data(dataset)
   .enter()

You can now access the data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment