Skip to content

Instantly share code, notes, and snippets.

@codetricity
Last active August 23, 2018 13:32
Show Gist options
  • Select an option

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

Select an option

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

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

Array of JSON Objects

var data = [
    {name: "panda", population: 2500}, 
    {name: "tiger", population: 4000},

Graph 2

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

var y = d3.scaleLinear()
    .domain([0, max2])
    .range([400, 0]);

reverse bar chart

Part we want

Adjust y coordinate

.attr("y", function(d){
    return y(d.population);
})

y coordinate

height problem

Adjust height

.attr("height", function(d){
    var height = 450 - y(d.population);
    console.log(height)
    return height;
})

final chart


SVG Group

group is the "g" tag. transform moves things translate moves in x, y direction rotate

var svg = d3.select("#canvas")
    .append("g")
    .attr("transform", "translate(200, 0)");

translate

Exercises

  • change x and y translation values

Challenge

rotate

Hint: transform, rotate(degrees, x, y)

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