【问题标题】:D3.js implement ellipsis for axis labels SVGD3.js 为轴标签 SVG 实现省略号
【发布时间】:2017-02-12 07:57:35
【问题描述】:

下面是显示轴的代码

var xScale = d3.scale.ordinal()
.rangeRoundBands([0, totalWidth], barSpacing(optionsConfig.chart.barSpacing))
.domain(chartData.map(function(d) {
    return d[xValue];
}));
// Assign Scale to X Axis
xAxis.scale(xScale);

如果文本太长,我想在 x 轴上显示省略号 我指的是this链接,它有以下解决方案

function wrap() {
        var self = d3.select(this),
            textLength = self.node().getComputedTextLength(),
            text = self.text();
        while (textLength > (width - 2 * padding) && text.length > 0) {
            text = text.slice(0, -1);
            self.text(text + '...');
            textLength = self.node().getComputedTextLength();
        }
    } 
text.append('tspan').text(function(d) { return d.name; }).each(wrap);

但是任何人都可以帮助我如何实现上述代码,因为我是 d3.js 的新手,或者请建议是否可以使用 CSS 或 SVG 方法来完成。

【问题讨论】:

    标签: css d3.js svg


    【解决方案1】:

    试试这个方法。

    function wrap() {
      var self = d3.select(this),
        textLength = self.node().getComputedTextLength(),
        text = self.text();
      while (textLength > (barWidth - 2 * padding) && text.length > 0) {
        text = text.slice(0, -1);
        self.text(text + '...');
        textLength = self.node().getComputedTextLength();
      }
    }
    
    var padding = 0, barWidth = x.bandwidth();
    var xAxis = d3.select(".axis--x"); //Selector should be the class given to the x axis
    
    xAxis.selectAll(".tick")
      .selectAll("text")
      .html("")
      .append('tspan').text(function(d) {
        return d;
      }).each(wrap);
    

    var svg = d3.select("svg"),
      margin = {
        top: 20,
        right: 20,
        bottom: 30,
        left: 40
      },
      width = +svg.attr("width") - margin.left - margin.right,
      height = +svg.attr("height") - margin.top - margin.bottom;
    
    
    var x = d3.scaleBand().rangeRound([0, width]).padding(0.1),
      y = d3.scaleLinear().rangeRound([height, 0]);
    
    var g = svg.append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
    var data = [{
      "letter": "Abcdeafg Abcdeafg",
      "frequency": 0.08167
    }, {
      "letter": "Bcdefagh Bcdefagh",
      "frequency": 0.01492
    }, {
      "letter": "Cdefgahi Cdefgahi",
      "frequency": 0.02782
    }, {
      "letter": "Defghaij Defghaij",
      "frequency": 0.04253
    }, {
      "letter": "Efghaijk Efghaijk",
      "frequency": 0.12702
    }, {
      "letter": "Fghijakl Fghijakl",
      "frequency": 0.02288
    }, {
      "letter": "Ghijaklm Ghijaklm",
      "frequency": 0.02015
    }, {
      "letter": "Hijklman Hijklman",
      "frequency": 0.06094
    }, {
      "letter": "Ijklmnao Ijklmnao",
      "frequency": 0.06966
    }, {
      "letter": "Jklmnoap Jklmnoap",
      "frequency": 0.00153
    }, {
      "letter": "Klmnopqa Klmnopqa",
      "frequency": 0.00772
    }, {
      "letter": "Lmnopqar Lmnopqar",
      "frequency": 0.04025
    }, {
      "letter": "Mnopqrsa Mnopqrsa",
      "frequency": 0.02406
    }, {
      "letter": "Nopqrsta Nopqrsta",
      "frequency": 0.06749
    }, {
      "letter": "Opqrstua Opqrstua",
      "frequency": 0.07507
    }, {
      "letter": "Pqrstuva Pqrstuva",
      "frequency": 0.01929
    }, {
      "letter": "Qrstuvwa Qrstuvwa",
      "frequency": 0.00095
    }, {
      "letter": "Rstuvwxya Rstuvwxya",
      "frequency": 0.05987
    }];
    data.forEach(function(d) {
      d.frequency = +d.frequency;
      return d;
    });
    
    x.domain(data.map(function(d) {
      return d.letter;
    }));
    y.domain([0, d3.max(data, function(d) {
      return d.frequency;
    })]);
    
    g.append("g")
      .attr("class", "axis axis--x")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x));
    
    g.append("g")
      .attr("class", "axis axis--y")
      .call(d3.axisLeft(y).ticks(10, "%"))
      .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", "0.71em")
      .attr("text-anchor", "end")
      .text("Frequency");
    
    g.selectAll(".bar")
      .data(data)
      .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(d) {
        return x(d.letter);
      })
      .attr("y", function(d) {
        return y(d.frequency);
      })
      .attr("width", x.bandwidth())
      .attr("height", function(d) {
        return height - y(d.frequency);
      });
    
    function wrap() {
      var self = d3.select(this),
        textLength = self.node().getComputedTextLength(),
        text = self.text();
      while (textLength > (barWidth - 2 * padding) && text.length > 0) {
        text = text.slice(0, -1);
        self.text(text + '...');
        textLength = self.node().getComputedTextLength();
      }
    }
    
    var padding = 0, barWidth = x.bandwidth();
    var xAxis = d3.select(".axis--x");
    
    xAxis.selectAll(".tick")
      .selectAll("text")
      .html("")
      .append('tspan').text(function(d) {
        return d;
      }).each(wrap);
    .bar {
      fill: steelblue;
    }
    .bar:hover {
      fill: brown;
    }
    .axis--x path {
      display: none;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
    <svg width="960" height="500"></svg>
    <script src="https://d3js.org/d3.v4.min.js"></script>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-29
      • 2014-02-02
      • 2022-01-03
      • 1970-01-01
      • 2016-06-19
      • 2012-08-16
      • 1970-01-01
      相关资源
      最近更新 更多