MAUI | ComponentOne
Controls / FlexChart / Charts Types / Specialized Charts / Tree Map
In This Topic
    Tree Map
    In This Topic

    TreeMaps are the data visualization tools that display the hierarchical data as a set of nested rectangles, while displaying the quantities for each category through area size of the corresponding rectangles. These charts are useful in giving a quick glimpse of patterns in huge hierarchical data sets without costing you much of screen real estate. For instance, below treemap shows the product-wise sales distribution across various categories without occupying much space.

    Treemap chart

    FlexChart for WinUI provides treemap through a stand alone control which is represented by the C1TreeMap class. You can bind the chart with data using the ItemsSource property. This class also provides Binding and BindingName properties for generating rectangular nodes for data items and their respective categories or groups.  While Binding property takes string value depicting the name of the property of data item that contains numeric data value, helpful in calculating the size of rectangular nodes, BindingName takes string value depicting the name of data items. ChildItemPath property ensures that a hierarchical structure of the provided data collection is maintained, by communicating to the control about the child items within the data.

    By default, the TreeMap control displays the squarified layout (as shown in the image above) in which treemap rectangles are arranged as approximate squares. This layout is very useful for displaying large data sets and makes it easy to make comparisons and point patterns. However, you can also display your treemap in the horizontal or vertical layout by setting the ChartType property of C1TreeMap class.

    Following XAML code is required to create the TreeMap chart using FlexChart:

    XAML
    Copy Code
    <c1:C1TreeMap x:Name="treeMap" Binding="sales" BindingName="type" ChildItemsPath="items"
              ChartType="Squarified" ItemsSource="{Binding Data}" MaxDepth="2"
              ToolTipContent="{}{value}" LegendPosition="Bottom">
        <c1:C1TreeMap.DataLabel>
            <c1:DataLabel Content="{}{type}" Position="Center"/>
        </c1:C1TreeMap.DataLabel>
    </c1:C1TreeMap>
    

    Following C# code is required to attach the TreeMap chart with the datasource:

    C#
    Copy Code
    public partial class TreeMap : ContentPage
    {
        object[] _data;
        public TreeMap()
        {
            InitializeComponent();
        }
    
        public List<int> MaxDepths => new() { 1, 2, 3, 4 };
    
        public object[] Data
        {
            get
            {
                if (_data == null)
                    _data = DataSource.CreateHierarchicalData();
                return _data;
            }
        }
    }
    
    class DataSource
    {
        public static object[] CreateHierarchicalData()
        {
            var data = new object[] {
                 new {
                  type = "Music",
                   items = new [] {
                    new {
                     type = "Country",
                      items = new [] {
                       new {
                        type = "Classic Country",
                         sales = rand()
                       }, new {
                        type = "Cowboy Country",
                         sales = rand()
                       }, new {
                        type = "Outlaw Country",
                         sales = rand()
                       }, new {
                        type = "Western Swing",
                         sales = rand()
                       }, new {
                        type = "Roadhouse Country",
                         sales = rand()
                       }
                      }
                    }, new {
                     type = "Rock",
                      items = new [] {
                       new {
                        type = "Hard Rock",
                         sales = rand()
                       }, new {
                        type = "Blues Rock",
                         sales = rand()
                       }, new {
                        type = "Funk Rock",
                         sales = rand()
                       }, new {
                        type = "Rap Rock",
                         sales = rand()
                       }, new {
                        type = "Guitar Rock",
                         sales = rand()
                       }, new {
                        type = "Progressive Rock",
                         sales = rand()
                       }
                      }
                    }, new {
                     type = "Classical",
                      items = new [] {
                       new {
                        type = "Symphonies",
                         sales = rand()
                       }, new {
                        type = "Chamber Music",
                         sales = rand()
                       }
                      }
                    }, new {
                     type = "Soundtracks",
                      items = new [] {
                       new {
                        type = "Movie Soundtracks",
                         sales = rand()
                       }, new {
                        type = "Musical Soundtracks",
                         sales = rand()
                       }
                      }
                    }, new {
                     type = "Jazz",
                      items = new [] {
                       new {
                        type = "Smooth Jazz",
                         sales = rand()
                       }, new {
                        type = "Vocal Jazz",
                         sales = rand()
                       }, new {
                        type = "Jazz Fusion",
                         sales = rand()
                       }, new {
                        type = "Swing Jazz",
                         sales = rand()
                       }, new {
                        type = "Cool Jazz",
                         sales = rand()
                       }, new {
                        type = "Traditional Jazz",
                         sales = rand()
                       }
                      }
                    }, new {
                     type = "Electronic",
                      items = new [] {
                       new {
                        type = "Electronica",
                         sales = rand()
                       }, new {
                        type = "Disco",
                         sales = rand()
                       }, new {
                        type = "House",
                         sales = rand()
                       }
                      }
                    }
                   }
                 }, new {
                  type = "Video",
                   items = new [] {
                    new {
                     type = "Movie",
                      items = new [] {
                       new {
                        type = "Kid & Family",
                         sales = rand()
                       }, new {
                        type = "Action & Adventure",
                         sales = rand()
                       }, new {
                        type = "Animation",
                         sales = rand()
                       }, new {
                        type = "Comedy",
                         sales = rand()
                       }, new {
                        type = "Drama",
                         sales = rand()
                       }, new {
                        type = "Romance",
                         sales = rand()
                       }
                      }
                    }, new {
                     type = "TV",
                      items = new [] {
                       new {
                        type = "Science Fiction",
                         sales = rand()
                       }, new {
                        type = "Documentary",
                         sales = rand()
                       }, new {
                        type = "Fantasy",
                         sales = rand()
                       }, new {
                        type = "Military & War",
                         sales = rand()
                       }, new {
                        type = "Horror",
                         sales = rand()
                       }
                      }
                    }
                   }
                 }, new {
                  type = "Books",
                   items = new [] {
                    new {
                     type = "Arts & Photography",
                      items = new [] {
                       new {
                        type = "Architecture",
                         sales = rand()
                       }, new {
                        type = "Graphic Design",
                         sales = rand()
                       }, new {
                        type = "Drawing",
                         sales = rand()
                       }, new {
                        type = "Photography",
                         sales = rand()
                       }, new {
                        type = "Performing Arts",
                         sales = rand()
                       }
                      }
                    }, new {
                     type = "Children's Books",
                      items = new [] {
                       new {
                        type = "Beginning Readers",
                         sales = rand()
                       }, new {
                        type = "Board Books",
                         sales = rand()
                       }, new {
                        type = "Chapter Books",
                         sales = rand()
                       }, new {
                        type = "Coloring Books",
                         sales = rand()
                       }, new {
                        type = "Picture Books",
                         sales = rand()
                       }, new {
                        type = "Sound Books",
                         sales = rand()
                       }
                      }
                    }, new {
                     type = "History",
                      items = new [] {
                       new {
                        type = "Ancient",
                         sales = rand()
                       }, new {
                        type = "Medieval",
                         sales = rand()
                       }, new {
                        type = "Renaissance",
                         sales = rand()
                       }
                      }
                    }, new {
                     type = "Mystery",
                      items = new [] {
                       new {
                        type = "Mystery",
                         sales = rand()
                       }, new {
                        type = "Thriller & Suspense",
                         sales = rand()
                       }
                      }
                    }, new {
                     type = "Romance",
                      items = new [] {
                       new {
                        type = "Action & Adventure",
                         sales = rand()
                       }, new {
                        type = "Holidays",
                         sales = rand()
                       }, new {
                        type = "Romantic Comedy",
                         sales = rand()
                       }, new {
                        type = "Romantic Suspense",
                         sales = rand()
                       }, new {
                        type = "Western",
                         sales = rand()
                       }, new {
                        type = "Historical",
                         sales = rand()
                       }
                      }
                    }, new {
                     type = "Sci-Fi & Fantasy",
                      items = new [] {
                       new {
                        type = "Fantasy",
                         sales = rand()
                       }, new {
                        type = "Gaming",
                         sales = rand()
                       }, new {
                        type = "Science Fiction",
                         sales = rand()
                       }
                      }
                    }
                   }
                 }, new {
                  type = "Electronics",
                   items = new object[] {
                    new {
                     type = "Camera",
                      items = new [] {
                       new {
                        type = "Digital Cameras",
                         sales = rand()
                       }, new {
                        type = "Film Photography",
                         sales = rand()
                       }, new {
                        type = "Lenses",
                         sales = rand()
                       }, new {
                        type = "Video",
                         sales = rand()
                       }, new {
                        type = "Accessories",
                         sales = rand()
                       }
                      }
                    }, new {
                     type = "Headphones",
                      items = new [] {
                       new {
                        type = "Earbud headphones",
                         sales = rand()
                       }, new {
                        type = "Over-ear headphones",
                         sales = rand()
                       }, new {
                        type = "On-ear headphones",
                         sales = rand()
                       }, new {
                        type = "Bluetooth headphones",
                         sales = rand()
                       }, new {
                        type = "Noise-cancelling headphones",
                         sales = rand()
                       }, new {
                        type = "Audiophile headphones",
                         sales = rand()
                       }
                      }
                    }, new {
                     type = "Cell Phones",
                      items = new object[] {
                       new {
                        type = "Cell Phones",
                         sales = rand()
                       }, new {
                        type = "Accessories",
                         items = new [] {
                          new {
                           type = "Batteries",
                            sales = rand()
                          }, new {
                           type = "Bluetooth Headsets",
                            sales = rand()
                          }, new {
                           type = "Bluetooth Speakers",
                            sales = rand()
                          }, new {
                           type = "Chargers",
                            sales = rand()
                          }, new {
                           type = "Screen Protectors",
                            sales = rand()
                          }
                         }
                       }
                      }
                    }, new {
                     type = "Wearable Technology",
                      items = new [] {
                       new {
                        type = "Activity Trackers",
                         sales = rand()
                       }, new {
                        type = "Smart Watches",
                         sales = rand()
                       }, new {
                        type = "Sports & GPS Watches",
                         sales = rand()
                       }, new {
                        type = "Virtual Reality Headsets",
                         sales = rand()
                       }, new {
                        type = "Wearable Cameras",
                         sales = rand()
                       }, new {
                        type = "Smart Glasses",
                         sales = rand()
                       }
                      }
                    }
                   }
                 }, new {
                  type = "Computers & Tablets",
                   items = new [] {
                    new {
                     type = "Desktops",
                      items = new [] {
                       new {
                        type = "All-in-ones",
                         sales = rand()
                       }, new {
                        type = "Minis",
                         sales = rand()
                       }, new {
                        type = "Towers",
                         sales = rand()
                       }
                      }
                    }, new {
                     type = "Laptops",
                      items = new [] {
                       new {
                        type = "2 in 1 laptops",
                         sales = rand()
                       }, new {
                        type = "Traditional laptops",
                         sales = rand()
                       }
                      }
                    }, new {
                     type = "Tablets",
                      items = new [] {
                       new {
                        type = "iOS",
                         sales = rand()
                       }, new {
                        type = "Andriod",
                         sales = rand()
                       }, new {
                        type = "Fire os",
                         sales = rand()
                       }, new {
                        type = "Windows",
                         sales = rand()
                       }
                      }
                    }
                   }
                 }
                };
            return data;
        }
    
        static Random rnd = new Random();
    
        static int rand()
        {
            return rnd.Next(10, 100);
        }
    }