I also posted this question on the Google Group https://groups.google.com/forum/?fromgroups#!topic/d3-js/DYiVeC544ws, but saw that it prefers that help questions are asked here. I'm just having trouble shifting the y-axis to the left of my graph to so that it does not get in the way of the first column. I provided an image on the google group so you can see what I'm talking about.
The code to create the axis is as follows:
var MARGINS = {top: 20, right: 20, bottom: 20, left: 60}; // margins around the graph
var xRange = d3.scale.linear().range([MARGINS.left, width - MARGINS.right]), // x range function
yRange = d3.scale.linear().range([height - MARGINS.top, MARGINS.bottom]), // y range function
xAxis = d3.svg.axis().scale(xRange).tickSize(16).tickSubdivide(true), // x axis function
yAxis = d3.svg.axis().scale(yRange).tickSize(10).orient("right").tickSubdivide(true); // y axis function
// create the visualization chart
var vis = d3.select("#chart")
.append("svg")
.attr("width", width)
.attr("height", height + margin);
// add in the x axis
vis.append("svg:g") // container element
.attr("class", "x axis") // so we can style it with CSS
.attr("transform", "translate(0," + height + ")") // move into position
// .call(xAxis); // add to the visualisation
// add in the y axis
vis.append("svg:g") // container element
.attr("class", "y axis") // so we can style it with CSS
.call(yAxis); // add to the visualisation
Use the transform attribute to shift the y-axis, just as you did for the x-axis. For example:
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + width + ",0)")
.call(yAxis);
This is only necessary when using right orientation. When the axis is oriented to the left, the default positioning is typically fine.
Also, I recommend using the following convention for margins:
width
and height
as the inner width and height.For example:
var margin = {top: 20, right: 10, bottom: 20, left: 10},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
If you prefer, you could also store the inner and outer dimensions, e.g.,
var outerWidth = 960,
outerHeight = 500,
innerWidth = outerWidth - margin.left - margin.right,
innerHeight = outerHeight - margin.top - margin.bottom;
Here's a visual explanation of the convention:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With