001
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
045 public class ViewChartAction extends Action {
046
047 @Override
048 public ActionForward execute(
049 ActionMapping actionMapping, ActionForm actionForm,
050 HttpServletRequest request, HttpServletResponse response)
051 throws Exception {
052
053 try {
054 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
055 WebKeys.THEME_DISPLAY);
056
057 long questionId = ParamUtil.getLong(request, "questionId");
058
059 String chartType = ParamUtil.getString(request, "chartType", "pie");
060
061 CategoryDataset dataset = PollsUtil.getVotesDataset(questionId);
062
063 String chartName = themeDisplay.translate("vote-results");
064 String xName = themeDisplay.translate("choice");
065 String yName = themeDisplay.translate("votes");
066
067 JFreeChart chart = null;
068
069 if (chartType.equals("area")) {
070 chart = ChartFactory.createAreaChart(
071 chartName, xName, yName, dataset, PlotOrientation.VERTICAL,
072 true, false, false);
073 }
074 else if (chartType.equals("horizontal_bar")) {
075 chart = ChartFactory.createBarChart(
076 chartName, xName, yName, dataset,
077 PlotOrientation.HORIZONTAL, true, false, false);
078 }
079 else if (chartType.equals("line")) {
080 chart = ChartFactory.createLineChart(
081 chartName, xName, yName, dataset, PlotOrientation.VERTICAL,
082 true, false, false);
083 }
084 else if (chartType.equals("vertical_bar")) {
085 chart = ChartFactory.createBarChart(
086 chartName, xName, yName, dataset, PlotOrientation.VERTICAL,
087 true, false, false);
088 }
089 else {
090 PieDataset pieData = DatasetUtilities.createPieDatasetForRow(
091 dataset, 0);
092
093 chart = ChartFactory.createPieChart(
094 chartName, pieData, true, false, false);
095 }
096
097 response.setContentType(ContentTypes.IMAGE_JPEG);
098
099 OutputStream os = response.getOutputStream();
100
101 ChartUtilities.writeChartAsJPEG(os, chart, 400, 400);
102
103 return null;
104 }
105 catch (Exception e) {
106 PortalUtil.sendError(e, request, response);
107
108 return null;
109 }
110 }
111
112 }