The d3 sunburst visualization does not support drawing with inline JSON data

I've been trying to create an inline sunburst diagram, but all I get is an empty block. Could someone please review my code and provide some guidance on what might be causing this issue? Thank you for your assistance!

Essentially, I have a sample code that I modified by switching the json loading to inline json in order to make it easier to parse the data.

function generate_sunburst() {
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("#sunburst-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(saveOldData);

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

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

// Save old values for transitions
function saveOldData(d) {
  d.x0 = d.x;
  d.dx0 = d.dx;
}

// Interpolate the arcs in data space
function interpolateArc(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(){
  generate_sunburst();
});

Answer №1

If you want to make use of enter() and actually achieve something meaningful, remember to first call the data() function on your selection.

var path = svg.selectAll("path")
    .data(partition.nodes)      // Don't forget this crucial line
    .enter().append("path")

Check out this live demonstration on jsfiddle

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to conceal the contents of an HTML element

I recently applied a background image to my "a" tag, but now I'm looking to hide only the HTML content within the tag. It's important that the background image and href address remain visible. Any suggestions on how to achieve this? Thank you in ...

What could be the reason for JSON_QUERY returning a null result?

I am completely puzzled by this situation, and I can't seem to figure out the issue with my approach. My method of generating a JSON object in SQL looks like this: select @output = ( select distinct lngEmpNo, txtFullName from ...

The React form may reset, but the information entered in the writeups section stays intact

After submitting my form, I am attempting to reset it back to its original state - completely blank with no data remaining. Although I have set all variables back to their initial values in the code, the writeups on the form persist after submission. Any ...

Displaying JSON data in a TextBox using C#

As a beginner in C#, I have a question regarding my program. It involves sending an API request to a server and displaying the data received in JSON format on a TextBox. public void button2_Click(object sender, EventArgs e) { var OTPSCODE = ...

Create a div element that initially aligns to the left, expands to the full width of the window, and then shrinks back

Hi there! I am currently diving into the world of javascript and jQuery, with a specific goal in mind. I want to create functionality where a div expands to the width of the window when clicked, then shrinks back down to its original size but switches alig ...

Attempting to update using the .put() method proved futile as it has been deprecated. I then attempted to use the updateOne

.put(function(req,res) { console.log(req.body.content); Article.update( {title:req.params.specificarticle}, {title:req.body.title, content:req.body.content}, {overwrite:true}, function(err) { if(!err){ res.send("Article has been updated successfully ...

Scheduling classes based on their index or data attributes

Here is a code snippet demonstrating the use of a variable instead of a hardcoded value: var i = 1; if ($(this).attr('data-target="1"')){} To pass the variable i instead of 1, you can modify the code like this: if ($(this).attr('data-targ ...

Using Object.assign() in Internet Explorer 11 while working with Angular 2

Despite the countless discussions on this issue, I have not found a solution that works for me. This is the current content of my polyfills.browser.ts file: import 'ie-shim'; import 'core-js/es7/reflect'; import 'reflect-metadat ...

Customize HTML template with unique data fields

Hey everyone, I'm trying to create a template html file and customize certain data within it. I've been looking for ways to achieve this and the only solution I came across is shown below: <div w3-include-html="tmp1.html"></div> ...

What is the best way to generate a dynamically interpolated string in JavaScript?

I'm currently developing a reusable UI component and am exploring options to allow the user of this component to provide their own template for a specific section within it. Utilizing TypeScript, I have been experimenting with string interpolation as ...

The Android webview encountered an error: XMLHttpRequest failed to load because the specified Origin <url> is not permitted by Access-Control-Allow-Origin restrictions

I have developed an application that loads an entire website in an Android WebView. The native code in the Android project communicates with the webpage using the Android Jockey Library, and vice versa. Everything is working smoothly except for one instan ...

CoffeeScript:: I can't understand why the function body returns when using ajax

Hey there, I'm new to Coffeescript and have a question regarding Ajax. jQuery -> api = getId: -> res = [] $.ajax dataType: "jsonp" url: "http://localhost:3004/videos.json" success: (data) => ...

What methods can be used to replicate a network delay while conducting unit tests for an AngularJS controller?

I am currently utilizing the $httpBackend service within the ngMock module to emulate a GET request. The following is a sample controller code snippet sourced from the AngularJS documentation: // Example controller code function MyController($scope, $http ...

Another instance where clearInterval function fails to work in JavaScript

Having gone through numerous solutions, I am still puzzled as to why this particular one isn't working. timerId = 0; $("#dumpStartId").click(function(){ var proId = $("#curProfileId").val(); timerId = setInterval(function(){ ...

Implementing clickable table rows in React Router Link for seamless navigation

I'm relatively new to working with React. In my project, there is a Component called Content, which acts as a container for two other components - List and Profile. class Content extends Component{ <HashRouter> <Route exact path="/ ...

What is the reason for this MongoDB document consistently having the identical nanoid?

Although I've managed to find a solution, my main concern is understanding the root cause of the bug. In the scenarios below, MongoDB documents are being created with identical id and date values respectively. id: { type: String, required: tru ...

The Three.js camera imported from Collada is unable to properly focus on an object within the scene

Having some trouble grasping the concept of Collada Animation in Three.js! I have an animation with a moving camera in 3Dsmax, and exported the scene into Collada. loader.load( ColladaName, function ( collada ) { model = collada.scene; model.upda ...

Parsing JSON data with various keys

Working with JSON data from various sources can be a challenge, especially when they have different keys. For example, you may encounter: { "a": 1, "b": 2 } And also: { "c": 1, "b": 2 } In such cases, having "b" present is guaranteed. H ...

Encountering an issue in Typescript where utilizing ref in ShaderMaterial triggers an error stating: "The anticipated type is originating from the property 'ref' declared within the 'PolygonMat' type definition."

While working on a shaderMaterial with the drei library, I encountered an issue with TypeScript when using ref: Type 'RefObject<PolygonMat>' is not assignable to type 'Ref<ShaderMaterial> | undefined'. I defined the type ...

Exploring Angular's capabilities with filtering and handling $http promises

Having an issue with filtering data from a JSON file that contains an array of 20 objects. Within my factory, I have implemented two functions: function fetchData() { return $http .get('mock.json') .success(_handleData) ...