- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试绘制多个 d3js 可缩放 TreeMap 。
我根据这个例子画了一个成功https://codepen.io/moktc/pen/XMGgwP .当我重复使用绘图功能时,第一个图表显示空白,就像它需要显示数据一样。
绘图函数是
function drawTreeMap(tree) {
treemap = d3.treemap()
.tile(d3.treemapSquarify.ratio(height / width * 0.69 *(1 + Math.sqrt(5))))
.size([width, height])
.round(false)
.paddingInner(1);
var id = "#" +tree;
svg = d3.select(id).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");
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");
};
当我使用此函数 twite 显示第二个树形图时,其数据与第一个树形图相同。第一个变成空白
最佳答案
svg
, grandparent
和 treemap
是在你的函数之外声明的所有对象 drawTreemap()
(这在您发布的代码中并不明显,但可以在 CodePen 上的完整代码示例中看到),因此两次调用该函数将对相同的对象执行两次。由于这些对象构成了树状图的一部分,因此您需要为第二个树状图单独复制这些对象,因此应在 drawTreemap()
中声明它们。 .
此外,请注意函数 display()
包含对祖 parent 的引用,未传递给 display()
作为论据,所以 display()
的两份副本需要为每个祖 parent 的副本单独申报。因此,display()
也应在 drawTreemap()
内声明(请注意 Javascript functions are closures ,即它们的定义包括对声明函数时范围内的所有变量的引用,因此可以声明引用不在全局范围内的变量的函数,而无需将它们传递给函数作为参数)。
三、函数initialize()
, accumulate()
, layout()
, treemap()
和 display()
需要为每个 TreeMap 单独调用,因此将这些调用包含在 drawTreemap()
中是有意义的.
因此你应该声明svg
, grandparent
, treemap
和 display()
本地内部 drawTreemap()
并包括对 initialize()
的调用, accumulate()
, layout()
, treemap()
和 display()
,可以按如下方式完成:
function drawTreemap(treeId) {
var id = "#" + treeId;
var svg = d3.select(id).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 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");
var treemap = d3.treemap()
//.tile(d3.treemapResquarify)
.size([width, height])
.round(false)
.paddingInner(1);
initialize(root);
accumulate(root);
layout(root);
treemap(root);
display(root);
function display(d) {
//...
}
}
这是完整的代码,基于您引用的 CodePen 上的示例:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<style>
text {
pointer-events: none;
}
.grandparent text {
font-weight: bold;
}
rect {
fill: none;
stroke: #fff;
}
rect.parent,
.grandparent rect {
stroke-width: 2px;
}
.grandparent rect {
fill: orange;
}
.grandparent:hover rect {
fill: #ee9700;
}
.children rect.parent,
.grandparent rect {
cursor: pointer;
}
.children rect.parent {
fill: #bbb;
fill-opacity: .5;
}
.children:hover rect.child {
fill: #bbb;
}
</style>
</head>
<body>
<p id="chart">
Sample zoomable treemap
</p>
<div id="tree1"></div>
<div id="tree2"></div>
<script src="js/d3.v4.js"></script>
<script>
var data = {
"name": 'Sample',
"shortName": 'Sample',
"children": [
{
"name": "6.1 Identify and plan learning needs",
"shortName": "AITSL-A61",
"size": null,
"children": [
{
"name": "Analyse the Standards for.",
"shortName": "AITSL-A61-H",
"size": 59,
"children": [
]
},
{
"name": "Demonstrate an of the role of the",
"shortName": "AITSL-A61-G",
"size": 448,
"children": [
]
},
{
"name": "Use knowledge of the Standards for ",
"shortName": "AITSL-A61-L",
"size": 59,
"children": [
]
},
{
"name": "Use the plan learning needs.",
"shortName": "AITSL-A61-P",
"size": 101,
"children": [
]
}
]
},
{
"name": "6.2 Engage in improve practice",
"shortName": "AITSL-A62",
"size": null,
"children": [
{
"name": "Participate in to update knowledge .",
"shortName": "AITSL-A62-P",
"size": 92,
"children": [
]
},
{
"name": "Understand appropriate sources of .",
"shortName": "AITSL-A62-G",
"size": 405,
"children": [
]
},
{
"name": "Plan for and critiquing ",
"shortName": "AITSL-A62-H",
"size": 49,
"children": [
]
},
{
"name": "Initiate to expand opportunities.",
"shortName": "AITSL-A62-L",
"size": 47,
"children": [
]
}
]
},
{
"name": "6.3 Engage with and improve practice",
"shortName": "AITSL-A63",
"size": null,
"children": [
{
"name": "Contribute to collegial and apply.",
"shortName": "AITSL-A63-P",
"size": 84,
"children": [
]
},
{
"name": "Initiate and engage in discussions.",
"shortName": "AITSL-A63-H",
"size": 51,
"children": [
]
},
{
"name": "Seek and feedback from .",
"shortName": "AITSL-A63-G",
"size": 458,
"children": [
]
},
{
"name": "Implement dialogue within by .",
"shortName": "AITSL-A63-L",
"size": 40,
"children": [
]
}
]
},
{
"name": "6.4 Apply improve learning",
"shortName": "AITSL-A64",
"size": null,
"children": [
{
"name": "Undertake .",
"shortName": "AITSL-A64-P",
"size": 76,
"children": [
]
},
{
"name": "Demonstrate an of the rationale.",
"shortName": "AITSL-A64-G",
"size": 426,
"children": [
]
},
{
"name": "Engage with to evaluate the .",
"shortName": "AITSL-A64-H",
"size": 54,
"children": [
]
},
{
"name": "Advocate, in and lead high-quality .",
"shortName": "AITSL-A64-L",
"size": 43,
"children": [
]
}
]
}
]
};
var margin = { top: 20, right: 0, bottom: 0, left: 0 },
width = 640, //640
height = 530,
formatNumber = d3.format(",d"),
transitioning;
var x = d3.scaleLinear()
.domain([0, width])
.range([0, width]);
var y = d3.scaleLinear()
.domain([0, height - margin.top - margin.bottom])
.range([0, height - margin.top - margin.bottom]);
var color = d3.scaleOrdinal()
.range(d3.schemeCategory10
.map(function (c) { c = d3.rgb(c); c.opacity = 0.6; return c; }));
var fader = function (color) { return d3.interpolateRgb(color, "#fff")(0.2); };
var format = d3.format(",d");
//var treemap;
//var svg, grandparent;
var root = d3.hierarchy(data)
.eachBefore(function (d) { d.id = (d.parent ? d.parent.id + "." : "") + d.data.shortName; })
.sum(function (d) { return d.size; }) // access the numeric attribute of the data
.sort(function (a, b) {
console.log('initial root sort a ' + a.value + ' b ' + b.value);
return b.height - a.height || b.value - a.value;
});
function updateDrillDown() {
drawTreemap("tree1");
drawTreemap("tree2");
};
function drawTreemap(treeId) {
var id = "#" + treeId;
var svg = d3.select(id).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 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");
var treemap = d3.treemap()
//.tile(d3.treemapResquarify)
.size([width, height])
.round(false)
.paddingInner(1);
initialize(root);
accumulate(root);
layout(root);
treemap(root);
display(root);
function display(d) {
grandparent
.datum(d.parent)
.on("click", transition)
.select("text")
.text(name(d));
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);
var children = g.selectAll(".child")
.data(function (d) { return d._children || [d]; })
.enter().append("g");
children.append("rect")
.attr("class", "child")
.call(rect)
.append("title")
.text(function (d) { return d.data.shortName + " (" + formatNumber(d.value) + ")"; });
children.append("text")
.attr("class", "ctext")
.text(function (d) { return d.data.shortName; })
.call(text2);
g.append("rect")
.attr("class", "parent")
.call(rect);
var t = g.append("text")
.attr("class", "ptext")
.attr("dy", ".75em")
t.append("tspan")
.text(function (d) { return d.data.shortName; });
t.append("tspan")
.attr("dy", "1.0em")
.text(function (d) { return formatNumber(d.value); });
t.call(text);
g.selectAll("rect")
.style("fill", function (d) { return color(d.data.shortName); });
function transition(d) {
if (transitioning || !d) return;
transitioning = true;
var g2 = display(d),
t1 = g1.transition().duration(750),
t2 = g2.transition().duration(750);
// Update the domain only after entering new elements.
x.domain([d.x0, d.x0 + d.x1]);
y.domain([d.y0, d.y0 + d.y1]);
// Enable anti-aliasing during the transition.
svg.style("shape-rendering", null);
// Draw child nodes on top of parent nodes.
svg.selectAll(".depth").sort(function (a, b) {
console.log('.depth sort a ' + a.depth + ' b ' + b.depth);
return a.depth - b.depth;
});
// Fade-in entering text.
g2.selectAll("text").style("fill-opacity", 0);
// Transition to the new view.
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);
// Remove the old node when the transition is finished.
t1.remove().on("end", function () {
svg.style("shape-rendering", "crispEdges");
transitioning = false;
});
}
return g;
}
} // end of drawTreemap()
function initialize(root) {
root.x = root.y = 0;
root.x1 = width;
root.y1 = height;
root.depth = 0;
}
// Aggregate the values for internal nodes. This is normally done by the
// treemap layout, but not here because of our custom implementation.
// We also take a snapshot of the original children (_children) to avoid
// the children being overwritten when when layout is computed.
function accumulate(d) {
console.log('accumulate called ' + d.data.name);
return (d._children = d.children)
? d.value = d.children.reduce(function (p, v) { return p + accumulate(v); }, 0)
: d.value;
}
// Compute the treemap layout recursively such that each group of siblings
// uses the same size (1×1) rather than the dimensions of the parent cell.
// This optimizes the layout for the current zoom state. Note that a wrapper
// object is created for the parent node for each group of siblings so that
// the parent’s dimensions are not discarded as we recurse. Since each group
// of sibling was laid out in 1×1, we must rescale to fit using absolute
// coordinates. This lets us use a viewport to zoom.
function layout(d) {
if (d._children) {
// treemap.nodes({_children: d._children});
// treemap(d);
d._children.forEach(function (c) {
c.x0 = d.x0 + c.x0 * d.x1;
c.y0 = d.y0 + c.y0 * d.y1;
c.x1 *= d.x1;
c.y1 *= d.y1;
c.parent = d;
layout(c);
});
}
}
function text(text) {
text.selectAll("tspan")
.attr("x", function (d) { return x(d.x0) + 6; })
text.attr("x", function (d) { return x(d.x0) + 6; })
.attr("y", function (d) { return y(d.y0) + 10; })
.style("opacity", function (d) {
console.log("text opacity setting textlength " + this.getComputedTextLength() + " d size " + (x(d.x0 + d.x1) - x(d.x0)));
return this.getComputedTextLength() < x(d.x0 + d.x1) - x(d.x0) ? 1 : 0;
});
}
function text2(text) {
text.attr("x", function (d) { return x(d.x0 + d.x1) - this.getComputedTextLength() - 6; })
.attr("y", function (d) { return y(d.y0 + d.y1) - 6; })
.style("opacity", function (d) { return this.getComputedTextLength() < x(d.x0 + d.x1) - x(d.x0) ? 1 : 0; });
}
function rect(rect) {
rect.attr("x", function (d) { return x(d.x0); })
.attr("y", function (d) { return y(d.y0); })
.attr("width", function (d) {
console.log('id ' + d.id + ' rect width ' + (d.x1 - d.x0));
return x(d.x0 + d.x1) - x(d.x0);
//return (d.x1 -d.x0);
})
.attr("height", function (d) {
console.log('id ' + d.id + ' rect height ' + (d.y1 - d.y0) + ' ordinal ' + (y(d.y1 + d.y0) - y(d.y0)));
return y(d.y0 + d.y1) - y(d.y0);
//return y(d.y1 - d.y0);
});
}
function name(d) {
return d.parent
? name(d.parent) + " / " + d.data.shortName + " (" + formatNumber(d.value) + ")"
: d.data.shortName + " (" + formatNumber(d.value) + ")";
}
//$(function () { // I have commented this out because I have not included jQuery.js
updateDrillDown();
//});
</script>
</body>
</html>
编辑以解释如何一次只显示一个图表:
一次只显示一个图表的一种方法,具体取决于 <select>
的值框,是绘制两个图表,但使用 display
隐藏其中一个样式属性。例如:
<!--Add this HTML-->
<select id="chart_selector" onchange="toggleChartVisibility();">
<option value="1" selected="selected">Chart 1</option>
<option value="2">Chart 2</option>
</select>
<br />
<div id="tree1"></div>
<div id="tree2" style="display:none;"></div><!--Add this style to the second <div>-->
<!-- ... -->
<script>
function toggleChartVisibility() {
var chosen = document.getElementById("chart_selector").value;
if (chosen == "1") {
document.getElementById("tree1").style.display = "block";
document.getElementById("tree2").style.display = "none";
} else {
document.getElementById("tree1").style.display = "none";
document.getElementById("tree2").style.display = "block";
}
}
</script>
使用 display
隐藏其中一个图表的好处是,每次用户更改所选图表时,图表都不会从头开始重新绘制。
或者,如果您希望在任何给定时间只在文档中绘制一个图表,并在每次用户更改所选图表时绘制一个图表,您可以通过触发 updateDrillDown()
来实现。直接来自对 <select>
的更改框并在 updateDrillDown()
中包含代码在绘制新图表之前清除旧图表:
<select id="chart_selector" onchange="updateDrillDown();">
<option value="1" selected="selected">Chart 1</option>
<option value="2">Chart 2</option>
</select>
<br />
<div id="tree1"></div>
<div id="tree2"></div>
和
function updateDrillDown() {
var chosen = document.getElementById("chart_selector").value;
if (chosen == "1") {
document.getElementById("tree2").innerHTML = "";
drawTreemap("tree1");
} else {
document.getElementById("tree1").innerHTML = "";
drawTreemap("tree2");
}
};
如果每个图表有不同的数据,您还应该包含创建root
的代码。里面drawTreemap()
并在 treeId
上添加条件决定使用哪些数据。
关于javascript - 如何绘制多个 d3js 可缩放 TreeMap ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56583313/
我学习 SDL 二维编程已有一段时间了,现在我想创建一个结合使用 SDL 和 OpenGL 的程序。我是这样设置的: SDL_Init(SDL_INIT_VIDEO); window = SDL_Cr
尝试查找可在地块中使用的不同类型项目的列表 来自不同样本的投影类型: projection = list(type = "equirectangular") projection = list(typ
我正在尝试使用 Java Graphics API 绘制 GIF,但无法使用下面的代码成功绘制 GIF。仅绘制 GIF 的第一张图像或缩略图,但不播放。 public void paintCompon
我目前正在使用 JFrame 并尝试绘制一个矩形,但我不知道如何执行代码 paint(Graphics g),如何获取 Graphics 对象? package com.raggaer.frame;
这个领域的新手,希望得到一些帮助。 我有一个"Missile.java" 类,我在那里画东西。我想绘制一个 ImageView,我正在使用以下代码: ImageView v = (ImageView)
下面列出了圆形的例子 这是我的 JavaScript 代码。 最佳答案 假设您的 randomColor 是正确的,您只需要: 从 canvas.onclick 中移除 context.clearR
我在绘制和缩放 ImageView 时遇到问题。请帮帮我.. 当我画一些东西然后拖动或缩放图像时 - 绘图保留在原处,如您在屏幕截图中所见。而且我只需要简单地在图片上绘图,并且可以缩放和拖动这张图片。
我们可以在形式之外绘制图像和文本...我的意思是在字面上... 我知道问这个问题很愚蠢但是我们能不能... 最佳答案 您可以通过创建表单并将其 TransparentColor 属性设置为背景色来“作
我在绘制/布局期间收到 3 个对象分配警告 super.onDraw(canvas); canvas.drawColor(Color.WHITE); Paint textPaint = new Pai
我有一个示例时间序列数据框: df = pd.DataFrame({'year':'1990','1991','1992','1993','1994','1995','1996',
我试图想出一种简洁的方法来绘制 R 数据框中所有列的 GridView 。问题是我的数据框中既有离散值又有数值。为简单起见,我们可以使用 R 提供的名为 iris 的示例数据集。我会使用 par(mf
我有一个由 10 列和 50 行组成的 data.frame。我使用 apply 函数逐列计算密度函数。现在我想绘制我一次计算的密度。 换句话说,而不是绘图... plot(den[[1]]) plo
我想知道我们如何才能在第一个和第二个组件之外绘制个人,如下所示: 最佳答案 这可能有效: pc.cr <- princomp(USArrests, cor = TRUE) pairs(pc.cr$lo
我是Pandas和matplotlib的新手,想绘制此DataFrame season won team matches pct_won 0 20
我正在尝试为 distplot 子图编写一个 for 循环。 我有一个包含许多不同长度列的数据框。 (不包括 NaN 值) fig = make_subplots( rows=len(asse
我想创建一个具有密度的 3d 图。 我使用函数 density 首先为特定的 x 值创建一个二维图,然后该函数创建密度并将它们放入 y 变量中。现在我有第二组 x 值并将其再次放入密度函数中,然后我得
全部, 我一直在研究全局所有 MTB 步道的索引。我是 Python 人,所以对于所有涉及的步骤,我都尝试使用 Python 模块。 我能够像这样从 OSM 立交桥 API 中获取关系: from O
我正在使用 e1071 包中的支持向量机对我的数据进行分类,并希望可视化机器实际如何进行分类。但是,在使用 plot.svm 函数时,出现无法解决的错误。 脚本: library("e1071") d
我制作了以下图表,它是使用 xts 对象创建的。 我使用的代码很简单 plot(graphTS1$CCLL, type = "l", las = 2, ylab = "(c)\nCC for I
在绘制状态图时,您如何知道哪些状态放在框中,哪些状态用于转换箭头?我注意到转换也是状态。 我正在查看 this page 上的图 1 : 最佳答案 转换不是状态。转换是将对象从一种状态移动到下一种状态
我是一名优秀的程序员,十分优秀!