Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用d3.js将标签添加到图表中?_Javascript_Svg_D3.js_Label_Sunburst Diagram - Fatal编程技术网

Javascript 如何使用d3.js将标签添加到图表中?

Javascript 如何使用d3.js将标签添加到图表中?,javascript,svg,d3.js,label,sunburst-diagram,Javascript,Svg,D3.js,Label,Sunburst Diagram,我正在制作一个用于创建Sunburst图表的交互式工具,如d3.js、svg和JQuery。绘制图表的代码来自该页面,只做了一些小的修改。我试图在图表的各个部分上绘制文本标签,但尽管元素显示在Web Inspector(Chrome)中,但它们在屏幕上不可见。我曾尝试从中改编代码,在某种程度上这是可行的(Web Inspector说元素是存在的),但我对元素本身为什么不出现感到困惑。这是我的代码-图形标签部分位于底部附近。我只想使用带有标签的示例页面中的代码,但是布局非常不同,我必须从头开始 v

我正在制作一个用于创建Sunburst图表的交互式工具,如d3.js、svg和JQuery。绘制图表的代码来自该页面,只做了一些小的修改。我试图在图表的各个部分上绘制文本标签,但尽管元素显示在Web Inspector(Chrome)中,但它们在屏幕上不可见。我曾尝试从中改编代码,在某种程度上这是可行的(Web Inspector说元素是存在的),但我对元素本身为什么不出现感到困惑。这是我的代码-图形标签部分位于底部附近。我只想使用带有标签的示例页面中的代码,但是布局非常不同,我必须从头开始

var width = 850,
height = 850,
radius = Math.min(width, height) / 2;

var svg = d3.select("#vis-wrap")
    .insert("svg", "*")
    .attr("width", width)
    .attr("height", height)
    .append("g")
    .attr("transform", "translate(" + width / 2 + "," + height * 0.52 + ")");

var partition = d3.layout.partition()
    .sort(null)
    .size([2 * Math.PI, radius * radius])
    .value(function(d) { return 1; });

var arc = d3.svg.arc()
    .startAngle(function(d) { return d.x; })
    .endAngle(function(d) { return d.x + d.dx; })
    .innerRadius(function(d) { return Math.sqrt(d.y); })
    .outerRadius(function(d) { return Math.sqrt(d.y + d.dy); });

var path = svg.datum(data).selectAll("path")
.data(partition.nodes)
.enter().append("path")
.attr("display", function(d) { return d.depth ? null : "none"; }) // hide inner ring
.attr("d", arc)
.style("stroke", "#fff")
.style("fill", function(d) {return d.color;} )
.style("fill-rule", "evenodd")
.each(stash);

// Problem here
path.insert("text", "*")
    .attr("transform", function(d) { return "rotate(" + (d.x + d.dx / 2 - Math.PI / 2) / Math.PI * 180 + ")"; })
    .attr("x", function(d) { return Math.sqrt(d.y); })
    .attr("dx", "6") // margin
    .attr("dy", ".35em") // vertical-align
    .text(function(d) { return d.name; });

d3.selectAll("input[name=mode]").on("change", function change() {
    var value = this.value === "count"
        ? function() { return 1; }
        : function(d) { return d.size; };

    path.data(partition.value(value).nodes)
        .transition()
        .duration(1500)
        .attrTween("d", arcTween);
  });


// Stash the old values for transition.
function stash(d) {
  d.x0 = d.x;
  d.dx0 = d.dx;
}

// Interpolate the arcs in data space.
function arcTween(a) {
  var i = d3.interpolate({x: a.x0, dx: a.dx0}, a);
  return function(t) {
    var b = i(t);
    a.x0 = b.x;
    a.dx0 = b.dx;
    return arc(b);
  };
}

d3.select(self.frameElement).style("height", height + "px");

您正在使文本成为路径的子对象,即

<path ...>
   <text>something</text>
</path>

某物
恐怕那是无效的。您需要使文本元素成为同级元素

令人困惑的是,您调用了创建svg的
元素,但它希望是该元素的子元素,即

svg.insert(“文本”)


而不是路径。插入(“文本”)

谢谢,这就像做梦一样!不过,我可能应该重命名我的“svg”变量…:)