JavaScript Infovis Spacetree: предотвратить центрирование выбранного узла на холсте
-
08-10-2019 - |
Вопрос
Я использую JavaScript Infovis Spacetree. У меня есть дерево, которое выглядит как следующее:
Однако я хочу выбрать узел «Сейчас», чтобы он выделил путь назад к корневому узлу, но предотвращает центрирование этого узла. IE:
Я старался setPos()
Но это не работает.
Есть идеи?
Добавлены шторы экрана на случай, если ссылки уходят:
Решение
Ах, что снова испортил график библиотеки: D
Давайте сделаем другой взгляд на функцию выбора, специально onComplete
Перезвоните:
onComplete: function(){ // what a mess!
group.hide(group.prepare(getNodesToHide.call(that)), complete); // hide the nodes???
geom.setRightLevelToShow(node, canvas); // guess what this already moves stuff around!
that.compute("current"); // recomputes the graphs position
that.graph.eachNode(function(n) { // sets up the moved node positions
var pos = n.pos.getc(true);
n.startPos.setc(pos.x, pos.y);
n.endPos.setc(pos.x, pos.y);
n.visited = false;
});
// hey look! We don't use a global translation offset! So we need to translate the HTML stuff extra
var offset = { x: complete.offsetX, y: complete.offsetY };
that.geom.translate(node.endPos.add(offset).$scale(-1), ["start", "current", "end"]);
// show the nodes again?
group.show(getNodesToShow.call(that));
// the first useful call in here, redraw the updated graph!
that.plot();
complete.onAfterCompute(that.clickedNode); // callback better leave them here
complete.onComplete();
}
Так что, поскольку вы вообще не хотите повторно расположение, мы можем рефакторировать (также известно как удаление некоторых строк) это:
onComplete: function(){
that.plot();
complete.onAfterCompute(that.clickedNode);
complete.onComplete();
}
Смотреть ма! Я спас тонн байтов !!! Это все, что нужно, остальное не делает ничего жизненного для графика.
Конечно, просто избавиться от функциональности может укусить вас когда-нибудь день, поэтому мы должны добавить center
параметр select
:
select: function(id, center, onComplete) {
....
onComplete: function(){
if (center) {
group.hide(group.prepare(getNodesToHide.call(that)), complete);
geom.setRightLevelToShow(node, canvas);
that.compute("current");
that.graph.eachNode(function(n) {
var pos = n.pos.getc(true);
n.startPos.setc(pos.x, pos.y);
n.endPos.setc(pos.x, pos.y);
n.visited = false;
});
var offset = { x: complete.offsetX, y: complete.offsetY };
that.geom.translate(node.endPos.add(offset).$scale(-1), ["start", "current", "end"]);
}
group.show(getNodesToShow.call(that));
that.plot();
complete.onAfterCompute(that.clickedNode);
complete.onComplete();
}
Другие советы
Установите позиции Offsetx и Offsetty, как это:
var st = new $jit.ST({
'injectInto': 'infovis',
//set duration for the animation
duration: 800,
//set animation transition type
transition: $jit.Trans.Quart.easeInOut,
//set distance between node and its children
levelDistance: 50,
//set max levels to show. Useful when used with
//the request method for requesting trees of specific depth
levelsToShow: 4,
orientation: 'top',
align: 'center',
//set node and edge styles
//set overridable=true for styling individual
//nodes or edges
offsetX: 0, offsetY: 110,
Node: {
height: 30,
width: 31,
//use a custom
//node rendering function
type: 'nodeline',
color: '#f76b14',
lineWidth: 1,
align: "center",
overridable: true
},
Infovis div, то есть div, который удерживает спаклее, не отобразит весь граф. Добавление следующего кода в OnComplete событие сделает трюк.
Это установило бы высоту DIV для размещения всего графика. Я использую ориентацию как «топ».
onComplete: function () {
var LastnodeTop = 0;
$("div.node").each(function () {
var pos = $(this).position();
if (pos.top > LastnodeTop)
LastnodeTop = pos.top;
});
var LastnodeTopStr = LastnodeTop.toString();
LastnodeTopStr = LastnodeTopStr.substring(0, 4);
var LastnodeTopInt = parseInt(LastnodeTopStr) + 100;
$("#infovis").attr("style", "height:" + LastnodeTopInt + "px");
}
Спасибо.