Rev 1967 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1967 | Rev 1989 | ||
---|---|---|---|
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 | ||
- | 4 | import jomm.utils.BytesUtils; |
|
- | 5 | import jomm.utils.MimeTypeGuesser; |
|
- | 6 | import org.jfree.chart.ChartFactory; |
|
- | 7 | import org.jfree.chart.JFreeChart; |
|
- | 8 | import org.jfree.chart.block.BlockBorder; |
|
- | 9 | import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; |
|
- | 10 | import org.jfree.chart.plot.CategoryPlot; |
|
- | 11 | import org.jfree.chart.plot.PiePlot; |
|
- | 12 | import org.jfree.chart.plot.PlotOrientation; |
|
- | 13 | import org.jfree.chart.plot.RingPlot; |
|
- | 14 | import org.jfree.chart.renderer.category.StackedBarRenderer; |
|
- | 15 | import org.jfree.chart.title.LegendTitle; |
|
- | 16 | import org.jfree.data.category.DefaultCategoryDataset; |
|
- | 17 | import org.jfree.data.general.DefaultPieDataset; |
|
- | 18 | import org.jfree.ui.RectangleInsets; |
|
- | 19 | import pt.estgp.estgweb.services.courses.coursereport.documentmodel.CourseReportDocument; |
|
- | 20 | import pt.estgp.estgweb.utils.Globals; |
|
3 | import pt.estgp.estgweb.utils.documentBuilder.AutoImageWraperComponent; |
21 | import pt.estgp.estgweb.utils.documentBuilder.ImageComponent; |
- | 22 | import pt.estgp.estgweb.web.controllers.utils.FileUploaded; |
|
- | 23 | ||
- | 24 | import javax.imageio.ImageIO; |
|
- | 25 | import java.awt.*; |
|
- | 26 | import java.awt.image.BufferedImage; |
|
- | 27 | import java.io.FileOutputStream; |
|
- | 28 | import java.io.IOException; |
|
- | 29 | import java.text.DecimalFormat; |
|
4 | 30 | ||
5 | /** |
31 | /** |
6 | * Created by jorgemachado on 31/10/17. |
32 | * Created by jorgemachado on 31/10/17. |
7 | */ |
33 | */ |
8 | public class GlobalLearningResultsChartYearImg extends AutoImageWraperComponent |
34 | public class GlobalLearningResultsChartYearImg extends ImageComponent |
9 | { |
35 | { |
- | 36 | ||
- | 37 | /** |
|
- | 38 | * |
|
- | 39 | * @param results |
|
- | 40 | * @return the tmp path for generated chart |
|
- | 41 | * @throws java.io.IOException |
|
- | 42 | */ |
|
- | 43 | public FileUploaded generateChart2tmp(UnitsLearningResultsTable results, CourseReportDocument courseReportDocument) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
|
- | 44 | { |
|
- | 45 | //Anos para aparecer no grafico e para serem percorridos no foreach |
|
- | 46 | java.util.List<UnitsLearningResultYear> years = results.getYears(); |
|
- | 47 | ||
- | 48 | 1.5.0/docs/api/java/lang/String.html">String tempName = BytesUtils.generateHexKey() + ".png"; |
|
- | 49 | ||
- | 50 | DefaultCategoryDataset data = new DefaultCategoryDataset(); |
|
- | 51 | ||
- | 52 | //Percorre cada ano, o year tem os métodos necessários para obter dados percentuais |
|
- | 53 | for(UnitsLearningResultYear year : years) { |
|
- | 54 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(year.getAprovados1013Percent()), "Aprovados 10-13", year.getAno() + "ºAno"); |
|
- | 55 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(year.getAprovados1416Percent()), "Aprovados 14-16", year.getAno() + "ºAno"); |
|
- | 56 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(year.getAprovados1720Percent()), "Aprovados 17-20", year.getAno() + "ºAno"); |
|
- | 57 | } |
|
- | 58 | //addvalue parametros(valor,fila,coluna) |
|
- | 59 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(results.getAprovados1013Percent()), "Aprovados 10-13","Total"); |
|
- | 60 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(results.getAprovados1416Percent()), "Aprovados 14-16","Total"); |
|
- | 61 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(results.getAprovados1720Percent()), "Aprovados 17-20","Total"); |
|
- | 62 | ||
- | 63 | //Cria o grafico com orientação e a data |
|
- | 64 | JFreeChart chart = ChartFactory.createStackedBarChart(null,null, null, data, PlotOrientation.VERTICAL, true, false, false); |
|
- | 65 | ||
- | 66 | //Fundo e linhas visiveis |
|
- | 67 | chart.setBackgroundPaint(new 1.5.0/docs/api/java/awt/Color.html">Color(255,255,255)); |
|
- | 68 | CategoryPlot plot = (CategoryPlot) chart.getPlot(); |
|
- | 69 | plot.setBackgroundPaint(new 1.5.0/docs/api/java/awt/Color.html">Color(255, 255, 255)); |
|
- | 70 | plot.setRangeGridlinePaint(1.5.0/docs/api/java/awt/Color.html">Color.GRAY); |
|
- | 71 | plot.setRangeGridlinesVisible(true); |
|
- | 72 | ||
- | 73 | ||
- | 74 | //Insets do grafico, com o padding |
|
- | 75 | RectangleInsets rectangle = new RectangleInsets(0,0,30F,0); |
|
- | 76 | chart.setPadding(rectangle); |
|
- | 77 | ||
- | 78 | //Estilo do texto no grafico |
|
- | 79 | 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)); |
|
- | 80 | 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)); |
|
- | 81 | ||
- | 82 | StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer(); |
|
- | 83 | //Cores do grafico |
|
- | 84 | renderer.setSeriesPaint(0, new 1.5.0/docs/api/java/awt/Color.html">Color(240, 130, 25)); |
|
- | 85 | renderer.setSeriesPaint(1, new 1.5.0/docs/api/java/awt/Color.html">Color(255, 218, 0)); |
|
- | 86 | renderer.setSeriesPaint(2, new 1.5.0/docs/api/java/awt/Color.html">Color(120, 173, 38)); |
|
- | 87 | //Grossura das barras |
|
- | 88 | renderer.setMaximumBarWidth(0.1); |
|
- | 89 | ||
- | 90 | //O primeiro parametro é {2} por defeito, o 2º formata os valores no chart. Mete-o visivel e formata a letra |
|
- | 91 | renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", new 1.5.0/docs/api/java/text/DecimalFormat.html">DecimalFormat("#.##'%'"))); |
|
- | 92 | renderer.setBaseItemLabelsVisible(true); |
|
- | 93 | 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)); |
|
- | 94 | ||
- | 95 | //Legenda do grafico |
|
- | 96 | LegendTitle legend = chart.getLegend(); |
|
- | 97 | 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)); |
|
- | 98 | legend.setFrame(BlockBorder.NONE); |
|
- | 99 | ||
- | 100 | ||
- | 101 | 1.5.0/docs/api/java/awt/image/BufferedImage.html">BufferedImage image = chart.createBufferedImage( 300*4, 200*4); |
|
- | 102 | 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; |
|
- | 103 | 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); |
|
- | 104 | 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)); |
|
- | 105 | ||
- | 106 | FileUploaded fileUploaded = new FileUploaded(); |
|
- | 107 | fileUploaded.setTmpName(tempName); |
|
- | 108 | fileUploaded.setTitle("Taxa global de aproveitamento"); |
|
- | 109 | fileUploaded.setDescription("Curso: " + courseReportDocument.getCourseName() + " (" + courseReportDocument.getCourseCode()+ ") ano " + courseReportDocument.getYear()); |
|
- | 110 | fileUploaded.setContentType(MimeTypeGuesser.getInstance().guessMimeType(output)); |
|
- | 111 | fileUploaded.setFileSize(output.length()); |
|
- | 112 | fileUploaded.setExtension("png"); |
|
- | 113 | return fileUploaded; |
|
- | 114 | } |
|
- | 115 | ||
- | 116 | 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 { |
|
- | 117 | ||
- | 118 | } |
|
- | 119 | ||
- | 120 | ||
10 | } |
121 | } |