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.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   */
57  public class ViewChartAction extends Action {
58  
59      public ActionForward execute(
60              ActionMapping mapping, ActionForm form, HttpServletRequest request,
61              HttpServletResponse response)
62          throws Exception {
63  
64          try {
65              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
66                  WebKeys.THEME_DISPLAY);
67  
68              long questionId = ParamUtil.getLong(request, "questionId");
69  
70              String chartType = ParamUtil.getString(request, "chartType", "pie");
71  
72              CategoryDataset dataset = PollsUtil.getVotesDataset(questionId);
73  
74              String chartName = LanguageUtil.get(
75                  themeDisplay.getCompanyId(), themeDisplay.getLocale(),
76                  "vote-results");
77              String xName = LanguageUtil.get(
78                  themeDisplay.getCompanyId(), themeDisplay.getLocale(),
79                  "choice");
80              String yName = LanguageUtil.get(
81                  themeDisplay.getCompanyId(), themeDisplay.getLocale(), "votes");
82  
83              JFreeChart chart = null;
84  
85              if (chartType.equals("area")) {
86                  chart = ChartFactory.createAreaChart(
87                      chartName, xName, yName, dataset,
88                      PlotOrientation.VERTICAL, true, false, false);
89              }
90              else if (chartType.equals("horizontal_bar")) {
91                  chart = ChartFactory.createBarChart(
92                      chartName, xName, yName, dataset,
93                      PlotOrientation.HORIZONTAL, true, false, false);
94              }
95              else if (chartType.equals("line")) {
96                  chart = ChartFactory.createLineChart(
97                      chartName, xName, yName, dataset,
98                      PlotOrientation.VERTICAL, true, false, false);
99              }
100             else if (chartType.equals("vertical_bar")) {
101                 chart = ChartFactory.createBarChart(
102                     chartName, xName, yName, dataset,
103                     PlotOrientation.VERTICAL, true, false, false);
104             }
105             else {
106                 PieDataset pieData =
107                     DatasetUtilities.createPieDatasetForRow(dataset, 0);
108 
109                 chart = ChartFactory.createPieChart(
110                     chartName, pieData, true, false, false);
111             }
112 
113             response.setContentType(ContentTypes.IMAGE_JPEG);
114 
115             OutputStream os = response.getOutputStream();
116 
117             ChartUtilities.writeChartAsJPEG(os, chart, 400, 400);
118 
119             return null;
120         }
121         catch (Exception e) {
122             PortalUtil.sendError(e, request, response);
123 
124             return null;
125         }
126     }
127 
128 }