旋转的x轴标签在d3.js条形图

时间:2015-09-12 14:49:02

标签: javascript d3.js

我在d3.js中有一个条形图。 x轴标签的文本太长并且彼此重叠,所以我通过将以下4个属性添加到x轴文本来旋转x轴文本:

      .selectAll("text")  
            .style("text-anchor", "end")
            .attr("dx", "-.8em")
            .attr("dy", ".15em")
            .attr("transform", "rotate(-65)" );

问题是分配给x轴标签的空间高度没有扩大,所以现在我只看到标签的最后几个字符。如何扩展分配给x轴标签的空间,使其扩展以显示所有旋转的文本?

这里是完整的d3.js代码:

var margin = {top: 20, right: 20, bottom: 30, left: 40},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var x = d3.scale.ordinal()
    .rangeRoundBands([0, width], .1);

var y = d3.scale.linear()
    .range([height, 0]);

var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom");

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left");

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 + ")");

d3.csv("segmentcount.csv", type, function(error, data) {
  if (error) throw error;

  x.domain(data.map(function(d) { return d.name; }));
  y.domain([0, d3.max(data, function(d) { return d.count; })]);

  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis)
      .selectAll("text")  
            .style("text-anchor", "end")
            .attr("dx", "-.8em")
            .attr("dy", ".15em")
            .attr("transform", "rotate(-65)" );

  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Count");

  svg.selectAll(".bar")
      .data(data)
    .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(d) { return x(d.name); })
      .attr("width", x.rangeBand())
      .attr("y", function(d) { return y(d.count); })
      .attr("height", function(d) { return height - y(d.count); })
      .style('fill', function(d) { return '#' + d.colour } );
});

非常感谢!

1 个答案:

答案 0 :(得分:2)

找到最大标签高度,并相应地转换x轴。 (因为标签是旋转的,我们比较标签宽度)。

代码段来说明:

<html>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<script>
	var margin = {top: 20, right: 20, bottom: 30, left: 40},
		width = 960 - margin.left - margin.right,
		height = 500 - margin.top - margin.bottom;

	var x = d3.scale.ordinal()
		.rangeRoundBands([0, width], .1);

	var xAxis = d3.svg.axis()
		.scale(x)
		.orient("bottom");

	var data = [
		{name: "long name", count: 20},
		{name: "name", count: 30},
		{name: "even longer name", count: 10},
		{name: "long name 4", count: 6},
		{name: "long name 5", count: 17},
		{name: "long name 6", count: 30},
		{name: "long name 7", count: 45}];

	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 + ")");

	x.domain(data.map(function (d) {
		return d.name;
	}));

	var gXAxis = svg.append("g")
		.attr("class", "x axis")
		.call(xAxis);
	gXAxis.selectAll("text")
		.style("text-anchor", "end")
		.attr("dx", "-.8em")
		.attr("dy", ".15em")
		.attr("transform", "rotate(-65)");

	// Find the maxLabel height, adjust the height accordingly and transform the x axis.
	var maxWidth = 0;
	gXAxis.selectAll("text").each(function () {
		var boxWidth = this.getBBox().width;
		if (boxWidth > maxWidth) maxWidth = boxWidth;
	});
	height = height - maxWidth;

	gXAxis.attr("transform", "translate(0," + height + ")");

	var y = d3.scale.linear()
		.range([height, 0]);

	y.domain([0, d3.max(data, function (d) {
		return d.count;
	})]);

	var yAxis = d3.svg.axis()
		.scale(y)
		.orient("left");

	svg.append("g")
		.attr("class", "y axis")
		.call(yAxis)
		.append("text")
		.attr("transform", "rotate(-90)")
		.attr("y", 6)
		.attr("dy", ".71em")
		.style("text-anchor", "end")
		.text("Count");

	svg.selectAll(".bar")
		.data(data)
		.enter().append("rect")
		.attr("class", "bar")
		.attr("x", function (d) {
			return x(d.name);
		})
		.attr("width", x.rangeBand())
		.attr("y", function (d) {
			return y(d.count);
		})
		.attr("height", function (d) {
			return height - y(d.count);
		})
		.style('fill', "blue");
</script>
</body>
</html>