Search in sources :

Example 1 with ChartGeneratorException

use of org.xwiki.chart.ChartGeneratorException in project xwiki-platform by xwiki.

the class DefaultChartGenerator method generate.

@Override
public byte[] generate(ChartModel model, Map<String, String> parameters) throws ChartGeneratorException {
    setDefaultParams(parameters);
    String type = parameters.get(TYPE_PARAM);
    String title = parameters.get(TITLE_PARAM);
    PlotGenerator generator;
    try {
        generator = contextComponentManager.get().getInstance(PlotGenerator.class, type);
    } catch (ComponentLookupException e) {
        throw new ChartGeneratorException(String.format("No such chart type : [%s].", type), e);
    }
    Plot plot = generator.generate(model, parameters);
    JFreeChart jfchart = new JFreeChart(title, plot);
    // Run customizations
    for (ChartCustomizer customizer : this.customizerProvider.get()) {
        customizer.customize(jfchart, parameters);
    }
    int width = Integer.parseInt(parameters.get(WIDTH_PARAM));
    int height = Integer.parseInt(parameters.get(HEIGHT_PARAM));
    try {
        return ChartUtilities.encodeAsPNG(jfchart.createBufferedImage(width, height));
    } catch (IOException ex) {
        throw new ChartGeneratorException("Error while png encoding the chart image.");
    }
}
Also used : ChartGeneratorException(org.xwiki.chart.ChartGeneratorException) ChartCustomizer(org.xwiki.chart.ChartCustomizer) PlotGenerator(org.xwiki.chart.internal.plot.PlotGenerator) Plot(org.jfree.chart.plot.Plot) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) IOException(java.io.IOException) JFreeChart(org.jfree.chart.JFreeChart)

Example 2 with ChartGeneratorException

use of org.xwiki.chart.ChartGeneratorException in project xwiki-platform by xwiki.

the class ChartMacro method generateChart.

/**
 * Builds the chart image according to the specifications passed in.
 *
 * @param parameters the macro parameters
 * @param content the macro content
 * @param context the macro transformation context, used for example to find out the current document reference
 * @throws MacroExecutionException if an error occurs while generating / saving the chart image
 */
private void generateChart(ChartMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    String source = computeSource(parameters.getSource(), content);
    DataSource dataSource;
    try {
        dataSource = this.componentManager.getInstance(DataSource.class, source);
    } catch (ComponentLookupException e) {
        throw new MacroExecutionException(String.format("Invalid source parameter [%s]", parameters.getSource()), e);
    }
    Map<String, String> sourceParameters = getSourceParameters(parameters, source);
    dataSource.buildDataset(content, sourceParameters, context);
    try {
        this.imageWriter.writeImage(new ImageId(parameters), this.chartGenerator.generate(dataSource.getChartModel(), sourceParameters));
    } catch (ChartGeneratorException e) {
        throw new MacroExecutionException("Error while rendering chart", e);
    }
}
Also used : ChartGeneratorException(org.xwiki.chart.ChartGeneratorException) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) DataSource(org.xwiki.rendering.internal.macro.chart.source.DataSource)

Aggregations

ChartGeneratorException (org.xwiki.chart.ChartGeneratorException)2 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)2 IOException (java.io.IOException)1 JFreeChart (org.jfree.chart.JFreeChart)1 Plot (org.jfree.chart.plot.Plot)1 ChartCustomizer (org.xwiki.chart.ChartCustomizer)1 PlotGenerator (org.xwiki.chart.internal.plot.PlotGenerator)1 DataSource (org.xwiki.rendering.internal.macro.chart.source.DataSource)1 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)1