javascript - Aster Plot 图例标签以及弧线

标签 javascript d3.js aster

我在我的项目中使用 d3 的 aster 图。 我想要图例标签以及圆外的圆弧半径。

我可以获得一个饼图示例,显示弧线沿线和弧线外的标签。 http://bl.ocks.org/Guerino1/2295263

但我无法在 d3 的 aster 图中实现相同的功能。 http://bl.ocks.org/bbest/2de0e25d4840c68f2db1

如有任何帮助,我们将不胜感激。 谢谢

最佳答案

有几个问题需要修复。

1.) 您必须在标签的 aster 图中引入边距。

2.) 然后,您必须采用外弧,添加 svg g 是否可以将路径与文本分组:

var outerGroup = svg.selectAll(".solidArc")
  .data(pie(data))
  .enter()
  .append("g")

outerGroup
  .append("path")
  .attr("fill", function(d) { return d.data.color; })
  .attr("class", "solidArc")
  .attr("stroke", "gray")
  .attr("d", arc)
  .on('mouseover', tip.show)
  .on('mouseout', tip.hide);

outerGroup
  .append("text")
  .attr("transform", function(d) {
    return "translate(" + centroid(60, width, d.startAngle, d.endAngle) + ")";
  })
  .attr("text-anchor", "middle")
  .text(function(d) { return d.data.label });

注意我必须创建自己的质心函数才能将标签移到圆弧之外。您链接的饼图示例中的代码对我不起作用(它使用旧的 d3 版本)。

这是我从 d3 源窃取的质心函数:

function centroid(innerR, outerR, startAngle, endAngle){
  var r = (innerR + outerR) / 2, a = (startAngle + endAngle) / 2 - (Math.PI / 2);
  return [ Math.cos(a) * r, Math.sin(a) * r ];
}

这是一个working example .


完整代码:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>Testing Pie Chart</title>

    <script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?2.4.5"></script>

    <style type="text/css">
        .slice text {
            font-size: 16pt;
            font-family: Arial;
        }
    </style>
</head>
<body>
<script type="text/javascript">

    var canvasWidth = 500, //width
            canvasHeight = 500,   //height
            outerRadius = 150,   //radius
            //outerRadius =  Math.min(canvasWidth, canvasHeight) / 2,
            color = d3.scale.category20(); //builtin range of colors
    innerRadius =0
    var colorsArray = ['#0099ff','#cc00ff','#ff3366','#cc3300','#ff6600','#ffff33','#cccc00','#0066ff'];
    var dataSet = [
        {"legendLabel":"Testing Text Is", "magnitude":30,'score':4.8,width:20,color:colorsArray[0] },
        {"legendLabel":"Two", "magnitude":8,'score':3.2,width:20,color:colorsArray[1] },
        {"legendLabel":"Three", "magnitude":40,'score':3.9,width:20,color:colorsArray[2] },
        {"legendLabel":"Four", "magnitude":50,'score':3.1,width:20,color:colorsArray[3] },
        {"legendLabel":"Five", "magnitude":16,'score':4.2,width:20,color:colorsArray[4] },
        {"legendLabel":"Six", "magnitude":50,'score':3.1,width:20,color:colorsArray[5] },
        {"legendLabel":"Seven", "magnitude":30,'score':4.3,width:20,color:colorsArray[6] },
        {"legendLabel":"Eight", "magnitude":20,'score':2.3,width:20,color:colorsArray[7] }
       ];

    var vis = d3.select("body")
            .append("svg:svg")
            .data([dataSet])
            .attr("width", canvasWidth)
            .attr("height", canvasHeight)
            .append("svg:g")
            .attr("transform", "translate(" + 1.5*outerRadius + "," + 1.5*outerRadius + ")") // relocate center of pie to 'outerRadius,outerRadius'

    var arc = d3.svg.arc()
            .outerRadius(outerRadius);
    var arc1 = d3.svg.arc()
            .innerRadius(innerRadius)
            .outerRadius(function (d) {
                return (outerRadius - innerRadius) * (d.data.score / 5.0) + innerRadius;
            });

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

    // Select all <g> elements with class slice (there aren't any yet)
    var arcs = vis.selectAll("g.slice")
            .data(pie)
            .enter()
            .append("svg:g")
            .attr("class", "slice");

    arcs.append("svg:path")
        //set the color for each slice to be chosen from the color function defined above
            .attr("fill", function(d, i) { return d.data.color; } )
        //this creates the actual SVG path using the associated data (pie) with the arc drawing function
            .attr("d", arc1);



  var text = arcs.append("svg:text")
            .attr("transform", function(d) {
                d.outerRadius = outerRadius + 75;
                d.innerRadius = outerRadius + 70;
                return "translate(" + arc.centroid(d) + ")";
            })
            .attr("text-anchor", "middle") //center the text on it's origin
            .style("fill", "black")
            .style("font", "bold 12px Arial")
     
    .each(function (d) {   
      var arr = d.data.legendLabel.split(" ");
      if (arr != undefined) {
          for (i = 0; i < arr.length; i++) {
              d3.select(this).append("tspan")
                  .text(arr[i])
                  .attr("dy", i ? "1.2em" : 0)
                  .attr("x", 0)
                  .attr("text-anchor", "middle")
                  .attr("class", "tspan" + i);
          }
      }
    });


    //.text(function(d, i) { return dataSet[i].legendLabel; })
         // .html(function(d, i) { return  '<tspan>'+dataSet[i].legendLabel+'</tspan></n><tspan>'+dataSet[i].score+'</tspan>'})
   /* arcs.append("foreignObject")
            .attr("transform", function(d) {
                d.outerRadius = outerRadius + 75;
                d.innerRadius = outerRadius + 70;
                return "translate(" + arc.centroid(d) + ")";
            })
            .attr("width", 50)
            .attr("height", 50)
            .append("xhtml:body")
            .style("font", "14px 'Helvetica Neue'")
            .html(function(d, i) { return dataSet[i].legendLabel+'<br>'+dataSet[i].score; });*/




</script>
</body>
</html>

关于javascript - Aster Plot 图例标签以及弧线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27873631/

相关文章:

javascript - JQuery Canvas drawImage() 不工作

javascript - 在 angular bootstrap ui typeahead 指令上触发按键事件

javascript - aster 图中重叠的标签

python - 连接到 Aster Teradata Python

javascript - 如何断言/单元测试服务器 JSON 响应?

javascript - 谷歌可视化饼图文本 anchor 问题和文本被切断

javascript - 如何在 `object` 标签内选择 SVG?

javascript - 如何访问原型(prototype)中的变量

d3.js - NV.D3 无法读取未定义的属性 'length'

sql - SQL:如何选择窗口函数以及聚合函数