Rev 1857 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1857 | Rev 1916 | ||
---|---|---|---|
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.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; |
|
3 | import pt.estgp.estgweb.utils.documentBuilder.AutoImageWraperComponent; |
32 | import pt.estgp.estgweb.utils.documentBuilder.ImageComponent; |
- | 33 | import pt.estgp.estgweb.web.controllers.utils.FileUploaded; |
|
- | 34 | ||
- | 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; |
|
4 | 47 | ||
5 | /** |
48 | /** |
6 | * Created by jorgemachado on 31/10/17. |
49 | * Created by joaocarvalho on 30/11/17. |
7 | */ |
50 | */ |
8 | public class GlobalLearningResultsChartYearImg extends AutoImageWraperComponent |
51 | public class GlobalLearningResultsChartYearImg extends ImageComponent |
9 | { |
52 | { |
- | 53 | public FileUploaded generateChart2tmp(UnitsLearningResultsTable results, CourseReportDocument courseReportDocument) throws 1.5.0/docs/api/java/io/IOException.html">IOException { |
|
- | 54 | List<UnitsLearningResultYear> years = results.getYears(); |
|
- | 55 | ||
- | 56 | 1.5.0/docs/api/java/lang/String.html">String tempName = BytesUtils.generateHexKey() + ".png"; |
|
- | 57 | ||
- | 58 | DefaultCategoryDataset data = new DefaultCategoryDataset(); |
|
- | 59 | for (UnitsLearningResultYear year : years) { |
|
- | 60 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(year.getAprovados1013Percent()), "Aprovados 10-13", year.getAno() + "º Ano"); |
|
- | 61 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(year.getAprovados1416Percent()), "Aprovados 14-16", year.getAno() + "º Ano"); |
|
- | 62 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(year.getAprovados1720Percent()), "Aprovados 17-20", year.getAno() + "º Ano"); |
|
- | 63 | } |
|
- | 64 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(results.getAprovados1013Percent()), "Aprovados 10-13", "Total"); |
|
- | 65 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(results.getAprovados1416Percent()), "Aprovados 14-16", "Total"); |
|
- | 66 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(results.getAprovados1720Percent()), "Aprovados 17-20", "Total"); |
|
- | 67 | ||
- | 68 | JFreeChart chart = ChartFactory.createStackedBarChart( |
|
- | 69 | null, |
|
- | 70 | null, |
|
- | 71 | null, |
|
- | 72 | data, |
|
- | 73 | PlotOrientation.VERTICAL, |
|
- | 74 | true, |
|
- | 75 | false, |
|
- | 76 | false); |
|
- | 77 | ||
- | 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 | ||
- | 108 | 1.5.0/docs/api/java/awt/image/BufferedImage.html">BufferedImage image = chart.createBufferedImage( 300*4, 200*4); |
|
- | 109 | 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; |
|
- | 110 | 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); |
|
- | 111 | 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)); |
|
- | 112 | ||
- | 113 | FileUploaded fileUploaded = new FileUploaded(); |
|
- | 114 | fileUploaded.setTmpName(tempName); |
|
- | 115 | fileUploaded.setTitle("Taxa global de aproveitamento"); |
|
- | 116 | fileUploaded.setDescription("Curso: " + courseReportDocument.getCourseName() + " (" + courseReportDocument.getCourseCode()+ ") ano " + courseReportDocument.getYear()); |
|
- | 117 | fileUploaded.setContentType(MimeTypeGuesser.getInstance().guessMimeType(output)); |
|
- | 118 | fileUploaded.setFileSize(output.length()); |
|
- | 119 | fileUploaded.setExtension("png"); |
|
- | 120 | ||
- | 121 | return fileUploaded; |
|
- | 122 | } |
|
- | 123 | ||
- | 124 | 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 { |
|
- | 125 | 1.5.0/docs/api/java/lang/String.html">String tempName = "test.png"; |
|
- | 126 | ||
- | 127 | DefaultCategoryDataset data = new DefaultCategoryDataset(); |
|
- | 128 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(29.5), "Aprovados 10-13", "1º Ano"); |
|
- | 129 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(50.5), "Aprovados 14-16", "1º Ano"); |
|
- | 130 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(20), "Aprovados 17-20", "1º Ano"); |
|
- | 131 | ||
- | 132 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(10), "Aprovados 10-13", "2º Ano"); |
|
- | 133 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(40), "Aprovados 14-16", "2º Ano"); |
|
- | 134 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(50), "Aprovados 17-20", "2º Ano"); |
|
- | 135 | ||
- | 136 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(50), "Aprovados 10-13", "3º Ano"); |
|
- | 137 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(10), "Aprovados 14-16", "3º Ano"); |
|
- | 138 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(40), "Aprovados 17-20", "3º Ano"); |
|
- | 139 | ||
- | 140 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(33.33), "Aprovados 10-13", "Total"); |
|
- | 141 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(33.33), "Aprovados 14-16", "Total"); |
|
- | 142 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(33.33), "Aprovados 17-20", "Total"); |
|
- | 143 | ||
- | 144 | ||
- | 145 | ||
- | 146 | JFreeChart chart = ChartFactory.createStackedBarChart( |
|
- | 147 | null, |
|
- | 148 | null, |
|
- | 149 | null, |
|
- | 150 | data, |
|
- | 151 | PlotOrientation.VERTICAL, |
|
- | 152 | true, |
|
- | 153 | false, |
|
- | 154 | false); |
|
- | 155 | ||
- | 156 | chart.setBackgroundPaint(1.5.0/docs/api/java/awt/Color.html">Color.WHITE); |
|
- | 157 | CategoryPlot plot = (CategoryPlot) chart.getPlot(); |
|
- | 158 | plot.setBackgroundPaint(1.5.0/docs/api/java/awt/Color.html">Color.WHITE); |
|
- | 159 | plot.setRangeGridlinePaint(1.5.0/docs/api/java/awt/Color.html">Color.GRAY); |
|
- | 160 | plot.setRangeGridlinesVisible(true); |
|
- | 161 | //plot.setRangeGridlineStroke(new BasicStroke(1.0f)); |
|
- | 162 | ||
- | 163 | RectangleInsets chartRectangle = new RectangleInsets(0,0,30F,0); |
|
- | 164 | chart.setPadding(chartRectangle); |
|
- | 165 | ||
- | 166 | 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)); |
|
- | 167 | 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)); |
|
- | 168 | ||
- | 169 | StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer(); |
|
- | 170 | // Remover gradiente das barras |
|
- | 171 | renderer.setBarPainter(new StandardBarPainter()); |
|
- | 172 | renderer.setSeriesPaint(0, new 1.5.0/docs/api/java/awt/Color.html">Color(237,127,32)); |
|
- | 173 | renderer.setSeriesPaint(1, new 1.5.0/docs/api/java/awt/Color.html">Color(255,198,0)); |
|
- | 174 | renderer.setSeriesPaint(2, new 1.5.0/docs/api/java/awt/Color.html">Color(122,173,38)); |
|
- | 175 | renderer.setMaximumBarWidth(0.2); |
|
- | 176 | ||
- | 177 | renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", new 1.5.0/docs/api/java/text/DecimalFormat.html">DecimalFormat("#.##'%'"))); |
|
- | 178 | renderer.setBaseItemLabelsVisible(true); |
|
- | 179 | 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)); |
|
- | 180 | ||
- | 181 | LegendTitle legend = chart.getLegend(); |
|
- | 182 | 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)); |
|
- | 183 | legend.setFrame(BlockBorder.NONE); |
|
- | 184 | ||
- | 185 | ||
- | 186 | ||
- | 187 | 1.5.0/docs/api/java/awt/image/BufferedImage.html">BufferedImage image = chart.createBufferedImage( 300*4, 200*4); |
|
- | 188 | 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; |
|
- | 189 | 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); |
|
- | 190 | 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)); |
|
- | 191 | } |
|
10 | } |
192 | } |
11 | 193 |