Rev 1857 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1857 | Rev 1929 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | package pt.estgp.estgweb.services.courses.coursereport.documentmodel.learningresults.components; |
1 | package pt.estgp.estgweb.services.courses.coursereport.documentmodel.learningresults.components; |
2 | 2 | ||
- | 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; |
|
3 | import pt.estgp.estgweb.utils.documentBuilder.AutoImageWraperComponent; |
19 | import pt.estgp.estgweb.utils.documentBuilder.AutoImageWraperComponent; |
- | 20 | import pt.estgp.estgweb.web.controllers.utils.FileUploaded; |
|
- | 21 | ||
- | 22 | import javax.imageio.ImageIO; |
|
- | 23 | import java.awt.*; |
|
- | 24 | import java.awt.image.BufferedImage; |
|
- | 25 | import java.io.File; |
|
- | 26 | import java.io.FileNotFoundException; |
|
- | 27 | import java.io.FileOutputStream; |
|
- | 28 | import java.io.IOException; |
|
- | 29 | import java.text.DecimalFormat; |
|
- | 30 | import java.util.List; |
|
- | 31 | import java.util.Map; |
|
- | 32 | import java.util.Random; |
|
- | 33 | ||
- | 34 | ||
4 | 35 | ||
5 | /** |
36 | /** |
6 | * Created by jorgemachado on 31/10/17. |
37 | * Created by ASUS on 14/01/18. |
7 | */ |
38 | */ |
8 | public class LearningResultsChartSem extends AutoImageWraperComponent |
39 | public class LearningResultsChartSem extends AutoImageWraperComponent |
9 | { |
40 | { |
10 | 1.5.0/docs/api/java/lang/String.html">String semesterCode; |
41 | 1.5.0/docs/api/java/lang/String.html">String semesterCode; |
11 | 42 | ||
12 | - | ||
13 | public LearningResultsChartSem() { |
43 | public LearningResultsChartSem() { |
14 | } |
44 | } |
15 | 45 | ||
16 | public LearningResultsChartSem(1.5.0/docs/api/java/lang/String.html">String semesterCode) { |
46 | public LearningResultsChartSem(1.5.0/docs/api/java/lang/String.html">String semesterCode) { |
17 | this.semesterCode = semesterCode; |
47 | this.semesterCode = semesterCode; |
- | 48 | setInternalCode(semesterCode); |
|
18 | } |
49 | } |
19 | 50 | ||
20 | public 1.5.0/docs/api/java/lang/String.html">String getSemesterCode() { |
51 | public 1.5.0/docs/api/java/lang/String.html">String getSemesterCode() { |
21 | return semesterCode; |
52 | return semesterCode; |
22 | } |
53 | } |
23 | 54 | ||
24 | public void setSemesterCode(1.5.0/docs/api/java/lang/String.html">String semesterCode) { |
55 | public void setSemesterCode(1.5.0/docs/api/java/lang/String.html">String semesterCode) { |
25 | this.semesterCode = semesterCode; |
56 | this.semesterCode = semesterCode; |
26 | } |
57 | } |
- | 58 | ||
- | 59 | public FileUploaded generateChart2tmp(UnitsLearningResultsTable results, CourseReportDocument courseReportDocument) throws 1.5.0/docs/api/java/io/IOException.html">IOException { |
|
- | 60 | List<UnitsLearningResultYear> years = results.getYears(); |
|
- | 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 | JFreeChart chart = ChartFactory.createStackedBarChart(null,null,null,data,PlotOrientation.HORIZONTAL,true,false,false); |
|
- | 78 | chart.setBackgroundPaint(1.5.0/docs/api/java/awt/Color.html">Color.WHITE); |
|
- | 79 | CategoryPlot plot = (CategoryPlot) chart.getPlot(); |
|
- | 80 | plot.setBackgroundPaint(1.5.0/docs/api/java/awt/Color.html">Color.WHITE); |
|
- | 81 | plot.setRangeGridlinePaint(1.5.0/docs/api/java/awt/Color.html">Color.GRAY); |
|
- | 82 | plot.setRangeGridlinesVisible(true); |
|
- | 83 | //plot.setRangeGridlineStroke(new BasicStroke(1.0f)); |
|
- | 84 | ||
- | 85 | RectangleInsets chartRectangle = new RectangleInsets(0,0,30F,0); |
|
- | 86 | chart.setPadding(chartRectangle); |
|
- | 87 | ||
- | 88 | 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)); |
|
- | 89 | 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)); |
|
- | 90 | ||
- | 91 | StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer(); |
|
- | 92 | // Remover gradiente das barras |
|
- | 93 | renderer.setBarPainter(new StandardBarPainter()); |
|
- | 94 | renderer.setSeriesPaint(0, new 1.5.0/docs/api/java/awt/Color.html">Color(237,127,32)); |
|
- | 95 | renderer.setSeriesPaint(1, new 1.5.0/docs/api/java/awt/Color.html">Color(255,198,0)); |
|
- | 96 | renderer.setSeriesPaint(2, new 1.5.0/docs/api/java/awt/Color.html">Color(122,173,38)); |
|
- | 97 | renderer.setMaximumBarWidth(0.2); |
|
- | 98 | ||
- | 99 | renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", new 1.5.0/docs/api/java/text/DecimalFormat.html">DecimalFormat("#.##'%'"))); |
|
- | 100 | renderer.setBaseItemLabelsVisible(true); |
|
- | 101 | 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)); |
|
- | 102 | ||
- | 103 | LegendTitle legend = chart.getLegend(); |
|
- | 104 | 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)); |
|
- | 105 | legend.setFrame(BlockBorder.NONE); |
|
- | 106 | ||
- | 107 | 1.5.0/docs/api/java/awt/image/BufferedImage.html">BufferedImage image = chart.createBufferedImage(300*4, 200*4); |
|
- | 108 | 1.5.0/docs/api/java/lang/String.html">String pathGraficoTiposAlunos = Globals.TMP_DIR + 1.5.0/docs/api/java/io/File.html">File.separator + tempName; |
|
- | 109 | 1.5.0/docs/api/java/io/File.html">File output = new 1.5.0/docs/api/java/io/File.html">File(pathGraficoTiposAlunos); |
|
- | 110 | 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)); |
|
- | 111 | ||
- | 112 | FileUploaded fileUploaded = new FileUploaded(); |
|
- | 113 | fileUploaded.setTmpName(tempName); |
|
- | 114 | fileUploaded.setTitle("Taxa global de aproveitamento"); |
|
- | 115 | fileUploaded.setDescription("Curso: " + courseReportDocument.getCourseName() + " (" + courseReportDocument.getCourseCode()+ ") ano " + courseReportDocument.getYear()); |
|
- | 116 | fileUploaded.setContentType(MimeTypeGuesser.getInstance().guessMimeType(output)); |
|
- | 117 | fileUploaded.setFileSize(output.length()); |
|
- | 118 | fileUploaded.setExtension("png"); |
|
- | 119 | ||
- | 120 | return fileUploaded; |
|
- | 121 | } |
|
- | 122 | ||
- | 123 | 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{ |
|
- | 124 | 1.5.0/docs/api/java/lang/String.html">String tempName = "test.png"; |
|
- | 125 | ||
- | 126 | DefaultCategoryDataset data = new DefaultCategoryDataset(); |
|
- | 127 | ||
- | 128 | for (int i=0; i<16; i++){ |
|
- | 129 | int s = new 1.5.0/docs/api/java/util/Random.html">Random().nextInt(75); |
|
- | 130 | int r = new 1.5.0/docs/api/java/util/Random.html">Random().nextInt((101 - s)); |
|
- | 131 | int a = (100 - s - r); |
|
- | 132 | ||
- | 133 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(s), "Sem elementos", "Engenharia de Software " + i); |
|
- | 134 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(r), "Reprovados", "Engenharia de Software " + i); |
|
- | 135 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(a), "Aprovados", "Engenharia de Software " + i); |
|
- | 136 | } |
|
- | 137 | ||
- | 138 | JFreeChart chart = ChartFactory.createStackedBarChart(null,null,null,data,PlotOrientation.HORIZONTAL, true, false, false); |
|
- | 139 | chart.setBackgroundPaint(1.5.0/docs/api/java/awt/Color.html">Color.WHITE); |
|
- | 140 | CategoryPlot plot = (CategoryPlot) chart.getPlot(); |
|
- | 141 | plot.setBackgroundPaint(1.5.0/docs/api/java/awt/Color.html">Color.WHITE); |
|
- | 142 | plot.setRangeGridlinePaint(1.5.0/docs/api/java/awt/Color.html">Color.GRAY); |
|
- | 143 | plot.setRangeGridlinesVisible(true); |
|
- | 144 | //plot.setRangeGridlineStroke(new BasicStroke(1.0f)); |
|
- | 145 | ||
- | 146 | RectangleInsets chartRectangle = new RectangleInsets(0,0,30F,0); |
|
- | 147 | chart.setPadding(chartRectangle); |
|
- | 148 | ||
- | 149 | 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)); |
|
- | 150 | 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)); |
|
- | 151 | ||
- | 152 | StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer(); |
|
- | 153 | // Remover gradiente das barras |
|
- | 154 | renderer.setBarPainter(new StandardBarPainter()); |
|
- | 155 | renderer.setSeriesPaint(0, new 1.5.0/docs/api/java/awt/Color.html">Color(237,127,32)); |
|
- | 156 | renderer.setSeriesPaint(1, new 1.5.0/docs/api/java/awt/Color.html">Color(255,198,0)); |
|
- | 157 | renderer.setSeriesPaint(2, new 1.5.0/docs/api/java/awt/Color.html">Color(122,173,38)); |
|
- | 158 | renderer.setMaximumBarWidth(0.1); |
|
- | 159 | ||
- | 160 | renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", new 1.5.0/docs/api/java/text/DecimalFormat.html">DecimalFormat("#.##'%'"))); |
|
- | 161 | renderer.setBaseItemLabelsVisible(true); |
|
- | 162 | 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)); |
|
- | 163 | ||
- | 164 | LegendTitle legend = chart.getLegend(); |
|
- | 165 | 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)); |
|
- | 166 | legend.setFrame(BlockBorder.NONE); |
|
- | 167 | ||
- | 168 | 1.5.0/docs/api/java/awt/image/BufferedImage.html">BufferedImage image = chart.createBufferedImage( 300*4, 400*4); |
|
- | 169 | 1.5.0/docs/api/java/lang/String.html">String pathGraficoTiposAlunos = Globals.TMP_DIR + 1.5.0/docs/api/java/io/File.html">File.separator + tempName; |
|
- | 170 | 1.5.0/docs/api/java/io/File.html">File output = new 1.5.0/docs/api/java/io/File.html">File(pathGraficoTiposAlunos); |
|
- | 171 | 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)); |
|
- | 172 | ||
- | 173 | } |
|
- | 174 | ||
27 | } |
175 | } |