Articles in this section
Category / Section

How to update the data source dynamically in the Blazor TreeMap?

4 mins read

Using the  ObservableCollection data type for the data source, we can dynamically update the data source in Blazor TreeMap. When the data source is updated, the ObservableCollection will immediately refresh the TreeMap component. In this section, we will explain how to dynamically update the data source in TreeMap using the ObservableCollection data type.

In the following example, we have two different lists that are set as an ObservableCollection data type with variables ListOne and ListTwo and two buttons for each list to dynamically update the data source of TreeMap. When you click any button, the list will be assigned to the DataSource property of the TreeMap. When the data source is updated, the TreeMap will be automatically refreshed.

The below code example demonstrates how to update the data source dynamically in the Blazor TreeMap.

Index.razor


@using Syncfusion.Blazor.TreeMap
@using System.Collections.ObjectModel;

<SfTreeMap @ref="@_chart" DataSource="Sublist" Width="450px"
           WeightValuePath="Value"
           TValue="Country"
           Palette='new string[] { "#8ecae6", "#219ebc", "#00b4d8", "#8ac926", "#06d6a0",
            "#ffd166", "#90be6d", "#f8961e", "#70d6ff", "#ffb600",
            "#a1ff0a", "#ff5c8a", "#909580", "#00a5cf", "#bf0603"}'>

    <TreeMapLeafItemSettings LabelPath="Name"></TreeMapLeafItemSettings>
</SfTreeMap>

<div class="row">
    <button @onclick="ShowList1" style="width:150px">Show List 1</button>    
    <button @onclick="ShowList2" style="width:150px">Show List 2</button>
</div>


@code {
    private SfTreeMap<Country>? _chart;
    public ObservableCollection<Country> Sublist { get; set; }

    protected override void OnInitialized()
    {
        base.OnInitialized();
        Sublist = ListOne;
    }


    public void ShowList1()
    {
        Sublist = ListOne;
    }

    public void ShowList2()
    {
        Sublist = ListTwo;
    }


    public class Country
    {
        public string Name { get; set; }
        public int Value { get; set; }
    }
    public ObservableCollection<Country> ListOne = new ObservableCollection<Country> {
        new Country  {Name="United States", Value=17946 },
        new Country  {Name="China", Value=10866 },
        new Country  {Name="Japan", Value=4123 },
        new Country  {Name="Germany", Value=3355 },
        new Country  {Name="United Kingdom", Value=2848 },
        new Country  {Name="France", Value=2421 },
        new Country  {Name="India", Value=2073 },
        new Country  {Name="Italy", Value=1814 },
        new Country  {Name="Brazil", Value=1774 },
        new Country  {Name="Canada", Value=1550 }
    };


    public ObservableCollection<Country> ListTwo = new ObservableCollection<Country> {
        new Country  {Name="Australia", Value=85214},
        new Country  {Name="New Zealand", Value=8412},
        new Country  {Name="Kenya", Value=410},
        new Country  {Name="Brasil", Value=2848 },
        new Country  {Name="Mexico", Value=7414},
        new Country  {Name="Canada", Value=2073 },
        new Country  {Name="Iran", Value=814 },
        new Country  {Name="Pakistan", Value=21774 },
        new Country  {Name="Mongolia", Value=81550 },
    };
}​

The following screenshot illustrates the output of the above code snippet.

Screenshot of the initial rendering of the TreeMap:

InitialRendering.png

Screenshot after dynamically updating the data source on a button click:

AfterDynamicallyAddedtheDataSource.png

View the sample in GitHub

Conclusion

I hope you enjoyed learning how to update the data source dynamically in the Blazor TreeMap component.

You can refer to our Blazor TreeMap feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Blazor TreeMap example to understand how to create and visualize data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, support portal, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied