在infovis javascript中下载Spacetree中的childnode

时间:2014-06-13 09:06:15

标签: java javascript infovis thejit space-tree

我是java和javascript的新手。我使用infovis Spacetree来构建一棵树。我想让父节点的子节点可以下载。我的意思是当用户单击父节点1的chilnode 1时,它应该下载从根目录存储的pdf文件。任何人都可以建议我如何实现这一目标。这是我在infovis中的spacetree的javascript代码

var labelType, useGradients, nativeTextSupport, animate;

(function() {
  var ua = navigator.userAgent,
      iStuff = ua.match(/iPhone/i) || ua.match(/iPad/i),
      typeOfCanvas = typeof HTMLCanvasElement,
      nativeCanvasSupport = (typeOfCanvas == 'object' || typeOfCanvas == 'function'),
      textSupport = nativeCanvasSupport 
        && (typeof document.createElement('canvas').getContext('2d').fillText == 'function');
   labelType = (!nativeCanvasSupport || (textSupport && !iStuff))? 'Native' : 'HTML';
  nativeTextSupport = labelType == 'Native';
  useGradients = nativeCanvasSupport;
  animate = !(iStuff || !nativeCanvasSupport);
})();
var Log = {
  elem: false,
  write: function(text){
    if (!this.elem) 
      this.elem = document.getElementById('log');
    this.elem.innerHTML = text;
    this.elem.style.left = (500 - this.elem.offsetWidth / 2) + 'px';
  }
};
function init(){

    var json = {
        id: "node1",
        name: "Stocks",
        data: {},
        children: [{
            id: "node2",
            name: "Stock1",
            data: {},
            children: [{
                id: "node3",
                name: "A",
                data: {},
                children: [{
                    id: "node4",
                    name: "child 1",
                    data: {},
                    children: []
                }, {
                    id: "node5",
                    name: "child 2",
                    data: {},
                    children: []
                }, {
                    id: "node6",
                    name: "child 3",
                    data: {},
                    children: []
                }]                
            }]
        }]};        
    var st = new $jit.ST({      
        injectInto: 'infovis',     
        duration: 800,     
        transition: $jit.Trans.Quart.easeInOut,      
        levelDistance: 50,       
        Navigation: {
          enable:true,
          panning:true
        },
            Node: {
            height: 20,
            width: 100,
            type: 'rectangle',
            color: '#aaa',
            overridable: true
        },
         Edge: {
            type: 'bezier',
            overridable: true
        },
        onBeforeCompute: function(node){
            Log.write("loading " + node.name);
        },

        onAfterCompute: function(){
            Log.write("done");
        },
        onCreateLabel: function(label, node){
            label.id = node.id;            
            label.innerHTML = node.name;
            label.onclick = function(){
                if(normal.checked) {
                  st.onClick(node.id);
                } else {
                st.setRoot(node.id, 'animate');
                }
            };
            var style = label.style;
            style.width = 60 + 'px';
            style.height = 17 + 'px';            
            style.cursor = 'pointer';
            style.color = '#333';
            style.fontSize = '0.8em';
            style.textAlign= 'center';
            style.paddingTop = '3px';
        },
        onBeforePlotNode: function(node){
            if (node.selected) {
                node.data.$color = "#ff7";
            }
            else {
                delete node.data.$color;

                if(!node.anySubnode("exist")) {

                    var count = 0;
                    node.eachSubnode(function(n) { count++; });
                    node.data.$color = ['#aaa', '#baa', '#caa', '#daa', '#eaa', '#faa'][count];                    
                }
            }
        },
        onBeforePlotLine: function(adj){
            if (adj.nodeFrom.selected && adj.nodeTo.selected) {
                adj.data.$color = "#eed";
                adj.data.$lineWidth = 3;
            }
            else {
                delete adj.data.$color;
                delete adj.data.$lineWidth;
            }
        }
    });
    st.loadJSON(json);   
    st.compute();  
    st.geom.translate(new $jit.Complex(-200, 0), "current");   
    st.onClick(st.root);   
    var top = $jit.id('r-top'), 
        left = $jit.id('r-left'), 
        bottom = $jit.id('r-bottom'), 
        right = $jit.id('r-right'),
        normal = $jit.id('s-normal');  
    function changeHandler() {
        if(this.checked) {
            top.disabled = bottom.disabled = right.disabled = left.disabled = true;
            st.switchPosition(this.value, "animate", {
                onComplete: function(){
                    top.disabled = bottom.disabled = right.disabled = left.disabled = false;
                }
            });
        }
    };
    top.onchange = left.onchange = bottom.onchange = right.onchange = changeHandler;
}

请指导我

1 个答案:

答案 0 :(得分:1)

您可以使用http://philogb.github.io/jit/static/v20/Docs/files/Visualizations/Spacetree-js.html

配置空间树

你也可以尝试这样的事情:

onCreateLabel: function(label, node){
        ...

        label.innerHTML = '<a href="">download file</a>'; 

        ...
    }

或者使JSON像:

{
"id": "Regions",
"name": "Regions<a href=Regions.aspx>XYZ</a>",
"data": {},
"children": []
}