【问题标题】:How to add string text and a variable in d3.js text attribute?如何在 d3.js 文本属性中添加字符串文本和变量?
【发布时间】:2020-07-02 19:29:42
【问题描述】:

我正在尝试将文本字段添加到我在 d3.js 中创建的树形图中。我知道在 C++ 中你可以做到

string str = "again";
cout << "Hello World " + str << endl;

哪个会输出

Hello World again

我正在尝试这样做是 d3.js,但我似乎可以得到它。我尝试添加另一个文本字段,但也不起作用。

这是我目前拥有的

cells.append("text")
              .attr("x", function(d) { return d.x0 +10 })
              .attr("y", function(d) { return d.y0 +27 })
              .style("font", "10px times")
              .text("Item count: " + function(d) {return d.data.itemCount})
              .attr("fill", "white")

哪个输出

Item count: function(d) {return d.data.itemCount}

但我需要它来输出,例如,

Item count: 2042

感谢您的帮助

【问题讨论】:

    标签: javascript d3.js text


    【解决方案1】:

    试试这个:

    .text(function(d) { return "Item count: " + d.data.itemCount })
    

    【讨论】:

      【解决方案2】:

      试试这个:

      cells.append("text")
                    .attr("x", function(d) { return d.x0 +10 })
                    .attr("y", function(d) { return d.y0 +27 })
                    .style("font", "10px times")
                    .text("Item count: ")
                    .append()
                    .text(function(d) {return d.data.itemCount})
                    .attr("fill", "white")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多