简体   繁体   English

更改Raphael“标签”的背景颜色

[英]Change the background colour of a Raphael “Label”

If I create a Label using Raphael, the default style is a black block with white text. 如果我使用Raphael创建Label ,则默认样式是带有白色文本的黑色块。

How can I change the background box colour, but not the text colour? 如何更改背景框颜色,而不是文本颜色? I've tried: 我试过了:

paper.label(x, y, value).attr("fill", colour)

but that also fills the text and I end up with invisible text. 但这也填补了文本,我最终得到了隐形文本。

I also can't simply change the default colour in this function because I need to have a few different ones depending on a line that it's added to: 我也不能简单地改变这个函数的默认颜色,因为我需要有几个不同的颜色,具体取决于它添加到的一行:

在此输入图像描述

As you noticed, 你注意到了,

Paper.label(x, y, value).attr(
    fill : color
);

changes both the background fill color and the text fill color, resulting in invisible text. 更改背景填充颜色和文本填充颜色,从而产生不可见的文本。

Unspecified correctly explained that this is an array, so each portion must be altered separately, as they illustrated. 未指定正确地解释了这是一个数组,因此每个部分必须单独更改,如图所示。 However, they didn't mention the easiest way to change update both sets of attributes, so I wanted to share this tip. 但是,他们没有提到更改更新两组属性的最简单方法,所以我想分享这个提示。 In order to do this, change the attributes into an array with two sets. 为此,请将属性更改为包含两个集的数组。 The first element is the background, and the second is the text. 第一个元素是背景,第二个元素是文本。

Paper.label(x, y, value).attr([{
    fill : backgroundColor
}, {
    fill : textColor
}]);

You can add any other applicable attributes to each part. 您可以为每个零件添加任何其他适用的属性。 Here is a working example: http://jsfiddle.net/VzpeG/1/ 这是一个工作示例: http//jsfiddle.net/VzpeG/1/

I was working on a graph similar to this and used: 我正在研究类似于此的图表并使用:

.attr({fill: "#EF7C4D"}) .attr({fill:“#EF7C4D”})

Let me know how this goes... 让我知道这是怎么回事......

var r = Raphael('divID', 320, 220);
text = r.text(100,100,"Hello").attr({fill:"#fff"});​​​​
text.label().attr({fill:"#f00"});

Here's a working fiddle http://jsfiddle.net/vpGyL/216/ 这是一个工作小提琴http://jsfiddle.net/vpGyL/216/

Set any color on text or on label both apply separately...Hope this helps ! 在文本或标签上设置任何颜色都单独应用...希望这有帮助!

Digging in furthur... Paper.label(x,y,text) is different from Element.label() 挖掘Paper.label(x,y,text) ... Paper.label(x,y,text)Element.label()不同

If you look at the source code Paper.Label(x,y,text) is a set of rectangle element & text element, so doing .attr({fill:"SomeColor"}) applies to the entire set, hence both rectangle & text share same color(Hence the invisibility). 如果查看源代码Paper.Label(x,y,text)是一组矩形元素和文本元素,那么.attr({fill:"SomeColor"})适用于整个集合,因此矩形和文本共享相同的颜色(因此不可见)。

Oh yeah If you just want to change the text color do this Raphael.g.txtattr.fill = "#yourColorCode" But this changes the text color globally on all the charts and tooltips(don't seem to be a good idea). 哦是的如果你只想更改文本颜色,请执行此操作Raphael.g.txtattr.fill = "#yourColorCode"但这会在所有图表和工具提示上全局更改文本颜色(似乎不是一个好主意)。

While Element.Label as the documentation says is takes the context element & embed in a label tooltip, basically whatever element you use, applying .label will embed it inside a rectangle 虽然文件中的Element.Label表示采用上下文元素并嵌入标签工具提示,但基本上你使用的element ,应用.label会将其嵌入矩形内

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM