Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
D3.js 包装多行标签_D3.js - Fatal编程技术网

D3.js 包装多行标签

D3.js 包装多行标签,d3.js,D3.js,我正在基于此构建一个可缩放的响应树形图。问题是,我使用的标签比原始的可视化效果更长,最终没有显示: function text(text) { text.selectAll("tspan") .attr("x", function(d) { return x(d.x) + 6; }) text.attr("x", function(d) { return x(d.x) + 6; }) .attr("y", function(d) { return

我正在基于此构建一个可缩放的响应树形图。问题是,我使用的标签比原始的可视化效果更长,最终没有显示:

function text(text) {
    text.selectAll("tspan")
        .attr("x", function(d) { return x(d.x) + 6; })
    text.attr("x", function(d) { return x(d.x) + 6; })
        .attr("y", function(d) { return y(d.y) + 6; })
        .style("opacity", function(d) { return this.getComputedTextLength() < x(d.x + d.dx) - x(d.x) ? 1 : 0; });
}
函数文本(文本){
text.selectAll(“tspan”)
.attr(“x”,函数(d){返回x(d.x)+6;})
attr(“x”,函数(d){返回x(d.x)+6;})
.attr(“y”,函数(d){返回y(d.y)+6;})
.style(“不透明”,函数(d){返回此.getComputedTextLength()
我看到的问题是,文本需要完全显示在第一行中,但我希望将其显示在多行中(在rect中)


迈克·博斯托克的一篇文章似乎解决了这个问题,但我不知道如何将它应用到树形图中

下面是一个快速修改,它在该示例中包装了父文本:


可缩放树映射模板
#图表{
背景:#fff;
字体系列:“Helvetica Neue”,Helvetica,Arial,无衬线;
}
.头衔{
字体大小:粗体;
字体大小:24px;
文本对齐:居中;
边缘顶部:6px;
边缘底部:6px;
}
正文{
指针事件:无;
}
.祖父母文本{
字体大小:粗体;
}
直肠{
填充:无;
冲程:#fff;
}
rect.parent,
.祖父母{
笔画宽度:2px;
}
矩形父对象{
指针事件:无;
}
.祖父母{
填充物:橙色;
}
.祖父母:悬停直线{
填充:#ee9700;
}
.children rect.parent,
.祖父母{
光标:指针;
}
.childrent.parent{
填充:#bbb;
填充不透明度:.5;
}
.children:hover rect.children{
填充:#bbb;
}
window.addEventListener('message',函数(e){
var opts=e.data.opts,
数据=e.data.data;
返回主(选项、数据);
});
var默认值={
保证金:{
排名:24,
右:0,,
底部:0,
左:0
},
根名称:“顶端”,
格式:“,d”,
标题:“,
宽度:960,
身高:500
};
主功能(o、数据){
变种根,
opts=$.extend(true,{},默认值,o),
formatNumber=d3.format(opts.format),
rname=opts.rootname,
保证金=选择保证金,
光=36+16;
$(“#图表”)。宽度(选择宽度)。高度(选择高度);
变量宽度=opts.width-margin.left-margin.right,
高度=opts.height-margin.top-margin.bottom-theight,
过渡;
var color=d3.scale.category20c();
var x=d3.scale.linear()
.domain([0,宽度])
.范围([0,宽度]);
变量y=d3.scale.linear()
.domain([0,高度])
.范围([0,高度]);
var treemap=d3.layout.treemap()
.儿童(功能(d,深度){
返回深度?空:d.\u子项;
})
.排序(功能(a、b){
返回a.value-b.value;
})
.比率(高度/宽度*0.5*(1+数学sqrt(5)))
.圆形(假);
var svg=d3.选择(“图表”).追加(“svg”)
.attr(“宽度”,宽度+边距。左侧+边距。右侧)
.attr(“高度”,高度+边距。底部+边距。顶部)
.style(“左边距”、-margin.left+“px”)
.style(“margin.right”、-margin.right+“px”)
.附加(“g”)
.attr(“转换”、“平移”(“+margin.left+”,“+margin.top+”)
.风格(“形状渲染”、“边缘”);
var祖父母=svg.append(“g”)
.attr(“阶级”、“祖父母”);
祖父母。附加(“rect”)
.attr(“y”,-margin.top)
.attr(“宽度”,宽度)
.attr(“高度”,边距,顶部);
祖父母。附加(“文本”)
.attr(“x”,6)
.attr(“y”,6-页边距。顶部)
.attr(“dy”,“.75em”);
如果(选择标题){
$(“#图表”).prepend(“

”+opts.title+”

”); } if(数组的数据实例){ 根={ 键:rname, 值:数据 }; }否则{ 根=数据; } 初始化(根); 积累(根); 布局(根); console.log(root); 显示(根); 如果(window.parent!==window){ var myheight=document.documentElement.scrollHeight | | document.body.scrollHeight; window.parent.postMessage({ 身高:我的身高 }, '*'); } 函数初始化(根){ root.x=root.y=0; root.dx=宽度; root.dy=高度; 根深度=0; } //聚合内部节点的值。这通常由 //树映射布局,但由于我们的自定义实现,这里没有。 //我们还拍摄了原始子对象(_children)的快照,以避免 //计算布局时覆盖的子级。 函数累加(d){ 返回(d._子项=d.values)?d.values=d.values.reduce(函数(p,v){ 返回p+累计(v); },0):d值; } //递归计算树映射布局,使每组同级 //使用相同的大小(1×1),而不是父单元格的尺寸。 //这将优化当前缩放状态的布局 //为父节点为每组同级创建对象,以便 //当我们递归时,父维度不会被丢弃 //兄弟姐妹的比例为1×1,我们必须使用绝对值重新缩放以适应 //坐标。这让我们可以使用视口进行缩放。 功能布局(d){ 如果(d.(儿童){ 树映射节点({ _儿童:d.(u children) }); d、 _children.forEach(函数(c){ c、 x=d.x+c.x*d.dx; c、 y=d.y+c.y*d.dy; c、 dx*=d.dx; c、 d