Document Solutions for Excel, Java Edition | Document Solutions
Features / Chart / Chart Types / Specialized Chart / Treemap
In This Topic
    Treemap
    In This Topic

    TreeMap is a chart type used to display hierarchical data as a set of nested rectangles. Treemap charts are used to represent hierarchical data in a tree-like structure. Data, organized as branches and sub-branches, is depicted with the help of rectangles. With Treemap charts, you can easily drill down huge data to an unlimited number of levels.

    Using Code

    Refer to the following code to add Treemap chart:

    Java
    Copy Code
    private static void TreemapChart() {
        // Initialize workbook
        Workbook workbook = new Workbook();
        // Fetch default worksheet
        IWorksheet worksheet = workbook.getWorksheets().get(0);
    
        // Prepare data for chart
        worksheet.getRange("A1:D16")
                .setValue(new Object[][] { 
                { "Region", "Subregion", "Country", "Population" },
                { "Asia", "Southern", "India", 1354051854 }, 
                { null, null, "Pakistan", 200813818 },
                { null, null, "Bangladesh", 166368149 }, 
                { null, null, "Others", 170220300 },
                { null, "Eastern", "China", 1415045928 }, 
                { null, null, "Japan", 127185332 },
                { null, null, "Others", 111652273 }, 
                { null, "South-Eastern", null, 655636576 },
                { null, "Western", null, 272298399 }, 
                { null, "Central", null, 71860465 },
                { "Africa", "Eastern", null, 433643132 }, 
                { null, "Western", null, 381980688 },
                { null, "Northern", null, 237784677 }, 
                { null, "Others", null, 234512021 },
                { "Europe", null, null, 742648010 }, 
                { "Others", null, null, 1057117703 } });
        worksheet.getRange("A:D").getColumns().autoFit();
        // Add Treemap Chart
        IShape treeMapChartShape = worksheet.getShapes().addChart(ChartType.Treemap, 250, 20, 360, 330);
    
        // Adding series to SeriesCollection
        treeMapChartShape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D16"), RowCol.Columns, true,
                true);
    
        // Configure Chart Title
        treeMapChartShape.getChart().getChartTitle().setText("World Population");
    
        // Saving workbook to Xlsx
        workbook.save("33-TreemapChart.xlsx", SaveFileFormat.Xlsx);