d3 sunburst不使用内嵌json绘制 [英] d3 sunburst doesn't draw with inline json

查看:137
本文介绍了d3 sunburst不使用内嵌json绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图绘制一个旭日形内联,但结果始终是一个空白块.有人可以看看并提示我为什么吗?非常感谢您的帮助!

I've been trying to draw a sunburst inline but the result is always an empty block. Can someone please take a look and hint me why? Thanks so much for your help!

基本上是示例代码,我只是尝试将json加载更改为内联json,因此对我来说解析数据会更容易.

Basically it's the sample code and I only tried to change the json loading to inline json so it'll be easier for me to parse data.

function draw_chart() {
var width = 960,
    height = 700,
    radius = Math.min(width, height) / 2,
    color = d3.scale.category20c();

var data = {
  "name": "flare",
  "children": [
  {
    "name": "analytics",
    "children": [
    {
      "name": "cluster",
      "children": [
      {"name": "AgglomerativeCluster", "size": 3938},
      {"name": "CommunityStructure", "size": 3812},
      {"name": "HierarchicalCluster", "size": 6714},
      {"name": "MergeEdge", "size": 743}
      ]
    }
    ]
  }
  ]
};


var svg = d3.select("#chart").append("svg")
    .attr("width", width)
    .attr("height", height)
    .data(data)
    .append("g")
    .attr("transform", "translate(" + width / 2 + "," + height * .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.selectAll("path")
    .enter().append("path")
      .attr("display", function(d) { return d.depth ? null : 1; }) // hide inner ring
      .attr("d", arc)
      .style("stroke", "#fff")
      .style("fill", function(d) { return color((d.children ? d : d.parent).name); })
      .style("fill-rule", "evenodd")
      .each(stash);

d3.selectAll("input").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");
}

$(document).ready(function(){
  draw_chart();
});

推荐答案

问题是您需要在选择的内容上调用data()才能调用enter()并执行任何有用的操作.

The problem is that you need to call data() on your selection in order to be able to call enter() and do anything useful.

var path = svg.selectAll("path")
    .data(partition.nodes)      // this is the line you were missing
    .enter().append("path")

请参见关于jsfiddle的工作示例

这篇关于d3 sunburst不使用内嵌json绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆