简体   繁体   English

在d3中的圆圈内将文本分成两行

[英]Breaking text into two lines inside circle in d3

I have modified an Orbit Layout from Mbostock Examples... But I have a problem in breaking text inside the node in two lines...我已经修改了 Mbostock 示例中的轨道布局......但是我在将节点内的文本分成两行时遇到了问题......

I have written a wrap function...but it seems that it doesn't work....我写了一个 wrap 函数...但它似乎不起作用....

All I need is the text inside the circle should come in Two lines.... Please advice.....我所需要的只是圆圈内的文字应该分成两行....请指教.....

<html>
<head>
    <title>Orbit Layout Modes</title>
    <meta charset="utf-8" />
</head>
<style>
    #viz, svg {
        width: 1000px;
        height: 1000px;
        padding-left:50px;
    }


    text {
        pointer-events: none;
    }

    #buttons {
        position: absolute;
        top: 0;
        left: 0;
    }

    circle.ring {
        fill: none;
        stroke: black;
        stroke-width: 2.5px;
        stroke-opacity: 0.5;
        padding-left: 70px;
    } 
</style>
<script>


    function makeViz() {

        d3.json("/NewFolder1/solarsystem.txt", function (data) { drawOrbit(data) });


    }

    function drawOrbit(_data) {

        orbitScale = d3.scale.linear()
                        .domain([0.5, 3])
                        .range([2.8, 0.5])
                        .clamp(false);

        radiusScale = d3.scale.linear()
                        .domain([210.64, 2500, 10000, 71492.68,8000])
                        .range([2+2, 4+2, 8+2, 16+2,32+2]);


       var planetColors = { "Application Management": "#004A80", "Designer Tools": "#009BC9", "Application Assembler": "#00CE7D", "Common Services": "yellow" }


        orbit = d3.layout.orbit()
                .size([350, 350])
                .children(function (d) { return d.values })
                .revolution(function (d) { return 1 / d.orbital_period })
                .orbitSize(function (d) { return orbitScale(d.depth) })
                .speed(.5)
                .mode(" ")
                .nodes(_data);

        d3.select("svg")
          .append("g")
          .attr("class", "viz")
          .attr("transform", "translate(50,50)")
          .selectAll("g.node").data(orbit.nodes())
          .enter()
          .append("g")
          .attr("class", "node")
          .attr("transform", function (d) { return "translate(" + d.x + "," + d.y + ")" })
          .on("mouseover", nodeOver)
          .on("mouseout", nodeOut)

        d3.selectAll("g.node")
        .append("circle")
        .attr("r", function (d) { return d.radius ? radiusScale(d.radius) : 70 })
        .style("fill", function (d) { return d.depth == 0 ? "#21E2EA" : d.depth == 1 ? planetColors[d.key] : "blue" });


        d3.selectAll("g.node").filter(function (d) { return d.depth == 0 })
       .append("text")
        .attr("x", -55)
        .attr("y", 10)
        //.style("text-anchor", "middle")
        .text("PLANET TORUS")
        .style("fill", "White")
         .call(wrap, 100);

        d3.selectAll("g.node").filter(function (d) { return d.depth == 1 })
        .append("text")
        .text(function (d) { return d.key })
        .attr("y", 30)
        .style("text-anchor", "middle")
        .style("fill", function (d) { return planetColors[d.key] });

        d3.select("g.viz")
        .selectAll("circle.ring")
        .data(orbit.orbitalRings())
        .enter()
        .insert("circle", "g")
        .attr("class", "ring")
        .attr("r", function (d) { return d.r })
        .attr("cx", function (d) { return d.x })
        .attr("cy", function (d) { return d.y })

        //d3.select("#buttons").append("button").html("solar").on("click", function () { newMode("solar") })
        //d3.select("#buttons").append("button").html("flat").on("click", function () { newMode("flat") })
        //d3.select("#buttons").append("button").html("atomic").on("click", function () { newMode("atomic") })
        //d3.select("#buttons").append("button").html("custom").on("click", function () { newMode([4, 4]) })

        orbit.on("tick", function () {
            d3.selectAll("g.node")
           .attr("transform", function (d) { return "translate(" + d.x + "," + d.y + ")" });
            d3.selectAll("circle.ring")
            .attr("cx", function (d) { return d.x })
            .attr("cy", function (d) { return d.y });
        });

        orbit.start();


        function wrap(text, width)
        {
            text.each(function ()
            {
                var text = d3.select(this),
                    words = text.text().split(/\s+/).reverse(),
                    word,
                    line = [],
                    lineNumber = 1,
                    lineHeight = 2.1, // ems
                    y = text.attr("y"),
                    dy = parseFloat(text.attr("dy")),
                    tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
                while (word = words.pop())
                {
                    line.push(word);
                    tspan.text(line.join(" "));
                    if (tspan.node().getComputedTextLength() > width)
                     {
                        line.pop();
                        tspan.text(line.join(" "));
                        line = [word];
                        tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
                    }
                }
            });
        }

        function type(d) {
            d.value = +d.value;
            return d;
        }

        function newMode(_mode)
        {
            orbit.mode(_mode)
                 .nodes(_data);

            d3.select("g.viz")
              .selectAll("circle.ring")
              .data(orbit.orbitalRings())
              .exit()
              .transition()
              .duration(500)
              .style("stroke-opacity", 0)
              .style("stroke-width", 3)
              .remove();

            d3.select("g.viz")
            .selectAll("circle.ring")
            .data(orbit.orbitalRings())
            .enter()
            .insert("circle", "g")
            .attr("class", "ring");

            d3.selectAll("circle.ring")
            .attr("r", function (d) { return d.r })
            .attr("cx", function (d) { return d.x })
            .attr("cy", function (d) { return d.y });

        }

        function nodeOver(d) {
            orbit.stop();

            if (d.depth == 2) {
                d3.select(this).append("text").text(d.label || d.key).style("text-anchor", "middle")
                .attr("y", 15)
                .attr("class", "moon");
            }
            d3.select(this).select("circle").style("stroke", "white  ").style("stroke-width", 3);
        }

        function nodeOut() {
            orbit.start();
            d3.selectAll("text.moon").remove();
            d3.selectAll("g.node > circle").style("stroke", "none").style("stroke-width", 0);
        }


    }

</script>
<body onload="makeViz()">
    <div id="viz">
        <svg></svg><div id="buttons"></div>
    </div>
    <footer>
        <script src="../D3/d3.min.js" charset="utf-8" type="text/javascript"></script>
        <script src="d3.layout.orbit.js" charset="utf-8" type="text/javascript"></script>
    </footer>
</body>
</html>

here is a fiddle with a sample code from your wrap function.这是一个来自你的 wrap 函数的示例代码的小提琴。 I couldnt understand the use of dy in particular, so I made it work with 'y' attribute of each tspan element.我无法特别理解 dy 的用法,所以我让它与每个 tspan 元素的 'y' 属性一起使用。 Check it out:一探究竟:

HTML HTML

</div>

JS JS

var data = ["lorem ipsem","dolor sit","quo ubique"]
    var svg = d3.select("div.svg").append("svg")
        .append("g")
      .attr("class","node").attr("transform", function (d) { return "translate(" + 10 + "," + 10 + ")" });

    svg.selectAll("g.node")
        .data(data)
      .enter()
      .append("text")
      .attr("y", 0)
      .attr("x", function(d,i){return 100*i;})
      .text(function(d){return d;})
      .call(wrap, 10);

       function wrap(text, width)
        {
            text.each(function ()
            {
                var text = d3.select(this),
                    words = text.text().split(/\s+/).reverse(),
                    word,
                    line = [],
                    lineNumber = 1,
                    lineHeight = 2,
                    x = text.attr("x"),
                    y = text.attr("y");
                    tspan = text.text(null).append("tspan").attr("x", x).attr("y", y);

                while (word = words.pop())
                {
                    line.push(word);
                    tspan.text(line.join(" "));
                    if (tspan.node().getComputedTextLength() > width)
                     {
                        line.pop();
                        tspan.text(line.join(" "));
                        line = [word];
                        tspan = text.append("tspan").attr("x", x).attr("y", ++lineNumber * lineHeight + y).text(word);
                    }
                }
            });
        }

DEMO演示

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

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