tnt.tree
Version:
TnT tree display
177 lines (160 loc) • 4.79 kB
JavaScript
var apijs = require("tnt.api");
var tree = {};
tree.node_display = function () {
"use strict";
var n = function (node) {
var proxy;
var thisProxy = d3.select(this).select(".tnt_tree_node_proxy");
if (thisProxy[0][0] === null) {
var size = d3.functor(n.size())(node);
proxy = d3.select(this)
.append("rect")
.attr("class", "tnt_tree_node_proxy");
} else {
proxy = thisProxy;
}
n.display().call(this, node);
var dim = this.getBBox();
proxy
.attr("x", dim.x)
.attr("y", dim.y)
.attr("width", dim.width)
.attr("height", dim.height);
};
var api = apijs (n)
.getset("size", 4.4)
.getset("fill", "black")
.getset("stroke", "black")
.getset("stroke_width", "1px")
.getset("display", function () {
throw "display is not defined in the base object";
});
api.method("reset", function () {
d3.select(this)
.selectAll("*:not(.tnt_tree_node_proxy)")
.remove();
});
return n;
};
tree.node_display.circle = function () {
var n = tree.node_display();
n.display (function (node) {
d3.select(this)
.append("circle")
.attr("r", function (d) {
return d3.functor(n.size())(node);
})
.attr("fill", function (d) {
return d3.functor(n.fill())(node);
})
.attr("stroke", function (d) {
return d3.functor(n.stroke())(node);
})
.attr("stroke-width", function (d) {
return d3.functor(n.stroke_width())(node);
})
.attr("class", "tnt_node_display_elem");
});
return n;
};
tree.node_display.square = function () {
var n = tree.node_display();
n.display (function (node) {
var s = d3.functor(n.size())(node);
d3.select(this)
.append("rect")
.attr("x", function (d) {
return -s;
})
.attr("y", function (d) {
return -s;
})
.attr("width", function (d) {
return s*2;
})
.attr("height", function (d) {
return s*2;
})
.attr("fill", function (d) {
return d3.functor(n.fill())(node);
})
.attr("stroke", function (d) {
return d3.functor(n.stroke())(node);
})
.attr("stroke-width", function (d) {
return d3.functor(n.stroke_width())(node);
})
.attr("class", "tnt_node_display_elem");
});
return n;
};
tree.node_display.triangle = function () {
var n = tree.node_display();
n.display (function (node) {
var s = d3.functor(n.size())(node);
d3.select(this)
.append("polygon")
.attr("points", (-s) + ",0 " + s + "," + (-s) + " " + s + "," + s)
.attr("fill", function (d) {
return d3.functor(n.fill())(node);
})
.attr("stroke", function (d) {
return d3.functor(n.stroke())(node);
})
.attr("stroke-width", function (d) {
return d3.functor(n.stroke_width())(node);
})
.attr("class", "tnt_node_display_elem");
});
return n;
};
// tree.node_display.cond = function () {
// var n = tree.node_display();
//
// // conditions are objects with
// // name : a name for this display
// // callback: the condition to apply (receives a tnt.node)
// // display: a node_display
// var conds = [];
//
// n.display (function (node) {
// var s = d3.functor(n.size())(node);
// for (var i=0; i<conds.length; i++) {
// var cond = conds[i];
// // For each node, the first condition met is used
// if (d3.functor(cond.callback).call(this, node) === true) {
// cond.display.call(this, node);
// break;
// }
// }
// });
//
// var api = apijs(n);
//
// api.method("add", function (name, cbak, node_display) {
// conds.push({ name : name,
// callback : cbak,
// display : node_display
// });
// return n;
// });
//
// api.method("reset", function () {
// conds = [];
// return n;
// });
//
// api.method("update", function (name, cbak, new_display) {
// for (var i=0; i<conds.length; i++) {
// if (conds[i].name === name) {
// conds[i].callback = cbak;
// conds[i].display = new_display;
// }
// }
// return n;
// });
//
// return n;
//
// };
module.exports = exports = tree.node_display;