简体   繁体   English

如何翻转 D3 图形上的 Y 轴?

[英]How to flip the Y axis on my D3 graph?

I have this graph :我有这个图:

http://jsfiddle.net/5c4sa6vb/1/ http://jsfiddle.net/5c4sa6vb/1/

Ive been trying to mess about with it and try flip the y axis.我一直在尝试弄乱它并尝试翻转 y 轴。 What i need is for the ticks to start at 0,0 at the bottom left.我需要的是刻度从左下角的 0,0 开始。 I have tried messing with the domain and range but cant seem to solve my problem :/我试过搞乱域和范围,但似乎无法解决我的问题:/

var y = d3.scale.linear()
    .range([0, width-200]);

Here's a version that works: fiddle这是一个有效的版本: fiddle

First, switching the order of the axis domain elements from ([0,whatever]) to ([whatever,0]) will reverse the scale.首先,将轴域元素的顺序从([0,whatever])切换到([whatever,0])将反转比例。

Second, a small mistake.第二,一个小错误。 You simply were using width in your y scale instead of height .您只是在y比例中使用width而不是height So since the graph isn't square, it wasn't the right length!因此,由于图形不是正方形,因此长度不正确!

Finally, I notice that you have a bunch of extra adjustments tucked in here and there, subtracting 200, the axisMovement variable, an extra g element around your svg , etc. So you'll have an easier time if you clean out those parts that are not needed.最后,我注意到这里和那里有一堆额外的调整,减去 200、 axisMovement变量、 svg周围的额外g元素等。所以如果你清理掉那些部分,你会更轻松不需要。 The purpose of setting up the margins using the convention that you did is so that these extra tweaks are unnecessary.使用您所做的约定设置边距的目的是使这些额外的调整是不必要的。 Taking advantage of elegant little tricks like these is what gets people really hooked on d3 :)利用这些优雅的小技巧让人们真正迷上了 d3 :)

The way to reverse the Y axis in 3D is simple :在 3D 中反转 Y 轴的方法很简单:

change改变

var y = d3.scale.linear()
    .range([0, width-200]);

for为了

   var y = d3.scale.linear()
        .range([width-200 , 0]);

just swap the parameters and it should do the trick !只需交换参数,它应该可以解决问题!

Demo Fiddle演示小提琴

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

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