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