调整标签之间的距离

D3.js Adjust labels closer to each other

本文关键字:距离 之间 标签 调整      更新时间:2023-09-26

我用长y轴标签(温度范围)绘制数据。如何调整标签,使它们在旋转90度时位于刻度中心,而不会偏离图形的边缘?我尝试使用this.getBBox()来应用基于标签迭代的转换,但这似乎不起作用。

谢谢!

    <!DOCTYPE html>
    <meta charset="utf-8">
    <style>
    
    .axis text {
      font: 10px sans-serif;
    }
    
    .axis path,
    .axis line {
      fill: none;
      stroke: #000;
      shape-rendering: crispEdges;
    }
    
    </style>
    <body>
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script>
    
    var margin = {top: 100, right: 100, bottom: 100, left: 100},
        width = 960 - margin.left - margin.right,
        height = 500 - margin.top - margin.bottom;
    
    var categories = {
                    temperature: ["29-30°C - Red","31-33° C - Green","> 33° C - Blue"],
                    sleep: ["7 - 8 hours", "5 - 6 hours", "4 or less hours"]
                    }
    
    var mindate = new Date(2012,0,1),
                maxdate = new Date(2012,0,31);
    
    var xScale = d3.time.scale()
            .domain([mindate, maxdate])    // values between for month of january
            .range([0, width - margin.right]);
    
    var xAxis = d3.svg.axis()
                .orient("bottom")
                .scale(xScale);
    
    var yKey = "temperature";
    var yScale = d3.scale.ordinal()
        .domain(categories[yKey])
        .rangePoints([0, height]);
    
    var yAxis = d3.svg.axis()
        .scale(yScale)
        .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 + ")");
    
    svg.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis);
    
    svg.append("g")
        .attr("class", "yAxis axis")
        .call(yAxis)
        .selectAll("text")
         .attr("x", 10)
         .attr("y",20)
         .attr("transform", "rotate(90)")
         .style("text-anchor", "start")
    </script>

这是我尝试过的,但这似乎没有移动标签太多。我不确定如何在不使用翻译的情况下设置以下函数中的"x"。

d3.selectAll(".yAxis text")
        .attr("transform", function(d,i) {
          return "translate(" + -20 + "," + ((this.getBBox().width/(i+2))) + ")rotate(-90)"
        })

啊哈!答案是d3.scale。序数有一个内边距设置,所以我要做的就是把yScale改成

var yScale = d3.scale.ordinal()
            .domain(categories[yKey])
            .rangePoints([0, height],1); 

 <!DOCTYPE html>
    <meta charset="utf-8">
    <style>
    
    .axis text {
      font: 10px sans-serif;
    }
    
    .axis path,
    .axis line {
      fill: none;
      stroke: #000;
      shape-rendering: crispEdges;
    }
    
    </style>
    <body>
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script>
    
    var margin = {top: 100, right: 100, bottom: 100, left: 100},
        width = 960 - margin.left - margin.right,
        height = 500 - margin.top - margin.bottom;
    
    var categories = {
                    temperature: ["29-30°C - Red","31-33° C - Green","> 33° C - Blue"],
                    sleep: ["7 - 8 hours", "5 - 6 hours", "4 or less hours"]
                    }
    
    var mindate = new Date(2012,0,1),
                maxdate = new Date(2012,0,31);
    
    var xScale = d3.time.scale()
            .domain([mindate, maxdate])    // values between for month of january
            .range([0, width - margin.right]);
    
    var xAxis = d3.svg.axis()
                .orient("bottom")
                .scale(xScale);
    
    var yKey = "temperature";
    var yScale = d3.scale.ordinal()
        .domain(categories[yKey])
        .rangePoints([0, height],1);
    
    var yAxis = d3.svg.axis()
        .scale(yScale)
        .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 + ")");
    
    svg.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis);
    
    svg.append("g")
        .attr("class", "yAxis axis")
        .call(yAxis)
        .selectAll("text")
         .attr("x", 10)
         .attr("y",20)
         .attr("transform", "rotate(90)")
         .style("text-anchor", "middle")
    </script>