001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.polls.action;
022:
023: import com.liferay.portal.kernel.language.LanguageUtil;
024: import com.liferay.portal.kernel.util.ParamUtil;
025: import com.liferay.portal.struts.ActionConstants;
026: import com.liferay.portal.theme.ThemeDisplay;
027: import com.liferay.portal.util.WebKeys;
028: import com.liferay.portlet.polls.util.PollsUtil;
029:
030: import java.io.OutputStream;
031:
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034: import javax.servlet.jsp.PageContext;
035:
036: import org.apache.struts.action.Action;
037: import org.apache.struts.action.ActionForm;
038: import org.apache.struts.action.ActionForward;
039: import org.apache.struts.action.ActionMapping;
040:
041: import org.jfree.chart.ChartFactory;
042: import org.jfree.chart.ChartUtilities;
043: import org.jfree.chart.JFreeChart;
044: import org.jfree.chart.plot.PlotOrientation;
045: import org.jfree.data.category.CategoryDataset;
046: import org.jfree.data.general.DatasetUtilities;
047: import org.jfree.data.general.PieDataset;
048:
049: /**
050: * <a href="ViewChartAction.java.html"><b><i>View Source</i></b></a>
051: *
052: * @author Brian Wing Shun Chan
053: *
054: */
055: public class ViewChartAction extends Action {
056:
057: public ActionForward execute(ActionMapping mapping,
058: ActionForm form, HttpServletRequest req,
059: HttpServletResponse res) throws Exception {
060:
061: try {
062: ThemeDisplay themeDisplay = (ThemeDisplay) req
063: .getAttribute(WebKeys.THEME_DISPLAY);
064:
065: long questionId = ParamUtil.getLong(req, "questionId");
066:
067: String chartType = ParamUtil.getString(req, "chartType",
068: "pie");
069:
070: CategoryDataset dataset = PollsUtil
071: .getVotesDataset(questionId);
072:
073: String chartName = LanguageUtil.get(themeDisplay
074: .getCompanyId(), themeDisplay.getLocale(),
075: "vote-results");
076: String xName = LanguageUtil.get(
077: themeDisplay.getCompanyId(), themeDisplay
078: .getLocale(), "choice");
079: String yName = LanguageUtil.get(
080: themeDisplay.getCompanyId(), themeDisplay
081: .getLocale(), "votes");
082:
083: JFreeChart chart = null;
084:
085: if (chartType.equals("area")) {
086: chart = ChartFactory.createAreaChart(chartName, xName,
087: yName, dataset, PlotOrientation.VERTICAL, true,
088: false, false);
089: } else if (chartType.equals("horizontal_bar")) {
090: chart = ChartFactory.createBarChart(chartName, xName,
091: yName, dataset, PlotOrientation.HORIZONTAL,
092: true, false, false);
093: } else if (chartType.equals("line")) {
094: chart = ChartFactory.createLineChart(chartName, xName,
095: yName, dataset, PlotOrientation.VERTICAL, true,
096: false, false);
097: } else if (chartType.equals("vertical_bar")) {
098: chart = ChartFactory.createBarChart(chartName, xName,
099: yName, dataset, PlotOrientation.VERTICAL, true,
100: false, false);
101: } else {
102: PieDataset pieData = DatasetUtilities
103: .createPieDatasetForRow(dataset, 0);
104:
105: chart = ChartFactory.createPieChart(chartName, pieData,
106: true, false, false);
107: }
108:
109: res.setContentType("image/jpeg");
110:
111: OutputStream out = res.getOutputStream();
112:
113: ChartUtilities.writeChartAsJPEG(out, chart, 400, 400);
114:
115: return null;
116: } catch (Exception e) {
117: req.setAttribute(PageContext.EXCEPTION, e);
118:
119: return mapping.findForward(ActionConstants.COMMON_ERROR);
120: }
121: }
122:
123: }
|