Autopsy  4.19.0
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  */
19 package org.sleuthkit.autopsy.datasourcesummary.uiutils;
20 
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.InvocationTargetException;
23 import java.util.Arrays;
24 import java.util.List;
25 import org.apache.poi.ss.usermodel.Sheet;
26 import org.apache.poi.ss.util.CellRangeAddress;
27 import org.apache.poi.xddf.usermodel.chart.LegendPosition;
28 import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
29 import org.apache.poi.xddf.usermodel.chart.XDDFDataSource;
30 import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
31 import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
32 import org.apache.poi.xddf.usermodel.chart.XDDFPieChartData;
33 import org.apache.poi.xssf.usermodel.XSSFChart;
34 import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
35 import org.apache.poi.xssf.usermodel.XSSFDrawing;
36 import org.apache.poi.xssf.usermodel.XSSFSheet;
37 import org.openxmlformats.schemas.drawingml.x2006.chart.CTPieChart;
42 
48 
49  private static final int DEFAULT_ROW_SIZE = 20;
50  private static final int DEFAULT_COL_SIZE = 10;
51  private static final int DEFAULT_ROW_PADDING = 1;
52  private static final int DEFAULT_COL_OFFSET = 1;
53 
55  private final int colOffset;
56  private final int rowPadding;
57  private final int colSize;
58  private final int rowSize;
59  private final String chartTitle;
60  private final String sheetName;
61 
72  public PieChartExport(String keyColumnHeader,
73  String valueColumnHeader, String valueFormatString,
74  String chartTitle,
75  List<PieChartItem> slices) {
76  this(keyColumnHeader, valueColumnHeader, valueFormatString, chartTitle, chartTitle, slices,
78  }
79 
96  public PieChartExport(String keyColumnHeader,
97  String valueColumnHeader, String valueFormatString,
98  String chartTitle, String sheetName,
99  List<PieChartItem> slices,
100  int colOffset, int rowPadding, int colSize, int rowSize) {
101 
102  this.tableExport = new ExcelTableExport<>(chartTitle,
103  Arrays.asList(
104  new ColumnModel<>(keyColumnHeader, (slice) -> new DefaultCellModel<>(slice.getLabel())),
105  new ColumnModel<>(valueColumnHeader, (slice) -> new DefaultCellModel<>(slice.getValue(), null, valueFormatString))
106  ),
107  slices);
108  this.colOffset = colOffset;
109  this.rowPadding = rowPadding;
110  this.colSize = colSize;
111  this.rowSize = rowSize;
112  this.chartTitle = chartTitle;
113  this.sheetName = sheetName;
114  }
115 
116  @Override
117  public String getSheetName() {
118  return sheetName;
119  }
120 
121  @Override
122  public void renderSheet(Sheet sheet, ExcelExport.WorksheetEnv env) throws ExcelExport.ExcelExportException {
123  write(sheet, 0, 0, env);
124  }
125 
126  @Override
127  public ItemDimensions write(Sheet sheet, int rowStart, int colStart, ExcelExport.WorksheetEnv env) throws ExcelExportException {
128  if (!(sheet instanceof XSSFSheet)) {
129  throw new ExcelExportException("Sheet must be an XSSFSheet in order to write.");
130  }
131 
132  XSSFSheet xssfSheet = (XSSFSheet) sheet;
133 
134  // write pie chart table data
135  ItemDimensions tableDimensions = tableExport.write(xssfSheet, rowStart + rowPadding, colStart, env);
136 
137  XSSFDrawing drawing = xssfSheet.createDrawingPatriarch();
138 
139  int chartColStart = colStart + 2 + colOffset;
140 
141  //createAnchor has arguments of (int dx1, int dy1, int dx2, int dy2, int col1, int row1, int col2, int row2);
142  XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, chartColStart, rowStart + rowPadding, chartColStart + colSize + 1, rowStart + rowSize + 1);
143 
144  XSSFChart chart = drawing.createChart(anchor);
145  chart.setTitleText(chartTitle);
146  chart.setTitleOverlay(false);
147  XDDFChartLegend legend = chart.getOrAddLegend();
148  legend.setPosition(LegendPosition.RIGHT);
149 
150  // CellRangeAddress has arguments of (int firstRow, int lastRow, int firstCol, int lastCol)
151  XDDFDataSource<String> cat = XDDFDataSourcesFactory.fromStringCellRange(xssfSheet,
152  new CellRangeAddress(tableDimensions.getRowStart() + 1, tableDimensions.getRowEnd(),
153  tableDimensions.getColStart(), tableDimensions.getColStart()));
154 
155  XDDFNumericalDataSource<Double> val = XDDFDataSourcesFactory.fromNumericCellRange(xssfSheet,
156  new CellRangeAddress(tableDimensions.getRowStart() + 1, tableDimensions.getRowEnd(),
157  tableDimensions.getColStart() + 1, tableDimensions.getColStart() + 1));
158 
159  // NOTE: There appears to be a classpath issue with POI (a version of 4.0.1 and 4.1.1 simultaneously)
160  // that is causing conflicts for XDDFPieChartData creation (i.e. the compiler thinks its using 4.1.1
161  // and the runtime thinks its using 4.0.1) Reflection is used below to use the 4.0.1 method while
162  // sidestepping compiler issues.
163  // XDDFPieChartData creation that can be used in poi >= 4.1.1:
164  // XDDFPieChartData data = (XDDFPieChartData) chart.createData(ChartTypes.PIE, bottomAxis, leftAxis);
165  // XDDFPieChartData creation that can be used in 4.0.1:
166  // XDDFPieChartData data = new XDDFPieChartData(chart.getCTChart().getPlotArea().addNewPieChart());
167  XDDFPieChartData data;
168  try {
169  Constructor<XDDFPieChartData> constructor = XDDFPieChartData.class.getConstructor(CTPieChart.class);
170  constructor.setAccessible(true);
171  data = constructor.newInstance(chart.getCTChart().getPlotArea().addNewPieChart());
172  } catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException | IllegalArgumentException ex) {
173  throw new ExcelExportException("Error while instantiating chart data.", ex);
174  }
175 
176  data.setVaryColors(true);
177  data.addSeries(cat, val);
178 
179  // Add data labels
180  if (!chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).isSetDLbls()) {
181  chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).addNewDLbls();
182  }
183 
184  chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).getDLbls().addNewShowVal().setVal(true);
185  chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).getDLbls().addNewShowSerName().setVal(false);
186  chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).getDLbls().addNewShowCatName().setVal(true);
187  chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).getDLbls().addNewShowPercent().setVal(true);
188  chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).getDLbls().addNewShowLegendKey().setVal(false);
189 
190  chart.plot(data);
191 
192  return new ItemDimensions(rowStart, colStart, Math.max(tableDimensions.getRowEnd(), rowStart + rowSize) + rowPadding, chartColStart + colSize);
193  }
194 
195 }
PieChartExport(String keyColumnHeader, String valueColumnHeader, String valueFormatString, String chartTitle, List< PieChartItem > slices)
PieChartExport(String keyColumnHeader, String valueColumnHeader, String valueFormatString, String chartTitle, String sheetName, List< PieChartItem > slices, int colOffset, int rowPadding, int colSize, int rowSize)
ItemDimensions write(Sheet sheet, int rowStart, int colStart, ExcelExport.WorksheetEnv env)
ItemDimensions write(Sheet sheet, int rowStart, int colStart, ExcelExport.WorksheetEnv env)
void renderSheet(Sheet sheet, ExcelExport.WorksheetEnv env)
final ExcelTableExport< PieChartItem,?extends ExcelCellModel > tableExport

Copyright © 2012-2021 Basis Technology. Generated on: Fri Aug 6 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.