Changing Colour

5 posts, 0 answers
  1. Developer
    Developer avatar
    38 posts
    Member since:
    Apr 2014

    Posted 05 Apr Link to this post

    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.
  2. Ivan Danchev
    Admin
    Ivan Danchev avatar
    837 posts

    Posted 06 Apr Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Developer
    Developer avatar
    38 posts
    Member since:
    Apr 2014

    Posted 06 Apr in reply to Ivan Danchev Link to this post

    Hi Ivan,

    Thanks for the suggestion.  Could this be expanded to just loop through the items and assign random colours?
  5. Developer
    Developer avatar
    38 posts
    Member since:
    Apr 2014

    Posted 06 Apr Link to this post

    Also is it possible to change the color of the text, otherwise you might get dark color with dark text.
  6. Ivan Danchev
    Admin
    Ivan Danchev avatar
    837 posts

    Posted 08 Apr Link to this post

    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
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017