Adjusting the height and the width of the bars

Let's create a scale that maps the count property of each element in data to a visual height for the corresponding bar. We'll use a linear scale. Remember to map HEIGHT of the graph to a very low data point and the top of the graph (0 in the range) map to a very high data value. Add this code to the bottom of the AJAX callback:

var yScale = d3.scaleLinear();
yScale.range([HEIGHT, 0]);
var yMin = d3.min(data, function(datum, index){
    return datum.count;
})
var yMax = d3.max(data, function(datum, index){
    return datum.count;
})
yScale.domain([yMin, yMax]);

We could use d3.extent, but we're going to need the individual min values later on. Immediately after the previous code, let's tell D3 to adjust ...

Get D3.js Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.