Dan*_*Duh 5 javascript charts highcharts d3.js donut-chart
在我的项目中,我有大量不同的图表,所有内容均由 D3J 处理。其中一张图表应该是带有标签的甜甜圈类型。所以基于此教程我制作了这个图表
。正如您所看到的,有时(取决于数据)标签文本会重叠。
我的问题是有什么方法可以使其像下面的示例一样,基于这里的 highcharts :
谢谢。
我认为有一个解决标签重叠问题的解决方案,当您调用标签时使用约束松弛:Solving D3 Label Placement with Constraint Relaxing。
我快速浏览了这里的标签,它似乎有效。这是我修改后的代码片段:
alpha = 0.5;
spacing = 5;
function relax() {
again = false;
text.each(function (d, i) {
a = this;
da = d3.select(a);
y1 = da.attr("y");
text.each(function (d, j) {
b = this;
// a & b are the same element and don't collide.
if (a == b) return;
db = d3.select(b);
// a & b are on opposite sides of the chart and
// don't collide
if (da.attr("text-anchor") != db.attr("text-anchor")) return;
// Now let's calculate the distance between
// these elements.
y2 = db.attr("y");
deltaY = y1 - y2;
// If spacing is greater than our specified spacing,
// they don't collide.
if (Math.abs(deltaY) > spacing) return;
// If the labels collide, we'll push each
// of the two labels up and down a little bit.
again = true;
sign = deltaY > 0 ? 1 : -1;
adjust = sign * alpha;
da.attr("y",+y1 + adjust);
db.attr("y",+y2 - adjust);
});
});
if(again) {
setTimeout(relax,20)
}
}
relax();
Run Code Online (Sandbox Code Playgroud)
只需按照上面链接的教程中的下一步操作即可更新折线,以跟随标签到达新位置。祝你好运!
| 归档时间: |
|
| 查看次数: |
3758 次 |
| 最近记录: |