title: D3.js Visualization - how to add Y axis gridlines
body: <p>I am trying to add a y gridline to a d3.js visualization I created. I have tried approaches from other posts like creating a function etc. </p>
  
  <p>I found some great examples like this <a href="http://bl.ocks.org/hunzy/11110940" rel="nofollow noreferrer">http://bl.ocks.org/hunzy/11110940</a> and this <a href="https://bl.ocks.org/d3noob/c506ac45617cf9ed39337f99f8511218" rel="nofollow noreferrer">https://bl.ocks.org/d3noob/c506ac45617cf9ed39337f99f8511218</a>. </p>
  
  <p>I tried ticksize, but it didn't work (maybe because I have written out labels?).</p>
  
  <p>I also tried this code, but got the error that d3.axisLeft doesn't exist. </p>
  
  <pre><code>// gridlines in y axis function
    function make_y_gridlines() {   
    return d3.axisLeft(yscale)
      .ticks(25)
      }
  // add the Y gridlines
     main.append('g')     
       .attr("class", "grid")
       .call(make_y_gridlines()
          .tickSize(-width)
          .tickFormat("")
      )
  </code></pre>
  
  <p>Yet I cannot make it work for my graph (although it's probably super simple).</p>
  
  <p>This is the code:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
      &lt;html&gt;
         &lt;head&gt;
            &lt;title&gt;The d3 test&lt;/title&gt;
  &lt;style&gt;
    .chart {
     }
  
  .main text {
           font: 10px sans-serif;  
   }
  
  .axis line, .axis path {
     shape-rendering: crispEdges;
     stroke: black;
     fill: none;
   }
  
  .tick line{
      opacity: 0.2;
      }
  
  &lt;/style&gt;
      &lt;script type="text/javascript" src="http://mbostock.github.com/d3/d3.v2.js"&gt;&lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;div class='content'&gt;
  
      &lt;/div&gt;
      &lt;script&gt;
        var data = [[5,1,2],[4,2,2],[5,2,2],[3,3,1],[4,3,1],[5,3,2],[1,4,1],[5,4,2],[5,5,2],[5,6,2],[5,7,2],
        [2,8,1],[4,8,1],[5,8,2],[1,9,1],[3,9,1],[5,9,1],[1,10,2],[2,10,1],[3,10,1],[5,10,2],[1,11,3],[2,11,1],[3,11,2],[4,11,1],[5,11,1],
        [1,12,3],[5,12,5],[1,13,3],[3,13,2],[5,13,2],[1,14,1],[5,14,2],[4,15,2],[5,15,2],[3,16,1],[4,16,1],[5,16,2],[2,17,1],[5,17,2],
        [5,18,2],[5,18,2],[1,19,1],[2,19,1],[3,19,2],[5,19,1],[5,20,2],[4,21,1],[5,21,1],[1,22,1],[5,22,2],[2,23,2],[4,23,1],[5,23,2],[1,24,3],[5,24,1],
                    ];
  
  var margin = {top: 30, right: 100, bottom: 30, left: 150}
    , width = 960 - margin.left - margin.right
    , height = 900 - margin.top - margin.bottom;
  
  var xscale = d3.scale.linear()
            .domain([0, d3.max(data, function(d) { return d[0]; })])
            .range([ 0, width ]);
  
  var yscale = d3.scale.linear()
          .domain([0, d3.max(data, function(d) { return d[1]; })])
          .range([ height, 0 ]);
  
  var rscale = d3.scale.linear()
   .domain([0, d3.max(data, function(d) { return d[2]; })])
   .range([ 0, 25 ]);
  
  var color = d3.scale.category10();
  
  var chart = d3.select('body')
  
  .append('svg:svg')
     .attr('width', width + margin.right + margin.left)
     .attr('height', height + margin.top + margin.bottom)
     .attr('class', 'chart')
  
  var main = chart.append('g')
     .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
     .attr('width', width)
     .attr('height', height)
     .attr('class', 'main')   
  
  // draw the x axis
  var xAxis = d3.svg.axis()
      .scale(xscale)
      .orient('bottom')
      .ticks(6)
      .tickFormat(function (d, i) {
          return ['','everyday','once a week','several times a week','1-3 times a month','less than once a month'][d];
      });
  
  
  main.append('g')
    .attr('transform', 'translate(0,' + height + ')')
    .attr('class', 'main axis date')
    .call(xAxis);
  
  // draw the y axis
  var yAxis = d3.svg.axis()
    .scale(yscale)
    .orient('left')
    .ticks(25)
    .tickPadding(10)
    .tickFormat(function (d, i) {
          return ['','A','B','C','D','E','F','G','H','I'
          ,'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X'
          ][d];
      });
  
  main.append('g')
    .attr('transform', 'translate(0,0)')
    .attr('class', 'main axis date')
    .call(yAxis);
  
  var g = main.append("svg:g"); 
  
  g.selectAll("scatter-dots")
    .data(data)
    .enter().append("svg:circle")
        .attr("cx", function (d,i) { return xscale(d[0]); } )
        .attr("cy", function (d) { return yscale(d[1]); } )
        .attr("r", function (d) { return rscale(d[2]); } )
        .style("fill", function(d) { return color(d[2]); });
        // .attr("r", 8);
  
  
      &lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>