通過PRATIK給出的答案也不太工作對我超樹 - 有我需要做得到節點圖像的顯示,並在超樹表示適當擴展一些小的修改。希望這個答案能夠幫助其他類似實現麻煩的用戶。 免責聲明我是一個新手程序員,很可能我錯過了Pratik答案中的一些必要的東西,它會使它工作。而且,如果沒有他的回答,我永遠不會接近,對此我很感激。
沒有更改json中的數據定義 - 按預期工作。對我來說的一個關鍵是確保設置overridable: true
,例如
var ht = new $jit.Hypertree({
Node: {
overridable: true,
dim: 9,
color: "#ffffff",
type: "square"
},
我需要圖像高度/寬度來稍後適當地縮放它們。我實現了它在由PRATIK提供的loadImages功能,因此它只能運行一次:
function loadImages(){
ht.graph.eachNode(function(node){
if(node.getData('type') == 'image'){
var img = new Image();
img.addEventListener('load', function(){
node.setData('image',img); // store this image object in node
node.setData('imageheight',this.height);
node.setData('imagewidth',this.width);
}, false);
img.src=node.getData('url');
}
});
}
沒有變化的loadImages()函數調用只是loadJSON後的時間。
我的自定義節點是這樣的:
$jit.Hypertree.Plot.NodeTypes.implement({
'image': {
'render': function(node, canvas){
var ctx = canvas.getCtx();
var pos = node.pos.getc().$scale(node.scale);
var nconfig = this.node;
if(node.getData('image') != 0){
var img = node.getData('image');
var dim = node.getData('dim');
var w = node.getData('imagewidth');
var h = node.getData('imageheight');
var r = 50/h; // Scaling factor to scale images to 50px height
w = r * w;
h = r * h;
var dist = Math.sqrt((pos.x*pos.x)+(pos.y*pos.y)); //dist to origin
var scale = 1 - (dist/node.scale);
// scale nodes based on distance to origin
var sw = nconfig.transform ? w * scale : w/dim/2;
var sh = nconfig.transform ? h * scale : h/dim/2;
if (node._depth < 2) {
ctx.drawImage(img,
pos.x - sh/2, //center images on nodes
pos.y - sw/2,
sw,
sh);
}
}
},
'contains': function(node,pos){
var npos = node.pos.getc().$scale(node.scale);
var h = node.getData('imageheight');
var w = node.getData('imagewidth');
return this.nodeHelper.rectangle.contains(npos, pos, w, h);
}
}
});
唯一懸而未決的問題是,圖像不會出現,直到我已單擊圖表後。我認爲這與img.addEventListener('load', function(){
有關,但一直未能找出造成問題的原因。任何想法都表示讚賞。