Javascript d3.js - 在堆叠图上移动 y 轴
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11252614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
d3.js - shifting y-axis on stacked graph chart
提问by Apollo
I also posted this question on the Google Group https://groups.google.com/forum/?fromgroups#!topic/d3-js/DYiVeC544ws, but saw that it prefers that help questions are asked here. I'm just having trouble shifting the y-axis to the left of my graph to so that it does not get in the way of the first column. I provided an image on the google group so you can see what I'm talking about.
我也在 Google Group https://groups.google.com/forum/?fromgroups#!topic/d3-js/DYiVeC544ws上发布了这个问题,但看到它更喜欢在此处提出帮助问题。我只是在将 y 轴移动到我的图表左侧时遇到了麻烦,这样它就不会妨碍第一列。我在 google group 上提供了一张图片,所以你可以看到我在说什么。
The code to create the axis is as follows:
创建轴的代码如下:
var MARGINS = {top: 20, right: 20, bottom: 20, left: 60}; // margins around the graph
var xRange = d3.scale.linear().range([MARGINS.left, width - MARGINS.right]), // x range function
yRange = d3.scale.linear().range([height - MARGINS.top, MARGINS.bottom]), // y range function
xAxis = d3.svg.axis().scale(xRange).tickSize(16).tickSubdivide(true), // x axis function
yAxis = d3.svg.axis().scale(yRange).tickSize(10).orient("right").tickSubdivide(true); // y axis function
// create the visualization chart
var vis = d3.select("#chart")
.append("svg")
.attr("width", width)
.attr("height", height + margin);
// add in the x axis
vis.append("svg:g") // container element
.attr("class", "x axis") // so we can style it with CSS
.attr("transform", "translate(0," + height + ")") // move into position
// .call(xAxis); // add to the visualisation
// add in the y axis
vis.append("svg:g") // container element
.attr("class", "y axis") // so we can style it with CSS
.call(yAxis); // add to the visualisation
回答by mbostock
Use the transform attributeto shift the y-axis, just as you did for the x-axis. For example:
使用transform 属性移动 y 轴,就像您对 x 轴所做的一样。例如:
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + width + ",0)")
.call(yAxis);
This is only necessary when using right orientation. When the axis is oriented to the left, the default positioning is typically fine.
这仅在使用正确方向时才需要。当轴指向左侧时,默认定位通常很好。
Also, I recommend using the following convention for margins:
另外,我建议对边距使用以下约定:
- Set the origin of the SVG element to the top-left corner of the inner chart area.
- Define
width
andheight
as the inner width and height.
- 将 SVG 元素的原点设置为内部图表区域的左上角。
- 定义
width
和height
作为内部宽度和高度。
For example:
例如:
var margin = {top: 20, right: 10, bottom: 20, left: 10},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
If you prefer, you could also store the inner and outer dimensions, e.g.,
如果您愿意,您还可以存储内部和外部尺寸,例如,
var outerWidth = 960,
outerHeight = 500,
innerWidth = outerWidth - margin.left - margin.right,
innerHeight = outerHeight - margin.top - margin.bottom;
Here's a visual explanation of the convention:
这是公约的视觉解释: