Search in sources :

Example 1 with IChartDataModel

use of org.pentaho.chart.data.IChartDataModel in project pentaho-platform by pentaho.

the class ChartComponent method execute.

/**
 * Called to process the chart definition and data set to produce a usable chart.
 *
 * @return state of execution. 'true' if execution was successful, otherwise false.
 * @throws ChartBootException
 * @throws ChartProcessingException
 * @throws ResourceException
 * @throws InvalidChartDefinition
 * @throws IOException
 * @throws PersistenceException
 */
public boolean execute() throws ChartBootException, ChartProcessingException, ResourceException, InvalidChartDefinition, IOException, PersistenceException {
    if (bootException != null) {
        throw new ChartBootException(bootException);
    }
    if (chartModel.getTheme() != null) {
        AbstractChartThemeFactory chartThemeFactory = new AbstractChartThemeFactory() {

            protected List<File> getThemeFiles() {
                ArrayList<File> themeFiles = new ArrayList<File>();
                themeFiles.add(new File(PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$
                "system/chartbeans/themes/Theme1.xml")));
                themeFiles.add(new File(PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$
                "system/chartbeans/themes/Theme2.xml")));
                themeFiles.add(new File(PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$
                "system/chartbeans/themes/Theme3.xml")));
                themeFiles.add(new File(PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$
                "system/chartbeans/themes/Theme4.xml")));
                themeFiles.add(new File(PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$
                "system/chartbeans/themes/Theme5.xml")));
                themeFiles.add(new File(PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$
                "system/chartbeans/themes/Theme6.xml")));
                themeFiles.add(new File(PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$
                "system/chartbeans/themes/Theme7.xml")));
                themeFiles.add(new File(PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$
                "system/chartbeans/themes/Theme8.xml")));
                return themeFiles;
            }
        };
        if (!(chartModel.getPlot() instanceof DialPlot)) {
            Theme chartTheme = chartThemeFactory.getTheme(chartModel.getTheme());
            if (chartTheme != null) {
                chartTheme.applyTo(chartModel);
            }
        }
    }
    // Make sure chart engine is loaded
    loadChartEngine();
    // Set chart engine on chartModel for the ChartFactory to use
    chartModel.setChartEngineId(chartEngine);
    InputStream is = null;
    // Transform IPentahoResultSet to an object array
    Object[][] data = processChartData(resultSet, valueColumn);
    try {
        IChartLinkGenerator chartLinkGenerator = contentLinkingTemplate == null ? null : new ChartLinkGenerator(contentLinkingTemplate);
        IChartDataModel chartDataModel = ChartBeanFactory.createChartDataModel(data, scalingFactor, convertNullsToZero, valueColumn, seriesColumn, categoryColumn, chartModel, resultSet.getMetaData());
        IOutput output = ChartBeanFactory.createChart(chartModel, chartDataModel, chartLinkGenerator);
        // Wrap output as necessary
        if (OpenFlashChartPlugin.PLUGIN_ID.equals(chartEngine)) {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            output.persistChart(outputStream, getOutputType(), chartWidth, chartHeight);
            // $NON-NLS-1$
            String persistedChart = new String(outputStream.toByteArray(), "utf-8");
            IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
            String flashContent = ChartBeansGeneratorUtil.mergeOpenFlashChartHtmlTemplate(persistedChart.replaceAll("\"", "\\\\\""), // $NON-NLS-1$ //$NON-NLS-2$
            requestContext.getContextPath() + this.getSwfPath() + "/" + // $NON-NLS-1$
            getSwfName());
            // $NON-NLS-1$
            is = new ByteArrayInputStream(flashContent.getBytes("utf-8"));
        } else if (JFreeChartPlugin.PLUGIN_ID.equals(chartEngine)) {
            if ("html".equals(outputType) || (chartLinkGenerator != null)) {
                // $NON-NLS-1$
                File imageFile = PentahoSystem.getApplicationContext().createTempFile(PentahoSessionHolder.getSession(), "tmp_chart_", ".png", // $NON-NLS-1$
                false);
                FileOutputStream outputStream = new FileOutputStream(imageFile);
                output.persistChart(outputStream, OutputTypes.FILE_TYPE_PNG, chartWidth, chartHeight);
                String imageMapName = null;
                String imageMap = null;
                if (chartLinkGenerator != null) {
                    imageMapName = imageFile.getName().substring(0, imageFile.getName().indexOf('.'));
                    imageMap = ((JFreeChartOutput) output).getMap(imageMapName);
                }
                String jFreeChartHtml = ChartBeansGeneratorUtil.mergeJFreeChartHtmlTemplate(imageFile, imageMap, imageMapName, chartWidth, chartHeight, PentahoRequestContextHolder.getRequestContext().getContextPath());
                // $NON-NLS-1$
                is = new ByteArrayInputStream(jFreeChartHtml.getBytes("utf-8"));
            } else {
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                output.persistChart(outputStream, getOutputType(), chartWidth, chartHeight);
                is = new ByteArrayInputStream(outputStream.toByteArray());
            }
        }
        int val = 0;
        // TODO: Buffer for more efficiency
        while ((val = is.read()) != -1) {
            outputStream.write(val);
        }
    } catch (NoChartDataException ex) {
        if (JFreeChartPlugin.PLUGIN_ID.equals(chartEngine)) {
            BufferedImage image = new BufferedImage(chartWidth, chartHeight, BufferedImage.TYPE_INT_ARGB);
            Graphics2D graphics = image.createGraphics();
            // $NON-NLS-1$
            graphics.setFont(new Font("serif", Font.BOLD, 14));
            graphics.setColor(Color.BLACK);
            // $NON-NLS-1$
            graphics.drawString("The chart data query returned no data.", 40, 40);
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            String outputType = "png";
            File imageFile = PentahoSystem.getApplicationContext().createTempFile(PentahoSessionHolder.getSession(), "tmp_chart_", ".png", // $NON-NLS-1$
            false);
            FileOutputStream fo = new FileOutputStream(imageFile);
            ImageIO.write(image, "png", fo);
            String jFreeChartHtml = ChartBeansGeneratorUtil.mergeJFreeChartHtmlTemplate(imageFile, null, null, chartWidth, chartHeight, PentahoRequestContextHolder.getRequestContext().getContextPath());
            // $NON-NLS-1$
            is = new ByteArrayInputStream(jFreeChartHtml.getBytes("utf-8"));
            int val = 0;
            while ((val = is.read()) != -1) {
                outputStream.write(val);
            }
        } else {
            String flashContent = ChartBeansGeneratorUtil.buildEmptyOpenFlashChartHtmlFragment("The chart data query returned no data.");
            // $NON-NLS-1$
            // $NON-NLS-1$
            is = new ByteArrayInputStream(flashContent.getBytes("utf-8"));
            int val = 0;
            // TODO: Buffer for more efficiency
            while ((val = is.read()) != -1) {
                outputStream.write(val);
            }
        }
    } catch (ChartDataOverflowException ex) {
        if (JFreeChartPlugin.PLUGIN_ID.equals(chartEngine)) {
            BufferedImage image = new BufferedImage(chartWidth, chartHeight, BufferedImage.TYPE_INT_ARGB);
            Graphics2D graphics = image.createGraphics();
            // $NON-NLS-1$
            graphics.setFont(new Font("serif", Font.BOLD, 14));
            graphics.setColor(Color.BLACK);
            // $NON-NLS-1$
            graphics.drawString(Messages.getInstance().getErrorString("ChartComponent.TOO_MANY_DATA_POINTS"), 5, 5);
            graphics.drawString(Messages.getInstance().getErrorString("ChartComponent.MAX_ALLOWED_DATA_POINTS", Integer.toString(ex.getMaxAllowedDataPoints())), 5, // $NON-NLS-1$
            25);
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            String outputType = getMimeType().equals("image/jpg") ? "jpeg" : "png";
            ImageIO.write(image, outputType, outputStream);
        } else {
            String flashContent = ChartBeansGeneratorUtil.buildEmptyOpenFlashChartHtmlFragment(Messages.getInstance().getErrorString("ChartComponent.TOO_MANY_DATA_POINTS_HTML", // $NON-NLS-1$
            Integer.toString(ex.getMaxAllowedDataPoints())));
            // $NON-NLS-1$
            is = new ByteArrayInputStream(flashContent.getBytes("utf-8"));
            int val = 0;
            // TODO: Buffer for more efficiency
            while ((val = is.read()) != -1) {
                outputStream.write(val);
            }
        }
    }
    return true;
}
Also used : IChartDataModel(org.pentaho.chart.data.IChartDataModel) ArrayList(java.util.ArrayList) DialPlot(org.pentaho.chart.model.DialPlot) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font) IChartLinkGenerator(org.pentaho.chart.IChartLinkGenerator) ChartDataOverflowException(org.pentaho.chart.plugin.ChartDataOverflowException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NoChartDataException(org.pentaho.chart.plugin.NoChartDataException) IChartLinkGenerator(org.pentaho.chart.IChartLinkGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JFreeChartOutput(org.pentaho.chart.plugin.jfreechart.outputs.JFreeChartOutput) AbstractChartThemeFactory(org.pentaho.chart.AbstractChartThemeFactory) Graphics2D(java.awt.Graphics2D) IPentahoRequestContext(org.pentaho.platform.api.engine.IPentahoRequestContext) ByteArrayInputStream(java.io.ByteArrayInputStream) IOutput(org.pentaho.chart.plugin.api.IOutput) FileOutputStream(java.io.FileOutputStream) Theme(org.pentaho.chart.model.Theme) File(java.io.File)

Aggregations

Font (java.awt.Font)1 Graphics2D (java.awt.Graphics2D)1 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 AbstractChartThemeFactory (org.pentaho.chart.AbstractChartThemeFactory)1 IChartLinkGenerator (org.pentaho.chart.IChartLinkGenerator)1 IChartDataModel (org.pentaho.chart.data.IChartDataModel)1 DialPlot (org.pentaho.chart.model.DialPlot)1 Theme (org.pentaho.chart.model.Theme)1 ChartDataOverflowException (org.pentaho.chart.plugin.ChartDataOverflowException)1 NoChartDataException (org.pentaho.chart.plugin.NoChartDataException)1 IOutput (org.pentaho.chart.plugin.api.IOutput)1 JFreeChartOutput (org.pentaho.chart.plugin.jfreechart.outputs.JFreeChartOutput)1 IPentahoRequestContext (org.pentaho.platform.api.engine.IPentahoRequestContext)1