简体   繁体   English

使用SVG在UI上添加文本

[英]Adding Text on UI with SVG

I have added a text on my UI using g element with svg. 我已经在我的UI上添加了一个文本,该文本使用带有svg的g元素。 However, I am stuck to put another text on UI. 但是,我仍然坚持在UI上放置其他文本。 Is there a way instead of copy and paste the following code to duplicate another text? 有没有一种方法可以复制并粘贴以下代码来复制另一个文本?

        var legendData = [{ color: "white", text: "MyData" }];
        var legends = svg.selectAll("g legends").data(legendData);
        var legendBox = legends.enter()
             .append("g")

        legendBox.append("text")
           .attr("dx", function (d) { return 20 })
           .attr("dy", function (d) { return 15 })
           .text(function (d) { return d.text })
           .attr("stroke", "white")



        // is it right way ????
        var legendData2 = [{ color: "white", text: "MyData2" }];
        var legends2 = svg.selectAll("g legends").data(legendData2);
        var legendBox2 = legends.enter()
             .append("g")

        legendBox2.append("text")
           .attr("dx", function (d) { return 20 })
           .attr("dy", function (d) { return 15 })
           .text(function (d) { return d.text })
           .attr("stroke", "white")

sum up based on Lars comments: 根据Lars的评论总结:

var legendData = [{ color: "white", text: "MyData", dx: 20, dy: 15 }, 
                  { color: "white", text: "MyData2", dx: 20, dy: 25 }, 
                  { color: "white", text: "MyData3", dx: 20, dy: 35 }];

    var legends = svg.selectAll("g legends").data(legendData);
    var legendBox = legends.enter()
         .append("g")

    legendBox.append("text")
       .attr("dx", function (d) { return 20 })
       .attr("dy", function (d) { return 15 })
       .text(function (d) { return d.text })
       .attr("stroke", "white")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM