Roy Pardee
Roy Pardee

Reputation: 196

adapting d3 sequences sunburst example to new data & getting js error

I'm attempting to repurpose the sequences sunburst example from the d3 gallery, and am getting no viz + a js error that I don't know how to fix.

"My" code can be found here. My sole change from the original (pretty much) is using my own (right now fake) data, to depict what happens to patients who are smokers & come in to see the doc. They can discuss both smoking cessation, and lung cancer screening, each of which have their own distinct pipelines.

The error comes from the buildHierarchy() function in sequences.js. This guy consumes the original sample data provided w/the sample just fine (provided in that repo as 'sample-data.csv'--change line 46 of sequences.js to switch over to that). But when I point it at my little fake dataset, it barfs with a:

Uncaught TypeError: Cannot read property 'push' of undefined sequences.js:303

I've stepped through that function & can see that it happens after that function goes to add a leaf node for the last component of the first line in the csv. At that point in the execution, children is not set, and so the call to push() on it fails.

I don't understand why the original data does not also cause this problem, or how to fix buildHierarchy() so it will work.

Any help gratefully accepted.

Upvotes: 0

Views: 186

Answers (1)

Brad-
Brad-

Reputation: 56

This should fix it, ran into a similar problem with this for some datasets. Just added the children:[] when creating a new child node. Also it will look slightly wrong unless you add an "-End" to the one element data bits (DiscussScreening and DiscussedCessation) like how the example has Product>End

function buildHierarchy(csv) {
    var root = {
        "name": "root",
        "children": []
    };
    for (var i = 0; i < csv.length; i++) {
        var sequence = csv[i][0];
        var size = +csv[i][1];
        if (isNaN(size)) { // e.g. if this is a header row
            continue;
        }
        var parts = sequence.split("-");
        var currentNode = root;
        for (var j = 0; j < parts.length; j++) {
            var children = currentNode["children"];
            var nodeName = parts[j];
            var childNode;
            if (j + 1 < parts.length) {
                // Not yet at the end of the sequence; move down the tree.
                var foundChild = false;
                for (var k = 0; k < children.length; k++) {
                    if (children[k]["name"] == nodeName) {
                        childNode = children[k];
                        foundChild = true;
                        break;
                    }
                }
                // If we don't already have a child node for this branch, create it.
                if (!foundChild) {
                    childNode = {
                        "name": nodeName,
                        "children": []
                    };
                    children.push(childNode);
                }
                currentNode = childNode;
            } else {
                // Reached the end of the sequence; create a leaf node.
                childNode = {
                    "name": nodeName,
                    "size": size,
                    "children" : []
                };
                children.push(childNode);
            }
        }
    }
    return root;
};

Upvotes: 1

Related Questions