简体   繁体   English

在d3.js中移动轴

[英]Moving the axes in d3.js

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 我想使轴与(0,0)坐标对齐,换句话说,我希望轴位于x = 0和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. 我当时在想,这样做的方法可能是在我所拥有的那一类的下面创建一个较小的svg,从零开始,然后将轴放在那里,然后从我现在拥有的那一类中删除它们。

Here's the full code: http://jsfiddle.net/v92q26L8/ 这是完整的代码: http : //jsfiddle.net/v92q26L8/

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. 目前,您正在使用包含轴的组( svg:g节点)上的转换将轴定位在底部和左侧。

To reposition the axes you simply need to adjust these transforms using your defined scales. 要重新放置轴,您只需要使用定义的比例来调整这些变换。

For your x axis 对于您的x轴

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

becomes 变成

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

for your y axis 为您的y轴

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

becomes 变成

.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. 术语“范围”在D3中具有非常特殊的含义,我建议使用xScale和yScale。

JS Fiddle JS小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM