Mapping when all types are the same

2 posts, 0 answers
  1. ultramods
    ultramods avatar
    18 posts
    Member since:
    Sep 2009

    Posted 01 Feb 2012 Link to this post

    I would like to create treemaps dynamically using only one type of object which will represent the hierarchy.

    My object looks like this:

    public class Asset
    {
    public Asset Parent {get;set;}
    public string Name {get;set;}
    public int Type {get;set;} 
    public List<Asset> Children {get;set;}  
    public List<PropertyProperties {get;set;}  
    }

    public class Property
    {
    public string Name {get;set;}
    public double Value {get;set;}  
    }

    I have attached a drawing of a typical model that I will be binding to the treemap.

    For this example I would like to create a treemap that will have Type Well as the leaf, size is "Oil Prod" and color is "WHP"

    My questions are:
    1: How can I do the mapping when all the types are the same (all asset) I want to specify at run time (through some config object) which asset is the leaf based on the asset type (Well) e.g asset.Type == "Well".

    2: How can I map to a size or color value when it's in a list of properties e.g. size = asset.Properties.Where(p => p.Name == "Oil Prod").FirstOrDefault().

    Thank you in advance for any help.
  2. Yavor
    Admin
    Yavor avatar
    401 posts

    Posted 06 Feb 2012 Link to this post

    Hi,

    RadTreeMap can be bound to objects of the same type with no problem. You can check out this online example that demonstrates a File / Folder scenario. Your treemap markup can look like this:

    <telerik:RadTreeMap Name="treemap">
        <telerik:RadTreeMap.TypeDefinitions>
            <telerik:TypeDefinition TargetTypeName="Asset" ChildrenPath="Children"
                                    ValuePath="Value" LabelPath="Name" />
        </telerik:RadTreeMap.TypeDefinitions>
    </telerik:RadTreeMap>

    Each time the treemap will construct a node it will get the information from the type definition, and because in your case you have only 1 type you need only 1 type definition.

    In the above markup we have bound the ValuePath to property called Value. This property is a read only property that runs a LINQ expression over the Properties collection. Here is some code:
    public class Asset
    {
        public Asset Parent { get; set; }
     
        public string Name { get; set; }
     
        public int Type { get; set; }
     
        public List<Asset> Children { get; set; }
     
        public List<Property> Properties { get; set; }
     
        public double Value
        {
            get
            {
                return this.Properties.Sum(c => c.Value);
            }
        }
    }

    You can modify this expression in any way you want. For example you can run one expression for parent nodes and other for leaf nodes.

    Hope this helps!
    All the best,
    Yavor
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. DevCraft banner
Back to Top