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