Search in sources :

Example 1 with ClientAnchor

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);
        }
    }
}
Also used : Drawing(org.apache.poi.ss.usermodel.Drawing) CTPlotArea(org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea) RichTextString(org.apache.poi.ss.usermodel.RichTextString) ScatterChartData(org.apache.poi.ss.usermodel.charts.ScatterChartData) PreferencesUtilsKt.setShouldShowExportHint(com.supercilex.robotscouter.util.PreferencesUtilsKt.setShouldShowExportHint) PreferencesUtilsKt.shouldShowExportHint(com.supercilex.robotscouter.util.PreferencesUtilsKt.shouldShowExportHint) XSSFChart(org.apache.poi.xssf.usermodel.XSSFChart) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) ChartAxis(org.apache.poi.ss.usermodel.charts.ChartAxis) ValueAxis(org.apache.poi.ss.usermodel.charts.ValueAxis) Row(org.apache.poi.ss.usermodel.Row) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) SpreadsheetUtils.getCellRangeAddress(com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getCellRangeAddress) Cell(org.apache.poi.ss.usermodel.Cell) SpreadsheetUtils.getStringForCell(com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getStringForCell) XSSFChart(org.apache.poi.xssf.usermodel.XSSFChart) Chart(org.apache.poi.ss.usermodel.Chart) CTChart(org.openxmlformats.schemas.drawingml.x2006.chart.CTChart) SpreadsheetUtils.getMetricForChart(com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getMetricForChart) AddTrace(com.google.firebase.perf.metrics.AddTrace)

Example 2 with ClientAnchor

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;
}
Also used : ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) HSSFClientAnchor(org.apache.poi.hssf.usermodel.HSSFClientAnchor) HSSFClientAnchor(org.apache.poi.hssf.usermodel.HSSFClientAnchor) ByteArrayInputStream(java.io.ByteArrayInputStream) Dimension(java.awt.Dimension) Sheet(org.apache.poi.ss.usermodel.Sheet) PictureData(org.apache.poi.ss.usermodel.PictureData)

Example 3 with ClientAnchor

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));
}
Also used : ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) HSSFClientAnchor(org.apache.poi.hssf.usermodel.HSSFClientAnchor) HSSFClientAnchor(org.apache.poi.hssf.usermodel.HSSFClientAnchor) Dimension(java.awt.Dimension) Sheet(org.apache.poi.ss.usermodel.Sheet)

Example 4 with ClientAnchor

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();
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) FileInputStream(java.io.FileInputStream) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) Picture(org.apache.poi.ss.usermodel.Picture) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Sheet(org.apache.poi.ss.usermodel.Sheet)

Example 5 with ClientAnchor

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();
}
Also used : ChartLegend(org.apache.poi.ss.usermodel.charts.ChartLegend) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) ChartAxis(org.apache.poi.ss.usermodel.charts.ChartAxis) ValueAxis(org.apache.poi.ss.usermodel.charts.ValueAxis) FileOutputStream(java.io.FileOutputStream) LineChartData(org.apache.poi.ss.usermodel.charts.LineChartData) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Chart(org.apache.poi.ss.usermodel.Chart)

Aggregations

ClientAnchor (org.apache.poi.ss.usermodel.ClientAnchor)25 Sheet (org.apache.poi.ss.usermodel.Sheet)16 Workbook (org.apache.poi.ss.usermodel.Workbook)13 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)12 Test (org.junit.Test)12 Chart (org.apache.poi.ss.usermodel.Chart)11 Cell (org.apache.poi.ss.usermodel.Cell)9 ChartLegend (org.apache.poi.ss.usermodel.charts.ChartLegend)8 CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)7 Row (org.apache.poi.ss.usermodel.Row)7 FileOutputStream (java.io.FileOutputStream)5 Comment (org.apache.poi.ss.usermodel.Comment)5 RichTextString (org.apache.poi.ss.usermodel.RichTextString)5 ChartAxis (org.apache.poi.ss.usermodel.charts.ChartAxis)5 ValueAxis (org.apache.poi.ss.usermodel.charts.ValueAxis)4 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)4 LineChartData (org.apache.poi.ss.usermodel.charts.LineChartData)3 ScatterChartData (org.apache.poi.ss.usermodel.charts.ScatterChartData)3 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)3 Dimension (java.awt.Dimension)2