use of org.apache.poi.ss.usermodel.ClientAnchor in project Robot-Scouter by SUPERCILEX.
the class SpreadsheetExporter method buildAverageCharts.
@AddTrace(name = "buildAverageCharts")
private void buildAverageCharts(Sheet sheet) {
if (isUnsupportedDevice())
return;
Drawing drawing = sheet.createDrawingPatriarch();
List<Cell> headerCells = getAdjustedList(sheet.getRow(0));
for (Cell cell : headerCells) {
int columnIndex = cell.getColumnIndex();
String headerName = cell.getStringCellValue();
ClientAnchor anchor = createChartAnchor(drawing, sheet.getLastRowNum() + 3, columnIndex, columnIndex + 1);
anchor.setRow2(anchor.getRow2() + 30);
Chart chart = drawing.createChart(anchor);
chart.getOrCreateLegend().setPosition(LegendPosition.BOTTOM);
ChartDataSource<String> categorySource = DataSources.fromArray(new String[] { headerName });
ScatterChartData data = chart.getChartDataFactory().createScatterChartData();
List<Row> dataRows = getAdjustedList(sheet);
for (Row row : dataRows) {
data.addSerie(categorySource, DataSources.fromNumericCellRange(sheet, new CellRangeAddress(row.getRowNum(), row.getRowNum(), columnIndex, columnIndex))).setTitle(row.getCell(0).getStringCellValue());
}
ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
chart.plot(data, bottomAxis, leftAxis);
if (chart instanceof XSSFChart) {
CTPlotArea plotArea = ((XSSFChart) chart).getCTChart().getPlotArea();
setChartAxisTitle(plotArea.getValAxArray(0).addNewTitle(), "Values");
setChartAxisTitle(plotArea.getCatAxArray(0).addNewTitle(), headerName);
}
}
}
use of org.apache.poi.ss.usermodel.ClientAnchor in project poi by apache.
the class ImageUtils method setPreferredSize.
/**
* Calculate and set the preferred size (anchor) for this picture.
*
* @param scaleX the amount by which image width is multiplied relative to the original width.
* @param scaleY the amount by which image height is multiplied relative to the original height.
* @return the new Dimensions of the scaled picture in EMUs
*/
public static Dimension setPreferredSize(Picture picture, double scaleX, double scaleY) {
ClientAnchor anchor = picture.getClientAnchor();
boolean isHSSF = (anchor instanceof HSSFClientAnchor);
PictureData data = picture.getPictureData();
Sheet sheet = picture.getSheet();
// in pixel
Dimension imgSize = getImageDimension(new ByteArrayInputStream(data.getData()), data.getPictureType());
// in emus
Dimension anchorSize = ImageUtils.getDimensionFromAnchor(picture);
final double scaledWidth = (scaleX == Double.MAX_VALUE) ? imgSize.getWidth() : anchorSize.getWidth() / EMU_PER_PIXEL * scaleX;
final double scaledHeight = (scaleY == Double.MAX_VALUE) ? imgSize.getHeight() : anchorSize.getHeight() / EMU_PER_PIXEL * scaleY;
double w = 0;
int col2 = anchor.getCol1();
int dx2 = 0;
//space in the leftmost cell
w = sheet.getColumnWidthInPixels(col2++);
if (isHSSF) {
w *= 1d - anchor.getDx1() / 1024d;
} else {
w -= anchor.getDx1() / (double) EMU_PER_PIXEL;
}
while (w < scaledWidth) {
w += sheet.getColumnWidthInPixels(col2++);
}
if (w > scaledWidth) {
//calculate dx2, offset in the rightmost cell
double cw = sheet.getColumnWidthInPixels(--col2);
double delta = w - scaledWidth;
if (isHSSF) {
dx2 = (int) ((cw - delta) / cw * 1024);
} else {
dx2 = (int) ((cw - delta) * EMU_PER_PIXEL);
}
if (dx2 < 0)
dx2 = 0;
}
anchor.setCol2(col2);
anchor.setDx2(dx2);
double h = 0;
int row2 = anchor.getRow1();
int dy2 = 0;
h = getRowHeightInPixels(sheet, row2++);
if (isHSSF) {
h *= 1 - anchor.getDy1() / 256d;
} else {
h -= anchor.getDy1() / (double) EMU_PER_PIXEL;
}
while (h < scaledHeight) {
h += getRowHeightInPixels(sheet, row2++);
}
if (h > scaledHeight) {
double ch = getRowHeightInPixels(sheet, --row2);
double delta = h - scaledHeight;
if (isHSSF) {
dy2 = (int) ((ch - delta) / ch * 256);
} else {
dy2 = (int) ((ch - delta) * EMU_PER_PIXEL);
}
if (dy2 < 0)
dy2 = 0;
}
anchor.setRow2(row2);
anchor.setDy2(dy2);
Dimension dim = new Dimension((int) Math.round(scaledWidth * EMU_PER_PIXEL), (int) Math.round(scaledHeight * EMU_PER_PIXEL));
return dim;
}
use of org.apache.poi.ss.usermodel.ClientAnchor in project poi by apache.
the class ImageUtils method getDimensionFromAnchor.
/**
* Calculates the dimensions in EMUs for the anchor of the given picture
*
* @param picture the picture containing the anchor
* @return the dimensions in EMUs
*/
public static Dimension getDimensionFromAnchor(Picture picture) {
ClientAnchor anchor = picture.getClientAnchor();
boolean isHSSF = (anchor instanceof HSSFClientAnchor);
Sheet sheet = picture.getSheet();
double w = 0;
int col2 = anchor.getCol1();
//space in the leftmost cell
w = sheet.getColumnWidthInPixels(col2++);
if (isHSSF) {
w *= 1 - anchor.getDx1() / 1024d;
} else {
w -= anchor.getDx1() / (double) EMU_PER_PIXEL;
}
while (col2 < anchor.getCol2()) {
w += sheet.getColumnWidthInPixels(col2++);
}
if (isHSSF) {
w += sheet.getColumnWidthInPixels(col2) * anchor.getDx2() / 1024d;
} else {
w += anchor.getDx2() / (double) EMU_PER_PIXEL;
}
double h = 0;
int row2 = anchor.getRow1();
h = getRowHeightInPixels(sheet, row2++);
if (isHSSF) {
h *= 1 - anchor.getDy1() / 256d;
} else {
h -= anchor.getDy1() / (double) EMU_PER_PIXEL;
}
while (row2 < anchor.getRow2()) {
h += getRowHeightInPixels(sheet, row2++);
}
if (isHSSF) {
h += getRowHeightInPixels(sheet, row2) * anchor.getDy2() / 256;
} else {
h += anchor.getDy2() / (double) EMU_PER_PIXEL;
}
w *= EMU_PER_PIXEL;
h *= EMU_PER_PIXEL;
return new Dimension((int) Math.rint(w), (int) Math.rint(h));
}
use of org.apache.poi.ss.usermodel.ClientAnchor in project poi by apache.
the class WorkingWithPictures method main.
public static void main(String[] args) throws IOException {
//create a new workbook
//or new HSSFWorkbook();
Workbook wb = new XSSFWorkbook();
try {
CreationHelper helper = wb.getCreationHelper();
//add a picture in this workbook.
InputStream is = new FileInputStream(args[0]);
byte[] bytes = IOUtils.toByteArray(is);
is.close();
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
//create sheet
Sheet sheet = wb.createSheet();
//create drawing
Drawing<?> drawing = sheet.createDrawingPatriarch();
//add a picture shape
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(1);
anchor.setRow1(1);
Picture pict = drawing.createPicture(anchor, pictureIdx);
//auto-size picture
pict.resize(2);
//save workbook
String file = "picture.xls";
if (wb instanceof XSSFWorkbook) {
// NOSONAR
file += "x";
}
OutputStream fileOut = new FileOutputStream(file);
try {
wb.write(fileOut);
} finally {
fileOut.close();
}
} finally {
wb.close();
}
}
use of org.apache.poi.ss.usermodel.ClientAnchor in project poi by apache.
the class LineChart method main.
public static void main(String[] args) throws IOException {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("linechart");
final int NUM_OF_ROWS = 3;
final int NUM_OF_COLUMNS = 10;
// Create a row and put some cells in it. Rows are 0 based.
Row row;
Cell cell;
for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
row = sheet.createRow((short) rowIndex);
for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
cell = row.createCell((short) colIndex);
cell.setCellValue(colIndex * (rowIndex + 1));
}
}
Drawing<?> drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
Chart chart = drawing.createChart(anchor);
ChartLegend legend = chart.getOrCreateLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);
LineChartData data = chart.getChartDataFactory().createLineChartData();
// Use a category axis for the bottom axis.
ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
data.addSeries(xs, ys1);
data.addSeries(xs, ys2);
chart.plot(data, bottomAxis, leftAxis);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();
}
Aggregations