简体   繁体   中英

d3.js how to dynamically add nodes to a tree #2

<?php
//Zoomable Partition Layout
//http://mbostock.github.io/d3/talk/20111018/partition.html
echo $data
?>

<div id="partition">
<script type="text/javascript">


var w = 690,
h = 700,
x = d3.scale.linear().range([0, w]),
y = d3.scale.linear().range([0, h]);

var vis = d3.select("#partition").append("div")
.attr("class", "chart")
.style("width", w + "px")
.style("height", h + "px")
.append("svg:svg")
.attr("width", w)
.attr("height", h);

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

d3.json("flare.json" , function(root) {

var g = vis.selectAll("g")
  .data(partition.nodes(root))
.enter().append("svg:g")
  .attr("transform", function(d) { return "translate(" + x(d.y) + "," + y(d.x) + ")"; })
  .on("click", click);

var kx = w / root.dx,
  ky = h / 1

I don't want to use "d3.json("flare.json" , function(root) {" because I get the data with jQuery and saved it in $data. So instead of d3.json I want to use "json = JSON.parse('<'?php echo $data ?>');" but I don't know how it works. Can someone help me?

<script type="text/javascript">
var jdata='${jsonvalue}' //json value 
d3.json("source dummy.json", function(json) {  // dummy.json for function call but processing json of jdata
root = JSON.parse(jdata);
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);
update(root);
});
</script>
Normally in D3 d3.json(function) must read the data from flare.json.For this case dymanically load the json [jdata] and processing the data for visualization

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM