com.liferay.portlet.polls.action.ViewChartAction.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.portlet.polls.action.ViewChartAction.java

Source

/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portlet.polls.action;

import com.liferay.portal.kernel.util.ContentTypes;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.util.WebKeys;
import com.liferay.portlet.polls.util.PollsUtil;

import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.general.PieDataset;

/**
 * @author Brian Wing Shun Chan
 */
public class ViewChartAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        try {
            ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

            long questionId = ParamUtil.getLong(request, "questionId");

            String chartType = ParamUtil.getString(request, "chartType", "pie");

            CategoryDataset dataset = PollsUtil.getVotesDataset(questionId);

            String chartName = themeDisplay.translate("vote-results");
            String xName = themeDisplay.translate("choice");
            String yName = themeDisplay.translate("votes");

            JFreeChart chart = null;

            if (chartType.equals("area")) {
                chart = ChartFactory.createAreaChart(chartName, xName, yName, dataset, PlotOrientation.VERTICAL,
                        true, false, false);
            } else if (chartType.equals("horizontal_bar")) {
                chart = ChartFactory.createBarChart(chartName, xName, yName, dataset, PlotOrientation.HORIZONTAL,
                        true, false, false);
            } else if (chartType.equals("line")) {
                chart = ChartFactory.createLineChart(chartName, xName, yName, dataset, PlotOrientation.VERTICAL,
                        true, false, false);
            } else if (chartType.equals("vertical_bar")) {
                chart = ChartFactory.createBarChart(chartName, xName, yName, dataset, PlotOrientation.VERTICAL,
                        true, false, false);
            } else {
                PieDataset pieData = DatasetUtilities.createPieDatasetForRow(dataset, 0);

                chart = ChartFactory.createPieChart(chartName, pieData, true, false, false);
            }

            response.setContentType(ContentTypes.IMAGE_JPEG);

            OutputStream os = response.getOutputStream();

            ChartUtilities.writeChartAsJPEG(os, chart, 400, 400);

            return null;
        } catch (Exception e) {
            PortalUtil.sendError(e, request, response);

            return null;
        }
    }

}