@Override public void buildDataset( String macroContent, Map<String, String> parameters, MacroTransformationContext context) throws MacroExecutionException { validateParameters(parameters); TableBlock tableBlock = getTableBlock(macroContent, context); int[] dataRange = getDataRange(tableBlock); TableDatasetBuilder datasetBuilder; setChartModel(new SimpleChartModel()); switch (getDatasetType()) { case CATEGORY: datasetBuilder = new TableCategoryDatasetBuilder(); break; case PIE: datasetBuilder = new TablePieDatasetBuilder(); break; case TIMETABLE_XY: datasetBuilder = new TableTimeTableXYDatasetBuilder(); break; default: throw new MacroExecutionException( String.format("Unsupported dataset type [%s]", getDatasetType().getName())); } setAxes(); datasetBuilder.setLocaleConfiguration(getLocaleConfiguration()); datasetBuilder.setParameters(parameters); if (SERIES_COLUMNS.equals(series)) { datasetBuilder.setTranspose(true); } buildDataset(tableBlock, dataRange, datasetBuilder); setDataset(datasetBuilder.getDataset()); }
/** * @param tableBlock The table block to parse. * @param startColumn The first column to include. * @param endColumn The last column to include. * @param startRow The first row to include. * @param datasetBuilder The dataset builder. * @throws MacroExecutionException if there are any errors in the table. */ private void getColumnKeys( TableBlock tableBlock, int startColumn, int endColumn, int startRow, TableDatasetBuilder datasetBuilder) throws MacroExecutionException { datasetBuilder.setNumberOfColumns(endColumn - startColumn + 1); if (startRow > 0) { TableRowBlock tableRow = (TableRowBlock) tableBlock.getChildren().get(startRow - 1); for (int i = startColumn; i <= endColumn; i++) { String key = cellContentAsString((TableCellBlock) tableRow.getChildren().get(i)); datasetBuilder.setColumnHeading(i - startColumn, key); } } else { for (int i = startColumn; i <= endColumn; i++) { datasetBuilder.setColumnHeading(i - startColumn, "C" + i); } } }
/** * Build a category dataset. * * @param tableBlock The table block to parse. * @param dataRange The data range. * @param datasetBuilder The dataset builder. * @throws MacroExecutionException if there are any errors. */ private void buildDataset( TableBlock tableBlock, int[] dataRange, TableDatasetBuilder datasetBuilder) throws MacroExecutionException { int startRow = dataRange[0]; int startColumn = dataRange[1]; int endRow = dataRange[2]; int endColumn = dataRange[3]; if (startRow == 0 && datasetBuilder.forceRowHeadings()) { startRow = 1; } if (startColumn == 0 && datasetBuilder.forceColumnHeadings()) { startColumn = 1; } getRowKeys(tableBlock, startRow, endRow, startColumn, datasetBuilder); getColumnKeys(tableBlock, startColumn, endColumn, startRow, datasetBuilder); for (int i = startRow; i <= endRow; i++) { if (i < tableBlock.getChildren().size()) { TableRowBlock tableRow = (TableRowBlock) tableBlock.getChildren().get(i); for (int j = startColumn; j <= endColumn; j++) { if (j < tableRow.getChildren().size()) { Number value = cellContentAsNumber((TableCellBlock) tableRow.getChildren().get(j)); datasetBuilder.setValue(i - startRow, j - startColumn, value); } else { throw new MacroExecutionException("Data range (columns) overflow."); } } } else { throw new MacroExecutionException("Data range (rows) overflow."); } } }
/** * @param tableBlock The table block to parse. * @param startRow The first row to include. * @param endRow The last row to include. * @param startColumn The first column to include. * @param datasetBuilder The dataset builder. * @throws MacroExecutionException if there are any errors in the table. */ private void getRowKeys( TableBlock tableBlock, int startRow, int endRow, int startColumn, TableDatasetBuilder datasetBuilder) throws MacroExecutionException { datasetBuilder.setNumberOfRows(endRow - startRow + 1); if (startColumn > 0) { Set<String> rowKeySet = new HashSet<String>(); for (int i = startRow; i <= endRow; i++) { TableRowBlock tableRow = (TableRowBlock) tableBlock.getChildren().get(i); String key = cellContentAsString((TableCellBlock) tableRow.getChildren().get(startColumn - 1)); datasetBuilder.setRowHeading(i - startRow, key); } } else { for (int i = startRow; i <= endRow; i++) { datasetBuilder.setRowHeading(i - startRow, "R" + i); } } }