001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.polls.action;
016    
017    import com.liferay.portal.kernel.util.ContentTypes;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.theme.ThemeDisplay;
020    import com.liferay.portal.util.PortalUtil;
021    import com.liferay.portal.util.WebKeys;
022    import com.liferay.portlet.polls.util.PollsUtil;
023    
024    import java.io.OutputStream;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletResponse;
028    
029    import org.apache.struts.action.Action;
030    import org.apache.struts.action.ActionForm;
031    import org.apache.struts.action.ActionForward;
032    import org.apache.struts.action.ActionMapping;
033    
034    import org.jfree.chart.ChartFactory;
035    import org.jfree.chart.ChartUtilities;
036    import org.jfree.chart.JFreeChart;
037    import org.jfree.chart.plot.PlotOrientation;
038    import org.jfree.data.category.CategoryDataset;
039    import org.jfree.data.general.DatasetUtilities;
040    import org.jfree.data.general.PieDataset;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     */
045    public class ViewChartAction extends Action {
046    
047            public ActionForward execute(
048                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
049                            HttpServletResponse response)
050                    throws Exception {
051    
052                    try {
053                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
054                                    WebKeys.THEME_DISPLAY);
055    
056                            long questionId = ParamUtil.getLong(request, "questionId");
057    
058                            String chartType = ParamUtil.getString(request, "chartType", "pie");
059    
060                            CategoryDataset dataset = PollsUtil.getVotesDataset(questionId);
061    
062                            String chartName = themeDisplay.translate("vote-results");
063                            String xName = themeDisplay.translate("choice");
064                            String yName = themeDisplay.translate("votes");
065    
066                            JFreeChart chart = null;
067    
068                            if (chartType.equals("area")) {
069                                    chart = ChartFactory.createAreaChart(
070                                            chartName, xName, yName, dataset,
071                                            PlotOrientation.VERTICAL, true, false, false);
072                            }
073                            else if (chartType.equals("horizontal_bar")) {
074                                    chart = ChartFactory.createBarChart(
075                                            chartName, xName, yName, dataset,
076                                            PlotOrientation.HORIZONTAL, true, false, false);
077                            }
078                            else if (chartType.equals("line")) {
079                                    chart = ChartFactory.createLineChart(
080                                            chartName, xName, yName, dataset,
081                                            PlotOrientation.VERTICAL, true, false, false);
082                            }
083                            else if (chartType.equals("vertical_bar")) {
084                                    chart = ChartFactory.createBarChart(
085                                            chartName, xName, yName, dataset,
086                                            PlotOrientation.VERTICAL, true, false, false);
087                            }
088                            else {
089                                    PieDataset pieData =
090                                            DatasetUtilities.createPieDatasetForRow(dataset, 0);
091    
092                                    chart = ChartFactory.createPieChart(
093                                            chartName, pieData, true, false, false);
094                            }
095    
096                            response.setContentType(ContentTypes.IMAGE_JPEG);
097    
098                            OutputStream os = response.getOutputStream();
099    
100                            ChartUtilities.writeChartAsJPEG(os, chart, 400, 400);
101    
102                            return null;
103                    }
104                    catch (Exception e) {
105                            PortalUtil.sendError(e, request, response);
106    
107                            return null;
108                    }
109            }
110    
111    }