Skip to content

Instantly share code, notes, and snippets.

@codetricity
Last active September 6, 2018 18:16
Show Gist options
  • Select an option

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

Select an option

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

Simple Axis Tutorial with D3

set up files

  1. Create js and css sub-directories in main project folder
  2. Create these files:
    • html file - index.html
      • HTML file must contain <body> and <head> tags.
    • blank JavaScript file - axis.js
    • atyle sheet - axis.cs
  3. Save d3.js into js folder. Use latest version, available here
  4. link html file to javascript file and style sheet.
  5. link html file to d3.js before linking to axis.js
  6. Open in browser window. Test in console with d3.version

set up svg viewport

  1. select body tag with d3.select
  2. append svg
  3. add attributes for width and height - use 800 x 400

assign to variable svg.

test in browser inspector

var svg = d3.select("body").append("svg")
    .attr("width", "800")
    .attr("height", "400");

add dataset

Add to top of code.

var dataset = [1200, 343, 256, 822, 745, 926, 2336];

create scale

use d3.scaleLinear to set the domain and range of the dataset to fit into the screen.

var xScale = d3.scaleLinear()
    .domain([0, 2336])
    .range([0, 800]);

create x axis

  1. append group to svg
  2. call d3.axisBottom
  3. pass xScale to d3.axisBottom

svg.append("g")
    .call(d3.axisBottom(xScale));

Imgur

set up y scale

var yScale = d3.scaleLinear()
    .domain([0,10])
    .range([0, 400]);

No dataset is needed.

 svg.append("g")
    .call(d3.axisLeft(yScale))
    .attr("transform", "translate(100, 0)");

Imgur

Reverse Scale

Reverse the order of the range of yScale.

Imgur

Add Margins

  1. create object to hold margin information

    var margin = {top: 100, bottom: 100, right: 100, left: 50};
    
  2. set up variables for width and height of chart

    var width = 800;
    var height = 600;
    
  3. create variables for svg width and height

    var svgWidth = 800 + margin.right + margin.left;
    var svgHeight = 600 + margin.top + margin.bottom;
    
  4. replace numbers in svg viewport with variables

     var svg = d3.select("body").append("svg")
       .attr("width", svgWidth)
       .attr("height", svgHeight);
    
  5. append group and translate viewport position

      var svg = d3.select("body").append("svg")
          .attr("width", svgWidth)
          .attr("height", svgHeight)
          .append("g")
              .attr("transform", "translate(" + margin.right +
                  "," + margin.top + ")");
    
  6. remove translation of y axis

Imgur

  1. use width and height variables for scale

  2. translate x axis to bottom

use height variable

  svg.append("g")
      .call(d3.axisBottom(xScale))
          .attr("transform", "translate(0, " +
              height + ")");

Imgur

Add Styles to x and y axis

  1. select all text elements of x axis

  2. Create class for x and y axis

    svg.append("g") .call(d3.axisBottom(xScale)) .attr("transform", "translate(0, " + height + ")") .selectAll("text") .attr("class", "xAxis");

  3. change font style in css

Imgur

  1. increase vertical spacing of label and tick mark

    .selectAll("text")
        .attr("class", "xAxis")
        .attr("transform", "translate(0, 4)");
    

Imgur

  1. apply style to y axis

Imgur

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