Skip to content

Instantly share code, notes, and snippets.

@JackHowa
Created June 24, 2018 23:37
Show Gist options
  • Save JackHowa/57ccead2fd6f7d2f40a49cf1990c4b2b to your computer and use it in GitHub Desktop.
Save JackHowa/57ccead2fd6f7d2f40a49cf1990c4b2b to your computer and use it in GitHub Desktop.
this is notes about the d3 portion of the fcc code camp
d3 on fcc won't let be me
@JackHowa
Copy link
Author

https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3

  • built on common web standards, including html, css and scalable vector graphics (svg)
  • bind the data to the dom
  • or, can transform into viz
  • helpful for giving you more control over the display

@JackHowa
Copy link
Author

https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3/add-document-elements-with-d3
screen shot 2018-06-24 at 18 44 09

  • d3.select('a') grabs the a tag node
  • can chain together, feels like jquery here
<body>
  <script>
    d3.select('body') /* grabs the body from dom */
      .append('h1') /* then creates an h1 element there */
      .text("Learning D3"); /* last, sets the text */
  </script>
</body>

@JackHowa
Copy link
Author

JackHowa commented Jun 24, 2018

https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3/select-a-group-of-elements-with-d3

  • d3.selectAll('a') would grab all the elements

screen shot 2018-06-24 at 18 47 37

<body>
  <ul>
    <li>Example</li>
    <li>Example</li>
    <li>Example</li>
  </ul>
  <script>
    // text can also change - not just create - text
    // access d3 object
    d3.selectAll('li')
      .text('list item');
  </script>
</body>

@JackHowa
Copy link
Author

https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3/work-with-data-in-d3

  • d3 has ... a data-driven approach
  • .data() method attaches data to selected elements

A common workflow pattern is to create a new element in the document for each piece of data in the set. D3 has the enter() method for this purpose.

  • enter() seems to fill in the gaps for data() attach

When enter() is combined with the data() method, it looks at the selected elements from the page and compares them to the number of data items in the set. If there are fewer elements than data items, it creates the missing elements.

  • enter() seems kind of like a looper, for each item in dataset
  • you add a data item, new element appended

screen shot 2018-06-24 at 18 59 36

<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
    
    // Add your code below this line
    d3.select('body')
      .selectAll('h2')
      .data(dataset)
      .enter()  
      .append('h2')
      .text('New Title')
  </script>
</body>

@JackHowa
Copy link
Author

JackHowa commented Jun 25, 2018

https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3/work-with-dynamic-data-in-d3

  • d in callback function as argument refers to the data point
  • used string literal for interpolation with automatic return
`${d} USD`

The D3 text() method can take a string or a callback function as an argument:

selection.text((d) => d)

<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
    
    d3.select("body").selectAll("h2")
      .data(dataset)
      .enter()
      .append("h2")
      .text(d => `${d} USD`);
      </script>
</body>

@JackHowa
Copy link
Author

JackHowa commented Jun 25, 2018

  • can get in-line styles via method .style('color', 'red)
  • sets style for all elements in the selection

screen shot 2018-06-24 at 19 11 36

<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
    
    d3.select("body").selectAll("h2")
      .data(dataset)
      .enter()
      .append("h2")
      .text(d => d + " USD")
      .style('font-family', 'verdana');
  </script>
</body>

@JackHowa
Copy link
Author

https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3/change-styles-based-on-data

  • woah this is cool
selection.style("color", (d) => {
  /* Logic that returns the color based on a condition */
});
  • used ternary here for readability with in-line
<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
    
    d3.select("body").selectAll("h2")
      .data(dataset)
      .enter()
      .append("h2")
      .text((d) => (d + " USD"))
      .style('color', d => d < 20 ? 'red' : 'green' );
    </script>
</body>

@JackHowa
Copy link
Author

JackHowa commented Jun 25, 2018

https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3/add-classes-with-d3

  • .attr() is similar to .style() because it can take in a callback function as an arg and it uses key-value pairs
  • d3 has .attr() to add any attribute to an element
  • adds class of 'container' : selection.attr("class", "container");
  • can assign multiple classnames to that target selection via space separated (stack)
<style>
  .bar {
    width: 25px;
    height: 100px;
    display: inline-block;
    background-color: blue;
  }
</style>
<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
    
    d3.select("body").selectAll("div")
      .data(dataset)
      .enter()
      .append("div")
      .attr('class', 'bar');      
  </script>
</body>

@JackHowa
Copy link
Author

@JackHowa
Copy link
Author

https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3/change-the-presentation-of-a-bar-chart

  • alright add margin between the elements
  • can multiple the height in-line, which is nice

after:
screen shot 2018-06-24 at 19 37 44

@JackHowa
Copy link
Author

https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3/learn-about-svg-in-d3

  • scalable in scalable vector graphic means that it won't be pixelated on any screen

added svg, still need to style ->-> not sure

<style>
  svg {
    background-color: pink;
  }
</style>
<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
    
    const w = 500;
    const h = 100;
    
    const svg = d3.select("body")
                  // Add your code below this line
                  .append('svg')
                  .style('height', h)
                  .style('width', (d) => w + 'px')
              
                  // Add your code above this line
  </script>
</body>

@JackHowa
Copy link
Author

  • interestingly, width and height attributes don't have any units
    -> it'll be 5:1 anywhere, cool
  • before was making the istake of adding a px at the end

screen shot 2018-06-25 at 21 19 53

<style>
  svg {
    background-color: pink;
  }
</style>
<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
    
    const w = 500;
    const h = 100;
    
    // attr feels a lot like jquery here
    const svg = d3.select("body")
                  .append('svg')
                  .attr('height', h)
                  .attr('width', w);
  </script>
</body>

@JackHowa
Copy link
Author

https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3/display-shapes-with-svg

  • coordinates are done just via attributes as well
<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
    
    const w = 500;
    const h = 100;
    
    const svg = d3.select("body")
                  .append("svg")
                  .attr("width", w)
                  .attr("height", h)
                  .append("rect")
                  .attr("width", 25)
                  .attr("height", 100)
                  .attr("x", 0)
                  .attr("y", 0)                  
  </script>
</body>

@JackHowa
Copy link
Author

JackHowa commented Jun 26, 2018

https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3/create-a-bar-for-each-data-point-in-the-set

  • rects must be appended to an svg element before appended to the body
<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
    
    const w = 500;
    const h = 100;
    
    const svg = d3.select("body")
                  .append("svg")
                  .attr("width", w)
                  .attr("height", h);
    
    svg.selectAll("rect")
       .data(dataset)
       .enter()
       .append('svg') /* need to first append svg before rect */
       .append('rect')
       .attr("x", 0)
       .attr("y", 0)
       .attr("width", 25)
       .attr("height", 100);
  </script>
</body>

@JackHowa
Copy link
Author

https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3/dynamically-set-the-coordinates-for-each-bar

  • loop through using a callback
  • don't need to write a for-loop because enter() already go through the data
<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
    
    const w = 500;
    const h = 100;
    
    const svg = d3.select("body")
                  .append("svg")
                  .attr("width", w)
                  .attr("height", h);
    
    svg.selectAll("rect")
       .data(dataset)
       .enter()
       .append("rect")
       .attr("x", (d, i) => i * 30) /* es6 implicit return */
       .attr("y", 0)
       .attr("width", 25)
       .attr("height", 100);
  </script>
</body>

@JackHowa
Copy link
Author

JackHowa commented Jun 26, 2018

https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3/dynamically-change-the-height-of-each-bar

  • can also return data value in the callback

screen shot 2018-06-25 at 21 38 33 1

<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
    
    const w = 500;
    const h = 100;
    
    const svg = d3.select("body")
                  .append("svg")
                  .attr("width", w)
                  .attr("height", h);
    
    svg.selectAll("rect")
       .data(dataset)
       .enter()
       .append("rect")
       .attr("x", (d, i) => i * 30)
       .attr("y", 0)
       .attr("width", 25)
       .attr("height", (d, i) => d * 3);
  </script>
</body>

@JackHowa
Copy link
Author

JackHowa commented Jun 26, 2018

https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3/invert-svg-elements

  • m is typically the multiplier for the data relationship to the actual ratio
  • In general, the relationship is y = h - m * d, where m is the constant that scales the data points.

  • wanted to make the multiplication more explicit with parens
  • have to accomodate how d3 scales things with the top upper being 0,0 by subtracting from height

screen shot 2018-06-25 at 22 01 03

<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
    
    const w = 500;
    const h = 100;
    
    const svg = d3.select("body")
                  .append("svg")
                  .attr("width", w)
                  .attr("height", h);
    
    svg.selectAll("rect")
       .data(dataset)
       .enter()
       .append("rect")
       .attr("x", (d, i) => i * 30)
       .attr("y", (d, i) => h - (3 * d))
       .attr("width", 25)
       .attr("height", (d, i) => 3 * d);
  </script>
</body>

@JackHowa
Copy link
Author

https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3/change-the-color-of-an-svg-element

  • fill attribute is what makes up the elements
<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
    
    const w = 500;
    const h = 100;
    
    const svg = d3.select("body")
                  .append("svg")
                  .attr("width", w)
                  .attr("height", h);
    
    svg.selectAll("rect")
       .data(dataset)
       .enter()
       .append("rect")
       .attr("x", (d, i) => i * 30)
       .attr("y", (d, i) => h - 3 * d)
       .attr("width", 25)
       .attr("height", (d, i) => 3 * d)
       .attr('fill', 'navy')
  </script>
</body>

@JackHowa
Copy link
Author

https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3/add-labels-to-d3-elements

  • Not sure why d returns itself but ok, guess it could be anything
  • Weird have to subtract to go HIGHER

screen shot 2018-06-25 at 22 24 55

<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
    
    const w = 500;
    const h = 100;
    
    const svg = d3.select("body")
                  .append("svg")
                  .attr("width", w)
                  .attr("height", h);
    
    svg.selectAll("rect")
       .data(dataset)
       .enter()
       .append("rect")
       .attr("x", (d, i) => i * 30)
       .attr("y", (d, i) => h - 3 * d)
       .attr("width", 25)
       .attr("height", (d, i) => 3 * d)
       .attr("fill", "navy");
    
    svg.selectAll("text")
       .data(dataset)
       .enter()
       .append('text')
       .attr('x', (d, i) => i * 30)
       /* three units higher is subtraction */
       .attr('y', (d, i) => h - (3 * d) - 3) 
       .text(d => d) /* returns d data value */ 
  </script>
<body>

@JackHowa
Copy link
Author

https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3/style-d3-labels

  • even text has a fill color property


screen shot 2018-06-25 at 22 28 53

<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
    
    const w = 500;
    const h = 100;
    
    const svg = d3.select("body")
                  .append("svg")
                  .attr("width", w)
                  .attr("height", h);
    
    svg.selectAll("rect")
       .data(dataset)
       .enter()
       .append("rect")
       .attr("x", (d, i) => i * 30)
       .attr("y", (d, i) => h - 3 * d)
       .attr("width", 25)
       .attr("height", (d, i) => d * 3)
       .attr("fill", "navy");
    
    svg.selectAll("text")
       .data(dataset)
       .enter()
       .append("text")
       .text((d) => d)
       .attr("x", (d, i) => i * 30)
       .attr("y", (d, i) => h - (3 * d) - 3)
       .style('font-size', '25px')
       .style('fill', 'red')
  </script>
</body>

@JackHowa
Copy link
Author

JackHowa commented Jun 26, 2018

https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3/add-a-hover-effect-to-a-d3-element

  • add a class via attr

screen shot 2018-06-25 at 22 32 48

<style>
  .bar:hover {
    fill: brown;
  }
</style>
<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
    
    const w = 500;
    const h = 100;
    
    const svg = d3.select("body")
                  .append("svg")
                  .attr("width", w)
                  .attr("height", h);
    
    svg.selectAll("rect")
       .data(dataset)
       .enter()
       .append("rect")
       .attr("x", (d, i) => i * 30)
       .attr("y", (d, i) => h - 3 * d)
       .attr("width", 25)
       .attr("height", (d, i) => 3 * d)
       .attr("fill", "navy")
       .attr('class', 'bar')
           
    svg.selectAll("text")
       .data(dataset)
       .enter()
       .append("text")
       .text((d) => d)
       .attr("x", (d, i) => i * 30)
       .attr("y", (d, i) => h - (3 * d) - 3);
       
  </script>
</body>

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