Autopsy 4.22.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
PieChartExport.java
Go to the documentation of this file.
1/*
2 * Autopsy Forensic Browser
3 *
4 * Copyright 2021 Basis Technology Corp.
5 * Contact: carrier <at> sleuthkit <dot> org
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19package org.sleuthkit.autopsy.report.modules.datasourcesummaryexport;
20
21import java.lang.reflect.Constructor;
22import java.lang.reflect.InvocationTargetException;
23import java.util.Arrays;
24import java.util.List;
25import org.apache.poi.ss.usermodel.Sheet;
26import org.apache.poi.ss.util.CellRangeAddress;
27import org.apache.poi.xddf.usermodel.chart.ChartTypes;
28import org.apache.poi.xddf.usermodel.chart.LegendPosition;
29import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
30import org.apache.poi.xddf.usermodel.chart.XDDFDataSource;
31import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
32import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
33import org.apache.poi.xddf.usermodel.chart.XDDFPieChartData;
34import org.apache.poi.xssf.usermodel.XSSFChart;
35import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
36import org.apache.poi.xssf.usermodel.XSSFDrawing;
37import org.apache.poi.xssf.usermodel.XSSFSheet;
38import org.openxmlformats.schemas.drawingml.x2006.chart.CTPieChart;
39import org.sleuthkit.autopsy.datasourcesummary.uiutils.PieChartItem;
40import org.sleuthkit.autopsy.report.modules.datasourcesummaryexport.ExcelExport.ExcelExportException;
41import org.sleuthkit.autopsy.report.modules.datasourcesummaryexport.ExcelExport.ExcelSheetExport;
42import org.sleuthkit.autopsy.report.modules.datasourcesummaryexport.ExcelSpecialFormatExport.ExcelItemExportable;
43import org.sleuthkit.autopsy.report.modules.datasourcesummaryexport.ExcelSpecialFormatExport.ItemDimensions;
44
49class PieChartExport implements ExcelItemExportable, ExcelSheetExport {
50
51 private static final int DEFAULT_ROW_SIZE = 20;
52 private static final int DEFAULT_COL_SIZE = 10;
53 private static final int DEFAULT_ROW_PADDING = 1;
54 private static final int DEFAULT_COL_OFFSET = 1;
55
56 private final ExcelTableExport<PieChartItem, ? extends CellModel> tableExport;
57 private final int colOffset;
58 private final int rowPadding;
59 private final int colSize;
60 private final int rowSize;
61 private final String chartTitle;
62 private final String sheetName;
63
74 PieChartExport(String keyColumnHeader,
75 String valueColumnHeader, String valueFormatString,
76 String chartTitle,
77 List<PieChartItem> slices) {
78 this(keyColumnHeader, valueColumnHeader, valueFormatString, chartTitle, chartTitle, slices,
79 DEFAULT_COL_OFFSET, DEFAULT_ROW_PADDING, DEFAULT_COL_SIZE, DEFAULT_ROW_SIZE);
80 }
81
98 PieChartExport(String keyColumnHeader,
99 String valueColumnHeader, String valueFormatString,
100 String chartTitle, String sheetName,
101 List<PieChartItem> slices,
102 int colOffset, int rowPadding, int colSize, int rowSize) {
103
104 this.tableExport = new ExcelTableExport<>(chartTitle,
105 Arrays.asList(
106 new ColumnModel<>(keyColumnHeader, (slice) -> new DefaultCellModel<>(slice.getLabel())),
107 new ColumnModel<>(valueColumnHeader, (slice) -> new DefaultCellModel<>(slice.getValue(), null, valueFormatString))
108 ),
109 slices);
110 this.colOffset = colOffset;
111 this.rowPadding = rowPadding;
112 this.colSize = colSize;
113 this.rowSize = rowSize;
114 this.chartTitle = chartTitle;
115 this.sheetName = sheetName;
116 }
117
118 @Override
119 public String getSheetName() {
120 return sheetName;
121 }
122
123 @Override
124 public void renderSheet(Sheet sheet, ExcelExport.WorksheetEnv env) throws ExcelExport.ExcelExportException {
125 write(sheet, 0, 0, env);
126 }
127
128 @Override
129 public ItemDimensions write(Sheet sheet, int rowStart, int colStart, ExcelExport.WorksheetEnv env) throws ExcelExportException {
130 if (!(sheet instanceof XSSFSheet)) {
131 throw new ExcelExportException("Sheet must be an XSSFSheet in order to write.");
132 }
133
134 XSSFSheet xssfSheet = (XSSFSheet) sheet;
135
136 // write pie chart table data
137 ItemDimensions tableDimensions = tableExport.write(xssfSheet, rowStart + rowPadding, colStart, env);
138
139 XSSFDrawing drawing = xssfSheet.createDrawingPatriarch();
140
141 int chartColStart = colStart + 2 + colOffset;
142
143 //createAnchor has arguments of (int dx1, int dy1, int dx2, int dy2, int col1, int row1, int col2, int row2);
144 XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, chartColStart, rowStart + rowPadding, chartColStart + colSize + 1, rowStart + rowSize + 1);
145
146 XSSFChart chart = drawing.createChart(anchor);
147 chart.setTitleText(chartTitle);
148 chart.setTitleOverlay(false);
149 XDDFChartLegend legend = chart.getOrAddLegend();
150 legend.setPosition(LegendPosition.RIGHT);
151
152 // CellRangeAddress has arguments of (int firstRow, int lastRow, int firstCol, int lastCol)
153 XDDFDataSource<String> cat = XDDFDataSourcesFactory.fromStringCellRange(xssfSheet,
154 new CellRangeAddress(tableDimensions.getRowStart() + 1, tableDimensions.getRowEnd(),
155 tableDimensions.getColStart(), tableDimensions.getColStart()));
156
157 XDDFNumericalDataSource<Double> val = XDDFDataSourcesFactory.fromNumericCellRange(xssfSheet,
158 new CellRangeAddress(tableDimensions.getRowStart() + 1, tableDimensions.getRowEnd(),
159 tableDimensions.getColStart() + 1, tableDimensions.getColStart() + 1));
160
161 XDDFPieChartData data = (XDDFPieChartData) chart.createData(ChartTypes.PIE, null, null);
162 data.setVaryColors(true);
163 data.addSeries(cat, val);
164
165 // Add data labels
166 if (!chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).isSetDLbls()) {
167 chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).addNewDLbls();
168 }
169
170 chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).getDLbls().addNewShowVal().setVal(true);
171 chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).getDLbls().addNewShowSerName().setVal(false);
172 chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).getDLbls().addNewShowCatName().setVal(true);
173 chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).getDLbls().addNewShowPercent().setVal(true);
174 chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).getDLbls().addNewShowLegendKey().setVal(false);
175
176 chart.plot(data);
177
178 return new ItemDimensions(rowStart, colStart, Math.max(tableDimensions.getRowEnd(), rowStart + rowSize) + rowPadding, chartColStart + colSize);
179 }
180
181}

Copyright © 2012-2024 Sleuth Kit Labs. Generated on:
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.