i using d3 collapsible tree , have webpage set 2 divs - on lefthand side tree , righthand side div has information fed tree. looking add clickable links text in div on right side. javascript code follows:
//margin tree div var margin = {top: -20, right: 20, bottom: -40, left: 110}, width = 960 - margin.right - margin.left, //960 min show childs height = 650 - margin.top - margin.bottom; // used zooming // come var realwidth = window.innerwidth; var realheight = window.innerheight; var m = [40, 240, 40, 240], w = realwidth -m[0] -m[0], h = realheight -m[0] -m[2]; var = 0, duration = 300, // duration of deployment of new child nodes root; var tree = d3.layout.tree() // call on d3 tree layout method .size([height - 30, width]); var diagonal = d3.svg.diagonal() // diagonal path generator .projection(function(d) { return [d.y, d.x]; }); // d.x , d.y switched path matches nodes // makes paths horizontal instead of vertical /********************************* * start function collapsibletree * *********************************/ function collapsibletree() { // reset function. removes existing tree before re-generating new one. d3.selectall(".collapsibletree").remove(); var svg = d3.select("#tree").append("svg") // append svg tree div .attr("width", width + margin.right + margin.left) .attr("height", height + margin.top + margin.bottom) .attr("class","collapsibletree") .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var tooltip = d3.select('#infobox') .append('div') .attr('class', 'tooltip') tooltip.append('div') .attr('class', 'name') tooltip.append('div') .attr('class', 'use_case') //.attr("class", "hyper"+'use_case').on("click", clicklink); //use_case.on("click",clicklink); tooltip.append('div') .attr('class', 'ast'); tooltip.append('div') .attr('class', 'section'); tooltip.append('div') .attr('class','data_element') //.attr("xlink:href", function(d) { return d.icon; }); tooltip.append('div') .attr('class','de_dataprovider'); tooltip.append('div') .attr('class','de_definitionowner'); tooltip.append('div') .attr('class','de_datacategory'); tooltip.append('div') .attr('class','de_definition'); tooltip.append('div') .attr('class', 'space'); tooltip.append('div') .attr('class', 'link'); d3.json("flare4.json", function(error, osm) { //load json data tree if (error) throw error; root = osm; root.x0 = height / 2; root.y0 = 0; function collapse(d) { //collapse tree children if (d.children) { d._children = d.children; d._children.foreach(collapse); d.children = null; } } root.children.foreach(collapse); //call collapse function , update tree update(root); }); d3.select(self.frameelement).style("height", "00px"); function update(source) { // compute new tree layout. var nodes = tree.nodes(root).reverse(); // return array of objects parents , child each var links = tree.links(nodes); // return array of objects. each has source , target properties // source , target nodes // normalize fixed-depth. nodes.foreach(function(d) { d.y = d.depth * 150;}); // determine horizontal spacing of nodes // calculate position on y axis of screen // update nodes… var node = svg.selectall("g.node") .data(nodes, function(d) { return d.id || (d.id = ++i); }); // binds nodes data array // enter new nodes @ parent's previous position. var nodeenter = node.enter() .append("g") // append g element each object .attr("class", "node") .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; }) // place nodes according x , y properties // x , y switch tree horizontal not vertical //.text(function(d) { return d.name; }) .on("click", click) // append circles nodes nodeenter.append("circle") .attr("r", 10) .style("fill", function(d) { return d._children ? "#d3d3d3" : "#fff"; }); // if nodes has children set color #d3d3d3 otherwise set #fff nodeenter.append("text") .attr("x", function(d) { return d.children || d._children ? -10 : 10; }) //distance of name in relation node (left/right) // if nodes has children set -10 otherwise set 10 .attr("dy", ".35em") //distance (up/down) node .attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; }) .text(function(d) { return d.name; }) .on("click", function(d){ tooltip.select('.name').html(d.name + "<br><br>"); tooltip.select('.use_case').html(d.use_case); tooltip.select('.ast').html(d.ast); tooltip.select('.section').html(d.section); tooltip.select('.data_element').html(d.data_element); tooltip.select('.de_dataprovider').html("<blockquote>" + d.de_dataprovider + "</bq>"); tooltip.select('.de_definitionowner').html("<blockquote>" + d.de_definitionowner + "</bq>"); tooltip.select('.de_datacategory').html("<blockquote>" + d.de_datacategory + "</bq>"); tooltip.select('.de_definition').html("<blockquote>" + d.de_definition + "</bq>"); tooltip.select('.space').html("<br><br>"); tooltip.select('.link').html(d.link); tooltip.style('display', 'block'); }) //.style("fill-opacity", 1) // .on('mouseout', function(d) {tooltip.style('display', 'none');}) // .attr("class", "hyper").on("click", clicklink); nodeenter.append("image") .attr("xlink:href", function(d) { return d.icon; }) .attr("x", "-12px") .attr("y", "-12px") .attr("width", "24px") .attr("height", "24px"); // transition nodes new position. var nodeupdate = node.transition() .duration(duration) .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; }); nodeupdate.select("circle") .attr("r", 4.5) .style("fill", function(d) { return d._children ? "#d3d3d3" : "#fff"; }) // if nodes has children set color #d3d3d3 otherwise set #fff nodeupdate.select("text") .style("fill-opacity", 1); // transition exiting nodes parent's new position. var nodeexit = node.exit().transition() .duration(duration) .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; }) .remove(); nodeexit.select("circle") .attr("r", 1e-6); nodeexit.select("text") .style("fill-opacity", 1e-6); // update links… //to come var link = svg.selectall("path.link") .data(links, function(d) { return d.target.id; }); // enter new links @ parent's previous position. link.enter().insert("path", "g") .attr("class", "link") .style("stroke", function(d) { return d.target.level; }) .attr("d", function(d) { var o = {x: source.x0, y: source.y0}; return diagonal({source: o, target: o}); }); // transition links new position. link.transition() .duration(duration) .style("stroke", function(d) { return d.target.level; }) .attr("d", diagonal); // transition exiting nodes parent's new position. link.exit().transition() .duration(duration) .style("stroke", function(d) { return d.target.level; }) .attr("d", function(d) { var o = {x: source.x, y: source.y}; return diagonal({source: o, target: o}); }) .remove(); // stash old positions transition. nodes.foreach(function(d) { d.x0 = d.x; d.y0 = d.y; }); } // toggle children on click. function click(d) { if (d.children) { d._children = d.children; d.children = null; } else { d.children = d._children; d._children = null; } update(d); } //to open url in new tab when text hyperlinked clicked //currently unused function clicklink(d) { window.open(d.link,'_blank'); } d3.select("svg") .call(d3.behavior.zoom() .scaleextent([0.5, 15]) .on("zoom", zoom)); function zoom() { var scale = d3.event.scale, translation = d3.event.translate, tbound = -h * scale, bbound = h * scale, lbound = (-w + m[1]) * scale, rbound = (w - m[3]) * scale; // limit translation thresholds translation = [ math.max(math.min(translation[0], rbound), lbound), math.max(math.min(translation[1], bbound), tbound) ]; d3.select(".collapsibletree") .attr("transform", "translate(" + translation + ")" + " scale(" + scale + ")"); } } /********************************* * end function collapsibletree * *********************************/ function resetcollapsibletree() { d3.selectall(".collapsibletree").remove(); collapsibletree(); } function overlay() { el = document.getelementbyid("overlay"); el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible"; }
specifically, have these classes created, each of elements give additional data associated node on tree, , send information div on side:
var tooltip = d3.select('#infobox') .append('div') .attr('class', 'tooltip') tooltip.append('div') .attr('class', 'name') tooltip.append('div') .attr('class', 'use_case') //.attr("class", "hyper"+'use_case').on("click", clicklink); //use_case.on("click",clicklink); tooltip.append('div') .attr('class', 'ast'); tooltip.append('div') .attr('class', 'section'); tooltip.append('div') .attr('class','data_element') //.attr("xlink:href", function(d) { return d.icon; }); tooltip.append('div') .attr('class','de_dataprovider'); tooltip.append('div') .attr('class','de_definitionowner'); tooltip.append('div') .attr('class','de_datacategory'); tooltip.append('div') .attr('class','de_definition'); tooltip.append('div') .attr('class', 'space'); tooltip.append('div') .attr('class', 'link');
and trying give of them ability clickable links. appreciated! thanks.
Comments
Post a Comment