通过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(){
有关,但一直未能找出造成问题的原因。任何想法都表示赞赏。