Search in sources :

Example 1 with DialPlot

use of org.pentaho.chart.model.DialPlot in project pentaho-platform by pentaho.

the class ChartAction method execute.

/**
 * Called to process the chart definition and data set to produce a usable chart.
 *
 * @throws ChartBootException
 * @throws ChartProcessingException
 * @throws ResourceException
 * @throws InvalidChartDefinition
 * @throws IOException
 * @throws PersistenceException
 * @see org.pentaho.platform.api.action.IAction#execute()
 */
public void execute() throws Exception {
    // 
    // Runtime value validation is now part of the execute operation
    // 
    validate();
    if (bootException != null) {
        throw new ChartBootException(bootException);
    }
    // Transform IPentahoResultSet to an object array
    Object[][] data = processChartData(chartData, valueColumn);
    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")));
                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;
    try {
        IChartLinkGenerator chartLinkGenerator = contentLinkingTemplate == null ? null : new ChartLinkGenerator(contentLinkingTemplate);
        is = ChartBeanFactory.createChart(data, scalingFactor, convertNullsToZero, valueColumn, seriesColumn, categoryColumn, chartModel, chartLinkGenerator, chartWidth, chartHeight, getOutputType());
        // Wrap output as necessary
        if (OpenFlashChartPlugin.PLUGIN_ID.equals(chartEngine)) {
            // Convert stream to string, insert into HTML fragment and re-stream it
            StringBuilder sb = new StringBuilder();
            int c = 0;
            // Build string
            while ((c = is.read()) >= 0) {
                sb.append((char) c);
            }
            IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
            String contextPath = requestContext.getContextPath();
            String flashContent = ChartBeansGeneratorUtil.mergeOpenFlashChartHtmlTemplate(// $NON-NLS-1$ //$NON-NLS-2$
            sb.toString().replaceAll("\"", "\\\\\""), // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
            contextPath + "/" + this.getSwfPath() + "/" + getSwfName());
            // $NON-NLS-1$
            is = new ByteArrayInputStream(flashContent.getBytes("utf-8"));
        }
        int val = 0;
        // TODO: Buffer for more efficiency
        while ((val = is.read()) != -1) {
            chartContentStream.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.", 5, 5);
            String outputType = // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            getMimeType(null).equals("image/jpg") ? "jpeg" : "png";
            ImageIO.write(image, outputType, chartContentStream);
        } else {
            String flashContent = ChartBeansGeneratorUtil.buildEmptyOpenFlashChartHtmlFragment(// $NON-NLS-1$
            "The chart data query returned no data.");
            // $NON-NLS-1$
            is = new ByteArrayInputStream(flashContent.getBytes("utf-8"));
            int val = 0;
            // TODO: Buffer for more efficiency
            while ((val = is.read()) != -1) {
                chartContentStream.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);
            graphics.drawString(Messages.getInstance().getErrorString("ChartAction.TOO_MANY_DATA_POINTS"), 5, // $NON-NLS-1$
            5);
            graphics.drawString(Messages.getInstance().getErrorString("ChartAction.MAX_ALLOWED_DATA_POINTS", Integer.toString(ex.getMaxAllowedDataPoints())), 5, // $NON-NLS-1$
            25);
            String outputType = // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            getMimeType(null).equals("image/jpg") ? "jpeg" : "png";
            ImageIO.write(image, outputType, chartContentStream);
        } else {
            String flashContent = ChartBeansGeneratorUtil.buildEmptyOpenFlashChartHtmlFragment(Messages.getInstance().getErrorString("ChartAction.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) {
                chartContentStream.write(val);
            }
        }
    }
}
Also used : 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) ArrayList(java.util.ArrayList) IChartLinkGenerator(org.pentaho.chart.IChartLinkGenerator) AbstractChartThemeFactory(org.pentaho.chart.AbstractChartThemeFactory) DialPlot(org.pentaho.chart.model.DialPlot) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D) IPentahoRequestContext(org.pentaho.platform.api.engine.IPentahoRequestContext) ByteArrayInputStream(java.io.ByteArrayInputStream) Theme(org.pentaho.chart.model.Theme) File(java.io.File)

Example 2 with DialPlot

use of org.pentaho.chart.model.DialPlot 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)2 Graphics2D (java.awt.Graphics2D)2 BufferedImage (java.awt.image.BufferedImage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 AbstractChartThemeFactory (org.pentaho.chart.AbstractChartThemeFactory)2 IChartLinkGenerator (org.pentaho.chart.IChartLinkGenerator)2 DialPlot (org.pentaho.chart.model.DialPlot)2 Theme (org.pentaho.chart.model.Theme)2 ChartDataOverflowException (org.pentaho.chart.plugin.ChartDataOverflowException)2 NoChartDataException (org.pentaho.chart.plugin.NoChartDataException)2 IPentahoRequestContext (org.pentaho.platform.api.engine.IPentahoRequestContext)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 IChartDataModel (org.pentaho.chart.data.IChartDataModel)1 IOutput (org.pentaho.chart.plugin.api.IOutput)1 JFreeChartOutput (org.pentaho.chart.plugin.jfreechart.outputs.JFreeChartOutput)1