I am wanting to get the JSON data from an external .json file. I cant
get this to work.
I can get the file successfully but the SpaceTree top node on the only
node that is loaded and is undefined.
If I do an "alert" of the data returned from the jQuery getJSON call
the alert displays [object Object].
This is the code:
JSON:
{"id": "node02",
"name": "1.0",
"data": {"name": "Joe Bloggs", "title": "CEO of Allianz UK"},
"children": []
}
-----------------------------------------
I have simplified it for testing
-------------------------------
The js code:
function init(){
var jsonfile = "orgchart-example.json";
var json = $.getJSON(jsonfile, function(data) {
// alert("success"+ json);
return data;
})
.success(function() { alert("second success"); })
.error(function(status) { alert("error" + status); })
.complete(function() { alert("complete"); });
alert(json);
//Create a new ST instance
var st = new $jit.ST({
//id of viz container element
injectInto: 'org-chart-container',
//set distance between node and its children
levelDistance: 50,
//set the orentation for root to be at top
orientation: 'top',
align:'center',
siblingOffset:14,
//set node, edge and label styles
//set overridable=true for styling individual
//nodes or edges
Node: {
overridable: true,
// type: 'stroke-rect',
type: 'rectangle',
height: 50,
width: 170,
//canvas specific styles
CanvasStyles: {
fillStyle: '#fff',
strokeStyle: '#ccc',
lineWidth: 1
}
},
Edge: {
type: 'bezier',
overridable: true,
color: '#c3c3c3',
lineWidth: 1
},
Label: {
type: 'HTML'
},
onBeforeCompute: function(node){
Log.write("loading " +
node.name);
},
onAfterCompute: function(){
Log.write("done");
},
onCreateLabel: function(label, node){
label.className = 'member';
label.id =
node.id;
label.innerHTML =
node.name;
if(!node.anySubnode("exist")) {
//count the number of subnodes
var count = 0;
node.eachSubnode(function(n) { count++; });
//append th more sub nodes icon
$("<span class='more'>[" + count + "]</span>").appendTo(label);
$(label).addClass("more");
}
else{
}
label.onclick = function(){
st.onClick(
node.id);
};
},
onPlaceLabel: function(label, node) {
},
onBeforePlotNode: function(node){
//add some color to the nodes in the path between the
//root node and the selected node.
if (node.selected) {
// node.data.$color = "#ff7";
//set the node style when selected
$('#' +
node.id ).addClass("selected");
}
else {
//remove the selected styling
$('#' +
node.id ).removeClass("selected");
}
},
onBeforePlotLine: function(adj){
if (adj.nodeFrom.selected && adj.nodeTo.selected) {
//adj.data.$color = "#009ee0";
adj.data.$color = "#003781";
adj.data.$lineWidth = 2;
}
else {
delete adj.data.$color;
delete adj.data.$lineWidth;
}
}
});
//load json data
st.loadJSON(json);
//compute node positions and layout
st.compute();
//emulate a click on the root node.
st.onClick(st.root);
}//END of init() function
$(document).ready(function() {
// Handler for .ready() called.
init();
});
Any help on this would be very helpful.
many thanks