1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.polls.action;
24  
25  import com.liferay.portal.kernel.util.ContentTypes;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.theme.ThemeDisplay;
28  import com.liferay.portal.util.PortalUtil;
29  import com.liferay.portal.util.WebKeys;
30  import com.liferay.portlet.polls.util.PollsUtil;
31  
32  import java.io.OutputStream;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  
37  import org.apache.struts.action.Action;
38  import org.apache.struts.action.ActionForm;
39  import org.apache.struts.action.ActionForward;
40  import org.apache.struts.action.ActionMapping;
41  
42  import org.jfree.chart.ChartFactory;
43  import org.jfree.chart.ChartUtilities;
44  import org.jfree.chart.JFreeChart;
45  import org.jfree.chart.plot.PlotOrientation;
46  import org.jfree.data.category.CategoryDataset;
47  import org.jfree.data.general.DatasetUtilities;
48  import org.jfree.data.general.PieDataset;
49  
50  /**
51   * <a href="ViewChartAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   */
55  public class ViewChartAction extends Action {
56  
57      public ActionForward execute(
58              ActionMapping mapping, ActionForm form, HttpServletRequest request,
59              HttpServletResponse response)
60          throws Exception {
61  
62          try {
63              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
64                  WebKeys.THEME_DISPLAY);
65  
66              long questionId = ParamUtil.getLong(request, "questionId");
67  
68              String chartType = ParamUtil.getString(request, "chartType", "pie");
69  
70              CategoryDataset dataset = PollsUtil.getVotesDataset(questionId);
71  
72              String chartName = themeDisplay.translate("vote-results");
73              String xName = themeDisplay.translate("choice");
74              String yName = themeDisplay.translate("votes");
75  
76              JFreeChart chart = null;
77  
78              if (chartType.equals("area")) {
79                  chart = ChartFactory.createAreaChart(
80                      chartName, xName, yName, dataset,
81                      PlotOrientation.VERTICAL, true, false, false);
82              }
83              else if (chartType.equals("horizontal_bar")) {
84                  chart = ChartFactory.createBarChart(
85                      chartName, xName, yName, dataset,
86                      PlotOrientation.HORIZONTAL, true, false, false);
87              }
88              else if (chartType.equals("line")) {
89                  chart = ChartFactory.createLineChart(
90                      chartName, xName, yName, dataset,
91                      PlotOrientation.VERTICAL, true, false, false);
92              }
93              else if (chartType.equals("vertical_bar")) {
94                  chart = ChartFactory.createBarChart(
95                      chartName, xName, yName, dataset,
96                      PlotOrientation.VERTICAL, true, false, false);
97              }
98              else {
99                  PieDataset pieData =
100                     DatasetUtilities.createPieDatasetForRow(dataset, 0);
101 
102                 chart = ChartFactory.createPieChart(
103                     chartName, pieData, true, false, false);
104             }
105 
106             response.setContentType(ContentTypes.IMAGE_JPEG);
107 
108             OutputStream os = response.getOutputStream();
109 
110             ChartUtilities.writeChartAsJPEG(os, chart, 400, 400);
111 
112             return null;
113         }
114         catch (Exception e) {
115             PortalUtil.sendError(e, request, response);
116 
117             return null;
118         }
119     }
120 
121 }