Search in sources :

Example 6 with GenerateException

use of com.xpn.xwiki.plugin.charts.exceptions.GenerateException in project xwiki-platform by xwiki.

the class AreaPlotFactory method create.

@Override
public Plot create(DataSource dataSource, ChartParams params) throws GenerateException, DataSourceException {
    Class rendererClass = params.getClass(ChartParams.RENDERER);
    if (rendererClass == null || XYItemRenderer.class.isAssignableFrom(rendererClass)) {
        XYItemRenderer renderer;
        if (rendererClass != null) {
            try {
                Constructor ctor = rendererClass.getConstructor(new Class[] {});
                renderer = (XYItemRenderer) ctor.newInstance(new Object[] {});
            } catch (Throwable e) {
                throw new GenerateException(e);
            }
        } else {
            renderer = new XYAreaRenderer();
        }
        ChartCustomizer.customizeXYItemRenderer(renderer, params);
        return XYPlotFactory.getInstance().create(dataSource, renderer, params);
    } else if (CategoryItemRenderer.class.isAssignableFrom(rendererClass)) {
        CategoryItemRenderer renderer;
        try {
            Constructor ctor = rendererClass.getConstructor(new Class[] {});
            renderer = (CategoryItemRenderer) ctor.newInstance(new Object[] {});
        } catch (Throwable e) {
            throw new GenerateException(e);
        }
        ChartCustomizer.customizeCategoryItemRenderer(renderer, params);
        return CategoryPlotFactory.getInstance().create(dataSource, renderer, params);
    } else {
        throw new GenerateException("Incompatible renderer class: " + rendererClass);
    }
}
Also used : XYAreaRenderer(org.jfree.chart.renderer.xy.XYAreaRenderer) CategoryItemRenderer(org.jfree.chart.renderer.category.CategoryItemRenderer) Constructor(java.lang.reflect.Constructor) XYItemRenderer(org.jfree.chart.renderer.xy.XYItemRenderer) GenerateException(com.xpn.xwiki.plugin.charts.exceptions.GenerateException)

Example 7 with GenerateException

use of com.xpn.xwiki.plugin.charts.exceptions.GenerateException in project xwiki-platform by xwiki.

the class ChartingPlugin method generateSvgChart.

private Chart generateSvgChart(JFreeChart jfchart, ChartParams params, XWikiContext context) throws IOException, GenerateException {
    // Get a DOMImplementation
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    // Create an instance of org.w3c.dom.Document
    Document document = domImpl.createDocument("http://www.w3.org/2000/svg", "svg", null);
    // Create an instance of the SVG Generator
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
    // Ask the chart to render into the SVG Graphics2D implementation
    Rectangle2D.Double rect = new Rectangle2D.Double(0, 0, params.getInteger(ChartParams.WIDTH).intValue(), params.getInteger(ChartParams.HEIGHT).intValue());
    jfchart.draw(svgGenerator, rect);
    boolean useCSS = false;
    StringWriter swriter = new StringWriter();
    svgGenerator.stream(swriter, useCSS);
    String svgText = swriter.toString();
    String pageURL = null;
    SVGPlugin svgPlugin = (SVGPlugin) context.getWiki().getPlugin("svg", context);
    if (svgPlugin == null) {
        throw new GenerateException("SVGPlugin not loaded");
    }
    String imageURL;
    try {
        imageURL = svgPlugin.getSVGImageURL(svgText, params.getInteger(ChartParams.HEIGHT).intValue(), params.getInteger(ChartParams.WIDTH).intValue(), context);
    } catch (SVGConverterException sce) {
        throw new GenerateException(sce);
    }
    return new ChartImpl(params, imageURL, pageURL);
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) DOMImplementation(org.w3c.dom.DOMImplementation) GenericDOMImplementation(org.apache.batik.dom.GenericDOMImplementation) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D) Document(org.w3c.dom.Document) SVGPlugin(com.xpn.xwiki.plugin.svg.SVGPlugin) GenerateException(com.xpn.xwiki.plugin.charts.exceptions.GenerateException) StringWriter(java.io.StringWriter) SVGConverterException(org.apache.batik.apps.rasterizer.SVGConverterException)

Example 8 with GenerateException

use of com.xpn.xwiki.plugin.charts.exceptions.GenerateException in project xwiki-platform by xwiki.

the class ChartingPlugin method generateChart.

public Chart generateChart(ChartParams params, XWikiContext context) throws GenerateException {
    try {
        // Obtain the corresponding data source and wrap it into a data source object
        DataSource dataSource = MainDataSourceFactory.getInstance().create(params.getMap(ChartParams.SOURCE), context);
        String type = params.getString(ChartParams.TYPE);
        Plot plot;
        try {
            String factoryClassName = ChartingPlugin.class.getPackage().getName() + ".plots." + Character.toUpperCase(type.charAt(0)) + type.toLowerCase().substring(1) + "PlotFactory";
            Class factoryClass = Class.forName(factoryClassName);
            Method method = factoryClass.getMethod("getInstance", new Class[] {});
            PlotFactory factory = (PlotFactory) method.invoke(null, new Object[] {});
            plot = factory.create(dataSource, params);
        } catch (InvocationTargetException e) {
            throw new GenerateException(e.getTargetException());
        } catch (Throwable e) {
            throw new GenerateException(e);
        }
        ChartCustomizer.customizePlot(plot, params);
        JFreeChart jfchart = new JFreeChart(plot);
        ChartCustomizer.customizeChart(jfchart, params);
        return generatePngChart(jfchart, params, context);
    } catch (IOException ioe) {
        throw new GenerateException(ioe);
    } catch (DataSourceException dse) {
        throw new GenerateException(dse);
    }
}
Also used : PlotFactory(com.xpn.xwiki.plugin.charts.plots.PlotFactory) Plot(org.jfree.chart.plot.Plot) Method(java.lang.reflect.Method) IOException(java.io.IOException) GenerateException(com.xpn.xwiki.plugin.charts.exceptions.GenerateException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JFreeChart(org.jfree.chart.JFreeChart) DataSource(com.xpn.xwiki.plugin.charts.source.DataSource) DataSourceException(com.xpn.xwiki.plugin.charts.exceptions.DataSourceException)

Example 9 with GenerateException

use of com.xpn.xwiki.plugin.charts.exceptions.GenerateException in project xwiki-platform by xwiki.

the class TableXYDatasetFactory method create.

public XYDataset create(DataSource dataSource, ChartParams params) throws GenerateException, DataSourceException {
    // String type = params.getStringParam("type");
    String dataSeries = params.getString(ChartParams.SERIES);
    DefaultTableXYDataset dataset = new DefaultTableXYDataset();
    if (dataSeries.equals("columns")) {
        if (!dataSource.hasHeaderColumn()) {
            throw new GenerateException("Header column required");
        }
        for (int column = 0; column < dataSource.getColumnCount(); column++) {
            XYSeries series = new XYSeries(dataSource.hasHeaderRow() ? dataSource.getHeaderRowValue(column) : ("Series " + (column + 1)), false, false);
            for (int row = 0; row < dataSource.getRowCount(); row++) {
                series.add(Double.parseDouble(dataSource.getHeaderColumnValue(row)), dataSource.getCell(row, column));
            }
            dataset.addSeries(series);
        }
    } else if (dataSeries.equals("rows")) {
        if (!dataSource.hasHeaderRow()) {
            throw new GenerateException("Header row required");
        }
        for (int row = 0; row < dataSource.getRowCount(); row++) {
            XYSeries series = new XYSeries(dataSource.hasHeaderColumn() ? dataSource.getHeaderColumnValue(row) : ("Series " + (row + 1)), false, false);
            for (int column = 0; column < dataSource.getColumnCount(); column++) {
                series.add(Double.parseDouble(dataSource.getHeaderRowValue(column)), dataSource.getCell(row, column));
            }
            dataset.addSeries(series);
        }
    } else {
        throw new GenerateException("Invalid series parameter:" + dataSeries);
    }
    return dataset;
}
Also used : XYSeries(org.jfree.data.xy.XYSeries) DefaultTableXYDataset(org.jfree.data.xy.DefaultTableXYDataset) GenerateException(com.xpn.xwiki.plugin.charts.exceptions.GenerateException)

Example 10 with GenerateException

use of com.xpn.xwiki.plugin.charts.exceptions.GenerateException in project xwiki-platform by xwiki.

the class TimeSeriesCollectionFactory method create.

public XYDataset create(DataSource dataSource, ChartParams params) throws GenerateException, DataSourceException {
    String dataSeries = params.getString(ChartParams.SERIES);
    TimeSeriesCollection dataset = new TimeSeriesCollection();
    Class timePeriodClass = params.getClass(ChartParams.TIME_PERIOD_CLASS);
    if (timePeriodClass == null) {
        timePeriodClass = Day.class;
    }
    DateFormat format = params.getDateFormat(ChartParams.DATE_FORMAT);
    if (format == null) {
        format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    }
    if (dataSeries.equals("columns")) {
        if (!dataSource.hasHeaderColumn()) {
            throw new GenerateException("Header column required");
        }
        for (int column = 0; column < dataSource.getColumnCount(); column++) {
            String seriesName;
            if (dataSource.hasHeaderRow()) {
                seriesName = dataSource.getHeaderRowValue(column);
            } else {
                seriesName = "Series " + (column + 1);
            }
            TimeSeries series = new TimeSeries(seriesName, timePeriodClass);
            for (int row = 0; row < dataSource.getRowCount(); row++) {
                RegularTimePeriod period;
                try {
                    Date date = format.parse(dataSource.getHeaderColumnValue(row));
                    Constructor ctor = timePeriodClass.getConstructor(new Class[] { Date.class });
                    period = (RegularTimePeriod) ctor.newInstance(new Object[] { date });
                } catch (Exception e) {
                    throw new GenerateException(e);
                }
                series.add(period, dataSource.getCell(row, column));
            }
            dataset.addSeries(series);
        }
    } else if (dataSeries.equals("rows")) {
        if (!dataSource.hasHeaderRow()) {
            throw new GenerateException("Header row required");
        }
        for (int row = 0; row < dataSource.getRowCount(); row++) {
            String seriesName;
            if (dataSource.hasHeaderColumn()) {
                seriesName = dataSource.getHeaderColumnValue(row);
            } else {
                seriesName = "Series " + (row + 1);
            }
            TimeSeries series = new TimeSeries(seriesName, timePeriodClass);
            for (int column = 0; column < dataSource.getColumnCount(); column++) {
                RegularTimePeriod period;
                try {
                    Date date = format.parse(dataSource.getHeaderRowValue(column));
                    Constructor ctor = timePeriodClass.getConstructor(new Class[] { Date.class });
                    period = (RegularTimePeriod) ctor.newInstance(new Object[] { date });
                } catch (Exception e) {
                    throw new GenerateException(e);
                }
                series.add(period, dataSource.getCell(row, column));
            }
            dataset.addSeries(series);
        }
    } else {
        throw new GenerateException("Invalid series parameter:" + dataSeries);
    }
    return dataset;
}
Also used : TimeSeries(org.jfree.data.time.TimeSeries) Constructor(java.lang.reflect.Constructor) GenerateException(com.xpn.xwiki.plugin.charts.exceptions.GenerateException) Date(java.util.Date) GenerateException(com.xpn.xwiki.plugin.charts.exceptions.GenerateException) DataSourceException(com.xpn.xwiki.plugin.charts.exceptions.DataSourceException) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) RegularTimePeriod(org.jfree.data.time.RegularTimePeriod) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

GenerateException (com.xpn.xwiki.plugin.charts.exceptions.GenerateException)10 Constructor (java.lang.reflect.Constructor)6 XYItemRenderer (org.jfree.chart.renderer.xy.XYItemRenderer)4 CategoryItemRenderer (org.jfree.chart.renderer.category.CategoryItemRenderer)3 DataSourceException (com.xpn.xwiki.plugin.charts.exceptions.DataSourceException)2 NumberAxis (org.jfree.chart.axis.NumberAxis)2 XYLineAndShapeRenderer (org.jfree.chart.renderer.xy.XYLineAndShapeRenderer)2 PlotFactory (com.xpn.xwiki.plugin.charts.plots.PlotFactory)1 DataSource (com.xpn.xwiki.plugin.charts.source.DataSource)1 SVGPlugin (com.xpn.xwiki.plugin.svg.SVGPlugin)1 Rectangle2D (java.awt.geom.Rectangle2D)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 SVGConverterException (org.apache.batik.apps.rasterizer.SVGConverterException)1 GenericDOMImplementation (org.apache.batik.dom.GenericDOMImplementation)1