- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 d3.js 组合了以下树状图。就选民投票率而言,它是前 20 名的州。 Link
我不确定如何将“值”添加到悬停中。理想情况下,它应该显示每个状态的唯一值。我可以为一个人(例如加利福尼亚州)做这件事,但我不确定如何使其灵活,以便它为不同的州生成其他值。
let margin = {
top: 0,
right: 0,
bottom: 0,
left: 0
},
width = 1100 - margin.left - margin.right,
height = 900 - margin.top - margin.bottom;
// append the svg object to the body of the page
let svg = d3
.select('#treemap')
.append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
// Read data
d3.csv('https://raw.githubusercontent.com/oihamza/Interactive-Data-Vis-Fall2020/master/Project%201/stateVoterTurnout.csv', function(data) {
// stratify the data: reformatting for d3.js
var root = d3
.stratify()
.id(function(d) {
return d.name;
}) // Name of the entity (column name is name in csv)
.parentId(function(d) {
return d.parent;
})(
// Name of the parent (column name is parent in csv)
data
);
// data is an object
console.log(data);
let values = data[1].value;
let tooltip = d3
.select('body')
.append('div')
.style('position', 'absolute')
.style('z-index', '10')
.style('visibility', 'hidden')
.style('background-color', 'white')
.style('border', 'solid')
.style('border-width', '2px')
.style('border-radius', '5px')
.style('padding', '5px')
.text(`${values} voters`);
root.sum(function(d) {
return +d.value;
}); // Compute the numeric value for each entity
// Then d3.treemap computes the position of each element of the hierarchy
// The coordinates are added to the root object above
d3.treemap().size([width, height]).padding(3)(root);
console.log(root.leaves());
// use this information to add rectangles:
svg
.selectAll('rect')
.data(root.leaves())
.enter()
.append('rect')
.attr('x', function(d) {
return d.x0;
})
.attr('y', function(d) {
return d.y0;
})
.attr('width', function(d) {
return d.x1 - d.x0;
})
.attr('height', function(d) {
return d.y1 - d.y0;
})
.style('stroke', 'black')
.style('fill', '#945f04')
.on('mouseover', function() {
return tooltip.style('visibility', 'visible');
})
.on('mousemove', function() {
return tooltip
.style('top', d3.event.pageY - 10 + 'px')
.style('left', d3.event.pageX + 10 + 'px');
})
.on('mouseout', function() {
return tooltip.style('visibility', 'hidden');
});
// and to add the text labels
svg
.selectAll('text')
.data(root.leaves())
.enter()
.append('text')
.attr('x', function(d) {
return d.x0 + 10;
}) // +10 to adjust position (more right)
.attr('y', function(d) {
return d.y0 + 20;
}) // +20 to adjust position (lower)
.text(function(d) {
return d.data.name;
})
.attr('font-size', '15px')
.attr('fill', 'white');
});
body,
h1,
h2,
h3,
h4,
h5,
h6 {
font-family: "Montserrat", sans-serif
}
.styling {
transform-origin: center center;
align-content: center;
position: relative;
}
.tooltip {
position: relative;
display: inline-block;
/* border-bottom: 1px dotted black; */
}
.tooltip .tooltiptext {
visibility: hidden;
width: 180px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
/* Position the tooltip */
position: absolute;
z-index: 1;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat">
<span class="styling"><div id="treemap"></div></span>
<!-- Loading this version of d3 -->
<script src="https://d3js.org/d3.v4.js"></script>
最佳答案
当您mouseover
或mousemove
矩形时,您会发现它被分配了带有第一个参数的数据(通常称为d
)——就像当您使用 .attr()
设置属性或使用 .style()
设置样式时,您会这样做。
您可以使用此 d
动态设置工具提示的 .text()
:
let margin = {
top: 0,
right: 0,
bottom: 0,
left: 0
},
width = 1100 - margin.left - margin.right,
height = 900 - margin.top - margin.bottom;
// append the svg object to the body of the page
let svg = d3
.select('#treemap')
.append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
// Read data
d3.csv('https://raw.githubusercontent.com/oihamza/Interactive-Data-Vis-Fall2020/master/Project%201/stateVoterTurnout.csv', function(data) {
// stratify the data: reformatting for d3.js
var root = d3
.stratify()
.id(function(d) {
return d.name;
}) // Name of the entity (column name is name in csv)
.parentId(function(d) {
return d.parent;
})(
// Name of the parent (column name is parent in csv)
data
);
let tooltip = d3
.select('body')
.append('div')
.style('position', 'absolute')
.style('z-index', '10')
.style('visibility', 'hidden')
.style('background-color', 'white')
.style('border', 'solid')
.style('border-width', '2px')
.style('border-radius', '5px')
.style('padding', '5px');
root.sum(function(d) {
return +d.value;
}); // Compute the numeric value for each entity
// Then d3.treemap computes the position of each element of the hierarchy
// The coordinates are added to the root object above
d3.treemap().size([width, height]).padding(3)(root);
// use this information to add rectangles:
svg
.selectAll('rect')
.data(root.leaves())
.enter()
.append('rect')
.attr('x', function(d) {
return d.x0;
})
.attr('y', function(d) {
return d.y0;
})
.attr('width', function(d) {
return d.x1 - d.x0;
})
.attr('height', function(d) {
return d.y1 - d.y0;
})
.style('stroke', 'black')
.style('fill', '#945f04')
.on('mouseover', function() {
tooltip.style('visibility', 'visible');
})
.on('mousemove', function(d) {
tooltip
.style('top', d3.event.pageY - 10 + 'px')
.style('left', d3.event.pageX + 10 + 'px')
.text(`${d.data.value} voters`);
})
.on('mouseout', function() {
tooltip.style('visibility', 'hidden');
});
// and to add the text labels
svg
.selectAll('text')
.data(root.leaves())
.enter()
.append('text')
.attr('x', function(d) {
return d.x0 + 10;
}) // +10 to adjust position (more right)
.attr('y', function(d) {
return d.y0 + 20;
}) // +20 to adjust position (lower)
.text(function(d) {
return d.data.name;
})
.attr('font-size', '15px')
.attr('fill', 'white');
});
body,
h1,
h2,
h3,
h4,
h5,
h6 {
font-family: "Montserrat", sans-serif
}
.styling {
transform-origin: center center;
align-content: center;
position: relative;
}
.tooltip {
position: relative;
display: inline-block;
/* border-bottom: 1px dotted black; */
}
.tooltip .tooltiptext {
visibility: hidden;
width: 180px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
/* Position the tooltip */
position: absolute;
z-index: 1;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat">
<span class="styling"><div id="treemap"></div></span>
<!-- Loading this version of d3 -->
<script src="https://d3js.org/d3.v4.js"></script>
在这些 .on()
函数中没有必要返回任何东西。
关于javascript - 将包含特定数据的鼠标悬停添加到 d3.js 树形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64851125/
我在 d3 (v5) 中制作了一个可折叠的树形图,可以在此处找到其简化版本:https://jsfiddle.net/philipnye/cwj9nkhr/ . 每个节点都有一个标签,但标签只显示在有
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 2 年前。 Improve this qu
我正在尝试为 ZingCharts 动态创建此数据数组,但被难住了。 基本上,我需要制作这个结构: var series = [{ text: "Democratic", style: {
我正在尝试为我的树状图(内存)使用 firstKey() 方法。我的代码如下所示: import java.util.*; //Code in the middle. System.out.print
我正在尝试复制以下 D3 可缩放树形图,以便更好地了解 D3 的工作原理: https://secure.polisci.ohio-state.edu/faq/d3/zoomabletreemap.h
我正在使用 this post 中的示例树状图在我的工作中,但还想跟踪哪行/列来自哪条数据。 我已将数据名称记录编辑为 names ,如下所示,并希望打印距离矩阵可视化底部和右侧的名称。我尝试在对de
我使用 d3.js 组合了以下树状图。就选民投票率而言,它是前 20 名的州。 Link 我不确定如何将“值”添加到悬停中。理想情况下,它应该显示每个状态的唯一值。我可以为一个人(例如加利福尼亚州)做
我正在尝试在数据更新时动态更新我的 d3 树形图。我有两个函数,一个是我最初调用的用于构建树形图的函数,另一个是用于重绘树形图的函数。当我重新绘制树状图时,我在树状图顶部看到一个细黑条,并且我正在绘制
我正在尝试放置 2 个 d3.js 树形图,如此处所示 http://bl.ocks.org/1249394在一页上。两棵树都将加载来自 Javascript 对象的不同数据。第一个将出现在: 第二
我使用从 TreeMap 派生的类和我自己的比较器作为 LinkedHashMap 中的键。使用这个构造,我发现了一些我无法解释自己的奇怪行为。也许你们中的一个人可以提供帮助。我尝试用原语重现我的问题
如何在使用 d3.js 单击时从 treemap 获取矩形的值?HTML正文如下, "ABC" "My rectangle tag here"
我有一个树形图,我试图在这里进行调整,但问题是,当我单击折叠的节点时,它会自动伸展树(Splay Tree)中的所有根节点,而不是仅展开直接子节点。这是一把 fiddle 。我该如何解决这个问题? h
尝试转换 Mike Bostock 的 zoomable treemap到 React 组件中。已经完成了一部分,但不确定当数据取决于渲染的 DOM(即宽度)时在组件之间传递数据的最佳方式。 遇到了本
我正在尝试使用 Javascript 对象创建一个小树形图。我正在使用 D3 并尝试说明元素(键、id)的嵌套结构而不是它们的值。 我的JSFiddle var d1 = { "time_
我想使用 Highcharts 创建一个有 4 个级别的树形图。构建 3 个级别的树形图效果很好。这是 fiddle :https://jsfiddle.net/khsemucx/2/和截图: var
My treemap body { font: 10px sans-serif; position: relative; } .node { box-sizing: borde
当我收到新数据时更新并重新渲染的 js 树形图。我建立了一个包含 50 个元素的数据集,效果很好。但是,我尝试在更大的数据集(包含 10,000 个元素)上运行相同的代码。而且它似乎无法渲染图形,有人
这是 JavaScript 代码: d3.json("city.json", function (error, root) { var nodes = cluster.nodes(ro
我是一名优秀的程序员,十分优秀!