use of org.pentaho.chart.model.ChartModel in project pentaho-platform by pentaho.
the class DefaultChartBeansGenerator method createChartAsHtml.
public String createChartAsHtml(IPentahoSession userSession, Map<String, Object> parameterMap, String serializedChartDataDefinition, String serializedChartModel, int chartWidth, int chartHeight) throws IOException {
ChartModel chartModel = ChartSerializer.deSerialize(serializedChartModel, ChartSerializationFormat.JSON);
String html = null;
if (chartModel.getChartEngineId() == null) {
// Load default value from system setting or take hard coded
// Hard coded final fall back is Open Flash Chart
String defaultChartEngine = PentahoSystem.getSystemSetting("chartbeans/chartbeans_config.xml", "default-chart-engine", // $NON-NLS-1$ //$NON-NLS-2$
OpenFlashChartPlugin.PLUGIN_ID);
if (defaultChartEngine == null) {
defaultChartEngine = OpenFlashChartPlugin.PLUGIN_ID;
}
chartModel.setChartEngineId(defaultChartEngine);
}
// Check for render engine override
String override = (String) parameterMap.get("renderEngine");
if (override != null) {
chartModel.setChartEngineId(override);
}
serializedChartModel = ChartSerializer.serialize(chartModel, ChartSerializationFormat.JSON);
if (JFreeChartPlugin.PLUGIN_ID.equals(chartModel.getChartEngineId())) {
// $NON-NLS-1$
final String SOLUTION_TMP_DIR = "system/tmp/";
File chartFileOnServer = new File(new File(PentahoSystem.getApplicationContext().getFileOutputPath(SOLUTION_TMP_DIR)), java.util.UUID.randomUUID().toString());
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(chartFileOnServer));
this.internalCreateChart(userSession, parameterMap, serializedChartDataDefinition, serializedChartModel, chartWidth, chartHeight, null, bos);
} finally {
IOUtils.closeQuietly(bos);
}
IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
String contextPath = requestContext.getContextPath();
// $NON-NLS-1$ //$NON-NLS-2$
String url = contextPath + "/";
// $NON-NLS-1$
final String IMAGE_URL_TEMPLATE = "{0}getImage?image={1}";
final String imageUrl = MessageFormat.format(IMAGE_URL_TEMPLATE, new String[] { url, chartFileOnServer.getName() });
html = this.mergeStaticImageHtmlTemplate(imageUrl);
} else if (OpenFlashChartPlugin.PLUGIN_ID.equals(chartModel.getChartEngineId())) {
ByteArrayOutputStream tmpOut = new ByteArrayOutputStream();
this.internalCreateChart(userSession, parameterMap, serializedChartDataDefinition, serializedChartModel, chartWidth, chartHeight, null, tmpOut);
// $NON-NLS-1$
final String ENCODING = "UTF-8";
ByteArrayInputStream in = new ByteArrayInputStream(tmpOut.toByteArray());
IOUtils.closeQuietly(tmpOut);
html = IOUtils.toString(in, ENCODING);
IOUtils.closeQuietly(in);
} else {
// $NON-NLS-1$
throw new IllegalArgumentException("unrecognized chart engine");
}
return html;
}
Aggregations