1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.language.LanguageUtil;
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   */
56  public class ViewChartAction extends Action {
57  
58      public ActionForward execute(
59              ActionMapping mapping, ActionForm form, HttpServletRequest request,
60              HttpServletResponse response)
61          throws Exception {
62  
63          try {
64              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
65                  WebKeys.THEME_DISPLAY);
66  
67              long questionId = ParamUtil.getLong(request, "questionId");
68  
69              String chartType = ParamUtil.getString(request, "chartType", "pie");
70  
71              CategoryDataset dataset = PollsUtil.getVotesDataset(questionId);
72  
73              String chartName = LanguageUtil.get(
74                  themeDisplay.getCompanyId(), themeDisplay.getLocale(),
75                  "vote-results");
76              String xName = LanguageUtil.get(
77                  themeDisplay.getCompanyId(), themeDisplay.getLocale(),
78                  "choice");
79              String yName = LanguageUtil.get(
80                  themeDisplay.getCompanyId(), themeDisplay.getLocale(), "votes");
81  
82              JFreeChart chart = null;
83  
84              if (chartType.equals("area")) {
85                  chart = ChartFactory.createAreaChart(
86                      chartName, xName, yName, dataset,
87                      PlotOrientation.VERTICAL, true, false, false);
88              }
89              else if (chartType.equals("horizontal_bar")) {
90                  chart = ChartFactory.createBarChart(
91                      chartName, xName, yName, dataset,
92                      PlotOrientation.HORIZONTAL, true, false, false);
93              }
94              else if (chartType.equals("line")) {
95                  chart = ChartFactory.createLineChart(
96                      chartName, xName, yName, dataset,
97                      PlotOrientation.VERTICAL, true, false, false);
98              }
99              else if (chartType.equals("vertical_bar")) {
100                 chart = ChartFactory.createBarChart(
101                     chartName, xName, yName, dataset,
102                     PlotOrientation.VERTICAL, true, false, false);
103             }
104             else {
105                 PieDataset pieData =
106                     DatasetUtilities.createPieDatasetForRow(dataset, 0);
107 
108                 chart = ChartFactory.createPieChart(
109                     chartName, pieData, true, false, false);
110             }
111 
112             response.setContentType("image/jpeg");
113 
114             OutputStream os = response.getOutputStream();
115 
116             ChartUtilities.writeChartAsJPEG(os, chart, 400, 400);
117 
118             return null;
119         }
120         catch (Exception e) {
121             PortalUtil.sendError(e, request, response);
122 
123             return null;
124         }
125     }
126 
127 }