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