Subversion Repositories bacoAlunos

Rev

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