package pt.estgp.estgweb.services.courses.coursereport.documentmodel.learningresults.components;
import jomm.utils.BytesUtils;
import jomm.utils.MimeTypeGuesser;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.RingPlot;
import org.jfree.data.general.DefaultPieDataset;
import pt.estgp.estgweb.services.courses.coursereport.documentmodel.CourseReportDocument;
import pt.estgp.estgweb.utils.Globals;
import pt.estgp.estgweb.utils.documentBuilder.ImageComponent;
import pt.estgp.estgweb.web.controllers.utils.FileUploaded;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by jorgemachado on 31/10/17.
*/
public class GlobalLearningResultsChartImg
extends ImageComponent
{
/**
*
* @param results
* @return the tmp path for generated chart
* @throws IOException
*/
public FileUploaded generateChart2tmp
(UnitsLearningResultsTable results,CourseReportDocument courseReportDocument
) throws 1.5.0/docs/api/java/io/IOException.html">IOException
{
float aprovados = results.
getAprovados();
float reprovados = results.
getReprovados();
float semElementos = results.
getSemElementos();
double aprovadosPercent =
((int)(results.
getAprovadosPercent()*100.0
))/
100.0;
double reprovadosPercent =
((int)(results.
getReprovadosPercent()*100.0
))/
100.0;
double semElementosPercent =
((int)(results.
getSemElementosPercent()*100.0
))/
100.0;
1.5.0/docs/api/java/lang/String.html">String tempName = BytesUtils.
generateHexKey() +
".png";
DefaultPieDataset data =
new DefaultPieDataset
();
if(reprovados
> 0)
data.
setValue("Reprovados " + reprovadosPercent +
"%",
new 1.5.0/docs/api/java/lang/Double.html">Double(reprovados
));
if(aprovados
> 0)
data.
setValue("Aprovados " + aprovadosPercent +
"%",
new 1.5.0/docs/api/java/lang/Double.html">Double(aprovados
));
if(semElementos
> 0)
data.
setValue("Sem Elementos " + semElementosPercent +
"%",
new 1.5.0/docs/api/java/lang/Double.html">Double(semElementos
));
JFreeChart chart = ChartFactory.
createRingChart(null, data,
false,
true,
false);
chart.
setBackgroundPaint(new 1.5.0/docs/api/java/awt/Color.html">Color(255,255,255
));
PiePlot plot =
(RingPlot
) chart.
getPlot();
plot.
setLabelBackgroundPaint(new 1.5.0/docs/api/java/awt/Color.html">Color(255, 249, 235
));
plot.
setLabelPaint(new 1.5.0/docs/api/java/awt/Color.html">Color(88, 88, 90
));
//plot.setDrawingSupplier(new ChartBuilderUtil.ChartDrawingSupplier());
plot.
setSectionPaint(0,
new 1.5.0/docs/api/java/awt/Color.html">Color(255, 0, 0
));
plot.
setSectionPaint(1,
new 1.5.0/docs/api/java/awt/Color.html">Color(15, 218, 10
));
plot.
setSectionPaint(2,
new 1.5.0/docs/api/java/awt/Color.html">Color(200, 200, 200
));
plot.
setBackgroundPaint(new 1.5.0/docs/api/java/awt/Color.html">Color(255, 255, 255
));
plot.
setOutlineVisible(false);
plot.
setLabelFont(new 1.5.0/docs/api/java/awt/Font.html">Font(
"Helvetica",
1.5.0/docs/api/java/awt/Font.html">Font.
PLAIN, 30
));
1.5.0/docs/api/java/awt/image/BufferedImage.html">BufferedImage image = chart.
createBufferedImage( 300
*4, 200
*4
);
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
;
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
);
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
));
FileUploaded fileUploaded =
new FileUploaded
();
fileUploaded.
setTmpName(tempName
);
fileUploaded.
setTitle("Taxa global de aproveitamento");
fileUploaded.
setDescription("Curso: " + courseReportDocument.
getCourseName() +
" (" + courseReportDocument.
getCourseCode()+
") ano " + courseReportDocument.
getYear());
fileUploaded.
setContentType(MimeTypeGuesser.
getInstance().
guessMimeType(output
));
fileUploaded.
setFileSize(output.
length());
fileUploaded.
setExtension("png");
return fileUploaded
;
}
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 {
float aprovados =
10;
float reprovados =
2;
float semElementos =
4;
double aprovadosPercent =
((int)10/16
*100.0
)/
100.0;
double reprovadosPercent =
((int)2/16
*100.0
)/
100.0;
double semElementosPercent =
((int)4/16
*100.0
)/
100.0;
1.5.0/docs/api/java/lang/String.html">String tempName = BytesUtils.
generateHexKey() +
".png";
DefaultPieDataset data =
new DefaultPieDataset
();
if(reprovados
> 0)
data.
setValue("Reprovados " + reprovadosPercent +
"%",
new 1.5.0/docs/api/java/lang/Double.html">Double(reprovados
));
if(aprovados
> 0)
data.
setValue("Aprovados " + aprovadosPercent +
"%",
new 1.5.0/docs/api/java/lang/Double.html">Double(aprovados
));
if(semElementos
> 0)
data.
setValue("Sem Elementos " + semElementosPercent +
"%",
new 1.5.0/docs/api/java/lang/Double.html">Double(semElementos
));
JFreeChart chart = ChartFactory.
createRingChart(null, data,
false,
true,
false);
chart.
setBackgroundPaint(new 1.5.0/docs/api/java/awt/Color.html">Color(255,255,255
));
PiePlot plot =
(RingPlot
) chart.
getPlot();
plot.
setLabelBackgroundPaint(new 1.5.0/docs/api/java/awt/Color.html">Color(255, 249, 235
));
plot.
setLabelPaint(new 1.5.0/docs/api/java/awt/Color.html">Color(88, 88, 90
));
//plot.setDrawingSupplier(new ChartBuilderUtil.ChartDrawingSupplier());
plot.
setSectionPaint(0,
new 1.5.0/docs/api/java/awt/Color.html">Color(255, 0, 0
));
plot.
setSectionPaint(1,
new 1.5.0/docs/api/java/awt/Color.html">Color(15, 218, 10
));
plot.
setSectionPaint(2,
new 1.5.0/docs/api/java/awt/Color.html">Color(200, 200, 200
));
plot.
setBackgroundPaint(new 1.5.0/docs/api/java/awt/Color.html">Color(255, 255, 255
));
plot.
setOutlineVisible(false);
plot.
setLabelFont(new 1.5.0/docs/api/java/awt/Font.html">Font(
"Helvetica",
1.5.0/docs/api/java/awt/Font.html">Font.
PLAIN, 30
));
1.5.0/docs/api/java/awt/image/BufferedImage.html">BufferedImage image = chart.
createBufferedImage( 300
*4, 200
*4
);
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
;
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
);
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
));
}
}