简体   繁体   English

d3.js如何分辨X轴和Y轴之间的区别?

[英]How does d3.js tell the difference between X-axis and Y-Axis?

So I am learning d3 lately and run into one small question: when people create x-axis and y-axis, how can you tell from the code which axis is being created first? 因此,我最近正在学习d3,并遇到一个小问题:当人们创建x轴和y轴时,如何从代码中知道首先创建哪个轴?

for example: 例如:

          .......
          .......

        //Create scale functions
        var xScale = d3.scale.linear()
                             .domain([0, d3.max(dataset, function(d) { return d[0]; })])
                             .range([padding, w - padding * 2]);

        var yScale = d3.scale.linear()
                             .domain([0, d3.max(dataset, function(d) { return d[1]; })])
                             .range([h - padding, padding]);


          .......
          .......
        //Define X axis
        var xAxis = d3.svg.axis()
                          .scale(xScale)
                          .orient("bottom")
                          .ticks(5);

        //Define Y axis
        var yAxis = d3.svg.axis()
                          .scale(yScale)
                          .orient("left")
                          .ticks(5);


          .......
          .......

        //Create X axis
        svg.append("g")
            .attr("class", "axis")
            .attr("transform", "translate(0," + (h - padding) + ")")
            .call(xAxis);

        //Create Y axis
        svg.append("g")
            .attr("class", "axis")
            .attr("transform", "translate(" + padding + ",0)")
            .call(yAxis);

For this code, I couldn't really find the difference between creating x-axis or y-axis? 对于此代码,我真的找不到创建x轴或y轴的区别吗? Is it because of the transform functions? 是因为转换功能吗? However, when I delete the transform functions for both x and y, the x goes back to the top while the y disappear, what really happened? 但是,当我同时删除x和y的转换函数时,x返回顶部,而y消失了,到底发生了什么? Is there a default function? 有默认功能吗?

code source: http://alignedleft.com/content/03-tutorials/01-d3/160-axes/6.html by Scott Murray. 代码源:Scott Murray的http://alignedleft.com/content/03-tutorials/01-d3/160-axes/6.html

The axis that gets created first is the one called first (with .call()), which in this case is the x-axis. 首先创建的轴是第一个(使用.call())的轴,在这种情况下,它是x轴。 The position of the axis depends on the orientation and the transformation applied. 轴的位置取决于方向和所应用的变换。 The orientation can be "bottom", "top", "left" or "right". 方向可以是“底部”,“顶部”,“左侧”或“右侧”。 Bottom and top specify a horizontal axis with ticks positioned either above or below the axis respectively, while left and right specify a vertical axis, with ticks either to the left or right of the axis respectively. 底部和顶部指定水平轴,刻度线分别位于该轴的上方或下方,而左侧和右侧指定垂直轴,刻度线分别位于该轴的左侧或右侧。 d3 doesn't know which is the x axis and which is the y axis. d3不知道哪个是x轴,哪个是y轴。 It simply knows there are axes and draws them where .orient and the transformation tell it to. 它只是知道有轴,并在.orient和变换告诉它的地方绘制它们。 Naturally people tend to call a horizontal axis "xAxis" or something like that, but you could call it "yAxis" and it would still be drawn as a horizontal axis. 自然,人们倾向于将水平轴称为“ xAxis”或类似名称,但您可以将其称为“ yAxis”,并且仍将其绘制为水平轴。 The transformation positions the axis. 变换将定位轴。 So if you delete the transformation for the x axis, it will no longer get transformed "h - padding" pixels down from the top, but instead will appear at the top of its container. 因此,如果删除x轴的转换,它将不再从顶部向下转换“ h-padding”像素,而是显示在其容器的顶部。 If you delete the transform for the y axis, it will be drawn at (0,0) of the container, ie the top left corner, and will extend upward but you won't be able to see it because it will extend outside the visible region of your container. 如果删除y轴的变换,它将在容器的(0,0)处绘制,即左上角,并且将向上延伸,但是您将无法看到它,因为它将延伸到y轴之外容器的可见区域。

See here for more details: https://github.com/mbostock/d3/wiki/SVG-Axes 请参阅此处以获取更多详细信息: https : //github.com/mbostock/d3/wiki/SVG-Axes

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

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