Rev 1914 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1830 | jmachado | 1 | package pt.estgp.estgweb.services.courses.coursereport.documentmodel.learningresults.components; |
2 | |||
1914 | grupo2 | 3 | import jomm.dao.impl.AbstractDao; |
4 | import jomm.utils.BytesUtils; |
||
5 | import jomm.utils.MimeTypeGuesser; |
||
6 | import jomm.utils.StreamsUtils; |
||
7 | import org.jfree.chart.ChartFactory; |
||
8 | import org.jfree.chart.JFreeChart; |
||
9 | import org.jfree.chart.axis.CategoryAxis; |
||
10 | import org.jfree.chart.axis.NumberAxis; |
||
11 | import org.jfree.chart.block.BlockBorder; |
||
12 | import org.jfree.chart.labels.ItemLabelAnchor; |
||
13 | import org.jfree.chart.labels.ItemLabelPosition; |
||
14 | import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; |
||
15 | import org.jfree.chart.plot.CategoryPlot; |
||
16 | import org.jfree.chart.plot.PiePlot; |
||
17 | import org.jfree.chart.plot.PlotOrientation; |
||
18 | import org.jfree.chart.plot.RingPlot; |
||
19 | import org.jfree.chart.renderer.category.StackedBarRenderer; |
||
20 | import org.jfree.chart.renderer.category.StandardBarPainter; |
||
21 | import org.jfree.chart.title.LegendTitle; |
||
22 | import org.jfree.data.category.DefaultCategoryDataset; |
||
23 | import org.jfree.data.general.DefaultPieDataset; |
||
24 | import org.jfree.ui.RectangleInsets; |
||
25 | import org.jfree.ui.TextAnchor; |
||
26 | import org.json.JSONException; |
||
27 | import org.json.JSONObject; |
||
28 | import pt.estgp.estgweb.services.courses.coursereport.documentmodel.CourseReportDocument; |
||
29 | import pt.estgp.estgweb.services.questionarios.QuestionariosReportsService; |
||
30 | import pt.estgp.estgweb.services.questionarios.pedagogico.reportprocessors.utils.DataTable; |
||
31 | import pt.estgp.estgweb.utils.Globals; |
||
32 | import pt.estgp.estgweb.utils.documentBuilder.ImageComponent; |
||
33 | import pt.estgp.estgweb.web.controllers.utils.FileUploaded; |
||
1830 | jmachado | 34 | |
1914 | grupo2 | 35 | import javax.imageio.ImageIO; |
36 | import java.awt.*; |
||
37 | import java.awt.image.BufferedImage; |
||
38 | import java.io.FileNotFoundException; |
||
39 | import java.io.FileOutputStream; |
||
40 | import java.io.IOException; |
||
41 | import java.io.InputStream; |
||
42 | import java.net.URL; |
||
43 | import java.net.URLConnection; |
||
44 | import java.text.DecimalFormat; |
||
45 | import java.text.NumberFormat; |
||
46 | import java.util.List; |
||
47 | |||
1830 | jmachado | 48 | /** |
1914 | grupo2 | 49 | * Created by danielmarmelo on 30/11/17. |
1830 | jmachado | 50 | */ |
1914 | grupo2 | 51 | public class GlobalLearningResultsChartYearImg extends ImageComponent |
1830 | jmachado | 52 | { |
1934 | grupo2 | 53 | public GlobalLearningResultsChartYearImg() { |
54 | setWidth(200); |
||
55 | setHeight(200); |
||
56 | } |
||
57 | |||
1914 | grupo2 | 58 | public FileUploaded generateChart2tmp(UnitsLearningResultsTable results, CourseReportDocument courseReportDocument) throws 1.5.0/docs/api/java/io/IOException.html">IOException { |
59 | List<UnitsLearningResultYear> years = results.getYears(); |
||
60 | |||
61 | 1.5.0/docs/api/java/lang/String.html">String tempName = BytesUtils.generateHexKey() + ".png"; |
||
62 | |||
63 | DefaultCategoryDataset data = new DefaultCategoryDataset(); |
||
64 | for (UnitsLearningResultYear year : years) { |
||
65 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(year.getAprovados1013Percent()), "Aprovados 10-13", year.getAno() + "º Ano"); |
||
66 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(year.getAprovados1416Percent()), "Aprovados 14-16", year.getAno() + "º Ano"); |
||
67 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(year.getAprovados1720Percent()), "Aprovados 17-20", year.getAno() + "º Ano"); |
||
68 | } |
||
69 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(results.getAprovados1013Percent()), "Aprovados 10-13", "Total"); |
||
70 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(results.getAprovados1416Percent()), "Aprovados 14-16", "Total"); |
||
71 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(results.getAprovados1720Percent()), "Aprovados 17-20", "Total"); |
||
72 | |||
73 | JFreeChart chart = ChartFactory.createStackedBarChart( |
||
74 | null, |
||
75 | null, |
||
76 | null, |
||
77 | data, |
||
78 | PlotOrientation.VERTICAL, |
||
79 | true, |
||
80 | false, |
||
81 | false); |
||
82 | |||
83 | chart.setBackgroundPaint(1.5.0/docs/api/java/awt/Color.html">Color.WHITE); |
||
84 | CategoryPlot plot = (CategoryPlot) chart.getPlot(); |
||
85 | plot.setBackgroundPaint(1.5.0/docs/api/java/awt/Color.html">Color.WHITE); |
||
86 | plot.setRangeGridlinePaint(1.5.0/docs/api/java/awt/Color.html">Color.GRAY); |
||
87 | plot.setRangeGridlinesVisible(true); |
||
88 | //plot.setRangeGridlineStroke(new BasicStroke(1.0f)); |
||
89 | |||
90 | RectangleInsets chartRectangle = new RectangleInsets(0,0,30F,0); |
||
91 | chart.setPadding(chartRectangle); |
||
92 | |||
93 | plot.getDomainAxis().setTickLabelFont(new 1.5.0/docs/api/java/awt/Font.html">Font("Helvetica", 1.5.0/docs/api/java/awt/Font.html">Font.PLAIN, 25)); |
||
94 | plot.getRangeAxis().setTickLabelFont(new 1.5.0/docs/api/java/awt/Font.html">Font("Helvetica", 1.5.0/docs/api/java/awt/Font.html">Font.PLAIN, 25)); |
||
95 | |||
96 | StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer(); |
||
97 | // Remover gradiente das barras |
||
98 | renderer.setBarPainter(new StandardBarPainter()); |
||
99 | renderer.setSeriesPaint(0, new 1.5.0/docs/api/java/awt/Color.html">Color(237,127,32)); |
||
100 | renderer.setSeriesPaint(1, new 1.5.0/docs/api/java/awt/Color.html">Color(255,198,0)); |
||
101 | renderer.setSeriesPaint(2, new 1.5.0/docs/api/java/awt/Color.html">Color(122,173,38)); |
||
102 | renderer.setMaximumBarWidth(0.2); |
||
103 | |||
104 | renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", new 1.5.0/docs/api/java/text/DecimalFormat.html">DecimalFormat("#.##'%'"))); |
||
105 | renderer.setBaseItemLabelsVisible(true); |
||
106 | renderer.setBaseItemLabelFont(new 1.5.0/docs/api/java/awt/Font.html">Font("Helvetica",1.5.0/docs/api/java/awt/Font.html">Font.PLAIN,25)); |
||
107 | |||
108 | LegendTitle legend = chart.getLegend(); |
||
109 | legend.setItemFont(new 1.5.0/docs/api/java/awt/Font.html">Font("Helvetica", 1.5.0/docs/api/java/awt/Font.html">Font.PLAIN,25)); |
||
110 | legend.setFrame(BlockBorder.NONE); |
||
111 | |||
112 | |||
113 | 1.5.0/docs/api/java/awt/image/BufferedImage.html">BufferedImage image = chart.createBufferedImage( 300*4, 200*4); |
||
114 | 1.5.0/docs/api/java/lang/String.html">String pathGraficoTiposAlunos = Globals.TMP_DIR + java.io.1.5.0/docs/api/java/io/File.html">File.separator + tempName; |
||
115 | java.io.1.5.0/docs/api/java/io/File.html">File output = new java.io.1.5.0/docs/api/java/io/File.html">File(pathGraficoTiposAlunos); |
||
116 | 1.5.0/docs/api/javax/imageio/ImageIO.html">ImageIO.write(image, "png", new 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream(output)); |
||
117 | |||
118 | FileUploaded fileUploaded = new FileUploaded(); |
||
119 | fileUploaded.setTmpName(tempName); |
||
120 | fileUploaded.setTitle("Taxa global de aproveitamento"); |
||
121 | fileUploaded.setDescription("Curso: " + courseReportDocument.getCourseName() + " (" + courseReportDocument.getCourseCode()+ ") ano " + courseReportDocument.getYear()); |
||
122 | fileUploaded.setContentType(MimeTypeGuesser.getInstance().guessMimeType(output)); |
||
123 | fileUploaded.setFileSize(output.length()); |
||
124 | fileUploaded.setExtension("png"); |
||
125 | |||
126 | return fileUploaded; |
||
127 | } |
||
128 | |||
129 | public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) throws 1.5.0/docs/api/java/io/IOException.html">IOException { |
||
130 | 1.5.0/docs/api/java/lang/String.html">String tempName = "test.png"; |
||
131 | |||
132 | DefaultCategoryDataset data = new DefaultCategoryDataset(); |
||
133 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(29.5), "Aprovados 10-13", "1º Ano"); |
||
134 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(50.5), "Aprovados 14-16", "1º Ano"); |
||
135 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(20), "Aprovados 17-20", "1º Ano"); |
||
136 | |||
137 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(10), "Aprovados 10-13", "2º Ano"); |
||
138 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(40), "Aprovados 14-16", "2º Ano"); |
||
139 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(50), "Aprovados 17-20", "2º Ano"); |
||
140 | |||
141 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(50), "Aprovados 10-13", "3º Ano"); |
||
142 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(10), "Aprovados 14-16", "3º Ano"); |
||
143 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(40), "Aprovados 17-20", "3º Ano"); |
||
144 | |||
145 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(33.33), "Aprovados 10-13", "Total"); |
||
146 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(33.33), "Aprovados 14-16", "Total"); |
||
147 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(33.33), "Aprovados 17-20", "Total"); |
||
148 | |||
149 | |||
150 | |||
151 | JFreeChart chart = ChartFactory.createStackedBarChart( |
||
152 | null, |
||
153 | null, |
||
154 | null, |
||
155 | data, |
||
156 | PlotOrientation.VERTICAL, |
||
157 | true, |
||
158 | false, |
||
159 | false); |
||
160 | |||
161 | chart.setBackgroundPaint(1.5.0/docs/api/java/awt/Color.html">Color.WHITE); |
||
162 | CategoryPlot plot = (CategoryPlot) chart.getPlot(); |
||
163 | plot.setBackgroundPaint(1.5.0/docs/api/java/awt/Color.html">Color.WHITE); |
||
164 | plot.setRangeGridlinePaint(1.5.0/docs/api/java/awt/Color.html">Color.GRAY); |
||
165 | plot.setRangeGridlinesVisible(true); |
||
166 | //plot.setRangeGridlineStroke(new BasicStroke(1.0f)); |
||
167 | |||
168 | RectangleInsets chartRectangle = new RectangleInsets(0,0,30F,0); |
||
169 | chart.setPadding(chartRectangle); |
||
170 | |||
171 | plot.getDomainAxis().setTickLabelFont(new 1.5.0/docs/api/java/awt/Font.html">Font("Helvetica", 1.5.0/docs/api/java/awt/Font.html">Font.PLAIN, 25)); |
||
172 | plot.getRangeAxis().setTickLabelFont(new 1.5.0/docs/api/java/awt/Font.html">Font("Helvetica", 1.5.0/docs/api/java/awt/Font.html">Font.PLAIN, 25)); |
||
173 | |||
174 | StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer(); |
||
175 | // Remover gradiente das barras |
||
176 | renderer.setBarPainter(new StandardBarPainter()); |
||
177 | renderer.setSeriesPaint(0, new 1.5.0/docs/api/java/awt/Color.html">Color(237,127,32)); |
||
178 | renderer.setSeriesPaint(1, new 1.5.0/docs/api/java/awt/Color.html">Color(255,198,0)); |
||
179 | renderer.setSeriesPaint(2, new 1.5.0/docs/api/java/awt/Color.html">Color(122,173,38)); |
||
180 | renderer.setMaximumBarWidth(0.2); |
||
181 | |||
182 | renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", new 1.5.0/docs/api/java/text/DecimalFormat.html">DecimalFormat("#.##'%'"))); |
||
183 | renderer.setBaseItemLabelsVisible(true); |
||
184 | renderer.setBaseItemLabelFont(new 1.5.0/docs/api/java/awt/Font.html">Font("Helvetica",1.5.0/docs/api/java/awt/Font.html">Font.PLAIN,25)); |
||
185 | |||
186 | LegendTitle legend = chart.getLegend(); |
||
187 | legend.setItemFont(new 1.5.0/docs/api/java/awt/Font.html">Font("Helvetica", 1.5.0/docs/api/java/awt/Font.html">Font.PLAIN,25)); |
||
188 | legend.setFrame(BlockBorder.NONE); |
||
189 | |||
190 | |||
191 | |||
192 | 1.5.0/docs/api/java/awt/image/BufferedImage.html">BufferedImage image = chart.createBufferedImage( 300*4, 200*4); |
||
193 | 1.5.0/docs/api/java/lang/String.html">String pathGraficoTiposAlunos = Globals.TMP_DIR + java.io.1.5.0/docs/api/java/io/File.html">File.separator + tempName; |
||
194 | java.io.1.5.0/docs/api/java/io/File.html">File output = new java.io.1.5.0/docs/api/java/io/File.html">File(pathGraficoTiposAlunos); |
||
195 | 1.5.0/docs/api/javax/imageio/ImageIO.html">ImageIO.write(image, "png", new 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream(output)); |
||
196 | } |
||
1830 | jmachado | 197 | } |