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

时间:2016-03-07 07:11:15

标签: javascript html5 css3 d3.js

我从Mbostock示例修改了一个Orbit Layout ... 但我在两行中打破节点内的文本时遇到了问题...

我已经编写了一个包装函数......但似乎它不起作用....

我需要的只是圆圈内的文字应该有两行...... 请建议.....

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

1 个答案:

答案 0 :(得分:1)

这里有一个来自你的wrap函数的示例代码。我无法理解dy的使用,所以我让它与'&#39; y&#39;每个tspan元素的属性。看看:

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