Skip to content

Instantly share code, notes, and snippets.

@codetricity
Created August 28, 2018 14:18
Show Gist options
  • Select an option

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

Select an option

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

This is the example from mint green man. video is on local drive E: under Videos\d3\

scale linear

Change Color to Teal

.attr("fill", "teal");

Adjust Color to Height of Data

.attr("fill", function(d) {
    return "rgb(0, 0, " + (d * 10) + ")";
});

Text

svg.selectAll("text")
   .data(dataset)
   .enter()
   .append("text")      
   .text(function(d) {
    return d;
})

Add Text Position

Chain attributes for x and y.

Set Color and Font Family

   .attr("font-family", "sans-serif")
   .attr("font-size", "11px")
   .attr("fill", "white");

finished chart

Perfect alignment - follow tutorial

http://alignedleft.com/tutorials/d3/making-a-bar-chart

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