我还在Google网上论坛https://groups.google.com/forum/?fromgroups#!topic/d3-js/DYiVeC544ws上发布了此问题,但看到它更喜欢此处提出的帮助问题.我只是将y轴移动到我的图形的左边,以便它不会妨碍第一列.我在Google群组中提供了一张图片,以便您可以看到我在说什么.
创建轴的代码如下:
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
Run Code Online (Sandbox Code Playgroud)
mbo*_*ock 25
使用transform属性移动y轴,就像对x轴一样.例如:
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + width + ",0)")
.call(yAxis);
Run Code Online (Sandbox Code Playgroud)
仅在使用正确方向时才需要这样做.当轴朝向左侧时,默认定位通常很好.
另外,我建议对边距使用以下约定:
width并height作为内部宽度和高度.例如:
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 + ")");
Run Code Online (Sandbox Code Playgroud)
如果您愿意,还可以存储内部和外部尺寸,例如,
var outerWidth = 960,
outerHeight = 500,
innerWidth = outerWidth - margin.left - margin.right,
innerHeight = outerHeight - margin.top - margin.bottom;
Run Code Online (Sandbox Code Playgroud)
这是对惯例的直观解释:
| 归档时间: |
|
| 查看次数: |
21245 次 |
| 最近记录: |