javascript - Moving the axes in d3.js - Stack Overflow

I have two axes in my graph right now, that are stuck at the very left and bottom of the graph. I want

I have two axes in my graph right now, that are stuck at the very left and bottom of the graph. I want to make the axes line up with the (0,0) coordinate, or in other words I want the axes to be at x=0 and y=0

Here's my axes code:

    //setup x
var xAxis = d3.svg.axis()
    .scale(xRange)
    .tickSize(5)
    .tickSubdivide(true),

    //setup y
    yAxis = d3.svg.axis()
    .scale(yRange)
    .tickSize(5)
    .orient("left")
    .tickSubdivide(true);

I was thinking that the way to do it might just be to make a smaller svg underneath the one that I have, that starts at zero, and put the axes there, and remove them from the one I have right now.

Here's the full code: /

I have two axes in my graph right now, that are stuck at the very left and bottom of the graph. I want to make the axes line up with the (0,0) coordinate, or in other words I want the axes to be at x=0 and y=0

Here's my axes code:

    //setup x
var xAxis = d3.svg.axis()
    .scale(xRange)
    .tickSize(5)
    .tickSubdivide(true),

    //setup y
    yAxis = d3.svg.axis()
    .scale(yRange)
    .tickSize(5)
    .orient("left")
    .tickSubdivide(true);

I was thinking that the way to do it might just be to make a smaller svg underneath the one that I have, that starts at zero, and put the axes there, and remove them from the one I have right now.

Here's the full code: http://jsfiddle/v92q26L8/

Share Improve this question asked Aug 25, 2015 at 13:35 gamehengamehen 3241 gold badge6 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

The key part of your code is the bit where you attach the axes

vis.append("svg:g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + (HEIGHT - MARGINS.bottom) + ")")
    .call(xAxis);

vis.append("svg:g")
    .attr("class", "y axis")
    .attr("transform", "translate(" + (MARGINS.left) + ",0)")
    .call(yAxis);

At the moment you are positioning the axes bottom and left using transforms on the groups (svg:g nodes) which contain them.

To reposition the axes you simply need to adjust these transforms using your defined scales.

For your x axis

.attr("transform", "translate(0," + (HEIGHT - MARGINS.bottom) + ")")

bees

.attr("transform", "translate(0," + yRange(0) + ")")

for your y axis

.attr("transform", "translate(" + (MARGINS.left) + ",0)")

bees

.attr("transform", "translate(" + xRange(0) + ",0)")

Additionally, it may be sensible to change the names of your scales. The term 'range' has a very particular meaning in D3, I'd suggest xScale and yScale.

JS Fiddle

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745639784a4637618.html

相关推荐

  • javascript - Moving the axes in d3.js - Stack Overflow

    I have two axes in my graph right now, that are stuck at the very left and bottom of the graph. I want

    22天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信