I am trying to create a 2D Cartesian coordinate system with its origin in the center of svg. Basically this:
My current approach is to translate the axes so that their origin is in the middle.
svg.append("g")
.attr("transform", "translate(" + 0 + "," + height/2 + ")")
.call(d3.axisBottom(x).ticks(100));
svg.append("g")
.attr("transform", "translate(" + width/2 + "," + 0 + ")")
.call(d3.axisLeft(y).ticks(100));
});
However, I need to adjust the range manually so that the Y axis is centered. The required value is different from each screen size, so I need help figuring out how to always get the coordinate axes centered on each screen.
var x = d3.scaleLinear().range([width/2-5*width,width/2+5*width]).domain([-50, 50]);
var y = d3.scaleLinear().range([height/2-5*height,height/2+3.244*width]).domain([50, -50]);
I created Fiddle to show you exactly how I created the coordinate axes.
source
share