package pt.estgp.estgweb.services.courses.coursereport.documentmodel.learningresults.components;
import jomm.utils.BytesUtils;
import jomm.utils.MimeTypeGuesser;
import org.apache.log4j.Category;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.RingPlot;
import org.jfree.data.category.DefaultCategoryDataset;
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";
DefaultCategoryDataset data =
new DefaultCategoryDataset
();
if(reprovados
> 0
)
data.
setValue(new 1.5.0/docs/api/java/lang/Double.html">Double(reprovados
),
"Reprovados " + reprovadosPercent +
"%",
"Reprovados " + reprovadosPercent +
"%" );
if(aprovados
> 0
)
data.
setValue(new 1.5.0/docs/api/java/lang/Double.html">Double(aprovados
),
"Aprovados " + aprovadosPercent +
"%",
"Aprovados " + aprovadosPercent +
"%");
if(semElementos
> 0
)
data.
setValue(new 1.5.0/docs/api/java/lang/Double.html">Double(semElementos
),
"Sem Elementos " + semElementosPercent +
"%",
"Sem Elementos " + semElementosPercent +
"%");
//Grafico Barras
JFreeChart chart = ChartFactory.
createBarChart(null,
null,
null,data, PlotOrientation.
VERTICAL,
true,
true,
false);
chart.
setBackgroundPaint(new 1.5.0/docs/api/java/awt/Color.html">Color(255,255,255
));
CategoryPlot plot = chart.
getCategoryPlot();
plot.
setRangeGridlinePaint(1.5.0/docs/api/java/awt/Color.html">Color.
black);
//plot.setDrawingSupplier(new ChartBuilderUtil.ChartDrawingSupplier());
/*
plot.setSectionPaint(0, new Color(255, 0, 0));
plot.setSectionPaint(1, new Color(15, 218, 10));
plot.setSectionPaint(2, new Color(200, 200, 200));
plot.setBackgroundPaint(new Color(255, 255, 255));
plot.setOutlineVisible(false);
plot.setLabelFont(new Font(
"Helvetica", Font.PLAIN, 30));
*/
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, 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";
DefaultCategoryDataset data =
new DefaultCategoryDataset
();
if(reprovados
> 0
)
data.
setValue(new 1.5.0/docs/api/java/lang/Double.html">Double(reprovados
),
"Reprovados " + reprovadosPercent +
"%",
"Reprovados2 " + reprovadosPercent +
"%" );
if(aprovados
> 0
)
data.
setValue(new 1.5.0/docs/api/java/lang/Double.html">Double(aprovados
),
"Aprovados " + aprovadosPercent +
"%",
"Aprovados2 " + aprovadosPercent +
"%");
if(semElementos
> 0
)
data.
setValue(new 1.5.0/docs/api/java/lang/Double.html">Double(semElementos
),
"Sem Elementos " + semElementosPercent +
"%",
"Sem Elementos2 " + semElementosPercent +
"%");
//Grafico Barras
JFreeChart chart = ChartFactory.
createBarChart(null,
null,
null,data, PlotOrientation.
VERTICAL,
true,
true,
false);
chart.
setBackgroundPaint(new 1.5.0/docs/api/java/awt/Color.html">Color(255,255,255
));
CategoryPlot plot = chart.
getCategoryPlot();
plot.
setRangeGridlinePaint(1.5.0/docs/api/java/awt/Color.html">Color.
black);
ChartFrame chatFrm =
new ChartFrame
("APROVADOS",chart,
true);
chatFrm.
setVisible(true);
chatFrm.
setSize(500,400
);
/*
plot.setLabelBackgroundPaint(new Color(255, 249, 235));
plot.setLabelPaint(new Color(88, 88, 90));
//plot.setDrawingSupplier(new ChartBuilderUtil.ChartDrawingSupplier());
plot.setSectionPaint(0, new Color(255, 0, 0));
plot.setSectionPaint(1, new Color(15, 218, 10));
plot.setSectionPaint(2, new Color(200, 200, 200));
plot.setBackgroundPaint(new Color(255, 255, 255));
plot.setOutlineVisible(false);
plot.setLabelFont(new Font(
"Helvetica", 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
));
}
}