KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > ui > component > charting > TimeSeriesCollectionChartComponent


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12 */

13 /*
14  * Copyright 2006 Pentaho Corporation. All rights reserved.
15  * This software was developed by Pentaho Corporation and is provided under the terms
16  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
17  * this file except in compliance with the license. If you need a copy of the license,
18  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
19  * BI Platform. The Initial Developer is Pentaho Corporation.
20  *
21  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
22  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
23  * the license for the specific language governing your rights and limitations.
24  *
25  * Created Jun 26, 2006
26  * @author Ron Troyer
27  */

28
29 package org.pentaho.ui.component.charting;
30
31 import java.io.IOException JavaDoc;
32 import java.io.PrintWriter JavaDoc;
33 import java.io.StringWriter JavaDoc;
34 import java.io.UnsupportedEncodingException JavaDoc;
35 import java.net.URLEncoder JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39
40 import org.dom4j.Document;
41 import org.dom4j.DocumentHelper;
42 import org.dom4j.Element;
43 import org.dom4j.Node;
44 import org.jfree.chart.ChartRenderingInfo;
45 import org.jfree.chart.entity.ChartEntity;
46 import org.jfree.chart.entity.StandardEntityCollection;
47 import org.jfree.chart.entity.XYItemEntity;
48 import org.jfree.chart.imagemap.ImageMapUtilities;
49 import org.jfree.data.general.Dataset;
50 import org.jfree.data.time.RegularTimePeriod;
51 import org.jfree.data.time.TimeSeriesCollection;
52 import org.pentaho.core.connection.IPentahoResultSet;
53 import org.pentaho.core.solution.ActionResource;
54 import org.pentaho.core.solution.IActionResource;
55 import org.pentaho.core.system.PentahoSystem;
56 import org.pentaho.core.ui.IPentahoUrlFactory;
57 import org.pentaho.core.util.TemplateUtil;
58 import org.pentaho.core.util.XmlHelper;
59 import org.pentaho.messages.Messages;
60 import org.pentaho.messages.util.LocaleHelper;
61 import org.pentaho.plugin.jfreechart.JFreeChartEngine;
62 import org.pentaho.plugin.jfreechart.TimeSeriesCollectionChartDefinition;
63
64 public class TimeSeriesCollectionChartComponent extends AbstractJFreeChartComponent {
65     private static final long serialVersionUID = -6268840271596447555L;
66
67     protected String JavaDoc seriesName = null;
68
69     public TimeSeriesCollectionChartComponent(int chartType, String JavaDoc definitionPath, int width, int height, IPentahoUrlFactory urlFactory, List JavaDoc messages) {
70         super(chartType, definitionPath, width, height, urlFactory, messages);
71         // Set the XSL file to be used to generate the HTML
72
setXsl("text/html", "Chart.xsl"); //$NON-NLS-1$ //$NON-NLS-2$
73
}
74
75     public TimeSeriesCollectionChartComponent(String JavaDoc definitionPath, IPentahoUrlFactory urlFactory, ArrayList JavaDoc messages) {
76         super(definitionPath, urlFactory, messages);
77         // Set the XSL file to be used to generate the HTML
78
setXsl("text/html", "Chart.xsl"); //$NON-NLS-1$ //$NON-NLS-2$
79
}
80
81     public TimeSeriesCollectionChartComponent(IPentahoUrlFactory urlFactory, List JavaDoc messages) {
82         super(urlFactory, messages);
83         // Set the XSL file to be used to generate the HTML
84
setXsl("text/html", "Chart.xsl"); //$NON-NLS-1$ //$NON-NLS-2$
85
}
86
87     public void setSeriesName(String JavaDoc seriesName) {
88         this.seriesName = seriesName;
89     }
90
91     public Dataset createChart(Document doc) {
92         if (solution != null) { // if we have a solution then get the values
93
values = getActionData();
94         } else {
95             // TODO support other methods of getting data
96
}
97
98         if (values == null) {
99             // we could not get any data
100
return null;
101         }
102         // get the chart node from the document
103
Node chartAttributes = doc.selectSingleNode("//" + CHART_NODE_NAME); //$NON-NLS-1$
104
// create the definition
105
TimeSeriesCollectionChartDefinition chartDefinition = new TimeSeriesCollectionChartDefinition((IPentahoResultSet) values, byRow, chartAttributes, getSession());
106
107         // set the misc values from chartDefinition
108
setChartType(chartDefinition.getChartType());
109         setTitle(chartDefinition.getTitle());
110
111         // get the URL template
112
Node urlTemplateNode = chartAttributes.selectSingleNode(URLTEMPLE_NODE_NAME);
113         if (urlTemplateNode != null) {
114             setUrlTemplate(urlTemplateNode.getText());
115         }
116
117         // get the additional parameter
118
Node paramName2Node = chartAttributes.selectSingleNode(PARAM2_NODE_NAME);
119         if (paramName2Node != null) {
120             seriesName = paramName2Node.getText();
121         }
122
123         if (chartDefinition.getWidth() != -1 && (width == -1)) {
124             setWidth(chartDefinition.getWidth());
125         }
126         if (chartDefinition.getHeight() != -1 && (height == -1)) {
127             setHeight(chartDefinition.getHeight());
128         }
129
130         return chartDefinition;
131     }
132
133     public boolean setDataAction(String JavaDoc chartDefinition) {
134         ActionResource resource = new ActionResource("", IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", //$NON-NLS-1$ //$NON-NLS-2$
135
chartDefinition);
136         try {
137             Document dataActionDocument = PentahoSystem.getSolutionRepository(getSession()).getResourceAsDocument(resource);
138             if (dataActionDocument == null) {
139                 return false;
140             }
141             Node dataNode = dataActionDocument.selectSingleNode("chart/data"); //$NON-NLS-1$
142
chartType = (int) XmlHelper.getNodeText("chart-type", dataNode, -1); //$NON-NLS-1$
143
solution = XmlHelper.getNodeText("data-solution", dataNode); //$NON-NLS-1$
144
actionPath = XmlHelper.getNodeText("data-path", dataNode); //$NON-NLS-1$
145
actionName = XmlHelper.getNodeText("data-action", dataNode); //$NON-NLS-1$
146
actionOutput = XmlHelper.getNodeText("data-output", dataNode); //$NON-NLS-1$
147
byRow = XmlHelper.getNodeText("data-orientation", dataNode, "rows").equals("rows"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
148
if (width == 0) {
149                 width = (int) XmlHelper.getNodeText("chart/width", dataActionDocument, 125); //$NON-NLS-1$
150
}
151             if (height == 0) {
152                 height = (int) XmlHelper.getNodeText("chart/height", dataActionDocument, 125); //$NON-NLS-1$
153
}
154         } catch (Exception JavaDoc e) {
155             error(Messages.getString("TimeSeriesCollectionChartComponent.ERROR_0001_INVALID_CHART_DEFINITION", chartDefinition), e); //$NON-NLS-1$
156
return false;
157         }
158         return true;
159     }
160
161     public Document getXmlContent() {
162
163         // Create a document that describes the result
164
Document result = DocumentHelper.createDocument();
165         String JavaDoc baseUrl = PentahoSystem.getApplicationContext().getBaseUrl();
166         setXslProperty("baseUrl", baseUrl); //$NON-NLS-1$
167

168         // load the XML document that defines the pie
169
ActionResource resource = new ActionResource(title, IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", //$NON-NLS-1$
170
definitionPath);
171         Document chartDefinition = null;
172         String JavaDoc mapName = "chart" + chartCount++; //$NON-NLS-1$
173
try {
174             chartDefinition = PentahoSystem.getSolutionRepository(getSession()).getResourceAsDocument(resource);
175         } catch (IOException JavaDoc e) {
176         }
177
178         if (chartDefinition == null) {
179             Element errorElement = result.addElement("error"); //$NON-NLS-1$
180
errorElement.addElement("title").setText(Messages.getString("ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART")); //$NON-NLS-1$ //$NON-NLS-2$
181
String JavaDoc message = Messages.getString("CHARTS.ERROR_0001_CHART_DEFINIION_MISSING", definitionPath); //$NON-NLS-1$
182
errorElement.addElement("message").setText(message); //$NON-NLS-1$
183
error(message);
184             return result;
185         }
186         // create a pie definition from the XML definition
187
dataDefinition = createChart(chartDefinition);
188
189         if (dataDefinition == null) {
190             Element errorElement = result.addElement("error"); //$NON-NLS-1$
191
errorElement.addElement("title").setText(Messages.getString("ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART")); //$NON-NLS-1$ //$NON-NLS-2$
192
String JavaDoc message = Messages.getString("CHARTS.ERROR_0002_CHART_DATA_MISSING", solution + "/" + actionPath + "/" + actionName); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
193
errorElement.addElement("message").setText(message); //$NON-NLS-1$
194
// System .out.println( result.asXML() );
195
return result;
196         }
197
198         // create an image for the dial using the JFreeChart engine
199
PrintWriter JavaDoc printWriter = new PrintWriter JavaDoc(new StringWriter JavaDoc());
200         // we'll dispay the title in HTML so that the dial image does not have
201
// to
202
// accommodate it
203
String JavaDoc chartTitle = ""; //$NON-NLS-1$
204
try {
205             if (width == -1) {
206                 width = Integer.parseInt(chartDefinition.selectSingleNode("/chart/width").getText()); //$NON-NLS-1$
207
}
208             if (height == -1) {
209                 height = Integer.parseInt(chartDefinition.selectSingleNode("/chart/height").getText()); //$NON-NLS-1$
210
}
211         } catch (Exception JavaDoc e) {
212             // go with the default
213
}
214         if (chartDefinition.selectSingleNode("/chart/" + URLTEMPLE_NODE_NAME) != null) { //$NON-NLS-1$
215
urlTemplate = chartDefinition.selectSingleNode("/chart/" + URLTEMPLE_NODE_NAME).getText(); //$NON-NLS-1$
216
}
217
218         if (chartDefinition.selectSingleNode("/chart/paramName") != null) { //$NON-NLS-1$
219
paramName = chartDefinition.selectSingleNode("/chart/paramName").getText(); //$NON-NLS-1$
220
}
221
222         Element root = result.addElement("charts"); //$NON-NLS-1$
223
TimeSeriesCollection chartDataDefinition = (TimeSeriesCollection) dataDefinition;
224         if (chartDataDefinition.getSeriesCount() > 0) {
225             // create temporary file names
226
String JavaDoc[] tempFileInfo = createTempFile();
227             String JavaDoc fileName = tempFileInfo[AbstractChartComponent.FILENAME_INDEX];
228             String JavaDoc filePathWithoutExtension = tempFileInfo[AbstractChartComponent.FILENAME_WITHOUT_EXTENSION_INDEX];
229
230             ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
231             JFreeChartEngine.saveChart(chartDataDefinition, chartTitle,
232                             "", filePathWithoutExtension, width, height, JFreeChartEngine.OUTPUT_PNG, printWriter, info, this); //$NON-NLS-1$
233
applyOuterURLTemplateParam();
234             popultateInfo(info);
235             Element chartElement = root.addElement("chart"); //$NON-NLS-1$
236
chartElement.addElement("mapName").setText(mapName); //$NON-NLS-1$
237
chartElement.addElement("width").setText(Integer.toString(width)); //$NON-NLS-1$
238
chartElement.addElement("height").setText(Integer.toString(height)); //$NON-NLS-1$
239
for (int row = 0; row < chartDataDefinition.getSeriesCount(); row++) {
240                 for (int column = 0; column < chartDataDefinition.getItemCount(row); column++) {
241                     Number JavaDoc value = chartDataDefinition.getY(row, column);
242                     Comparable JavaDoc rowKey = chartDataDefinition.getSeriesKey(row);
243                     RegularTimePeriod columnKey = chartDataDefinition.getSeries(row).getTimePeriod(column);
244                     Element valueElement = chartElement.addElement("value2D"); //$NON-NLS-1$
245
valueElement.addElement("value").setText(value.toString()); //$NON-NLS-1$
246
valueElement.addElement("row-key").setText(rowKey.toString()); //$NON-NLS-1$
247
valueElement.addElement("column-key").setText(columnKey.toString()); //$NON-NLS-1$
248
}
249             }
250             String JavaDoc mapString = ImageMapUtilities.getImageMap(mapName, info);
251             chartElement.addElement("imageMap").setText(mapString); //$NON-NLS-1$
252
chartElement.addElement("image").setText(fileName); //$NON-NLS-1$
253
}
254         return result;
255     }
256
257     private void popultateInfo(ChartRenderingInfo info) {
258         if (urlTemplate == null) {
259             return;
260         }
261         Iterator JavaDoc iter = info.getEntityCollection().iterator();
262         while (iter.hasNext()) {
263             ChartEntity entity = (ChartEntity) iter.next();
264             if (entity instanceof XYItemEntity) {
265                 if (urlTemplate != null) {
266                     XYItemEntity xyItemEntity = (XYItemEntity) entity;
267                     if (paramName == null) {
268                         xyItemEntity.setURLText(urlTemplate);
269                     } else {
270                         try {
271                             int seriesIndex = xyItemEntity.getSeriesIndex();
272                             int itemIndex = xyItemEntity.getItem();
273                             String JavaDoc xySeriesKey = (String JavaDoc) ((TimeSeriesCollection) xyItemEntity.getDataset()).getSeriesKey(seriesIndex);
274                             String JavaDoc encodedVal = URLEncoder.encode(xySeriesKey, LocaleHelper.getSystemEncoding());
275                             String JavaDoc drillURL = TemplateUtil.applyTemplate(urlTemplate, paramName, encodedVal);
276                             String JavaDoc itemValueStr = ((TimeSeriesCollection) xyItemEntity.getDataset()).getX(seriesIndex, itemIndex).toString();
277                             encodedVal = URLEncoder.encode(itemValueStr, LocaleHelper.getSystemEncoding());
278                             if (seriesName == null) {
279                                 drillURL = TemplateUtil.applyTemplate(drillURL, "SERIES", encodedVal); //$NON-NLS-1$
280
} else {
281                                 drillURL = TemplateUtil.applyTemplate(drillURL, seriesName, encodedVal);
282                             }
283                             xyItemEntity.setURLText(drillURL);
284                         } catch (UnsupportedEncodingException JavaDoc e) {
285                             // TODO Auto-generated catch block
286
e.printStackTrace();
287                         }
288                     }
289                 }
290             }
291         }
292     }
293
294     public boolean validate() {
295         // TODO Auto-generated method stub
296
return true;
297     }
298
299 }
300
Popular Tags