- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何将 text
元素的文本对齐设置为 SVG g
元素(父框)的中心?
比如 html div 元素上的 text-align: center
。
我尝试通过 D3.js 构建树状图
这是我尝试的代码:
leaf.append("text")
.attr("fill", "#fff")
//.style("text-anchor", "end")
.attr("width", d => d.x1 - d.x0)
.attr("height", d => d.y1 - d.y0)
// .attr("y", 0)
// .attr("x", 0)
// .attr("dy", 0)
.attr("font-size", d => Math.min(d.x1 - d.x0, d.y1 - d.y0) / 6)
.selectAll("tspan")
.data(d => d.data.name.split(/(?=[A-Z][^A-Z])/g).concat(format(d.data.volume)).concat(format(d.data.pc)))
.enter().append("tspan")
// .style("width", d => d.x1 - d.x0)
// .style("height", d => d.y1 - d.y0)
//.style("text-anchor", "middle")
.attr("dy", "1.1em")
.attr("x", "5")
.attr("font-weight", (d, i, nodes) => i === nodes.length - 1 ? "200" : "700")
.attr("font-size", (d) => d.value)
.text(d => d);
请查看 JSFiddle 上的示例代码
最佳答案
SVG 元素没有您在代码中使用的任何 HTML 属性。
此处的简单解决方案是设置 x
属性到矩形的中间,使用 x0
和 x1
由 TreeMap 生成器创建的属性,结合 text-anchor: middle
.但是,由于<tspan>
绑定(bind)了奇怪的数据元素,我们必须向后弯腰才能从 parent 那里得到数据:
.attr("x", function() {
const parentData = d3.select(this.parentNode).datum();
return (parentData.x1 - parentData.x0) / 2;
});
这是您的代码,仅进行了更改:
let data = {
"name": "topLevel element",
"children": [{
"name": "group 1",
"children": [{
"name": "first child",
"volume": 567.2,
"value": 1250,
"price": 1201,
"pc": 1
},
{
"name": "secondary",
"volume": 7809.1,
"value": 12450,
"price": 13201,
"pc": 2
},
{
"name": "another",
"volume": 2163.7,
"value": 12450,
"price": 13201,
"pc": 3
},
{
"name": "like svg",
"volume": 1756.5,
"value": 12450,
"price": 13201,
"pc": 4
},
{
"name": "abc",
"volume": 1201.7,
"value": 12450,
"price": 13201,
"pc": 5
}
]
},
{
"name": "Group 2",
"children": [{
"name": "tripppp",
"volume": 2999.3,
"value": 12450,
"price": 13201,
"pc": 6
},
{
"name": "pardon",
"volume": 10398.3,
"value": 12450,
"price": 13201,
"pc": 7
},
{
"name": "world d3",
"volume": 10150.5,
"value": 12450,
"price": 13201,
"pc": 8
},
{
"name": "other bit",
"volume": 2652.6,
"value": 12450,
"price": 13201,
"pc": 7
},
{
"name": "rest train",
"volume": 2894,
"value": 12450,
"price": 13201,
"pc": 4
},
{
"name": "big data",
"volume": 6281.2,
"value": 12450,
"price": 13201,
"pc": 3
},
{
"name": "relase note",
"volume": 2431.3,
"value": 12450,
"price": 13201,
"pc": 3
},
{
"name": "seventeen",
"volume": 2373.5,
"value": 12450,
"price": 13201,
"pc": 2
},
{
"name": "fourth",
"volume": 1377.1,
"value": 12450,
"price": 13201,
"pc": 1
}
]
}
]
};
let chartDiv = document.getElementById("chart");
let svg = d3.select(chartDiv).append("svg");
var width = chartDiv.clientWidth;
var height = chartDiv.clientHeight;
let chart = () => {
const root = treemap(filteredData);
const svg = d3.select("svg");
svg
.attr("width", width)
.attr("height", height)
.classed("svg-content-responsive", true)
.attr("preserveAspectRatio", "xMinYMin meet")
.attr("viewBox", `0 0 ${width} ${height}`);
const leaf = svg.selectAll("g")
.data(root.leaves())
.enter().append("g")
.attr("transform", d => `translate(${d.x0},${d.y0})`);
leaf.append("title")
.text(d => `${d.ancestors().reverse().map(d => d.data.name).join("/")}\n${format(d.value)}`);
leaf.append("rect")
.attr("fill", "green")
.attr("fill-opacity", 1.0)
// .attr("text-anchor", "start")
// .attr("dominant-baseline", "central")
.attr("width", d => d.x1 - d.x0)
.attr("height", d => d.y1 - d.y0)
.attr("class", function(d) {
return "node level-" + d.depth;
});
// .on("mouseover", (d) => {
// d3.select(this)
// .style("stroke", "yellow")
// .style("opacity", 1)
// });
// leaf.append("clipPath")
// .attr("id", d => (d.clipUid = ("#clip")).id)
// .append("use")
// .attr("xlink:href", d => d.leafUid.href);
leaf.append("text")
.attr("fill", "#fff")
//.style("text-anchor", "end")
// .attr("clip-path", d => d.clipUid)
.attr("text-anchor", "middle")
.attr("width", d => d.x1 - d.x0)
.attr("height", d => d.y1 - d.y0)
// .attr("y", 0)
// .attr("x", 0)
// .attr("dy", 0)
.attr("font-size", d => Math.min(d.x1 - d.x0, d.y1 - d.y0) / 6)
.selectAll("tspan")
.data(d => d.data.name.split(/(?=[A-Z][^A-Z])/g).concat(format(d.data.volume)).concat(format(d.data.pc)))
.enter().append("tspan")
// .style("width", d => d.x1 - d.x0)
// .style("height", d => d.y1 - d.y0)
// .attr("lengthAdjust" ,"spacingAndGlyphs")
// .attr("transform", "(250 250 250 250)")
//.style("text-anchor", "middle")
// .attr("dominant-baseline", "central")
.attr("dy", "1.1em")
.attr("x", function() {
const parentData = d3.select(this.parentNode).datum();
return (parentData.x1 - parentData.x0) / 2;
})
// .attr("dx", 0)
// .attr("y", 18)
// .attr("y", (d, i, nodes) => `${(i === nodes.length ) * 1.3 + 1.2 + i * 1.9}em`)
.attr("font-weight", (d, i, nodes) => i === nodes.length - 1 ? "200" : "700")
// .attr("font-family", "Vazir")
.attr("font-size", (d) => d.value)
.text(d => d);
return svg.node();
}
let filteredData = d3.hierarchy(data)
.sum(d => d.volume)
.sort((a, b) => b.height - a.height || b.value - a.value);
let treemap = d3.treemap()
.size([width, height])
.padding(1)
.paddingRight(3)
.round(true);
let format = d3.format(",d");
chart();
* {
font-family: 'Vazir', sans-serif;
}
/* Make the chart container fill the page using CSS. */
#chart {
background: #212121;
position: fixed;
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
font-family: 'Vazir', sans-serif;
}
g text {
text-shadow: 1px 2px 0 #555;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<link href="https://cdn.rawgit.com/rastikerdar/vazir-font/v19.2.0/dist/font-face.css" rel="stylesheet" type="text/css" />
<div id="chart"></div>
PS:我写“仅进行了更改”,因为您的代码有几个明显的问题,例如分配 width
和一个 margin
到一个文本元素,我没有触及它。
关于javascript - 在 TreeMap 中将文本元素对齐到中间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55032873/
表架构 DROP TABLE bla; CREATE TABLE bla (id INTEGER, city INTEGER, year_ INTEGER, month_ INTEGER, val I
我需要拆分字符串/或从具有以下结构的字符串中获取更容易的子字符串。 字符串将来自 window.location.pathname 或 window.location.href,看起来像 text/n
每当将对象添加到数组中时,我都会尝试更新 TextView ,并在 TextView 中显示该文本,如下所示: "object 1" "object 2" 问题是,每次将新对象添加到数组时,它都会覆盖
我目前正在寻找使用 Java 读取网站可见文本并将其存储为纯文本字符串的方法。 换句话说,我想转换成这样: Hello stupid World进入“ Hello World ” 或者类似的东西 Un
我正在尝试以文本和 HTML 格式发送电子邮件,但无法正确发送正确的 header 。特别是,我想设置 Content-Type header ,但我找不到如何为 html 和文本部分单独设置它。 这
我尝试了上面的代码,但我无法绑定(bind)文本,我怎样才能将资源内部文本 bloc
我刚刚完成了 Space Shooter 教程,由于没有 GUIText 对象,所以我创建了 UI.Text 对象并进行了相应的编码。它在统一播放器中有效,但在构建 Web 应用程序后无效。我花了一段
我有这个代码: - (IBAction)setButtonPressed:(id)sender { NSUserDefaults *sharedDefaults = [[NSUserDefau
抱歉标题含糊不清,但我想不出我想在标题中做什么。无论如何,对于图像上的文本,我使用了 JLabel 文本并将其添加到图标中。 JLabel icon = new JLabel(new Imag
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我在将 Twitter 嵌入到我从 HTML 5 转换的 wordpress 运行网站时遇到问题。 我遇到的问题是推文不是我的自定义字体... 这是我无法使用任何 css 定位的 HTML 代码,我正
我正在尝试找到解决由于使用以下形式的代码而导致的冗余字符串连接问题的最佳方法: logger.debug("Entering loop, arg is: " + arg) // @1 在大多数情况下,
我写了这个测试 @Test public void removeRequestTextFromRouteError() throws Exception { String input = "F
我目前正在创建一个正则表达式来拆分所有匹配以下格式的字符串:&[文本],并且需要获取文本。字符串可能类似于:something &[text] &[text] everything &[text] 等
有没有办法将标题文本从一个词变形为另一个词,同时保留两个词中使用的字母?我看过的许多 css 文本动画大多是视觉的,很少有旋转整个单词的。 我想要做的是从一个词过渡,例如“BEACH”到“CHANGE
总结matplotlib绘图如何设置坐标轴刻度大小和刻度。 上代码: ?
我在容器 (1) 中创建了容器 (2)。你能帮忙如何向容器(1)添加文本吗?下面是我的代码 return Scaffold( body: Padding( padding: c
我似乎找不到任何人或任何人这样做过。我试图限制我们使用的图像数量,并想创建一个带有渐变作为其“颜色”的文本,并在其周围设置渐变轮廓/描边 到目前为止,我还没有看到任何将两者结合在一起的东西。 我可以自
我正在为视频游戏暗黑破坏神 2 使用 discord.py 构建一个不和谐机器人。其中一项功能要求机器人从暗黑破坏神 2 屏幕截图中提取项目的名称和属性。我目前正在为此使用 pytesseract,但
我很难弄清楚如何旋转 strip.text theme 中的属性来自 ggplot2 .我使用的是 R 版本 3.4.2 和 ggplot2 版本 2.2.1。 以下是 MWE 的数据。 > dput
我是一名优秀的程序员,十分优秀!