Rev 1928 | 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 | |||
1925 | grupo2 | 3 | import jomm.utils.BytesUtils; |
4 | import jomm.utils.MimeTypeGuesser; |
||
5 | import org.jfree.chart.ChartFactory; |
||
6 | import org.jfree.chart.JFreeChart; |
||
7 | import org.jfree.chart.block.BlockBorder; |
||
8 | import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; |
||
9 | import org.jfree.chart.plot.CategoryPlot; |
||
10 | import org.jfree.chart.plot.PlotOrientation; |
||
11 | import org.jfree.chart.renderer.category.StackedBarRenderer; |
||
12 | import org.jfree.chart.renderer.category.StandardBarPainter; |
||
13 | import org.jfree.chart.title.LegendTitle; |
||
14 | import org.jfree.data.category.DefaultCategoryDataset; |
||
15 | import org.jfree.ui.RectangleInsets; |
||
16 | import pt.estgp.estgweb.services.courses.coursereport.documentmodel.CourseReportDocument; |
||
17 | import pt.estgp.estgweb.utils.Globals; |
||
18 | import pt.estgp.estgweb.utils.documentBuilder.ImageComponent; |
||
19 | import pt.estgp.estgweb.web.controllers.utils.FileUploaded; |
||
1830 | jmachado | 20 | |
1925 | grupo2 | 21 | import javax.imageio.ImageIO; |
22 | import java.awt.*; |
||
23 | import java.awt.image.BufferedImage; |
||
24 | import java.io.FileNotFoundException; |
||
25 | import java.io.FileOutputStream; |
||
26 | import java.io.IOException; |
||
27 | import java.text.DecimalFormat; |
||
28 | import java.util.List; |
||
29 | import java.util.Map; |
||
30 | import java.util.Random; |
||
31 | |||
1830 | jmachado | 32 | /** |
1925 | grupo2 | 33 | * Created by danielmarmelo on 13/12/17. |
1830 | jmachado | 34 | */ |
1925 | grupo2 | 35 | public class LearningResultsChartSem extends ImageComponent |
1830 | jmachado | 36 | { |
37 | 1.5.0/docs/api/java/lang/String.html">String semesterCode; |
||
38 | |||
39 | |||
40 | public LearningResultsChartSem() { |
||
41 | } |
||
42 | |||
43 | public LearningResultsChartSem(1.5.0/docs/api/java/lang/String.html">String semesterCode) { |
||
44 | this.semesterCode = semesterCode; |
||
1925 | grupo2 | 45 | setInternalCode(semesterCode); |
1934 | grupo2 | 46 | setWidth(300); |
47 | setHeight(400); |
||
1830 | jmachado | 48 | } |
49 | |||
50 | public 1.5.0/docs/api/java/lang/String.html">String getSemesterCode() { |
||
51 | return semesterCode; |
||
52 | } |
||
53 | |||
54 | public void setSemesterCode(1.5.0/docs/api/java/lang/String.html">String semesterCode) { |
||
55 | this.semesterCode = semesterCode; |
||
56 | } |
||
1925 | grupo2 | 57 | |
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 | for (UnitsLearningResultSemester semester : year.getSemesters()) { |
||
66 | if (semester.getSemestre().equals(getSemesterCode())) { |
||
67 | for (UnitsLearningResultUc uc : semester.getUcs()) { |
||
68 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(uc.getSemElementosPercent()), "Sem elementos", uc.getName()); |
||
69 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(uc.getReprovadosPercent()), "Reprovados", uc.getName()); |
||
70 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(uc.getAprovadosPercent()), "Aprovados", uc.getName()); |
||
71 | } |
||
72 | break; |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 | |||
77 | |||
78 | JFreeChart chart = ChartFactory.createStackedBarChart( |
||
79 | null, |
||
80 | null, |
||
81 | null, |
||
82 | data, |
||
83 | PlotOrientation.HORIZONTAL, |
||
84 | true, |
||
85 | false, |
||
86 | false); |
||
87 | |||
88 | chart.setBackgroundPaint(1.5.0/docs/api/java/awt/Color.html">Color.WHITE); |
||
89 | CategoryPlot plot = (CategoryPlot) chart.getPlot(); |
||
90 | plot.setBackgroundPaint(1.5.0/docs/api/java/awt/Color.html">Color.WHITE); |
||
91 | plot.setRangeGridlinePaint(1.5.0/docs/api/java/awt/Color.html">Color.GRAY); |
||
92 | plot.setRangeGridlinesVisible(true); |
||
93 | //plot.setRangeGridlineStroke(new BasicStroke(1.0f)); |
||
94 | |||
95 | RectangleInsets chartRectangle = new RectangleInsets(0,0,30F,0); |
||
96 | chart.setPadding(chartRectangle); |
||
97 | |||
98 | 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)); |
||
99 | 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)); |
||
100 | |||
101 | StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer(); |
||
102 | // Remover gradiente das barras |
||
103 | renderer.setBarPainter(new StandardBarPainter()); |
||
104 | renderer.setSeriesPaint(0, new 1.5.0/docs/api/java/awt/Color.html">Color(237,127,32)); |
||
105 | renderer.setSeriesPaint(1, new 1.5.0/docs/api/java/awt/Color.html">Color(255,198,0)); |
||
106 | renderer.setSeriesPaint(2, new 1.5.0/docs/api/java/awt/Color.html">Color(122,173,38)); |
||
1928 | grupo2 | 107 | renderer.setMaximumBarWidth(0.1); |
1925 | grupo2 | 108 | |
109 | renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", new 1.5.0/docs/api/java/text/DecimalFormat.html">DecimalFormat("#.##'%'"))); |
||
110 | renderer.setBaseItemLabelsVisible(true); |
||
111 | 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)); |
||
112 | |||
113 | LegendTitle legend = chart.getLegend(); |
||
114 | 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)); |
||
115 | legend.setFrame(BlockBorder.NONE); |
||
116 | |||
117 | |||
1934 | grupo2 | 118 | 1.5.0/docs/api/java/awt/image/BufferedImage.html">BufferedImage image = chart.createBufferedImage(300*4, 400*4); |
1925 | grupo2 | 119 | 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; |
120 | 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); |
||
121 | 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)); |
||
122 | |||
123 | FileUploaded fileUploaded = new FileUploaded(); |
||
124 | fileUploaded.setTmpName(tempName); |
||
125 | fileUploaded.setTitle("Taxa global de aproveitamento"); |
||
126 | fileUploaded.setDescription("Curso: " + courseReportDocument.getCourseName() + " (" + courseReportDocument.getCourseCode()+ ") ano " + courseReportDocument.getYear()); |
||
127 | fileUploaded.setContentType(MimeTypeGuesser.getInstance().guessMimeType(output)); |
||
128 | fileUploaded.setFileSize(output.length()); |
||
129 | fileUploaded.setExtension("png"); |
||
130 | |||
131 | return fileUploaded; |
||
132 | } |
||
133 | |||
134 | 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 { |
||
135 | 1.5.0/docs/api/java/lang/String.html">String tempName = "test.png"; |
||
136 | |||
137 | DefaultCategoryDataset data = new DefaultCategoryDataset(); |
||
138 | |||
139 | for (int i = 0; i < 16; i++) { |
||
140 | int s = new 1.5.0/docs/api/java/util/Random.html">Random().nextInt(75); |
||
141 | int r = new 1.5.0/docs/api/java/util/Random.html">Random().nextInt((101 - s)); |
||
142 | int a = (100 - s - r); |
||
143 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(s), "Sem elementos", "Engenharia de Software " + i); |
||
144 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(r), "Reprovados", "Engenharia de Software " + i); |
||
145 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(a), "Aprovados", "Engenharia de Software " + i); |
||
146 | } |
||
147 | |||
148 | |||
149 | JFreeChart chart = ChartFactory.createStackedBarChart( |
||
150 | null, |
||
151 | null, |
||
152 | null, |
||
153 | data, |
||
154 | PlotOrientation.HORIZONTAL, |
||
155 | true, |
||
156 | false, |
||
157 | false); |
||
158 | |||
159 | chart.setBackgroundPaint(1.5.0/docs/api/java/awt/Color.html">Color.WHITE); |
||
160 | CategoryPlot plot = (CategoryPlot) chart.getPlot(); |
||
161 | plot.setBackgroundPaint(1.5.0/docs/api/java/awt/Color.html">Color.WHITE); |
||
162 | plot.setRangeGridlinePaint(1.5.0/docs/api/java/awt/Color.html">Color.GRAY); |
||
163 | plot.setRangeGridlinesVisible(true); |
||
164 | //plot.setRangeGridlineStroke(new BasicStroke(1.0f)); |
||
165 | |||
166 | RectangleInsets chartRectangle = new RectangleInsets(0,0,30F,0); |
||
167 | chart.setPadding(chartRectangle); |
||
168 | |||
169 | 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)); |
||
170 | 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)); |
||
171 | |||
172 | StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer(); |
||
173 | // Remover gradiente das barras |
||
174 | renderer.setBarPainter(new StandardBarPainter()); |
||
175 | renderer.setSeriesPaint(0, new 1.5.0/docs/api/java/awt/Color.html">Color(237,127,32)); |
||
176 | renderer.setSeriesPaint(1, new 1.5.0/docs/api/java/awt/Color.html">Color(255,198,0)); |
||
177 | renderer.setSeriesPaint(2, new 1.5.0/docs/api/java/awt/Color.html">Color(122,173,38)); |
||
178 | renderer.setMaximumBarWidth(0.1); |
||
179 | |||
180 | renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", new 1.5.0/docs/api/java/text/DecimalFormat.html">DecimalFormat("#.##'%'"))); |
||
181 | renderer.setBaseItemLabelsVisible(true); |
||
182 | 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)); |
||
183 | |||
184 | LegendTitle legend = chart.getLegend(); |
||
185 | 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)); |
||
186 | legend.setFrame(BlockBorder.NONE); |
||
187 | |||
188 | |||
189 | |||
190 | 1.5.0/docs/api/java/awt/image/BufferedImage.html">BufferedImage image = chart.createBufferedImage( 300*4, 400*4); |
||
191 | 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; |
||
192 | 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); |
||
193 | 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)); |
||
194 | } |
||
1830 | jmachado | 195 | } |