【问题标题】:Having text in D3.js rectangles在 D3.js 矩形中包含文本
【发布时间】:2022-01-16 02:41:21
【问题描述】:

我正在进行 Ajax 调用以从 API 获取数据并从返回的数据对象中绘制矩形。

如何在矩形中包含文本(对象 ID)?

        let height = window.innerHeight;
        
        var canvas = d3.select("body")
            .append("svg")
            .attr("width", "100%")
            .attr("height", height)
            
        $(document).ready(function(){
            getData();
        });
        
        function getData() {
            $.ajax({
                url: "http://api.ntjp.se/coop/api/v1/connectionPoints",
                success: function(result){
                    drawRectangles(result);
                }
            });
        }
        
        function drawRectangles(data) {
            
            var rectangles = canvas.selectAll("rect")
                .data(data)
                .enter()
                .append("rect")
                .attr("width", 80)
                .attr("height", 50)
                .style("stroke-width", "1")
                .style("fill", "none")
                .style("stroke", "black")
                .attr("y", function(d, i) { return i * 100});
            
        }

【问题讨论】:

    标签: javascript d3.js


    【解决方案1】:

    在 SVG 中,recttext 等形状不能包含其他形状。传统的做法是采用特殊元素g(组),它本身没有形状,但可以容纳多个形状元素。

    然后,您可以为每个g 元素绘制recttext,并将g 元素及其内容准确地定位在您想要的位置。

    let height = window.innerHeight;
    
    var canvas = d3.select("body")
      .append("svg")
      .attr("width", "100%")
      .attr("height", height);
    
    var data = [
      {
        "id": 4,
        "platform": "NTJP",
        "environment": "PROD",
        "snapshotTime": "2021-12-12T00:05:06+0100"
      },
      {
        "id": 5,
        "platform": "SLL",
        "environment": "PROD",
        "snapshotTime": "2021-12-12T00:00:04+0100"
      },
      {
        "id": 6,
        "platform": "NTJP",
        "environment": "QA",
        "snapshotTime": "2021-12-12T00:05:05+0100"
      },
      {
        "id": 7,
        "platform": "SLL",
        "environment": "QA",
        "snapshotTime": "2021-12-12T00:00:05+0100"
      },
      {
        "id": 8,
        "platform": "NTJP",
        "environment": "TEST",
        "snapshotTime": "2021-12-12T00:05:06+0100"
      }
    ];
    
    var nodes = canvas.selectAll(".node")
      .data(data)
      .enter()
      .append("g")
      .attr("class", "node")
      .attr("transform", function(_, i) {
        return `translate(0, ${i * 100})`;
      })
    
    nodes
      .append("rect")
      .attr("width", 80)
      .attr("height", 50)
      .style("stroke-width", "1")
      .style("fill", "none")
      .style("stroke", "black");
    
    nodes
      .append("text")
      // To make the `text` node have access to the data related to the `g` node
      .datum(function(d) { return d; })
      .attr("y", 25)
      .attr("x", 40)
      .attr("dy", "0.5em")
      .attr("text-anchor", "middle")
      .text(function(d) { return d.platform; });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/6.2.0/d3.min.js"></script>

    【讨论】:

    • 非常感谢! “变换”部分是做什么的?
    • transformg 元素和所有内容重新定位到 0px 和向下 i * 100px
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    • 2019-02-17
    • 2021-02-19
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多