KickJava   Java API By Example, From Geeks To Geeks.

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


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

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