Javascript D3数据格式,如可缩放的太阳爆发图

Javascript D3数据格式,如可缩放的太阳爆发图,javascript,json,d3.js,zooming,sunburst-diagram,Javascript,Json,D3.js,Zooming,Sunburst Diagram,我将数据格式化为flare.json,在本例中使用: 我只是想知道d3可缩放图表使用什么函数来获取这种格式的数据 在flare.json中是这样的 { name: "stuff", children: [ .... ] } 在这个例子中,它被转换成这个。这是哪一行 { children: Array[17] depth: 1 dx: 0.6028744305756647 dy: 0.25 name: "A name would appear he

我将数据格式化为
flare.json
,在本例中使用:

我只是想知道
d3可缩放图表使用什么函数来获取这种格式的数据

在flare.json中是这样的

{
   name: "stuff",
   children: [
    ....
   ]

}
在这个例子中,它被转换成这个。这是哪一行

{
  children: Array[17]
  depth: 1
  dx: 0.6028744305756647
  dy: 0.25
  name: "A name would appear here"
  parent: Object
  value: 39850000.06
  x: 0
  y: 0.25
}
图表

    var total_revenue = json.total_revenue;
    json = json.chart_data;
  var width = 840,
      height = width,
      radius = width / 2,
      x = d3.scale.linear().range([0, 2 * Math.PI]),
      y = d3.scale.pow().exponent(1.3).domain([0, 1]).range([0, radius]),
      padding = 5,
      duration = 1000;

  var div = d3.select("#chart_render");

  div.select("img").remove();

  var vis = div.append("svg")
      .attr("width", width + padding * 2)
      .attr("height", height + padding * 2)
    .append("g")
      .attr("transform", "translate(" + [radius + padding, radius + padding] + ")");

  var partition = d3.layout.partition()
      .value(function(d) { return d.size });

  var arc = d3.svg.arc()
      .startAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x))); })
      .endAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x + d.dx))); })
      .innerRadius(function(d) { return Math.max(0, d.y ? y(d.y) : d.y); })
      .outerRadius(function(d) { return Math.max(0, y(d.y + d.dy)); });


    console.log(json);
    var nodes = partition.nodes({children: json});

    var path = vis.selectAll("path").data(nodes);
    path.enter().append("path")
        .attr("id", function(d, i) { return "path-" + i; })
        .attr("d", arc)
        .attr("fill-rule", "evenodd")
        .style("fill", colour)
        .on("click", click);

    var text = vis.selectAll("text").data(nodes);
    var textEnter = text.enter().append("text")
        .style("fill-opacity", function(d) {
            var relative_percent = 0;
            var relative_total = 0;
            //console.log(d);

            if (d.depth != 0) {
                for(var i = 0; i < d.parent.children.length; i++) {
                    relative_total += d.parent.children[i].value;
                }
                //console.log(relative_total);
                relative_percent = d.value/total_revenue*100;

                if (relative_percent > 1) {
                    return '1';
                } else {
                    return '0';
                }
            }
        })
        .style("fill", function(d) {
          return "#fff";
        })
        .attr("text-anchor", function(d) {
          return x(d.x + d.dx / 2) > Math.PI ? "end" : "start";
        })
        .attr("dy", ".2em")
        .attr("transform", function(d) {
          var multiline = (d.name || "").split(" ").length > 1,
              angle = x(d.x + d.dx / 2) * 180 / Math.PI - 90,
              rotate = angle + (multiline ? -.5 : 0);
          return "rotate(" + rotate + ")translate(" + (y(d.y) + padding) + ")rotate(" + (angle > 90 ? -180 : 0) + ")";
        })
        .on("click", click);
    textEnter.append("tspan")
        .attr("x", 0)
        .text(function(d) { return d.depth ? d.name.split(" ")[0] : ""; });
    textEnter.append("tspan")
        .attr("x", 0)
        .attr("dy", "1em")
        .text(function(d) { return d.depth ? d.name.split(" ")[1] || "" : ""; });

    function click(d) {
      path.transition()
        .duration(duration)
        .attrTween("d", arcTween(d));

      // Somewhat of a hack as we rely on arcTween updating the scales.
      text.style("visibility", function(e) {
            return isParentOf(d, e) && e.value > 1500000 ? null : d3.select(this).style("visibility");
          })
        .transition()
          .duration(duration)
          .attrTween("text-anchor", function(d) {
            return function() {
              return x(d.x + d.dx / 2) > Math.PI ? "end" : "start";
            };
          })
          .attrTween("transform", function(d) {
            var multiline = (d.name || "").split(" ").length > 1;
            return function() {
              var angle = x(d.x + d.dx / 2) * 180 / Math.PI - 90,
                  rotate = angle + (multiline ? -.5 : 0);
              return "rotate(" + rotate + ")translate(" + (y(d.y) + padding) + ")rotate(" + (angle > 90 ? -180 : 0) + ")";
            };
          })
          .style("fill-opacity", function(e) { return isParentOf(d, e) ? 1 : 1e-6; })
          .each("end", function(e) {
            d3.select(this).style("visibility", function (d) {

          //    var relative_total = 0;
          //    var relative_percent = 0;

                // for(var i = 0; i < d.parent.children.length; i++) {
                //  relative_total += d.parent.children[i].value;
                // }

                // console.log(relative_total);

                // relative_percent = d.value/relative_total*100;
                // console.log(relative_percent);
                return isParentOf(d, e) && e.value > 1500000 ? null : "hidden";
            })
          });
    }

  function isParentOf(p, c) {
    if (p === c) return true;
    if (p.children) {
      return p.children.some(function(d) {
        return isParentOf(d, c);
      });
    }
    return false;
  }

  function colour(d) {

    if (d.depth == 0) {
      return "rgb(250, 250, 250)";
    } else if (d.depth == 1) {
      return 'rgb(86, 135, 209)';
    } else if (d.depth == 2) {
      return 'rgb(222, 120, 59)';
    } else if (d.depth == 3) {
      return 'rgb(106, 185, 117)';
    }

    // if (d.children) {
    //   // There is a maximum of two children!
    //   var colours = d.children.map(colour),
    //       a = d3.hsl(colours[0]),
    //       b = d3.hsl(colours[1]);
    //   // L*a*b* might be better here...
    //   return d3.hsl((a.h + b.h) / 2, a.s * 1.2, a.l / 1.2);
    // }
    // return d.colour || "#fff";
  }

  // Interpolate the scales!
  function arcTween(d) {
    var my = maxY(d),
        xd = d3.interpolate(x.domain(), [d.x, d.x + d.dx]),
        yd = d3.interpolate(y.domain(), [d.y, my]),
        yr = d3.interpolate(y.range(), [d.y ? 20 : 0, radius]);
    return function(d) {
      return function(t) { x.domain(xd(t)); y.domain(yd(t)).range(yr(t)); return arc(d); };
    };
  }

  function maxY(d) {
    return d.children ? Math.max.apply(Math, d.children.map(maxY)) : d.y + d.dy;
  }

  // http://www.w3.org/WAI/ER/WD-AERT/#color-contrast
  function brightness(rgb) {
    return rgb.r * .299 + rgb.g * .587 + rgb.b * .114;
  }
var total_revenue=json.total_revenue;
json=json.chart_数据;
var宽度=840,
高度=宽度,
半径=宽度/2,
x=d3.scale.linear().range([0,2*Math.PI]),
y=d3.scale.pow().指数(1.3).域([0,1]).范围([0,radius]),
填充=5,
持续时间=1000;
var div=d3。选择(“图表渲染”);
div.select(“img”).remove();
var vis=div.append(“svg”)
.attr(“宽度”,宽度+填充*2)
.attr(“高度”,高度+填充*2)
.附加(“g”)
.attr(“变换”、“平移”(+[radius+padding,radius+padding]+));
var partition=d3.layout.partition()
.value(函数(d){返回d.size});
var arc=d3.svg.arc()
.startAngle(函数(d){返回Math.max(0,Math.min(2*Math.PI,x(d.x));})
.endAngle(函数(d){返回Math.max(0,Math.min(2*Math.PI,x(d.x+d.dx));})
.innerRadius(函数(d){return Math.max(0,d.y?y(d.y):d.y);})
.outerRadius(函数(d){return Math.max(0,y(d.y+d.dy));});
log(json);
var nodes=partition.nodes({children:json});
var path=vis.selectAll(“路径”).数据(节点);
path.enter().append(“路径”)
.attr(“id”,函数(d,i){return“path-”+i;})
.attr(“d”,弧)
.attr(“填充规则”、“偶数”)
.样式(“填充”,颜色)
。开启(“点击”,点击);
var text=vis.selectAll(“text”).数据(节点);
var textEnter=text.enter().append(“文本”)
.样式(“填充不透明度”,函数(d){
var相对百分比=0;
var相对_总计=0;
//控制台日志(d);
如果(d.深度!=0){
for(var i=0;i1){
返回“1”;
}否则{
返回“0”;
}
}
})
.样式(“填充”,功能(d){
返回“#fff”;
})
.attr(“文本锚定”,函数(d){
返回x(d.x+d.dx/2)>Math.PI?“结束”:“开始”;
})
.attr(“dy”,“.2em”)
.attr(“转换”,函数(d){
var multiline=(d.name | |“”).split(“”)。长度>1,
角度=x(d.x+d.dx/2)*180/Math.PI-90,
旋转=角度+(多行?-.5:0);
返回“旋转(“+旋转+”)平移(+(y(d.y)+填充)+”)旋转(+(角度>90?-180:0)+”);
})
。开启(“点击”,点击);
textEnter.append(“tspan”)
.attr(“x”,0)
.text(函数(d){返回d.depth?d.name.split(“”[0]:“”;});
textEnter.append(“tspan”)
.attr(“x”,0)
.attr(“dy”、“1em”)
.text(函数(d){return d.depth?d.name.split(“”[1]| |“”:“”)});
功能点击(d){
path.transition()
.持续时间(持续时间)
.attrTween(“d”,arcTween(d));
//有点像黑客,因为我们依靠arcTween更新天平。
文本样式(“可见性”,功能(e){
返回isParentOf(d,e)和&e.value>1500000?null:d3.选择(this.style)(“可见性”);
})
.transition()
.持续时间(持续时间)
.attrTween(“文本锚定”,函数(d){
返回函数(){
返回x(d.x+d.dx/2)>Math.PI?“结束”:“开始”;
};
})
.attrTween(“转换”,函数(d){
var multiline=(d.name | |“”).split(“”)。长度>1;
返回函数(){
变量角度=x(d.x+d.dx/2)*180/Math.PI-90,
旋转=角度+(多行?-.5:0);
返回“旋转(“+旋转+”)平移(+(y(d.y)+填充)+”)旋转(+(角度>90?-180:0)+”);
};
})
.style(“填充不透明度”,函数(e){返回isParentOf(d,e)?1:1e-6;})
.每个(“结束”,功能(e){
d3.选择(此).样式(“可见性”,功能(d){
//var相对_总计=0;
//var相对百分比=0;
//for(var i=0;i1500000?空:“隐藏”;
})
});
}
函数isParentOf(p,c){
如果(p==c)返回true;
如果(p.儿童){
返回p.children.some(函数(d){
返回isParentOf(d,c);
});
}
返回false;
}
功能色(d){
如果(d.depth==0){
返回“rgb(250250250)”;
}否则如果(d.depth==1){
返回“rgb(86135209)”;
}否则如果(d.depth==2){
返回“rgb(222、120、59)”;
}否则,如果(d.深度==3){
返回“rgb(106185117)”;
}
//如果(d.儿童){
////最多有两个孩子!
//var COLORS=d.儿童地图(颜色),
//a=d3.hsl(颜色[0]),
//b=d3.hsl(颜色[1]);
////这里的L*a*b*可能更好。。。
//返回d3.hsl((a.h+b.h)/2,a.s*1.2,a.l/1.2);
// }
//返回d.color | |“#fff”;
}
//插值比例!
函数arcTween(d){
var my=maxY(d),
xd=d3.interpolate(x.domain(),[d。
var nodes = partition.nodes({children: json});
  var partition = d3.layout.partition()
      .value(function(d) { return d.size });
var nodes = partition.nodes({children: json});
var path = vis.selectAll("path").data(nodes);
var text = vis.selectAll("text").data(nodes);
.text(function(d) { return d.depth ? d.name.split(" ")[0] : ""; });