Skip to content

Instantly share code, notes, and snippets.

View codetricity's full-sized avatar

Craig Oda codetricity

  • Oppkey
  • Palo Alto
View GitHub Profile

Review code sections from previous lesson.

rect

svg.selectAll("rect")

text

svg.selectAll("text")
@codetricity
codetricity / 2018-08-29.md
Created August 28, 2018 14:18
Bar Chart Lesson #4

Invert Vertical "y axis"

  1. change y to the height of viewport minus the datapoint
  2. change the height of the bar to the datapoint

Adjust the scale of each bar

Use d3.scaleLinear

@codetricity
codetricity / 2018-08-28.md
Last active August 28, 2018 14:17
bar chart using dataset

use anonymous function to move x coordinate

20 bars

replace the static integer assigned to x to an anonymous function. Use the index value i. Multiply it by the width of each bar plus a margin.

function(d, i){
    return ADD_THING_TO_RETURN_HERE

}

Bar Chart

  1. reverse linear scale output (range)
  2. set starting y coordinate of rectangle to be length height of reversed rectangle
  3. adjust height of each rectangle in bar chart

bar chart

Reverse Linear Scale

<!DOCTYPE html>
<meta charset="utf-8">
<svg id = "canvas" width="800" height = "450"></svg>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<script>
var data = [
{name: "panda", population: 2500},
{name: "tiger", population: 4000},
{name: "whooping crane", population: 400},
{name: "blue whale", population: 25000},

using min, max, extent in charts

Simple Array - Numbers Only

Using the simple array of numbers only, create a bar chart.

var dataOnly = [2500, 4000, 400, 25000, 45000];

Chart Example

<!DOCTYPE html>
<meta charset="utf-8">
<svg id = "canvas" width="800" height = "450"></svg>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<script>
var data = [
{name: "panda", population: 2500},
{name: "tiger", population: 4000},
{name: "whooping crane", population: 400},
{name: "blue whale", population: 25000},
@codetricity
codetricity / 20180823.html
Created August 22, 2018 14:18
starting point for lesson on 2018 23 08. Extend to use scaleLinear
<!DOCTYPE html>
<meta charset="utf-8">
<svg id = "canvas" width="800" height = "400"></svg>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<script>
var data = [
{name: "panda", population: 2500},
{name: "tiger", population: 4000},
{name: "whooping crane", population: 400},
@codetricity
codetricity / Caitlyn-2018-08-22.md
Last active August 22, 2018 14:21
d3 - min, max, extent, for data with labels

Note on lesson: easily completed

Array of Objects - With Labels

<!DOCTYPE html>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<script>

// replace data below with data from endangered animals