Разбиение текста из json на несколько строк для отображения надписей в раскладке D3
Я новичок в d3.js и кодирования в целом. Это мой вопрос:
Я пытаюсь найти способ разбить длинные отображаемые имена объектов раскладки сил в строках.
Я хотел бы иметь возможность определить, где разбить эти строки, и я предполагаю, что это можно сделать из файла json.
Я знаю, что подобные вопросы уже задавались, но я просто могуне могу найти, куда поместить код или почему мои предыдущие попытки не далиЯ был успешным. Это код, который у меня есть:
var width = 960,
height = 800,
root;
var force = d3.layout.force()
.linkDistance(120)
.charge(-600)
.gravity(.06)
.size([width, height])
.on("tick", tick);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var link = svg.selectAll(".link"),
node = svg.selectAll(".node");
d3.json("graph.json", function(error, json) {
root = json;
update();
});
function update() {
var nodes = flatten(root),
links = d3.layout.tree().links(nodes);
// Restart the force layout.
force
.nodes(nodes)
.links(links)
.start();
// Update links.
link = link.data(links, function(d) { return d.target.id; });
link.exit().remove();
link.enter().insert("line", ".node")
.attr("class", "link");
// Update nodes.
node = node.data(nodes, function(d) { return d.id; });
node.exit().remove();
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.on("click", click)
.call(force.drag);
nodeEnter.append("circle")
.attr("r", function(d) { return Math.sqrt(d.size) / 3 || 10; });
nodeEnter.append("text")
.attr("dy", "0.3em")
.text(function(d) { return d.name; });
node.select("circle")
.style("fill", color);
}
// Divide text
text.append("text")
.each(function (d) {
var arr = d.name.split(" ");
if (arr != undefined) {
for (i = 0; i < arr.length; i++) {
d3.select(this).append("tspan")
.text(arr[i])
.attr("dy", i ? "1.2em" : 0)
.attr("x", 0)
.attr("text-anchor", "middle")
.attr("class", "tspan" + i);
}
}
});
// Divide text
function tick() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
}
function color(d) {
return d._children ? "#9ecae1" // collapsed package
: d.children ? "#ffffff" // expanded package
: "#ffcc50"; // leaf node
}
// Toggle children on click.
function click(d) {
if (d3.event.defaultPrevented) return; // ignore drag
if (d.children) {
d._children = d.children;
d.children = null;
} else if (d._children) {
d.children = d._children;
d._children = null;
} else {
// This was a leaf node, so redirect.
window.open(d.url, 'popUpWindow','height=600,width=800,left=10,top=10,resizable=yes,scrollbars=no,toolbar=no,menubar=no,location=no,directories=no,status=yes');
}
update();
}
// Returns a list of all nodes under the root.
function flatten(root) {
var nodes = [], i = 0;
function recurse(node) {
if (node.children) node.children.forEach(recurse);
if (!node.id) node.id = ++i;
nodes.push(node);
}
recurse(root);
return nodes;
}
И это информация JSON:
{
"name": "flare",
"children": [
{
"name": "analytics",
"children": [
{
"name": "cluster",
"children": [
{"name": "Agglomerative Cluster", "size": 3938},
{"name": "Community Structure", "size": 3812},
{"name": "Hierarchical Cluster", "size": 6714},
{"name": "Merge Edge", "size": 743}
]
},
{
"name": "graph",
"children": [
{"name": "Betweenness Centrality", "size": 3534},
{"name": "Link Distance", "size": 5731},
{"name": "Max Flow Min Cut", "size": 7840},
{"name": "Shortest Paths", "size": 5914},
{"name": "Spanning Tree", "size": 3416}
]
},
{
"name": "optimization",
"children": [
{"name": "Aspect Ratio Banker", "size": 7074}
]
}
]
}
]
Я хотел бы быть в состоянии решить, например, разорвать Аспект / Отношение Банкир или Аспект / Банкир.