-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
ViewChartAction.java
162 lines (121 loc) · 4.58 KB
/
ViewChartAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* Copyright (c) 2000-present 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.samplestruts.struts.action;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import java.io.OutputStream;
import java.util.Enumeration;
import javax.portlet.PortletSessionUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;
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.category.DefaultCategoryDataset;
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 actionMapping, ActionForm actionForm,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
if (_log.isInfoEnabled()) {
_log.info("execute");
}
String attrName = "chart_name";
// Application scoped session attributes can be fetched from the
// servlet directly. Portlet scoped session attributes can be
// fetched from Sun's PortletSessionUtil.
HttpSession session = request.getSession();
String chartName = (String)session.getAttribute(attrName);
//(String)_getAttribute(request, attrName);
// Chart
String chartType = request.getParameter("chart_type");
CategoryDataset dataset = _getDataset();
String xName = "Soda";
String yName = "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("image/jpeg");
OutputStream out = response.getOutputStream();
ChartUtilities.writeChartAsJPEG(out, chart, 400, 400);
return actionMapping.findForward("/common/null.jsp");
}
catch (Exception e) {
request.setAttribute(PageContext.EXCEPTION, e);
return actionMapping.findForward("/common/error.jsp");
}
}
private Object _getAttribute(HttpServletRequest request, String attrName) {
// Go through all the session attributes and use Sun's
// PortletSessionUtil to match the correct attribute name
HttpSession session = request.getSession();
Enumeration<String> enu = session.getAttributeNames();
while (enu.hasMoreElements()) {
String encodedAttrName = enu.nextElement();
String decodedAttrName = PortletSessionUtil.decodeAttributeName(
encodedAttrName);
if (decodedAttrName.equals(attrName)) {
return session.getAttribute(encodedAttrName);
}
}
return null;
}
private CategoryDataset _getDataset() throws Exception {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
String seriesName = "";
dataset.addValue(Integer.valueOf(9), seriesName, "Coke");
dataset.addValue(Integer.valueOf(21), seriesName, "Pepsi");
return dataset;
}
private static Log _log = LogFactoryUtil.getLog(ViewChartAction.class);
}