【问题标题】:curved text around D3.js pie chartD3.js 饼图周围的弯曲文本
【发布时间】:2017-08-11 01:27:07
【问题描述】:

我正在构建一个 3D-Js 图表,我希望饼图文本围绕它自己。

这正是我想要的:

我有两个问题:我现在正在打印两个图表(标签打印在另一个图表中)并使用 CSS 调整它们。

-首先我不知道如何在同一个图表中设置标签,所以我用 CSS 调整它。

-我不知道如何设置正确的角度以使标签图表适合第一个图表。

如果有办法,请将它们打印在同一张图表中。

这是我的代码:

  <script src="http://d3js.org/d3.v3.min.js"></script>
  <div id = "svgContent"></div>

  <script>
    var data = [{label:"BC", value:50},{label:"Alb", value:20},{label:"Mani",value:100},{label:"Sascn", value:80},{label:"ORIO", value:20}];

    var margin = {top:40,left:40,right:40,bottom:40};
    width = 300;
    height = 300;
    radius = Math.min(width-100,height-100)/2;
    var color = d3.scale.category10().range(["#e8af92", "#a9e892"]);
    var arc = d3.svg.arc()  
      .outerRadius(radius -230)
      .innerRadius(radius - 50)
      .cornerRadius(20);
    var arcOver = d3.svg.arc()
    .outerRadius(radius +5000)
    .innerRadius(200);

    var a=width/2 - 20;
    var b=height/2 - 90;
    var svg = d3.select("#svgContent").append("svg")
      .attr("viewBox", "0 0 " + width + " " + height/2)
      .attr("preserveAspectRatio", "xMidYMid meet")
      .append("g")
      .attr("transform","translate("+a+","+b+")");

      var pie = d3.layout.pie()
      .sort(null)
      .value(function(d){return d.value;})
      .padAngle(.4);

    var g = svg.selectAll(".arc")
      .data(pie(data))
      .enter();

    g.append("path")
      .attr("d",arc)
      .style("fill",function(d){return color(d.data.value);})
      .attr("d", arc);


  </script>
<div id="datas" style="margin-top:-580px;margin-left:-10px;">
<script>


    var delta = (2*Math.PI)/5;
    var arc = d3.svg.arc()
      .innerRadius(185)
      .outerRadius(185)
      .startAngle(function(d,i){return (i)*delta;})
      .endAngle(function(d,i){return (i+1)*delta;});

       var svg = d3.select("#datas").append("svg")
      .attr("width", 960)
      .attr("height", 500)
      .selectAll("g")
      .data(data)
      .enter()
      .append("g")
      .attr("transform", "translate(480,250)rotate(00)");

    var thing = svg.append("g")
      .attr("id","thing")
      .style("fill","navy")
      .attr("class", "label");

    var arcs = svg.append("path")
      .attr("id", function(d,i){return "s"+i;})
      .attr("d",arc);

    thing.append("text")
      .style("font-size",10)
      .style("text-anchor","middle")
      .append("textPath")
      .attr("textLength",function(d,i){return d.label.length *8 ;})
      .attr("xlink:href",function(d,i){return "#s"+i;})
      .attr("startOffset",function(d,i){return "130";})
    //  .attr("dy","em")
      .text(function(d){return d.label.toUpperCase();});
</script>
</div>

【问题讨论】:

    标签: javascript css d3.js charts


    【解决方案1】:

    这里是你的代码重构了一下:

    <!DOCTYPE html>
    <html>
    
    <head>
      <link rel="stylesheet" href="style.css">
      <script src="script.js"></script>
    </head>
    
    <body>
      <script src="http://d3js.org/d3.v3.min.js"></script>
      <div id="svgContent"></div>
    
      <script>
        var data = [{
          label: "BC",
          value: 50
        }, {
          label: "Alb",
          value: 20
        }, {
          label: "Mani",
          value: 100
        }, {
          label: "Sascn",
          value: 80
        }, {
          label: "ORIO",
          value: 20
        }];
    
        var margin = {
          top: 40,
          left: 40,
          right: 40,
          bottom: 40
        };
        width = 300;
        height = 300;
        radius = Math.min(width - 100, height - 100) / 2;
        var color = d3.scale.category10().range(["#e8af92", "#a9e892"]);
        var arc = d3.svg.arc()
          .outerRadius(radius - 230)
          .innerRadius(radius - 50)
          .cornerRadius(20);
        var arcOver = d3.svg.arc()
          .outerRadius(radius + 5000)
          .innerRadius(200);
    
        var a = width / 2 - 20;
        var b = height / 2 - 90;
    
        var svg = d3.select("#svgContent").append("svg")
          .attr("viewBox", "0 0 " + width + " " + height / 2)
          .attr("preserveAspectRatio", "xMidYMid meet")
    
        var defs = svg.append("defs");
    
        svg = svg.append("g")
          .attr("transform", "translate(" + a + "," + b + ")");
    
        var pie = d3.layout.pie()
          .sort(null)
          .value(function(d) {
            return d.value;
          })
          .padAngle(.4);
          
        data = pie(data);
          
        // build out the defs
        defs.selectAll("path")
          .data(data)
          .enter()
          .append("path")
          .attr("id", function(d,i){
            return "arc" + i;
          })
          .attr("d", arc);
    
        // and the arcs
        var g = svg.selectAll(".arc")
          .data(data)
          .enter()
          .append("g");
    
        g.append("path")
          .style("fill", function(d) {
            return color(d.data.value);
          })
          .attr("d", arc);
    
        // add the texts
        g.append("text")
          .append("textPath")
          .attr("xlink:href", function(d,i){
            return "#arc" + i;
          })
          .text(function(d){
            return d.data.label;
          })
          .style("font", "8px sans-serif");
          
      </script>
    
    </body>
    
    </html>

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 2016-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多