从 d3 升级到 v3 时未定义 dX 和 dY 值.js v4

  • 本文关键字:dX dY v4 js 未定义 d3 v3 d3.js
  • 更新时间 :
  • 英文 :


我一直在尝试将可缩放的树状图从 v3 移动到 v4,但我需要的最后一块我无法获取 dx 和 dy 值来正确绘制我的矩形。在我的布局函数中,子 dx 和 dy 是未定义的,这只会导致另一个未定义。所以当它最终达到直截时,对我来说是没有用的。

我哪里做错了?

按照建议使用块构建器链接。

http://blockbuilder.org/jebzaki/b2252c00d51ffaca0c7408b394b168a5

var margin = {top: 25, right: 0, bottom: 0, left: 0},
width = 960,
height = 640 - margin.top - margin.bottom,
formatNumber = d3.format(",d"),
transitioning;
var x = d3.scaleLinear()
.domain([0, width])
.range([0, width]);
var y = d3.scaleLinear()
.domain([0, height])
.range([0, height]);
var color = d3.scaleThreshold()
.domain([-5,-0,0,5])
.range(["#BB0000","#600A0A","#404040","#064D15","#1CA41C"]);
var svg = d3.select("#heatmap").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.bottom + margin.top)
.style("margin-left", -margin.left + "px")
.style("margin.right", -margin.right + "px")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.style("shape-rendering", "crispEdges");
var treemap = d3.treemap()
.tile(d3.treemapSquarify.ratio(height / width * 0.5 * (1 + Math.sqrt(5))))
.size([width, height])
.round(false)
.paddingInner(1);
var grandparent = svg.append("g")
.attr("class", "grandparent");
grandparent.append("rect")
.attr("y", -margin.top)
.attr("width", width)
.attr("height", margin.top);
grandparent.append("text")
.attr("x", 6)
.attr("y", 6 - margin.top)
.attr("dy", ".75em");
d3.json("new-data.json", function(error, data) {
if (error) throw error;
var root = d3.hierarchy(data)
.eachBefore(function(d) { d.id = (d.parent ? d.parent.id + "." : "") + d.data.name; })
.sum((d) => {d.value})
.sort(function(a, b) { return b.height - a.height || b.value - a.value; });
initialize(root);
accumulate(root);
layout(root);
treemap(root);
display(root);
function initialize(root) {
root.x = root.y = 0;
root.dx = width;
root.dy = height;
root.depth = 0;
}
function accumulate(d) {
return (d._children = d.children)
? d.value = d.children.reduce(function(p, v) { return p + accumulate(v); }, 0)
: d.value;
}
function layout(d) {
if (d._children) {
d._children.forEach(function(c) {
c.x = d.x + c.x * d.dx;
c.y = d.y + c.y * d.dy;
c.dx *= d.dx;
c.dy *= d.dy;
c.parent = d;
layout(c);
});
}
}
function display(d) {
grandparent
.datum(d.parent)
.on("click", transition)
.select("text")
.text(name(d));
grandparent
.datum(d.parent)
.select("rect")
.attr("fill", function(){ return color(d.data.change)})
var g1 = svg.insert("g", ".grandparent")
.datum(d)
.attr("class", "depth");
var g = g1.selectAll("g")
.data(d._children)
.enter().append("g");
g.filter(function(d) { return d._children; })
.classed("children", true)
.on("click", transition);
g.selectAll(".child")
.data(function(d) { return d._children || [d]; })
.enter().append("rect")
.attr("class", "child")
.call(rect);
d3.select("#heatmap").select("#tooltip").remove();
var div = d3.select("#heatmap").append("div")
.attr("id", "tooltip")
.style("opacity", 0);

g.append("svg:a")
.attr("xlink:href", function(d) {
if(!d._children){
var url = "#";
return url; 
}
})
.append("rect")
.attr("class", "parent")
.call(rect)
.on("mouseover", function(d) {
if (d.data.name !== "Results") {
d3.select("#tooltip").transition()
.duration(200)
.style("opacity", 1);
d3.select("#tooltip").html("<h3>"+d.data.name+"</h3><table>"+
"<tr><td>"+d.data.value+"</td><td> ("+d.data.change+"%)</td></tr>"+
"</table>")
.style("left", (d3.event.pageX-document.getElementById("heatmap").offsetLeft + 20) + "px")
.style("top", (d3.event.pageY-document.getElementById("heatmap").offsetTop - 60) + "px");
}
})
.on("mouseout", function(d) {
d3.select("#tooltip").transition()
.duration(500)
.style("opacity", 0);
})
g.append("text")
.attr("dy", ".75em")
.text(function(d) { return d.data.name; })
.call(text);
function transition(d) {
if (transitioning || !d) return;
transitioning = true;
var g2 = display(d),
t1 = g1.transition().duration(750),
t2 = g2.transition().duration(750);
x.domain([d.x0, d.x0 + d.x1]);
y.domain([d.y0, d.y0 + d.y1]);
svg.style("shape-rendering", null);
svg.selectAll(".depth").sort(function(a, b) { return a.depth - b.depth; });
g2.selectAll("text").style("fill-opacity", 0);
t1.selectAll("text").call(text).style("fill-opacity", 0);
t2.selectAll("text").call(text).style("fill-opacity", 1);
t1.selectAll("rect").call(rect);
t2.selectAll("rect").call(rect);
t1.remove().each("end", function() {
svg.style("shape-rendering", "crispEdges");
transitioning = false;
});
}
return g;
}
function text(text) {
text.attr("x", function(d) { return x(d.x0) + (x(d.x0 + d.dx) - x(d.x0))/2; })
.attr("y", function(d) { return y(d.y0) + (y(d.y0 + d.dy) - y(d.y0))/2; })
.attr("dy", 0)
.attr("font-size", function(d) { var w=x(d.x0 + d.dx) - x(d.x0),
h=y(d.y0 + d.dy) - y(d.y0),
t=(d.data.name).length/1.3;
var tf=Math.min(Math.floor(w/t),h/3);
return (tf>=5)?Math.min(tf, 30):0; })
.attr("fill", "white")
.attr("text-anchor", "middle");
}
function rect(rect) {
rect.attr("x", function(d) { return x(d.x0); })
.attr("y", function(d) { return y(d.y0); })
.attr("width", function(d) { return x(d.x0 + d.dx) - x(d.x0); })
.attr("height", function(d) { return y(d.y0 + d.dy) - y(d.y0); })
.attr("fill", function(d){return color(parseFloat(d.data.change));});
}
function name(d) {
return d.parent
? "Sector : "+d.data.name+" (Back to Overall Market)"
: "Overall "+d.data.name;
}
});

我会将其作为评论发布,因为我不确定这是否是您实际要求的答案("移动可缩放树状图"让我感到困惑,不确定这是否是一个错字?(,但我缺乏这样做的代表。

这里有一个小提琴:http://jsfiddle.net/b238u0hy/5/

我注意到你一定在关注这个bl.ock:https://bl.ocks.org/me1er/c64479f1ac8a5f993027f40a36c35dd9。

然后,我将你的代码与原始代码进行了比较,你无法创建矩形的原因是因为你设置了"dx","dy",而不是使用d3.hierarchy生成的"x1"和"y1"值。

此处的示例 dy/dx 问题:

c.x = d.x + c.x * d.dx;
c.y = d.y + c.y * d.dy;
c.dx *= d.dx;
c.dy *= d.dy;
c.parent = d;
layout(c);

我发现原始代码的可读性稍微高一些,所以我更改了原始代码以适应您的代码(虽然,因为我在工作,所以我对一些事情很懒惰,对不起(:

  • 我注释掉了你的颜色功能,因为它不起作用。
  • 我将数据加载为变量并注释掉 d3.json((。
  • 我没有费心从原始代码中删除 shortName、text2 的东西,我只是更改了值名称以便它运行。

编辑:我忘了重新添加工具提示,这是工具提示的小提琴:http://jsfiddle.net/74t8b9sp/(一些小的更改,我在"mousemove"上将其更改为"mousemove",因此工具提示跟随您的鼠标,舍入值以用于显示目的,并删除了您最初添加的内部矩形和<a>(。

最新更新