我已经从Mbostock示例修改了轨道布局...
但是我在将节点内的文本分为两行时遇到了问题...

我写了一个wrap函数...但是似乎不起作用....

我需要的是圆圈内的文本应以两行显示。
请指教.....

<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>

最佳答案

这是一个带有包装函数示例代码的小提琴。我不能特别了解dy的用法,因此我将它与每个tspan元素的'y'属性一起使用。一探究竟:

的HTML
    

</div>


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

关于javascript - 在d3中将文本分成两行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35838058/

10-11 23:29