Project

General

Profile

Download (5.41 KB) Statistics
| Branch: | Tag: | Revision:
1

    
2

    
3
window.eu_etaxonomy_cdm_vaadin_jscomponent_D3CTree = function() {
4
    var connector = this;
5
    var diagramElement = connector.getElement();
6
    var margin = {top: 20, right: 120, bottom: 20, left: 120},
7
    width = 960 - margin.right - margin.left,
8
    height = 800 - margin.top - margin.bottom;
9

    
10
    var i = 0,
11
    duration = 750,
12
    root;
13

    
14
    var tree;
15
    var diagonal;
16

    
17
    var svg = d3.select(diagramElement).append("svg")
18
    .attr("width", width + margin.right + margin.left)
19
    .attr("height", height + margin.top + margin.bottom)
20
    .append("g")
21
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
22

    
23
    var selectedNode;
24
    
25
    var orientations = {
26
            "topbottom": {
27
                size: [width, height],
28
                x: function(d) { return d.x; },
29
                y: function(d) { return d.y; }
30
            },
31
            "rightleft": {
32
                size: [height, width],
33
                x: function(d) { return width - d.y; },
34
                y: function(d) { return d.x; }
35
            },
36
            "bottomtop": {
37
                size: [width, height],
38
                x: function(d) { return d.x; },
39
                y: function(d) { return height - d.y; }
40
            },
41
            "leftright": {
42
                size: [height, width],
43
                x: function(d) { return d.y; },
44
                y: function(d) { return d.x; }
45
            }
46
    };
47
    // default setting is left-right
48
    var orientation = orientations.rightleft;    
49
    var tAnchorWithChildren = "end";
50
    var tAnchorWithoutChildren = "start";
51
    var dirMult = 1;
52
    this.onStateChange = function() {
53
        crTree = this.getState().conceptRelationshipTree;
54

    
55
        if(crTree) {
56
            root = JSON.parse(connector.getState().conceptRelationshipTree);
57
                        
58
            if(root.direction === "left-right") {               
59
                orientation = orientations.leftright;       
60
                tAnchorWithChildren = "end";
61
                tAnchorWithoutChildren = "start";
62
                dirMult = 1;
63
            }
64
            if(root.direction === "right-left") {                
65
                orientation = orientations.rightleft;      
66
                tAnchorWithChildren = "start";
67
                tAnchorWithoutChildren = "end";
68
                dirMult = -1;
69
            }
70
                        
71
            diagonal = d3.svg.diagonal().projection(function(d) { return [orientation.x(d), orientation.y(d)]; });            
72
            tree = d3.layout.tree().size(orientation.size);
73

    
74

    
75
            update(root);
76
            d3.select(self.frameElement).style("height", "800px");
77
        }           
78
    }
79

    
80
    
81
    function update(source) {
82

    
83
        // Compute the new tree layout.
84
        var nodes = tree.nodes(root).reverse(),
85
        links = tree.links(nodes);
86

    
87
        // Normalize for fixed-depth.
88
        nodes.forEach(function(d) { d.y = d.depth * 180; });
89

    
90
        // Update the nodes…
91
        var node = svg.selectAll("g.node")
92
        .data(nodes, function(d) { return d.id || (d.id = ++i); });
93

    
94

    
95
        // Enter any new nodes at the parent's previous position.
96
        var nodeEnter = node.enter().append("g")
97
        .attr("class", "node")
98
        .attr("transform", function(d) { return "translate(" + orientation.x(source) + "," + orientation.y(source) + ")"; })
99
        .on("click", click);
100

    
101
        nodeEnter.append("circle")
102
        .attr("r", function(d) { return d.type === "taxon" ? 5 : 10; })
103
        .style("fill", function(d) { return d === source && d.type === "conceptr" ? "#DF7401" : "#fff"; });
104
        
105
        nodeEnter.append("text")
106
        .attr("x", function(d) { 
107
            if(d.type === "conceptr") { 
108
                return dirMult*50;
109
            } else {
110
                return d.children || d._children ? -1*dirMult*10 : dirMult*10; 
111
            }
112
        })
113
        .attr("y", function(d) { return d.type === "conceptr" ? -20 : 0; })
114
        .attr("dy", ".35em")
115
        .attr("text-anchor", function(d) { return d.children || d._children ? tAnchorWithChildren : tAnchorWithoutChildren; })
116
        .text(function(d) { return d.name; })
117
        .style("fill-opacity", 1e-6);
118

    
119
        // Transition nodes to their new position.
120
        var nodeUpdate = node.transition()
121
        .duration(duration)
122
        .attr("transform", function(d) { return "translate(" + orientation.x(d) + "," + orientation.y(d) + ")"; });
123

    
124
        nodeUpdate.select("circle")
125
        .attr("r", function(d) { return d.type === "taxon" ? 5 : 10; })
126
        .style("fill", function(d) { return d === selectedNode && d.type === "conceptr" ? "#DF7401" : "#fff"; });
127

    
128
        nodeUpdate.select("text")
129
        .style("fill-opacity", 1);
130

    
131
        // Update the links…
132
        var link = svg.selectAll("path.link")
133
        .data(links, function(d) { return d.target.id; });
134

    
135
        // Enter any new links at the parent's previous position.
136
        link.enter().insert("path", "g")
137
        .attr("class", "link")
138
        .attr("d", function(d) {
139
            var o = {x: source.x, y: source.y};
140
            return diagonal({source: o, target: o});
141
        });
142

    
143
        // Transition links to their new position.
144
        link.transition()
145
        .duration(duration)
146
        .attr("d", diagonal);
147

    
148

    
149
    }
150

    
151
//  Toggle children on click.
152
    function click(d) {
153
        //root.children.forEach(collapse);
154

    
155
        if(d.type === "conceptr") {
156
            connector.select(d.uuid);
157
        }
158

    
159
        selectedNode = d;
160
        update(d);
161
    }
162
}
    (1-1/1)