Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save codetricity/7dd601d8329e034492564d3b0964561c to your computer and use it in GitHub Desktop.
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

var data = [
    { grade: "A", value: 4},
    { grade: "B", value: 3},
    { grade: "C", value: 2}
]


// use anonymous function
// d3.min or d3.max
// d3.min(data, anonymous-function)
// anonymous function will loop through each line of data

var min2 = d3.min(data, function(d)
{
    return d.value;
});

console.log(min2);

</script>

Challenges

  • Output max for the example above
  • Output extent for the example above

Exercise

Using the example above, use d3.scaleLinear to create the y axis of a population graph.

The format is:

d3.scaleLinear()
    .domain([min, max])
    .range([min, max])

Assume that the svg viewport has a height of 400 (size of screen you can draw the graph on).

Steps

  1. assign variable called populationExtent to the output of d3.extent
  2. print out the content of populationExtent as a test
  3. assign variable y to the output of d3.scaleLinear (this output will be a function called y)
  4. in the developer console, test different values of y(populationNumber). For example, y(25000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment