1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.polls.action;
16  
17  import com.liferay.portal.kernel.util.ContentTypes;
18  import com.liferay.portal.kernel.util.ParamUtil;
19  import com.liferay.portal.theme.ThemeDisplay;
20  import com.liferay.portal.util.PortalUtil;
21  import com.liferay.portal.util.WebKeys;
22  import com.liferay.portlet.polls.util.PollsUtil;
23  
24  import java.io.OutputStream;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.struts.action.Action;
30  import org.apache.struts.action.ActionForm;
31  import org.apache.struts.action.ActionForward;
32  import org.apache.struts.action.ActionMapping;
33  
34  import org.jfree.chart.ChartFactory;
35  import org.jfree.chart.ChartUtilities;
36  import org.jfree.chart.JFreeChart;
37  import org.jfree.chart.plot.PlotOrientation;
38  import org.jfree.data.category.CategoryDataset;
39  import org.jfree.data.general.DatasetUtilities;
40  import org.jfree.data.general.PieDataset;
41  
42  /**
43   * <a href="ViewChartAction.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   */
47  public class ViewChartAction extends Action {
48  
49      public ActionForward execute(
50              ActionMapping mapping, ActionForm form, HttpServletRequest request,
51              HttpServletResponse response)
52          throws Exception {
53  
54          try {
55              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
56                  WebKeys.THEME_DISPLAY);
57  
58              long questionId = ParamUtil.getLong(request, "questionId");
59  
60              String chartType = ParamUtil.getString(request, "chartType", "pie");
61  
62              CategoryDataset dataset = PollsUtil.getVotesDataset(questionId);
63  
64              String chartName = themeDisplay.translate("vote-results");
65              String xName = themeDisplay.translate("choice");
66              String yName = themeDisplay.translate("votes");
67  
68              JFreeChart chart = null;
69  
70              if (chartType.equals("area")) {
71                  chart = ChartFactory.createAreaChart(
72                      chartName, xName, yName, dataset,
73                      PlotOrientation.VERTICAL, true, false, false);
74              }
75              else if (chartType.equals("horizontal_bar")) {
76                  chart = ChartFactory.createBarChart(
77                      chartName, xName, yName, dataset,
78                      PlotOrientation.HORIZONTAL, true, false, false);
79              }
80              else if (chartType.equals("line")) {
81                  chart = ChartFactory.createLineChart(
82                      chartName, xName, yName, dataset,
83                      PlotOrientation.VERTICAL, true, false, false);
84              }
85              else if (chartType.equals("vertical_bar")) {
86                  chart = ChartFactory.createBarChart(
87                      chartName, xName, yName, dataset,
88                      PlotOrientation.VERTICAL, true, false, false);
89              }
90              else {
91                  PieDataset pieData =
92                      DatasetUtilities.createPieDatasetForRow(dataset, 0);
93  
94                  chart = ChartFactory.createPieChart(
95                      chartName, pieData, true, false, false);
96              }
97  
98              response.setContentType(ContentTypes.IMAGE_JPEG);
99  
100             OutputStream os = response.getOutputStream();
101 
102             ChartUtilities.writeChartAsJPEG(os, chart, 400, 400);
103 
104             return null;
105         }
106         catch (Exception e) {
107             PortalUtil.sendError(e, request, response);
108 
109             return null;
110         }
111     }
112 
113 }