TimeSeriesCollectionChartComponent.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.pentaho.ui.component.charting |
![]() |
![]() |
Pentaho |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* Copyright 2006 Pentaho Corporation. All rights reserved.
* This software was developed by Pentaho Corporation and is provided under the terms
* of the Mozilla Public License, Version 1.1, or any later version. You may not use
* this file except in compliance with the license. If you need a copy of the license,
* please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
* BI Platform. The Initial Developer is Pentaho Corporation.
*
* Software distributed under the Mozilla Public License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
* the license for the specific language governing your rights and limitations.
*/
/*
* Copyright 2006 Pentaho Corporation. All rights reserved.
* This software was developed by Pentaho Corporation and is provided under the terms
* of the Mozilla Public License, Version 1.1, or any later version. You may not use
* this file except in compliance with the license. If you need a copy of the license,
* please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
* BI Platform. The Initial Developer is Pentaho Corporation.
*
* Software distributed under the Mozilla Public License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
* the license for the specific language governing your rights and limitations.
*
* Created Jun 26, 2006
* @author Ron Troyer
*/
package org.pentaho.ui.component.charting;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.entity.XYItemEntity;
import org.jfree.chart.imagemap.ImageMapUtilities;
import org.jfree.data.general.Dataset;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.TimeSeriesCollection;
import org.pentaho.commons.connection.IPentahoResultSet;
import org.pentaho.core.solution.ActionResource;
import org.pentaho.core.solution.IActionResource;
import org.pentaho.core.system.PentahoSystem;
import org.pentaho.core.ui.IPentahoUrlFactory;
import org.pentaho.core.util.TemplateUtil;
import org.pentaho.messages.Messages;
import org.pentaho.messages.util.LocaleHelper;
import org.pentaho.plugin.jfreechart.JFreeChartEngine;
import org.pentaho.plugin.jfreechart.TimeSeriesCollectionChartDefinition;
public class TimeSeriesCollectionChartComponent extends AbstractJFreeChartComponent {
private static final long serialVersionUID = -6268840271596447555L;
protected String seriesName = null;
public TimeSeriesCollectionChartComponent(int chartType, String definitionPath, int width, int height,
IPentahoUrlFactory urlFactory, List messages) {
super(chartType, definitionPath, width, height, urlFactory, messages);
}
public TimeSeriesCollectionChartComponent(String definitionPath, IPentahoUrlFactory urlFactory, ArrayList messages) {
super(definitionPath, urlFactory, messages);
}
public TimeSeriesCollectionChartComponent(IPentahoUrlFactory urlFactory, List messages) {
super(urlFactory, messages);
}
public void setSeriesName(String seriesName) {
this.seriesName = seriesName;
}
public Dataset createChart(Document doc) {
if (solution != null) { // if we have a solution then get the values
values = getActionData();
} else {
// TODO support other methods of getting data
}
if (values == null) {
// we could not get any data
return null;
}
// get the chart node from the document
Node chartAttributes = doc.selectSingleNode("//" + CHART_NODE_NAME); //$NON-NLS-1$
// create the definition
TimeSeriesCollectionChartDefinition chartDefinition = new TimeSeriesCollectionChartDefinition(
(IPentahoResultSet) values, byRow, chartAttributes, getSession());
// set the misc values from chartDefinition
setChartType(chartDefinition.getChartType());
setTitle(chartDefinition.getTitle());
// get the URL template
Node urlTemplateNode = chartAttributes.selectSingleNode(URLTEMPLE_NODE_NAME);
if (urlTemplateNode != null) {
setUrlTemplate(urlTemplateNode.getText());
}
// get the additional parameter
Node paramName2Node = chartAttributes.selectSingleNode(PARAM2_NODE_NAME);
if (paramName2Node != null) {
seriesName = paramName2Node.getText();
}
if (chartDefinition.getWidth() != -1 && (width == -1)) {
setWidth(chartDefinition.getWidth());
}
if (chartDefinition.getHeight() != -1 && (height == -1)) {
setHeight(chartDefinition.getHeight());
}
return chartDefinition;
}
public Document getXmlContent() {
// Create a document that describes the result
Document result = DocumentHelper.createDocument();
String baseUrl = PentahoSystem.getApplicationContext().getBaseUrl();
setXslProperty("baseUrl", baseUrl); //$NON-NLS-1$
// load the XML document that defines the pie
ActionResource resource = new ActionResource(title, IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", //$NON-NLS-1$
definitionPath);
Document chartDefinition = null;
String mapName = "chart" + chartCount++; //$NON-NLS-1$
try {
chartDefinition = getResourceAsDocument(getSession(), resource);
} catch (IOException e) {
}
if (chartDefinition == null) {
Element errorElement = result.addElement("error"); //$NON-NLS-1$
errorElement
.addElement("title").setText(Messages.getString("ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART")); //$NON-NLS-1$ //$NON-NLS-2$
String message = Messages.getString("CHARTS.ERROR_0001_CHART_DEFINIION_MISSING", definitionPath); //$NON-NLS-1$
errorElement.addElement("message").setText(message); //$NON-NLS-1$
error(message);
return result;
}
// create a pie definition from the XML definition
dataDefinition = createChart(chartDefinition);
if (dataDefinition == null) {
Element errorElement = result.addElement("error"); //$NON-NLS-1$
errorElement
.addElement("title").setText(Messages.getString("ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART")); //$NON-NLS-1$ //$NON-NLS-2$
String message = Messages.getString(
"CHARTS.ERROR_0002_CHART_DATA_MISSING", solution + "/" + actionPath + "/" + actionName); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
errorElement.addElement("message").setText(message); //$NON-NLS-1$
// System .out.println( result.asXML() );
return result;
}
// create an image for the dial using the JFreeChart engine
PrintWriter printWriter = new PrintWriter(new StringWriter());
// we'll dispay the title in HTML so that the dial image does not have
// to
// accommodate it
String chartTitle = ""; //$NON-NLS-1$
try {
if (width == -1) {
width = Integer.parseInt(chartDefinition.selectSingleNode("/chart/width").getText()); //$NON-NLS-1$
}
if (height == -1) {
height = Integer.parseInt(chartDefinition.selectSingleNode("/chart/height").getText()); //$NON-NLS-1$
}
} catch (Exception e) {
// go with the default
}
if (chartDefinition.selectSingleNode("/chart/" + URLTEMPLE_NODE_NAME) != null) { //$NON-NLS-1$
urlTemplate = chartDefinition.selectSingleNode("/chart/" + URLTEMPLE_NODE_NAME).getText(); //$NON-NLS-1$
}
if (chartDefinition.selectSingleNode("/chart/paramName") != null) { //$NON-NLS-1$
paramName = chartDefinition.selectSingleNode("/chart/paramName").getText(); //$NON-NLS-1$
}
Element root = result.addElement("charts"); //$NON-NLS-1$
TimeSeriesCollection chartDataDefinition = (TimeSeriesCollection) dataDefinition;
if (chartDataDefinition.getSeriesCount() > 0) {
// create temporary file names
String[] tempFileInfo = createTempFile();
String fileName = tempFileInfo[AbstractChartComponent.FILENAME_INDEX];
String filePathWithoutExtension = tempFileInfo[AbstractChartComponent.FILENAME_WITHOUT_EXTENSION_INDEX];
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
JFreeChartEngine.saveChart(chartDataDefinition, chartTitle,
"", filePathWithoutExtension, width, height, JFreeChartEngine.OUTPUT_PNG, printWriter, info, this); //$NON-NLS-1$
applyOuterURLTemplateParam();
populateInfo(info);
Element chartElement = root.addElement("chart"); //$NON-NLS-1$
chartElement.addElement("mapName").setText(mapName); //$NON-NLS-1$
chartElement.addElement("width").setText(Integer.toString(width)); //$NON-NLS-1$
chartElement.addElement("height").setText(Integer.toString(height)); //$NON-NLS-1$
for (int row = 0; row < chartDataDefinition.getSeriesCount(); row++) {
for (int column = 0; column < chartDataDefinition.getItemCount(row); column++) {
Number value = chartDataDefinition.getY(row, column);
Comparable rowKey = chartDataDefinition.getSeriesKey(row);
RegularTimePeriod columnKey = chartDataDefinition.getSeries(row).getTimePeriod(column);
Element valueElement = chartElement.addElement("value2D"); //$NON-NLS-1$
valueElement.addElement("value").setText(value.toString()); //$NON-NLS-1$
valueElement.addElement("row-key").setText(rowKey.toString()); //$NON-NLS-1$
valueElement.addElement("column-key").setText(columnKey.toString()); //$NON-NLS-1$
}
}
String mapString = ImageMapUtilities.getImageMap(mapName, info);
chartElement.addElement("imageMap").setText(mapString); //$NON-NLS-1$
chartElement.addElement("image").setText(fileName); //$NON-NLS-1$
}
return result;
}
private void populateInfo(ChartRenderingInfo info) {
if (urlTemplate == null) {
return;
}
Iterator iter = info.getEntityCollection().iterator();
while (iter.hasNext()) {
ChartEntity entity = (ChartEntity) iter.next();
if (entity instanceof XYItemEntity) {
if (urlTemplate != null) {
XYItemEntity xyItemEntity = (XYItemEntity) entity;
if (paramName == null) {
xyItemEntity.setURLText(urlTemplate);
} else {
try {
int seriesIndex = xyItemEntity.getSeriesIndex();
int itemIndex = xyItemEntity.getItem();
String xySeriesKey = (String) ((TimeSeriesCollection) xyItemEntity.getDataset())
.getSeriesKey(seriesIndex);
String encodedVal = URLEncoder.encode(xySeriesKey, LocaleHelper.getSystemEncoding());
String drillURL = TemplateUtil.applyTemplate(urlTemplate, paramName, encodedVal);
String itemValueStr = ((TimeSeriesCollection) xyItemEntity.getDataset()).getX(seriesIndex, itemIndex)
.toString();
encodedVal = URLEncoder.encode(itemValueStr, LocaleHelper.getSystemEncoding());
if (seriesName == null) {
drillURL = TemplateUtil.applyTemplate(drillURL, "SERIES", encodedVal); //$NON-NLS-1$
} else {
drillURL = TemplateUtil.applyTemplate(drillURL, seriesName, encodedVal);
}
xyItemEntity.setURLText(drillURL);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
public boolean validate() {
// TODO Auto-generated method stub
return true;
}
}
The table below shows all metrics for TimeSeriesCollectionChartComponent.java.




