我在散点图中添加x轴标签时遇到问题。它不会显示,但y轴标签会显示。
在了解如何使其发挥作用时,我从this link获取了与this in-depth explanation一致的信息。
下面是一个显示问题的最小.htm代码。 x轴标签部分:底部的评论// !!! DOES NOT SHOW UP:
。
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="http://d3js.org/d3.v2.min.js" charset="utf-8"></script>
<script>
var data = [
{x: 10.0, y: 9.14},
{x: 8.0, y: 8.14},
{x: 13.0, y: 8.74},
{x: 9.0, y: 8.77},
{x: 11.0, y: 9.26},
{x: 14.0, y: 8.10},
{x: 6.0, y: 6.13},
{x: 4.0, y: 3.10},
{x: 12.0, y: 9.13},
{x: 7.0, y: 7.26},
{x: 5.0, y: 4.74},
];
var margin = {top: 40, right: 40, bottom: 40, left: 40},
width = 960,
height = 500;
var x = pad(d3.scale.linear()
.domain(d3.extent(data, function(d) { return d.x; }))
.range([0, width - margin.left - margin.right]), 40);
var y = pad(d3.scale.linear()
.domain(d3.extent(data, function(d) { return d.y; }))
.range([height - margin.top - margin.bottom, 0]), 40);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickPadding(8);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickPadding(8);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.attr("class", "dot chart")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("cx", function(d) { return x(d.x); })
.attr("cy", function(d) { return y(d.y); })
.attr("r", 12);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + y.range()[0] + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
// !!! DOES NOT SHOW UP:
// Add the text label for the x axis
svg.append("text")
.attr("transform", "translate(" + (width / 2) + " ," + (height + margin.bottom) + ")")
.style("text-anchor", "middle")
.text("x axis label");
// text label for the y axis
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left)
.attr("x", 0 - (height / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("y axis label");
function pad(scale, k) {
var range = scale.range();
if (range[0] > range[1]) k *= -1;
return scale.domain([range[0] - k, range[1] + k].map(scale.invert)).nice();
}
</script>
答案 0 :(得分:1)
您正在翻译文本方式超出SVG高度。
必须是height - margin.bottom
,而不是height + margin.bottom
:
.attr("transform", "translate(" + (width / 2) + " ," + (height - margin.bottom) + ")")
这是您更新的代码:
path,
line {
fill: none;
stroke: black;
}
&#13;
<body>
<script src="https://d3js.org/d3.v2.min.js" charset="utf-8"></script>
<script>
var data = [{
x: 10.0,
y: 9.14
}, {
x: 8.0,
y: 8.14
}, {
x: 13.0,
y: 8.74
}, {
x: 9.0,
y: 8.77
}, {
x: 11.0,
y: 9.26
}, {
x: 14.0,
y: 8.10
}, {
x: 6.0,
y: 6.13
}, {
x: 4.0,
y: 3.10
}, {
x: 12.0,
y: 9.13
}, {
x: 7.0,
y: 7.26
}, {
x: 5.0,
y: 4.74
}, ];
var margin = {
top: 40,
right: 40,
bottom: 40,
left: 40
},
width = 960,
height = 500;
var x = pad(d3.scale.linear()
.domain(d3.extent(data, function(d) {
return d.x;
}))
.range([0, width - margin.left - margin.right]), 40);
var y = pad(d3.scale.linear()
.domain(d3.extent(data, function(d) {
return d.y;
}))
.range([height - margin.top - margin.bottom, 0]), 40);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickPadding(8);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickPadding(8);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.attr("class", "dot chart")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("cx", function(d) {
return x(d.x);
})
.attr("cy", function(d) {
return y(d.y);
})
.attr("r", 12);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + y.range()[0] + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
// Add the text label for the x axis
svg.append("text")
.attr("transform", "translate(" + (width / 2) + " ," + (height - margin.bottom) + ")")
.style("text-anchor", "middle")
.text("x axis label");
// !!! DOES NOT SHOW UP:
// text label for the y axis
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left)
.attr("x", 0 - (height / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("y axis label");
function pad(scale, k) {
var range = scale.range();
if (range[0] > range[1]) k *= -1;
return scale.domain([range[0] - k, range[1] + k].map(scale.invert)).nice();
}
</script>
&#13;