java根据时间修改背景颜色_使用D3根据背景更改文本颜色

我使用D3,js创建了一个简单的条形图,并遇到了文本颜色问题 . 当文本在栏内时它看起来很好,但如果文本太长则变得不可读 .

037db884-fd68-438e-877a-3a9a93832a6b.png

如何根据红色矩形宽度更改文本颜色(如果文本在矩形内 - 否则为默认颜色 - 其他颜色并使其在Chrome / FF / IE11 / Edge中工作,如果可能的话?

将文字附加到栏:

bar.append("text")

.attr('class', 'x-name-text')

.attr("x", 10)

.attr("y", barHeight / 2)

.attr("dy", ".35em")

.text(function(d) {

return d.name;

});

UPDATE: 可能的解决方案:

1)追加下层 text :

bar.append("text")

.attr('class', 'x-name-text')

.attr("x", 10)

.attr("y", barHeight / 2)

.attr("fill", "red")

.attr("dy", ".35em")

.text(function(d) {

return d.name;

});

2)追加 rect :(不要忘记每个 rect 的唯一 id )

bar.append("rect")

.attr("fill", color(0))

.attr("id", function(d) {

return d.id;

})

...

3)追加 clipPath 并使用相同的 id 将其与 xlink:href 链接到必要的 rect :

// clipPath depending on rect width

bar.append('clipPath')

.attr('id', function(listItem, index) {

return listItem.id + '' + index;

})

.append('use')

.attr('xlink:href', function(listItem){

return '#' + listItem.id;

});

4)追加覆盖 text 的 clip-path 样式属性链接到 clipPath SVG元素的id:

bar.append('text')

.attr("x", 10)

.attr("y", barHeight / 2)

.attr("dy", ".35em")

.attr('class', 'overflow-name')

.style('clip-path', function(listItem, index) {

return 'url(#' + listItem.id + '' + index + ')';

})

.text(function(d) {

return d.name;

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值