Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

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

Use array.length to make formula more flexible

Change the above formula to use the length of the dataset array.

Try with 24 array values.

Try with 5 array values.

5 bars

Add margin

Create variable for barPadding.

.attr("width", w / dataset.length - barPadding)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment