How to create TreeMap chart

Possible Usage Scenarios

A treemap chart provides a hierarchical view of your data and makes it easy to spot patterns, such as which items are a store’s best sellers. The tree branches are represented by rectangles and each sub-branch is shown as a smaller rectangle. The treemap chart displays categories by color and proximity and can easily show lots of data which would be difficult with other chart types.

todo:image_alt_text

TreeMap chart

After running the code below, you will see the TreeMap chart as shown below.

todo:image_alt_text

Sample Code

The following sample code loads the sample Excel file and generates the output Excel file.

// Create an instance of Workbook
Workbook workbook = new Workbook("treemap.xlsx");
// Access the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Add a Treemap chart
int pieIdx = worksheet.getCharts().add(ChartType.TREEMAP, 5, 6, 20, 12);
// Retrieve the Chart object
Chart chart = worksheet.getCharts().get(pieIdx);
// Set the legend can be showed
chart.setShowLegend(true);
// Set the chart title name
chart.getTitle().setText("TreeMap Chart");
// Add series data range(D2:D13,actually)
chart.getNSeries().add("D2:F13", true);
// Set category data(A2:A13 is incorrect )
chart.getNSeries().setCategoryData("A2:C13");
// Show the DataLabels with category names
chart.getNSeries().get(0).getDataLabels().setShowCategoryName(true);
// Fill the PlotArea area with nothing
chart.getPlotArea().getArea().getFillFormat().setFillType(FillType.NONE);
// Save the Excel file
workbook.save("out.xlsx");