Search in sources :

Example 16 with XSSFChart

use of org.apache.poi.xssf.usermodel.XSSFChart in project ocvn by devgateway.

the class TendersByItemExcelControllerTest method numberOfTendersByItemExcelChart.

@Test
public void numberOfTendersByItemExcelChart() throws Exception {
    LangYearFilterPagingRequest filter = getLangYearFilterMockRequest();
    tendersByItemExcelController.numberOfTendersByItemExcelChart(filter, mockHttpServletResponse);
    final byte[] responseOutput = mockHttpServletResponse.getContentAsByteArray();
    final Workbook workbook = new XSSFWorkbook(new ByteArrayInputStream(responseOutput));
    Assert.assertNotNull(workbook);
    final Sheet sheet = workbook.getSheet(ChartType.barcol.toString());
    Assert.assertNotNull("check chart type, sheet name should be the same as the type", sheet);
    final XSSFDrawing drawing = (XSSFDrawing) sheet.getDrawingPatriarch();
    final List<XSSFChart> charts = drawing.getCharts();
    Assert.assertEquals("number of charts", 1, charts.size());
    final XSSFChart chart = charts.get(0);
    Assert.assertEquals("chart title", translationService.getValue(filter.getLanguage(), "charts:bidsByItem:title"), chart.getTitle().getString());
    final List<? extends XSSFChartAxis> axis = chart.getAxis();
    Assert.assertEquals("number of axis", 2, axis.size());
    final CTChart ctChart = chart.getCTChart();
    Assert.assertEquals("Check if we have 1 bar chart", 1, ctChart.getPlotArea().getBarChartArray().length);
}
Also used : XSSFChart(org.apache.poi.xssf.usermodel.XSSFChart) LangYearFilterPagingRequest(org.devgateway.ocds.web.rest.controller.request.LangYearFilterPagingRequest) CTChart(org.openxmlformats.schemas.drawingml.x2006.chart.CTChart) ByteArrayInputStream(java.io.ByteArrayInputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) XSSFDrawing(org.apache.poi.xssf.usermodel.XSSFDrawing) Test(org.junit.Test)

Example 17 with XSSFChart

use of org.apache.poi.xssf.usermodel.XSSFChart in project ocvn by devgateway.

the class ProcurementActivityByYearControllerTest method procurementActivityExcelChart.

@Test
public void procurementActivityExcelChart() throws Exception {
    LangYearFilterPagingRequest filter = getLangYearFilterMockRequest();
    procurementActivityByYearController.procurementActivityExcelChart(filter, mockHttpServletResponse);
    final byte[] responseOutput = mockHttpServletResponse.getContentAsByteArray();
    final Workbook workbook = new XSSFWorkbook(new ByteArrayInputStream(responseOutput));
    Assert.assertNotNull(workbook);
    final Sheet sheet = workbook.getSheet(ChartType.line.toString());
    Assert.assertNotNull("check chart type, sheet name should be the same as the type", sheet);
    final XSSFDrawing drawing = (XSSFDrawing) sheet.getDrawingPatriarch();
    final List<XSSFChart> charts = drawing.getCharts();
    Assert.assertEquals("number of charts", 1, charts.size());
    final XSSFChart chart = charts.get(0);
    Assert.assertEquals("chart title", translationService.getValue(filter.getLanguage(), "charts:overview:title"), chart.getTitle().getString());
    final List<? extends XSSFChartAxis> axis = chart.getAxis();
    Assert.assertEquals("number of axis", 2, axis.size());
    final CTChart ctChart = chart.getCTChart();
    Assert.assertEquals("Check if we have 1 line chart", 1, ctChart.getPlotArea().getLineChartArray().length);
}
Also used : XSSFChart(org.apache.poi.xssf.usermodel.XSSFChart) LangYearFilterPagingRequest(org.devgateway.ocds.web.rest.controller.request.LangYearFilterPagingRequest) CTChart(org.openxmlformats.schemas.drawingml.x2006.chart.CTChart) ByteArrayInputStream(java.io.ByteArrayInputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) XSSFDrawing(org.apache.poi.xssf.usermodel.XSSFDrawing) Test(org.junit.Test)

Example 18 with XSSFChart

use of org.apache.poi.xssf.usermodel.XSSFChart in project Robot-Scouter by SUPERCILEX.

the class SpreadsheetExporter method buildTeamChart.

@AddTrace(name = "buildTeamChart")
private void buildTeamChart(Row row, TeamHelper teamHelper, Map<Chart, Pair<LineChartData, List<ChartAxis>>> chartData, Map<Metric<Void>, Chart> chartPool) {
    if (isUnsupportedDevice())
        return;
    Sheet sheet = row.getSheet();
    int rowNum = row.getRowNum();
    int lastDataCellNum = row.getSheet().getRow(0).getLastCellNum() - 2;
    Chart chart = null;
    Pair<Integer, Metric<Void>> nearestHeader = null;
    List<Row> rows = getAdjustedList(row.getSheet());
    for (int i = row.getRowNum() - 1; i >= 0; i--) {
        Metric metric = getMetricForScouts(mScouts.get(teamHelper), mCache.getMetricKey(rows.get(i)));
        if (metric.getType() == HEADER) {
            nearestHeader = Pair.create(i, metric);
            Chart cachedChart = chartPool.get(metric);
            if (cachedChart != null)
                chart = cachedChart;
            break;
        }
    }
    chartFinder: if (nearestHeader == null) {
        for (Chart possibleChart : chartData.keySet()) {
            if (possibleChart instanceof XSSFChart) {
                XSSFChart xChart = (XSSFChart) possibleChart;
                if (xChart.getGraphicFrame().getAnchor().getRow1() == SINGLE_ITEM) {
                    nearestHeader = Pair.create(0, getMetricForChart(xChart, chartPool));
                    chart = xChart;
                    break chartFinder;
                }
            }
        }
        nearestHeader = Pair.create(0, new Metric.Header("", null));
    }
    LineChartData data;
    if (chart == null) {
        Drawing drawing = sheet.createDrawingPatriarch();
        Integer headerIndex = nearestHeader.first + 1;
        int startChartIndex = lastDataCellNum + 3;
        chart = drawing.createChart(createChartAnchor(drawing, getChartRowIndex(headerIndex, new ArrayList<>(chartData.keySet())), startChartIndex, startChartIndex + 10));
        LineChartData lineChartData = chart.getChartDataFactory().createLineChartData();
        data = lineChartData;
        ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
        ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
        leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
        ChartLegend legend = chart.getOrCreateLegend();
        legend.setPosition(LegendPosition.RIGHT);
        chartData.put(chart, Pair.create(lineChartData, Arrays.asList(bottomAxis, leftAxis)));
        chartPool.put(nearestHeader.second, chart);
    } else {
        data = chartData.get(chart).first;
    }
    ChartDataSource<String> categorySource = DataSources.fromStringCellRange(sheet, new CellRangeAddress(0, 0, 1, lastDataCellNum));
    data.addSeries(categorySource, DataSources.fromNumericCellRange(sheet, new CellRangeAddress(rowNum, rowNum, 1, lastDataCellNum))).setTitle(row.getCell(0).getStringCellValue());
}
Also used : Drawing(org.apache.poi.ss.usermodel.Drawing) ArrayList(java.util.ArrayList) RichTextString(org.apache.poi.ss.usermodel.RichTextString) ChartLegend(org.apache.poi.ss.usermodel.charts.ChartLegend) PreferencesUtilsKt.setShouldShowExportHint(com.supercilex.robotscouter.util.PreferencesUtilsKt.setShouldShowExportHint) PreferencesUtilsKt.shouldShowExportHint(com.supercilex.robotscouter.util.PreferencesUtilsKt.shouldShowExportHint) XSSFChart(org.apache.poi.xssf.usermodel.XSSFChart) ChartAxis(org.apache.poi.ss.usermodel.charts.ChartAxis) ValueAxis(org.apache.poi.ss.usermodel.charts.ValueAxis) LineChartData(org.apache.poi.ss.usermodel.charts.LineChartData) Metric(com.supercilex.robotscouter.data.model.Metric) Row(org.apache.poi.ss.usermodel.Row) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) SpreadsheetUtils.getCellRangeAddress(com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getCellRangeAddress) Sheet(org.apache.poi.ss.usermodel.Sheet) 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 19 with XSSFChart

use of org.apache.poi.xssf.usermodel.XSSFChart in project poi by apache.

the class TestXSSFChartTitle method testExistingChartWithTitle.

@Test
public void testExistingChartWithTitle() throws IOException {
    Workbook wb = XSSFTestDataSamples.openSampleWorkbook("chartTitle_withTitle.xlsx");
    XSSFChart chart = getChartFromWorkbook(wb, "Sheet1");
    assertNotNull(chart);
    XSSFRichTextString originalTitle = chart.getTitleText();
    assertNotNull(originalTitle);
    final String myTitle = "My chart title";
    assertFalse(myTitle.equals(originalTitle.toString()));
    chart.setTitleText(myTitle);
    XSSFRichTextString queryTitle = chart.getTitleText();
    assertNotNull(queryTitle);
    assertEquals(myTitle, queryTitle.toString());
    wb.close();
}
Also used : XSSFChart(org.apache.poi.xssf.usermodel.XSSFChart) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 20 with XSSFChart

use of org.apache.poi.xssf.usermodel.XSSFChart in project poi by apache.

the class TestXSSFChartTitle method testExistingChartNoTitle.

@Test
public void testExistingChartNoTitle() throws IOException {
    Workbook wb = XSSFTestDataSamples.openSampleWorkbook("chartTitle_noTitle.xlsx");
    XSSFChart chart = getChartFromWorkbook(wb, "Sheet1");
    assertNotNull(chart);
    assertNull(chart.getTitleText());
    final String myTitle = "My chart title";
    chart.setTitleText(myTitle);
    XSSFRichTextString queryTitle = chart.getTitleText();
    assertNotNull(queryTitle);
    assertEquals(myTitle, queryTitle.toString());
    wb.close();
}
Also used : XSSFChart(org.apache.poi.xssf.usermodel.XSSFChart) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Aggregations

XSSFChart (org.apache.poi.xssf.usermodel.XSSFChart)32 Workbook (org.apache.poi.ss.usermodel.Workbook)19 Test (org.junit.Test)19 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)18 CTChart (org.openxmlformats.schemas.drawingml.x2006.chart.CTChart)18 Sheet (org.apache.poi.ss.usermodel.Sheet)17 XSSFDrawing (org.apache.poi.xssf.usermodel.XSSFDrawing)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 LangYearFilterPagingRequest (org.devgateway.ocds.web.rest.controller.request.LangYearFilterPagingRequest)13 ChartAxis (org.apache.poi.ss.usermodel.charts.ChartAxis)11 CTPlotArea (org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea)11 CustomChartSeries (org.devgateway.toolkit.web.excelcharts.CustomChartSeries)7 CTValAx (org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx)5 AddTrace (com.google.firebase.perf.metrics.AddTrace)3 SpreadsheetUtils.getCellRangeAddress (com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getCellRangeAddress)3 SpreadsheetUtils.getMetricForChart (com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getMetricForChart)3 PreferencesUtilsKt.setShouldShowExportHint (com.supercilex.robotscouter.util.PreferencesUtilsKt.setShouldShowExportHint)3 PreferencesUtilsKt.shouldShowExportHint (com.supercilex.robotscouter.util.PreferencesUtilsKt.shouldShowExportHint)3 Chart (org.apache.poi.ss.usermodel.Chart)3 RichTextString (org.apache.poi.ss.usermodel.RichTextString)3