D3、jsを使用して単純な棒グラフを作成し、テキストの色に問題がありました。 バーの中にテキストが入っているとかなり見えますが、テキストが長すぎると読みにくくなります。テキストを追加は他の色をし、これを可能ならば、それはクローム/ FF/IE11 /エッジで動作しますD3を使用して背景に応じてテキストの色を変更します
- どのようにテキストが矩形の内側にある場合(赤RECT幅に応じて、テキストの色を変更することができます - それ以外の場合は、デフォルトの色?
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
を使用して、必要なrect
にxlink:href
とリンク:
// 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)オーバーレイを追加しますclipPath
SVG要素のidにリンクヤーtext
clip-path
とスタイルpropery:
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;
});
可能な複製(のhttp:/ /stackoverflow.com/questions/29273237/svg-text-color-with-correspond-to-background) – altocumulus