Search code examples
javascriptraphaelgraphael

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.

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:

enter image description here


Solution

  • 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/