D3.js Visualization - How To Add Y Axis Gridlines
Solution 1:
Your examples use more recent versions so of d3 (v3 and v4 respectively). The documentation for version 2, which you are using, is a bit sparse now from what I can see, but the version you are using likely doesn't include the tick sizing functions you want.
If you update your d3.js version to version 3 rather than version 2, you can set your y axis ticks to stretch across plot area:
yAxis.innerTickSize(-width)
Using your code I got this to work simply by using d3.js v3
<scriptsrc="http://d3js.org/d3.v3.js"></script>
Without some minor changes, using d3 v4 will break your code as there are some namespace changes: d3.scale.linear
is nw d3.scaleLinear
now for example.
Solution 2:
You're using (for whatever reason) D3 version 2. While it's advised to use a newer version, it's possible to create some gridlines, getting the position of the y axis ticks:
d3.selectAll("g.yAxis g")
.append("line")
.attr("class", "gridline")
.attr("x1", 0)
.attr("y1", 0)
.attr("x2", width)
.attr("y2", 0);
Here is a demo with your code and D3 v2:
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 axisvar 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 axisvar 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', 'yAxis main axis date')
.call(yAxis);
d3.selectAll("g.yAxis g")
.append("line")
.attr("class", "gridline")
.attr("x1", 0)
.attr("y1", 0)
.attr("x2", width)
.attr("y2", 0);
var g = main.append("svg:g");
g.selectAll("scatter-dots")
.data(data)
.enter().append("svg:circle")
.attr("cx", function (d,i) { returnxscale(d[0]); } )
.attr("cy", function (d) { returnyscale(d[1]); } )
.attr("r", function (d) { returnrscale(d[2]); } )
.style("fill", function(d) { returncolor(d[2]); });
// .attr("r", 8);
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.gridline {
opacity: 0.25;
}
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/d3/2.10.0/d3.v2.js"></script>
Post a Comment for "D3.js Visualization - How To Add Y Axis Gridlines"