Rev 1907 | Rev 1909 | 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 | |||
1906 | grupo6 | 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.axis.CategoryAxis; |
||
1907 | grupo6 | 8 | import org.jfree.chart.axis.NumberAxis; |
9 | import org.jfree.chart.block.BlockBorder; |
||
10 | import org.jfree.chart.labels.ItemLabelAnchor; |
||
11 | import org.jfree.chart.labels.ItemLabelPosition; |
||
12 | import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; |
||
13 | import org.jfree.chart.plot.CategoryPlot; |
||
14 | import org.jfree.chart.plot.PiePlot; |
||
15 | import org.jfree.chart.plot.PlotOrientation; |
||
16 | import org.jfree.chart.plot.RingPlot; |
||
17 | import org.jfree.chart.renderer.category.StackedBarRenderer; |
||
18 | import org.jfree.chart.renderer.category.StandardBarPainter; |
||
1906 | grupo6 | 19 | import org.jfree.data.category.DefaultCategoryDataset; |
20 | import org.jfree.data.general.DefaultPieDataset; |
||
21 | import org.jfree.ui.RectangleInsets; |
||
1907 | grupo6 | 22 | import org.jfree.ui.TextAnchor; |
1906 | grupo6 | 23 | import pt.estgp.estgweb.services.courses.coursereport.documentmodel.CourseReportDocument; |
24 | import pt.estgp.estgweb.utils.Globals; |
||
25 | import pt.estgp.estgweb.utils.documentBuilder.ImageComponent; |
||
26 | import pt.estgp.estgweb.web.controllers.utils.FileUploaded; |
||
1830 | jmachado | 27 | |
1906 | grupo6 | 28 | import javax.imageio.ImageIO; |
29 | import java.awt.*; |
||
30 | import java.awt.image.BufferedImage; |
||
31 | import java.io.FileOutputStream; |
||
32 | import java.io.IOException; |
||
1907 | grupo6 | 33 | import java.text.DecimalFormat; |
34 | import java.text.NumberFormat; |
||
1906 | grupo6 | 35 | import java.util.List; |
36 | |||
1830 | jmachado | 37 | /** |
38 | * Created by jorgemachado on 31/10/17. |
||
39 | */ |
||
1906 | grupo6 | 40 | public class GlobalLearningResultsChartYearImg extends ImageComponent |
1830 | jmachado | 41 | { |
1906 | grupo6 | 42 | /** |
43 | * |
||
44 | * @param results |
||
45 | * @return the tmp path for generated chart |
||
46 | * @throws IOException |
||
47 | */ |
||
48 | public FileUploaded generateChart2tmp(UnitsLearningResultsTable results, CourseReportDocument courseReportDocument) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
49 | { |
||
50 | List<UnitsLearningResultYear> unitsLearningResultYearList = results.getYears(); |
||
51 | |||
52 | |||
53 | 1.5.0/docs/api/java/lang/String.html">String tempName = BytesUtils.generateHexKey() + ".png"; |
||
54 | |||
55 | DefaultCategoryDataset data = new DefaultCategoryDataset(); |
||
56 | |||
57 | for(UnitsLearningResultYear temp: unitsLearningResultYearList) { |
||
1908 | grupo6 | 58 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double((int)(temp.getAprovados1013Percent())),"Aprovados 10-13",""+temp.ano + "º Ano"); |
59 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double((int)(temp.getAprovados1416Percent())),"Aprovados 14-16",""+temp.ano + "º Ano"); |
||
60 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double((int)(temp.getAprovados1720Percent())),"Aprovados 17-20",""+temp.ano + "º Ano"); |
||
1906 | grupo6 | 61 | } |
62 | |||
1907 | grupo6 | 63 | for(UnitsLearningResultYear temp: unitsLearningResultYearList) { |
1908 | grupo6 | 64 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double((int)(temp.getAprovados1013Percent())),"Aprovados 10-13","2º Ano"); |
65 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double((int)(temp.getAprovados1416Percent())),"Aprovados 14-16","2º Ano"); |
||
66 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double((int)(temp.getAprovados1720Percent())),"Aprovados 17-20","2º Ano"); |
||
1907 | grupo6 | 67 | } |
68 | |||
69 | for(UnitsLearningResultYear temp: unitsLearningResultYearList) { |
||
1908 | grupo6 | 70 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double((int)(temp.getAprovados1013Percent())),"Aprovados 10-13","3º Ano"); |
71 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double((int)(temp.getAprovados1416Percent())),"Aprovados 14-16","3º Ano"); |
||
72 | data.addValue(new 1.5.0/docs/api/java/lang/Double.html">Double(32),"Aprovados 17-20","3º Ano"); |
||
1907 | grupo6 | 73 | } |
74 | |||
75 | |||
76 | |||
1906 | grupo6 | 77 | JFreeChart chart = ChartFactory.createStackedBarChart(null,null,null,data, PlotOrientation.VERTICAL,true,false,false); |
78 | |||
79 | CategoryPlot plot = chart.getCategoryPlot(); |
||
80 | RectangleInsets chartRectangle = new RectangleInsets(0,0,30F,0); |
||
81 | chart.setPadding(chartRectangle); |
||
82 | plot.setBackgroundPaint(1.5.0/docs/api/java/awt/Color.html">Color.WHITE); |
||
83 | CategoryAxis axis = plot.getDomainAxis(); |
||
1907 | grupo6 | 84 | NumberAxis axis2 = (NumberAxis) plot.getRangeAxis(); |
85 | 1.5.0/docs/api/java/text/NumberFormat.html">NumberFormat pctFormat = new 1.5.0/docs/api/java/text/DecimalFormat.html">DecimalFormat("#'%'"); |
||
1906 | grupo6 | 86 | axis.setTickLabelFont(new 1.5.0/docs/api/java/awt/Font.html">Font("Helvetica", 1.5.0/docs/api/java/awt/Font.html">Font.PLAIN, 30)); |
87 | axis2.setTickLabelFont(new 1.5.0/docs/api/java/awt/Font.html">Font("Helvetica", 1.5.0/docs/api/java/awt/Font.html">Font.PLAIN, 30)); |
||
1907 | grupo6 | 88 | axis2.setNumberFormatOverride(pctFormat); |
1906 | grupo6 | 89 | |
1907 | grupo6 | 90 | 1.5.0/docs/api/java/awt/Color.html">Color brown = new 1.5.0/docs/api/java/awt/Color.html">Color(237,125,49); |
91 | 1.5.0/docs/api/java/awt/Color.html">Color yellow = new 1.5.0/docs/api/java/awt/Color.html">Color(255,192,0); |
||
92 | 1.5.0/docs/api/java/awt/Color.html">Color green = new 1.5.0/docs/api/java/awt/Color.html">Color(112,173,71); |
||
93 | |||
94 | StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer(); |
||
95 | renderer.setGradientPaintTransformer(null); |
||
96 | plot.setRangeGridlinePaint(1.5.0/docs/api/java/awt/Color.html">Color.GRAY); |
||
97 | plot.setRangeGridlinesVisible(true); |
||
98 | plot.setRangeGridlineStroke(new 1.5.0/docs/api/java/awt/BasicStroke.html">BasicStroke(1.0f)); |
||
99 | renderer.setSeriesPaint(0, brown); |
||
100 | renderer.setSeriesPaint(1, yellow); |
||
101 | renderer.setSeriesPaint(2, green); |
||
102 | renderer.setMaximumBarWidth(.20); |
||
103 | |||
104 | renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",pctFormat)); |
||
105 | renderer.setBaseItemLabelsVisible(true); |
||
106 | ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.HALF_ASCENT_CENTER); |
||
107 | renderer.setBasePositiveItemLabelPosition(position); |
||
108 | 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,30)); |
||
109 | |||
1906 | grupo6 | 110 | chart.getLegend().setItemFont(new 1.5.0/docs/api/java/awt/Font.html">Font("Helvetica", 1.5.0/docs/api/java/awt/Font.html">Font.PLAIN,30)); |
1907 | grupo6 | 111 | chart.getLegend().setFrame(BlockBorder.NONE); |
1906 | grupo6 | 112 | |
113 | |||
1907 | grupo6 | 114 | |
1906 | grupo6 | 115 | 1.5.0/docs/api/java/awt/image/BufferedImage.html">BufferedImage image = chart.createBufferedImage( 300*4, 200*4); |
116 | 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; |
||
117 | 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); |
||
118 | 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)); |
||
119 | |||
120 | FileUploaded fileUploaded = new FileUploaded(); |
||
121 | fileUploaded.setTmpName(tempName); |
||
122 | fileUploaded.setTitle("Taxa global de aproveitamento"); |
||
123 | fileUploaded.setDescription("Curso: " + courseReportDocument.getCourseName() + " (" + courseReportDocument.getCourseCode()+ ") ano " + courseReportDocument.getYear()); |
||
124 | fileUploaded.setContentType(MimeTypeGuesser.getInstance().guessMimeType(output)); |
||
125 | fileUploaded.setFileSize(output.length()); |
||
126 | fileUploaded.setExtension("png"); |
||
127 | return fileUploaded; |
||
128 | } |
||
129 | |||
130 | 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 { |
||
131 | float aprovados = 10; |
||
132 | float reprovados = 2; |
||
133 | float semElementos = 4; |
||
134 | double aprovadosPercent = ((int)10/16*100.0)/100.0; |
||
135 | double reprovadosPercent = ((int)2/16*100.0)/100.0; |
||
136 | double semElementosPercent = ((int)4/16*100.0)/100.0; |
||
137 | |||
138 | 1.5.0/docs/api/java/lang/String.html">String tempName = BytesUtils.generateHexKey() + ".png"; |
||
139 | |||
140 | DefaultPieDataset data = new DefaultPieDataset(); |
||
141 | if(reprovados > 0) |
||
142 | data.setValue("Reprovados " + reprovadosPercent + "%", new 1.5.0/docs/api/java/lang/Double.html">Double(reprovados)); |
||
143 | if(aprovados > 0) |
||
144 | data.setValue("Aprovados " + aprovadosPercent + "%", new 1.5.0/docs/api/java/lang/Double.html">Double(aprovados)); |
||
145 | if(semElementos > 0) |
||
146 | data.setValue("Sem Elementos " + semElementosPercent + "%", new 1.5.0/docs/api/java/lang/Double.html">Double(semElementos)); |
||
147 | JFreeChart chart = ChartFactory.createRingChart(null, data, false, true, false); |
||
148 | chart.setBackgroundPaint(new 1.5.0/docs/api/java/awt/Color.html">Color(255,255,255)); |
||
149 | PiePlot plot = (RingPlot) chart.getPlot(); |
||
150 | plot.setLabelBackgroundPaint(new 1.5.0/docs/api/java/awt/Color.html">Color(255, 249, 235)); |
||
151 | plot.setLabelPaint(new 1.5.0/docs/api/java/awt/Color.html">Color(88, 88, 90)); |
||
152 | //plot.setDrawingSupplier(new ChartBuilderUtil.ChartDrawingSupplier()); |
||
153 | |||
154 | plot.setSectionPaint(0, new 1.5.0/docs/api/java/awt/Color.html">Color(47, 35, 255)); |
||
155 | plot.setSectionPaint(1, new 1.5.0/docs/api/java/awt/Color.html">Color(15, 218, 10)); |
||
156 | plot.setSectionPaint(2, new 1.5.0/docs/api/java/awt/Color.html">Color(200, 200, 200)); |
||
157 | plot.setBackgroundPaint(new 1.5.0/docs/api/java/awt/Color.html">Color(255, 255, 255)); |
||
158 | plot.setOutlineVisible(false); |
||
159 | |||
160 | |||
161 | |||
162 | plot.setLabelFont(new 1.5.0/docs/api/java/awt/Font.html">Font( |
||
163 | "Helvetica", 1.5.0/docs/api/java/awt/Font.html">Font.PLAIN, 30)); |
||
164 | |||
165 | |||
166 | 1.5.0/docs/api/java/awt/image/BufferedImage.html">BufferedImage image = chart.createBufferedImage( 300*4, 200*4); |
||
167 | 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; |
||
168 | 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); |
||
169 | 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)); |
||
170 | |||
171 | |||
172 | } |
||
1830 | jmachado | 173 | } |