This is a migrated thread and some comments may be shown as answers.

Changing Colour

4 Answers 84 Views
TreeMap
This is a migrated thread and some comments may be shown as answers.
Developer
Top achievements
Rank 1
Developer asked on 05 Apr 2016, 01:32 PM
Hi,
I have radtreemap that has a single parent_id.
At the moment it’s all renders in pretty much the same colour, (albeit slightly different tones)
Is there a way to force each item (square) to have a different colour?
Thanks.

4 Answers, 1 is accepted

Sort by
0
Ivan Danchev
Telerik team
answered on 06 Apr 2016, 06:44 AM
Hello,

You can specify the TreeMap items colors by setting their Color property. In order to do that you can subscribe to the control's ItemDataBound event, which will fire for every item, and in its handler check the necessary value (I used the item's Text property in the code snippet below) and based on it set a specific color to the item:
protected void RadTreeMap1_ItemDataBound(object sender, Telerik.Web.UI.TreeMapItemDataBoundEventArguments e)
{
    switch (e.Item.Text)
    {
        case "Item1":
            e.Item.Color = System.Drawing.Color.Green;
            break;
        case "Item2":
            e.Item.Color = System.Drawing.Color.Yellow;
            break;
        case "Item3":
            e.Item.Color = System.Drawing.Color.Blue;
            break;
        default:
            break;
    }
}

Regards,
Ivan Danchev
Telerik
0
Developer
Top achievements
Rank 1
answered on 06 Apr 2016, 07:41 AM
Hi Ivan,

Thanks for the suggestion.  Could this be expanded to just loop through the items and assign random colours?
0
Developer
Top achievements
Rank 1
answered on 06 Apr 2016, 08:02 AM
Also is it possible to change the color of the text, otherwise you might get dark color with dark text.
0
Ivan Danchev
Telerik team
answered on 08 Apr 2016, 03:43 PM
Hello,

You don't need to loop over the items, because the ItemDataBound event fires for every item. In order to set random colors to the items you can create a method returning System.Drawing.Color, implement your logic for generating a random color and call the method in the ItemDataBound event:
protected void RadTreeMap1_ItemDataBound(object sender, Telerik.Web.UI.TreeMapItemDataBoundEventArguments e)
{
    e.Item.Color = GetColor();
}

With regard to your second question, while the TreeMap item's text color can be set with CSS, the item does not have a corresponding property, so you will not be able to set the text color depending on the background color in the code-behind.

Regards,
Ivan Danchev
Telerik
Tags
TreeMap
Asked by
Developer
Top achievements
Rank 1
Answers by
Ivan Danchev
Telerik team
Developer
Top achievements
Rank 1
Share this question
or