Search in sources :

Example 36 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class AxisConfigurator method setAxes.

/**
 * Set the axes in the chart model.
 *
 * @param plotType The target plot type.
 * @param chartModel The target chart model.
 * @throws MacroExecutionException if the axes are incorrectly configured.
 */
public void setAxes(PlotType plotType, SimpleChartModel chartModel) throws MacroExecutionException {
    AxisType[] defaultAxisTypes = plotType.getDefaultAxisTypes();
    for (int i = 0; i < axisTypes.length; i++) {
        AxisType type = axisTypes[i];
        if (i >= defaultAxisTypes.length) {
            if (type != null) {
                throw new MacroExecutionException("To many axes for plot type.");
            }
            continue;
        }
        if (type == null) {
            type = defaultAxisTypes[i];
        }
        Axis axis;
        switch(type) {
            case NUMBER:
                NumberAxis numberAxis = new NumberAxis();
                axis = numberAxis;
                setNumberLimits(numberAxis, i);
                break;
            case CATEGORY:
                axis = new CategoryAxis();
                break;
            case DATE:
                DateAxis dateAxis = new DateAxis();
                axis = dateAxis;
                dateAxis.setTickMarkPosition(DateTickMarkPosition.END);
                if (axisDateFormat[i] != null) {
                    try {
                        DateFormat dateFormat = new SimpleDateFormat(axisDateFormat[i], localeConfiguration.getLocale());
                        dateAxis.setDateFormatOverride(dateFormat);
                    } catch (IllegalArgumentException e) {
                        throw new MacroExecutionException(String.format("Invalid date format [%s].", axisDateFormat[i]));
                    }
                }
                setDateLimits(dateAxis, i);
                break;
            default:
                throw new MacroExecutionException(String.format("Unsupported axis type [%s]", type.getName()));
        }
        chartModel.setAxis(i, axis);
    }
}
Also used : DateAxis(org.jfree.chart.axis.DateAxis) NumberAxis(org.jfree.chart.axis.NumberAxis) CategoryAxis(org.jfree.chart.axis.CategoryAxis) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) AxisType(org.xwiki.chart.axis.AxisType) SimpleDateFormat(java.text.SimpleDateFormat) ValueAxis(org.jfree.chart.axis.ValueAxis) NumberAxis(org.jfree.chart.axis.NumberAxis) DateAxis(org.jfree.chart.axis.DateAxis) Axis(org.jfree.chart.axis.Axis) CategoryAxis(org.jfree.chart.axis.CategoryAxis)

Example 37 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class LocaleConfiguration method setParameter.

/**
 * Let an implementation set a parameter.
 *
 * @param key The key of the parameter.
 * @param value The value of the parameter.
 * @return {@code true} if the parameter was claimed.
 * @throws MacroExecutionException if the parameter is invalid in some way.
 */
public boolean setParameter(String key, String value) throws MacroExecutionException {
    boolean claimed = true;
    if (LOCALE_PARAM.equals(key)) {
        boolean valid = true;
        Locale l;
        try {
            l = LocaleUtils.toLocale(value);
            if (!LocaleUtils.isAvailableLocale(l)) {
                valid = false;
            } else {
                this.locale = l;
            }
        } catch (IllegalArgumentException e) {
            valid = false;
        }
        if (!valid) {
            throw new MacroExecutionException(String.format("Invalid locale string [%s].", value));
        }
    } else if (DATEFORMAT_PARAM.equals(key)) {
        this.dateFormatString = value;
    } else {
        claimed = false;
    }
    return claimed;
}
Also used : Locale(java.util.Locale) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Example 38 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class AbstractTableBlockDataSource method buildDataset.

@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());
}
Also used : SimpleChartModel(org.xwiki.rendering.internal.macro.chart.source.SimpleChartModel) TableBlock(org.xwiki.rendering.block.TableBlock) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Example 39 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class AbstractTableBlockDataSource method getDataRangeFromParameter.

/**
 * Parses the range parameter and return an array on the form [startRow, startColumn, endRow, endColumn].  Any
 * element in the array may be {@code null} to indicate no bound.
 *
 * @return An array as described above.
 * @throws MacroExecutionException if the range parameter cannot be parsed.
 */
private Integer[] getDataRangeFromParameter() throws MacroExecutionException {
    Integer startColumn = null;
    Integer endColumn = null;
    Integer startRow = null;
    Integer endRow = null;
    if (range != null) {
        Matcher m = RANGE_PATTERN.matcher(range);
        if (!m.matches()) {
            throw new MacroExecutionException(String.format("Invalid range specification: [%s].", range));
        }
        startColumn = getColumnNumberFromIdentifier(m.group(1));
        startRow = getRowNumberFromIdentifier(m.group(2));
        endColumn = getColumnNumberFromIdentifier(m.group(3));
        endRow = getRowNumberFromIdentifier(m.group(4));
        if (startColumn != null && endColumn != null && startColumn > endColumn) {
            throw new MacroExecutionException(String.format("Invalid data range, end column mustn't come before start column: [%s].", range));
        }
        if (startRow != null && endRow != null && startRow > endRow) {
            throw new MacroExecutionException(String.format("Invalid data range, end row mustn't come before start row: [%s].", range));
        }
    }
    return new Integer[] { startRow, startColumn, endRow, endColumn };
}
Also used : Matcher(java.util.regex.Matcher) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Example 40 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class AbstractTableBlockDataSource method getDataRange.

/**
 * Calculates the data-range that is to be used for plotting the chart.
 *
 * @param tableBlock the {@link TableBlock}.
 * @return an integer array consisting of start-row, start-column, end-row and end-column of the data range.
 * @throws MacroExecutionException if it's not possible to determine the data range correctly.
 */
protected int[] getDataRange(TableBlock tableBlock) throws MacroExecutionException {
    Integer[] r = getDataRangeFromParameter();
    int rowCount = tableBlock.getChildren().size();
    if (rowCount > 0) {
        TableRowBlock firstRow = (TableRowBlock) tableBlock.getChildren().get(0);
        int columnCount = firstRow.getChildren().size();
        if (columnCount > 0) {
            return new int[] { r[0] != null ? r[0] : 0, r[1] != null ? r[1] : 0, r[2] != null ? r[2] : rowCount - 1, r[3] != null ? r[3] : columnCount - 1 };
        }
    }
    throw new MacroExecutionException("Data table is incomplete.");
}
Also used : MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) TableRowBlock(org.xwiki.rendering.block.TableRowBlock)

Aggregations

MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)48 Test (org.junit.Test)12 Block (org.xwiki.rendering.block.Block)12 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)10 DocumentReference (org.xwiki.model.reference.DocumentReference)9 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)7 MacroBlock (org.xwiki.rendering.block.MacroBlock)7 XDOM (org.xwiki.rendering.block.XDOM)7 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)6 Expectations (org.jmock.Expectations)5 DocumentDisplayerParameters (org.xwiki.display.internal.DocumentDisplayerParameters)5 StringReader (java.io.StringReader)4 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)4 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)4 IncludeMacroParameters (org.xwiki.rendering.macro.include.IncludeMacroParameters)4 HashMap (java.util.HashMap)3 AttachmentReference (org.xwiki.model.reference.AttachmentReference)3 GroupBlock (org.xwiki.rendering.block.GroupBlock)3 MacroMarkerBlock (org.xwiki.rendering.block.MacroMarkerBlock)3 TableBlock (org.xwiki.rendering.block.TableBlock)3