如何创建树状图表

可能的使用场景

树状图表提供了数据的分级视图,可轻松找出模式,比如哪些项目是商店的畅销品。树的分支由矩形代表,每个子分支显示为较小的矩形。树状图表通过颜色和临近性显示类别,并且可以轻松显示大量数据,这在其他图表类型中可能会很困难。

todo:image_alt_text

树状图表

运行下面的代码后,您将会看到如下所示的树状图表。

todo:image_alt_text

示例代码

以下示例代码加载 样本 Excel 文件 并生成 输出 Excel 文件

// 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");