1
2
3
4
5 package ecar.servlet.grafico;
6
7 import java.awt.Color;
8 import java.awt.Font;
9 import java.io.IOException;
10 import java.util.Iterator;
11 import java.util.List;
12 import java.util.Map;
13
14 import javax.servlet.ServletException;
15 import javax.servlet.http.HttpServlet;
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletResponse;
18
19 import org.apache.log4j.Logger;
20 import org.jfree.chart.ChartFactory;
21 import org.jfree.chart.ChartUtilities;
22 import org.jfree.chart.JFreeChart;
23 import org.jfree.chart.axis.CategoryAxis;
24 import org.jfree.chart.axis.ValueAxis;
25 import org.jfree.chart.plot.CategoryPlot;
26 import org.jfree.chart.plot.PlotOrientation;
27 import org.jfree.chart.renderer.category.CategoryItemRenderer;
28 import org.jfree.chart.title.TextTitle;
29 import org.jfree.data.category.DefaultCategoryDataset;
30
31 import comum.util.Pagina;
32 import comum.util.Util;
33
34 import ecar.dao.CorDao;
35 import ecar.dao.OrgaoDao;
36 import ecar.exception.ECARException;
37 import ecar.pojo.Cor;
38 import ecar.pojo.OrgaoOrg;
39 import ecar.servlet.grafico.bean.PosicaoBean;
40
41
42
43
44
45 public class GraficoEvolucaoPosicoes extends HttpServlet {
46
47
48
49 private static final long serialVersionUID = 1924552455684086649L;
50 private Logger logger = null;
51
52
53
54
55
56
57
58
59 public GraficoEvolucaoPosicoes() {
60 this.logger = Logger.getLogger(this.getClass());
61
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public final void service(HttpServletRequest request,
78 HttpServletResponse response) throws ServletException, IOException {
79
80 try {
81
82 List listAvaliacoes = (List) request.getSession().getAttribute( "listAvaliacoes");
83 CorDao corDao = new CorDao(null);
84 OrgaoDao orgaoDao = new OrgaoDao(null);
85 OrgaoOrg orgao = null;
86 String msgGrafico = "";
87
88
89 String strOrgao = "Em Monitoramento";
90 if (!"".equals(Pagina.getParamStr(request, "codOrgao"))) {
91 orgao = (OrgaoOrg) orgaoDao.buscar(OrgaoOrg.class, Long
92 .valueOf(Pagina.getParamStr(request, "codOrgao")));
93 if (orgao.getSiglaOrg() != null
94 && !"".equals(orgao.getSiglaOrg()))
95 strOrgao = orgao.getSiglaOrg();
96 else
97 strOrgao = orgao.getDescricaoOrg();
98 }
99
100 JFreeChart grafico;
101 DefaultCategoryDataset dataset = new DefaultCategoryDataset();
102 Map<String, Integer> mapCoresConfiguradas = null;
103 List <String>listOrdemCores = null;
104
105 Iterator itAvaliacoes = listAvaliacoes.iterator();
106
107 while (itAvaliacoes.hasNext()) {
108 PosicaoBean posicaoBean = (PosicaoBean) itAvaliacoes.next();
109
110 String serie = posicaoBean.getARef().getNomeAref();
111 mapCoresConfiguradas = corDao.contadorDePosicoesPorCores(posicaoBean);
112
113
114 if (listOrdemCores==null)
115 listOrdemCores = corDao.ordenarCores(mapCoresConfiguradas.keySet());
116
117
118 for (String codCorConfig : listOrdemCores) {
119 if (codCorConfig.equals(Cor.BRANCO)) {
120 dataset.addValue(mapCoresConfiguradas.get(codCorConfig),Cor.LABEL_BRANCO, serie);
121 } else if (codCorConfig.equals(Cor.NAO_LIBERADO)) {
122 dataset.addValue(mapCoresConfiguradas.get(codCorConfig),Cor.LABEL_NAO_LIBERADO, serie);
123
124
125 } else if (Util.ehValor(codCorConfig)){
126 Cor corConfig = (Cor) corDao.buscar(Cor.class, Long.parseLong(codCorConfig));
127
128
129 if (corConfig.getCodCorGrafico()==null && !corConfig.getSignificadoCor().endsWith(" *")) {
130 corConfig.setSignificadoCor(corConfig.getSignificadoCor() + " *");
131 }
132
133 dataset.addValue(mapCoresConfiguradas.get(codCorConfig),corConfig.getSignificadoCor(), serie);
134 }
135 }
136 }
137
138 grafico = ChartFactory.createBarChart3D("Evolução das Posições - "
139 + strOrgao, "Período(s) de Referência", "Quantidades",
140 dataset, PlotOrientation.VERTICAL, true, true, true);
141
142 CategoryPlot plot = grafico.getCategoryPlot();
143
144 TextTitle title = new TextTitle();
145 title.setFont(new Font("Arial", Font.PLAIN, 15));
146 title.setText("Evolução das Posições - " + strOrgao);
147
148 grafico.setTitle(title);
149
150 CategoryAxis categoryaxis = plot.getDomainAxis();
151 categoryaxis.setTickLabelFont(new Font("Arial", Font.PLAIN, 8));
152
153 ValueAxis valueaxis = plot.getRangeAxis();
154 valueaxis.setTickLabelFont(new Font("Arial", Font.PLAIN, 8));
155
156
157 CategoryItemRenderer renderer = plot.getRenderer();
158
159 int numeroCor = 0;
160
161
162 for (String codCorConfig : listOrdemCores) {
163
164 if (Util.ehValor(codCorConfig) ) {
165 Cor corConfig = (Cor) corDao.buscar(Cor.class, Long.parseLong(codCorConfig));
166
167 if (corConfig.getCodCorGrafico()!=null){
168 Color colorConfig = new Color(Integer.parseInt(corConfig.getCodCorGrafico().replace("#", ""), 16));
169 renderer.setSeriesPaint(numeroCor++, colorConfig);
170 } else {
171 renderer.setSeriesPaint(numeroCor++, Color.DARK_GRAY);
172 }
173 }
174 else if (codCorConfig.equals(Cor.BRANCO)) {
175
176 renderer.setSeriesPaint(numeroCor++, Color.WHITE);
177 }
178 else if (codCorConfig.equals(Cor.NAO_LIBERADO)) {
179
180 renderer.setSeriesPaint(numeroCor++, Color.LIGHT_GRAY);
181 }
182
183
184
185
186
187 }
188
189
190 response.setContentType("image/jpeg");
191 int comp = (listAvaliacoes.size() * 150);
192
193 if (comp <= 150) {
194 comp = comp * 2;
195 }
196 ChartUtilities.writeChartAsJPEG(response.getOutputStream(), grafico, comp, 450);
197
198 response.getOutputStream().flush();
199 response.getOutputStream().close();
200
201 } catch (IOException e) {
202 this.logger.error(e);
203 throw new ServletException(e);
204 } catch (ECARException e) {
205 this.logger.error(e);
206 throw new ServletException(e);
207 } catch (Exception e) {
208 this.logger.error(e);
209 throw new ServletException(e);
210 }
211 }
212
213
214 }