var m = [0, 120, 20, 320], w = 1280 - m[1] - m[3], h = 800 - m[0] - m[2], i = 0, root; var tree = d3.layout.tree() .size([h, w]); var diagonal = d3.svg.diagonal() .projection(function(d) { return [d.y, d.x]; }); var vis = d3.select("#body").append("svg:svg") .attr("width", w + m[1] + m[3]) .attr("height", h + m[0] + m[2]) .append("svg:g") .attr("transform", "translate(" + m[3] + "," + m[0] + ")"); //d3.json("../openlearn_tree_json/", function(json) { //root = json; root={'id': 1, 'children': [{'name': '11What next?', 'id': 65}, {'id': 2, 'children': [{'name': 'A satisfactory understanding of the basic building blocks of musical theory and notation', 'id': 3}, {'name': 'An understanding of music theory comparable to that demanded by Grade 3 of the Associated Board of the Royals Schools of Music theory syllabus', 'id': 4}, {'name': 'An understanding of music theory that enables you to move on to Open University Level 2 and Level 3 Music courses, e.g. A224 Inside Music', 'id': 5}], 'name': 'Learning Outcomes'}, {'name': 'Next Steps', 'id': 66}, {'id': 6, 'children': [{'name': '1.2Working through the unit', 'id': 8}, {'name': '1.1The content', 'id': 7}], 'name': '1Introduction'}, {'id': 9, 'children': [{'name': '2.1The staff', 'id': 10}, {'name': '2.2Clefs', 'id': 11}, {'name': '2.3The great staff', 'id': 12}, {'name': '2.4Middle C and ledger lines', 'id': 13}, {'name': '2.5Writing notes', 'id': 14}, {'name': '2.6Reading notes', 'id': 15}, {'name': '2.7Review 1', 'id': 16}], 'name': '2The basics'}, {'id': 34, 'children': [{'name': '5.1Notes of the bass and treble staves: a reminder', 'id': 35}, {'name': '5.2Semitones and tones, and the scale of C major', 'id': 36}, {'name': '5.3Semitones and tones, and the scale of G major', 'id': 37}, {'name': '5.4Major scales having key signatures with sharps', 'id': 38}, {'name': '5.5Semitones and tones and the scale of F major', 'id': 39}, {'name': '5.6Major scales having key signatures with flats', 'id': 40}, {'name': '5.7Review 6', 'id': 41}, {'name': '5.8Minor scales: the natural form', 'id': 42}, {'name': '5.9Minor scales: the melodic and harmonic forms', 'id': 43}, {'name': '5.10Relative minor and relative major', 'id': 44}, {'name': '5.11Review 7', 'id': 45}], 'name': '5Pitch'}, {'id': 46, 'children': [{'name': '6.2Accidentals in practice', 'id': 48}, {'name': '6.3Identifying the degrees of the scale', 'id': 49}, {'name': '6.4Intervals', 'id': 50}, {'name': '6.5The perfect fifth, the minor third and the circle of fifths', 'id': 51}, {'name': '6.1Accidentals', 'id': 47}], 'name': '6Accidentals, degrees of the scale and intervals'}, {'id': 17, 'children': [{'name': '3.1Time values', 'id': 18}, {'name': '3.2Triplets', 'id': 19}, {'name': '3.3Time signatures', 'id': 20}, {'name': '3.4Review 2', 'id': 21}, {'name': '3.5Dots and ties', 'id': 22}, {'name': '3.6Grouping and beaming notes', 'id': 23}, {'name': '3.7Review 3', 'id': 24}, {'name': '3.8More time signatures: compound time', 'id': 25}, {'name': '3.9Grouping and beaming notes in compound time', 'id': 26}, {'name': '3.10Review 4', 'id': 27}], 'name': '3Rhythm and metre'}, {'name': '7Harmony: the triad', 'id': 52}, {'id': 53, 'children': [{'name': '8.3Following piano scores in practice', 'id': 56}, {'name': '8.1A selected list of performance directions', 'id': 54}, {'name': '8.2Following piano scores', 'id': 55}], 'name': '8Scores: performance directions'}, {'id': 57, 'children': [{'name': 'Group 1: identifying various different elements', 'id': 58}, {'name': 'Group 2: identifying various different elements', 'id': 59}, {'name': 'Group 3: identifying various different elements', 'id': 60}, {'name': 'Group 4: identifying triads in major keys', 'id': 61}], 'name': '9Review 8'}, {'id': 28, 'children': [{'name': '4.4More on grouping rests', 'id': 32}, {'name': '4.5Review 5', 'id': 33}, {'name': '4.1Values and dots', 'id': 29}, {'name': '4.2Grouping rests', 'id': 30}, {'name': '4.3Grouping rests in compound time', 'id': 31}], 'name': '4Rests'}, {'id': 62, 'children': [{'name': '10.2Where to find trainers and recommended reading', 'id': 64}, {'name': '10.1How to use trainers', 'id': 63}], 'name': '10Trainers for eye and ear'}], 'name': 'An introduction to music theory'} root.x0 = h / 2; root.y0 = 0; function toggleAll(d) { if (d.children) { d.children.forEach(toggleAll); toggle(d); } } // Initialize the display to show a few nodes. root.children.forEach(toggleAll); //toggle(root.children[1]); //toggle(root.children[1].children[2]); //toggle(root.children[9]); //toggle(root.children[9].children[0]); update(root); //}); function update(source) { var duration = d3.event && d3.event.altKey ? 5000 : 500; // Compute the new tree layout. var nodes = tree.nodes(root).reverse(); // Normalize for fixed-depth. nodes.forEach(function(d) { d.y = d.depth * 280; }); // Update the nodes var node = vis.selectAll("g.node") .data(nodes, function(d) { return d.id || (d.id = ++i); }); // Enter any new nodes at the parents previous position. var nodeEnter = node.enter().append("svg:g") .attr("class", "node") .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; }) .on("click", function(d) { toggle(d); update(d); }); nodeEnter.append("svg:circle") .attr("r", 1e-6) .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; }); nodeEnter.append("svg:text") .attr("x", function(d) { return d.children || d._children ? -10 : 10; }) .attr("dy", ".35em") .attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; }) .text(function(d) { return d.name; }) .style("fill-opacity", 1e-6); // Transition nodes to their new position. var nodeUpdate = node.transition() .duration(duration) .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; }); nodeUpdate.select("circle") .attr("r", 4.5) .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; }); nodeUpdate.select("text") .style("fill-opacity", 1); // Transition exiting nodes to the parents new position. var nodeExit = node.exit().transition() .duration(duration) .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; }) .remove(); nodeExit.select("circle") .attr("r", 1e-6); nodeExit.select("text") .style("fill-opacity", 1e-6); // Update the links var link = vis.selectAll("path.link") .data(tree.links(nodes), function(d) { return d.target.id; }); // Enter any new links at the parents previous position. link.enter().insert("svg:path", "g") .attr("class", "link") .attr("d", function(d) { var o = {x: source.x0, y: source.y0}; return diagonal({source: o, target: o}); }) .transition() .duration(duration) .attr("d", diagonal); // Transition links to their new position. link.transition() .duration(duration) .attr("d", diagonal); // Transition exiting nodes to the parents new position. link.exit().transition() .duration(duration) .attr("d", function(d) { var o = {x: source.x, y: source.y}; return diagonal({source: o, target: o}); }) .remove(); // Stash the old positions for transition. nodes.forEach(function(d) { d.x0 = d.x; d.y0 = d.y; }); } // Toggle children. function toggle(d) { if (d.children) { d._children = d.children; d.children = null; } else { d.children = d._children; d._children = null; } }