package gradebook;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.io.FileOutputStream;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import org.jfree.data.general.DefaultPieDataset;
public class Charts{
/** Creates a new instance of Charts */
public static JFreeChart generateBarChart() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(75, "Average", "Grade One");
dataset.setValue(50, "Average", "Grade Two");
dataset.setValue(50, "Average", "Grade Three");
dataset.setValue(50, "Average", "Grade Four");
dataset.setValue(50, "Average", "Grade Five");
JFreeChart chart = ChartFactory.createBarChart(
"Grade Averages", "Grades", "Percent Average",
dataset, PlotOrientation.VERTICAL, false, true, false);
return chart;
}
public static void chartWriter(){
String desktopPath = System.getProperty("user.home") + "//Desktop//barchart.pdf";
writeChartToPDF(generateBarChart(), 700, 400, desktopPath);
}
public static void writeChartToPDF(JFreeChart chart, int width, int height, String filename){
PdfWriter writer = null;
Document document = new Document();
try {
writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
PdfContentByte contentByte = writer.getDirectContent();
PdfTemplate template = contentByte.createTemplate(width, height);
Graphics2D graphics2D = template.createGraphics(width, height, new DefaultFontMapper());
Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, width, height);
chart.draw(graphics2D, rectangle2D);
graphics2D.dispose();
contentByte.addTemplate(template, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
document.close();
JOptionPane.showMessageDialog(null, "A PDF file has been created.");
}
}