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.language.LanguageUtil;
26  import com.liferay.portal.kernel.util.ContentTypes;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.WebKeys;
31  import com.liferay.portlet.polls.util.PollsUtil;
32  
33  import java.io.OutputStream;
34  
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  
38  import org.apache.struts.action.Action;
39  import org.apache.struts.action.ActionForm;
40  import org.apache.struts.action.ActionForward;
41  import org.apache.struts.action.ActionMapping;
42  
43  import org.jfree.chart.ChartFactory;
44  import org.jfree.chart.ChartUtilities;
45  import org.jfree.chart.JFreeChart;
46  import org.jfree.chart.plot.PlotOrientation;
47  import org.jfree.data.category.CategoryDataset;
48  import org.jfree.data.general.DatasetUtilities;
49  import org.jfree.data.general.PieDataset;
50  
51  /**
52   * <a href="ViewChartAction.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
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(ContentTypes.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 }