- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想为圆环图中的每条弧线添加标签。我通过获取每个弧的质心并添加来添加,但不知何故它没有添加到正确的位置。我无法弄清楚,所以我需要一些帮助。我已经在 codepen 中添加了我的代码。链接是here .我的 donut 应该是这样的。
示例代码是:
svg.selectAll(".dataText")
.data(data_ready)
.enter()
.each(function (d) {
var centroid = arc.centroid(d);
d3.select(this)
.append('rect')
.attr("class", "dataBG_" + d.data.value.label)
.attr('x', (centroid[0]) - 28)
.attr('y', (centroid[1]) - 5)
.attr('rx', '10px')
.attr('ry', '10px')
.attr("width", 50)
.attr("height", 20)
.style('fill', d.data.value.color)
.style("opacity", 1.0);
d3.select(this)
.append('text')
.attr("class", "dataText_" + d.data.value.label)
.style('fill', 'white')
.style("font-size", "11px")
.attr("dx", (centroid[0]) - 7)
.attr("dy", centroid[1] + 7)
.text(Math.round((d.data.value.value)) + "%");
});
提前致谢。
最佳答案
codepen 上的“坏”状态与理想状态之间的区别在于,在您不喜欢的状态中,您采用质心,然后将文本置于其中心。粗圆弧的质心是从一个线段帽的中点到另一个的圆弧的中点。如果它有一些有限的厚度并且是一个物理对象,这大致是形状的“质心”。我不认为这是你想要的。你想要的是外弧的中点。没有生成它的函数,但计算起来很容易。此外,我认为您希望为文本 anchor 位于图表左侧的弧与图表右半部分的弧以不同的方式证明文本。我将复制您的代码并对其进行修改,并附上注释解释。
// for some reason I couldn't get Math.Pi to work in d3.js, so
// I'm just going to calculate it once here in the one-shot setup
var piValue = Math.acos(-1);
// also, I'm noting the inner radius here and calculating the
// the outer radius (this is similar to what you do in codepen.)
var innerRadius = 40
var thickness = 30
var outerRadius = innerRadius + thickness
svg.selectAll(".dataText")
.data(data_ready)
.enter()
.each(function (d) {
// I'm renaming "centroid" to "anchor - just a
// point that relates to where you want to put
// the label, regardless of what it means geometrically.
// no more call to arc.centroid
// var centroid = arc.centroid(d);
// calculate the angle halfway between startAngle and
// endAngle. We can just average them because the convention
// seems to be that angles always increase, even if you
// if you pass the 2*pi/0 angle, and that endAngle
// is always greater than startAngle. I subtract piValue
// before dividing by 2 because in "real" trigonometry, the
// convention is that a ray that points in the 0 valued
// angles are measured against the positive x-axis, which
// is angle 0. In D3.pie conventions, the 0-angle points upward
// along the y-axis. Subtracting pi/2 to all angles before
// doing any trigonometry fixes that, because x and y
// are handled normally.
var bisectAngle = (d.startAngle + d.endAngle - piValue) / 2.0
var anchor = [ outerRadius * Math.cos(bisectAngle), outerRadius * Math.sin(bisectAngle) ];
d3.select(this)
.append('rect')
.attr("class", "dataBG_" + d.data.value.label)
// now if you stopped and didn't change anything more, you'd
// have something kind of close to what you want, but to get
// it closer, you want the labels to "swing out" from the
// from the circle - to the left on the left half of the
// the chart and to the right on the right half. So, I'm
// replacing your code with fixed offsets to code that is
// sensitive to which side we're on. You probably also want
// to replace the constants with something related to the
// the dynamic size of the label background, but I leave
// that as an "exercise for the reader".
// .attr('x', anchor[0] - 28)
// .attr('y', anchor[1] - 5)
.attr('x', anchor[0] < 0 ? anchor[0] - 48 : anchor[0] - 2)
.attr('y', anchor[1] - 10
.attr('rx', '10px')
.attr('ry', '10px')
.attr("width", 50)
.attr("height", 20)
.style('fill', d.data.value.color)
.style("opacity", 1.0);
d3.select(this)
.append('text')
.attr("class", "dataText_" + d.data.value.label)
.style('fill', 'white')
.style("font-size", "11px")
// changing the text centering code to match the box
// box-centering code above. Again, rather than constants,
// you're probably going to want something a that
// that adjusts to the size of the background box
// .attr("dx", anchor[0] - 7)
// .attr("dy", anchor[1] + 7)
.attr("dx", anchor[0] < 0 ? anchor[0] - 28 : anchor[0] + 14)
.attr("dy", anchor[1] + 4)
.text(Math.round((d.data.value.value)) + "%");
});
我测试过。您的代码笔示例中的此代码。如果我影响了您对每个人的示例,我深表歉意 - 我不熟悉 codepen,也不知道协作规则。这只是建议,通过一些调整可以提高效率,但我想保持平行以明确我要更改的内容和原因。希望这能给您一些好的想法。
关于d3.js - 在 rect svg 中添加文本并将其附加到圆环图中的圆弧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62592698/
好的,所以我有一个自定义的 rect 函数。自定义rect如下: typedef struct tagRECTEx{ // long left; // long top; // long right;
我在 Rectangles 上找到了这个很棒的数学运算文件: https://gist.github.com/Noitidart/90ea1ebd30156df9ef530c6a9a1b6ea7 及其
在我的 onDraw 中,我拥有构建整个 View 所需的所有代码,但我如何检测我是否只想执行部分重绘。我想应该通过调用 canvas.invalidate(Rect rect); 来触发部分重绘。正
我对以下游戏 map 有疑问: 我想把所有的陆地区域(黄色附近)保存在一个数组中,这样计算机以后就知道用户的点击是在水面上(鼠标指针的x/y坐标不在数组中)还是在国内(x/y 坐标光标不在数组中)。我
我尝试制作一个简单的可视化列,其中矩形的高度由比例确定: var heightScale = d3.scaleLinear() .domain([150,2500]) .range([
我在 WPF 中有一个小项目,我需要在其中交换 UIElement。类似于 iGoogle 的功能。 由于我发不了图片(信誉不够)我会用文字说明。我有一个这样定义的 3x3 网格: 0 1
这个问题已经有答案了: How do I detect collision in pygame? (5 个回答) 已关闭 6 个月前。 问候, 我在 pygame 中从事的项目似乎遇到了一些麻烦。对于
我正在使用: check = pygame.image.load("check.png") checkrect = check.get_rect() checkrect = checkrect.mov
我试图让我的玩家 rect 在侧面和底部与我的敌人 rect 碰撞,因此玩家 rect 不会抛出敌人的 rect 但我不知道为什么它一直将我传送到它根本不起作用 VIDEo platform.rec
这个问题已经有答案了: How do I get the snake to grow and chain the movement of the snake's body? (1 个回答) 已关闭 3
API 是否应该提供 Rect::contains(Point) 或 Point::is_inside(Rect) 或两者? 或 Math::contains(Point, Rect) 因为它是对称的
我在我的项目中使用 Konva.js,我特别需要在使用 Konva.Rect() 类定义的可拖动矩形内定义文本。有没有一种方法可以在不使用单独的 Konva.Text 的情况下实现这一点() 或 Ko
'annot' CGPDFDictionary 'Rect' 如何转换为 objective-c Rect: 最佳答案 Adobes PDF Spec状态: Rectangles are used t
我正在尝试在 UIView 中绘制一个字符串。 class ViewController: UIViewController { override func viewDidLoad() { s
我正在使用 D3 制作图表。我希望用户能够通过调整单列水平柱形图上堆叠条形的大小来输入他们的选择。 条形堆叠在一起,每个条形都有 .call(drag) 事件监听器。 此拖动事件返回错误。 var d
我刚刚在官方文档中浏览了 pygame 的 .rect 方法。 我们有2个案例, pygame.rect.move(arg1,arg2) 用于在屏幕上移动 .rect 对象 和 pygame.rect
所以我尝试使用实例为我的游戏进行碰撞检测。代码如下: class Paddle: def __init__(self, x, y, width, height, rect): self.x =
我是否正确使用了drawPixmap()? 本质上,我的目标是拍摄一张图 block 图像,然后用自定义图 block 图像替换单个图 block 。 我能够将两个图像都加载到屏幕上,但是当我调用 d
这个想法很简单:我有一个 SVG 路径和一个 Rect,我需要让路径穿过矩形,由此产生的“洞”应该是透明的。 这是 jsfiddle,其中包含一份它现在的样子: https://jsfiddle.ne
很抱歉问了一个菜鸟问题,但是有人能告诉我这两者之间的区别吗 cv:: Rect rect; int width = rect.width; int height = rect.height; 还有这个
我是一名优秀的程序员,十分优秀!