【发布时间】:2018-02-11 22:59:40
【问题描述】:
以下脚本创建一个带有三个标签和一个标题的点刻度轴。 body css 选择器定义了 body 中所有文本元素的 font-family 和 font-size。虽然轴标题受 css 规则的影响,但轴刻度标签不受影响,尽管它们本身就是文本元素。我知道我可以使用 .axis 文本选择器设置刻度标签样式。也许我在这里遗漏了一些明显的东西,但是是什么阻止了刻度标签与正文选择器一起呈现?
代码如下:
<!doctype html>
<meta charset="utf-8">
<script src="d3.min.V4.js"></script>
<style>
body {
font-family: Courier;
font-size: 18px;
}
</style>
<body>
</body>
<script>
var margin = {top: 20, right: 60, bottom: 40, left: 70},
width = 600,
height = 100;
var svg = d3.select('body').append('svg')
.attr('width', width)
.attr('height', height);
var xScale = d3.scalePoint().domain(["blue", "red", "green"]).range([margin.left, width-margin.right]);
// Add the x Axis
svg.append("g")
.attr("transform", "translate(0," + (height - margin.bottom) + ")")
.attr("class", "axis")
.call(d3.axisBottom(xScale)
);
//x axis title
svg.append('text')
.text('Colors')
.attr('x', width/2)
.attr('y', height - 5)
.style("text-anchor", "middle");
</script>
【问题讨论】:
标签: javascript css d3.js svg