Last active
February 25, 2016 22:56
-
-
Save Redchards/2ed5902b1800cc66e82b to your computer and use it in GitHub Desktop.
Exercise correction for my project group
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.chartgen; | |
| import java.awt.Graphics2D; | |
| import java.io.ByteArrayOutputStream; | |
| import java.io.File; | |
| import java.io.FileOutputStream; | |
| import java.io.IOException; | |
| //import java.util.Arrays; | |
| import java.util.List; | |
| import javax.imageio.ImageIO; | |
| import org.jfree.chart.ChartFactory; | |
| import org.jfree.chart.ChartFrame; | |
| import org.jfree.chart.JFreeChart; | |
| import org.jfree.chart.LegendItem; | |
| import org.jfree.chart.plot.PiePlot; | |
| import org.jfree.chart.title.LegendTitle; | |
| import org.jfree.chart.title.TextTitle; | |
| import org.jfree.chart.title.Title; | |
| import org.jfree.data.category.CategoryDataset; | |
| import org.jfree.data.general.DefaultPieDataset; | |
| import com.itextpdf.text.BadElementException; | |
| import com.itextpdf.text.Document; | |
| import com.itextpdf.text.DocumentException; | |
| import com.itextpdf.text.Image; | |
| import java.awt.Font; | |
| import com.itextpdf.text.PageSize; | |
| import com.itextpdf.text.pdf.PdfContentByte; | |
| import com.itextpdf.text.pdf.PdfPCell; | |
| import com.itextpdf.text.pdf.PdfPTable; | |
| import com.itextpdf.awt.PdfGraphics2D; | |
| import com.itextpdf.awt.geom.Dimension; | |
| import java.awt.geom.Rectangle2D; | |
| import java.awt.image.BufferedImage; | |
| import com.itextpdf.text.pdf.PdfTemplate; | |
| import com.itextpdf.text.pdf.PdfWriter; | |
| public class Main { | |
| public static void main(String[] args) throws IOException { | |
| // TODO Auto-generated method stub | |
| /*ChartQuery quer = null; | |
| try { | |
| quer = new ChartQuery(Arrays.asList(new String[]{"Type2", "MonTitre", "5", "Right", "Name", "Salary"})); | |
| CSVParser parser = CSVParser.parse(new File("resources/Salaries.csv"), StandardCharsets.UTF_8, CSVFormat.RFC4180); | |
| for(CSVRecord parsed : parser) { | |
| System.out.println(parsed.toString()); | |
| } | |
| List<ChartQuery> queryList = QueryListBuilder.buildFromFile("resources/QueryTst.csv"); | |
| for(ChartQuery query : queryList) { | |
| System.out.println(query.toString()); | |
| } | |
| } | |
| catch(QueryCreationException e) { | |
| System.out.println(e.getMessage()); | |
| } | |
| System.out.println(quer.toString()); | |
| System.out.println("Print command line : "); | |
| for(String c : args) { | |
| System.out.println(c); | |
| }*/ | |
| CommandLineParser cmd = null; | |
| try { | |
| cmd = new CommandLineParser(args); | |
| if(cmd.isValid()) { | |
| List<ChartQuery> l = QueryListBuilder.buildFromFile(cmd.getQueryFile()); | |
| for(ChartQuery query : l) { | |
| System.out.println(query); | |
| } | |
| } | |
| } catch (InvalidCSVFormatException | BadCommandLineArgumentException e) { | |
| // TODO Auto-generated catch block | |
| System.out.println(e.getMessage()); | |
| } | |
| /* Create the document object. | |
| * At this point, no real file was created. | |
| */ | |
| Document doc = new Document(); | |
| //http://developers.itextpdf.com/examples/itext-action-second-edition/chapter-14#515-directorcharts.java | |
| //http://www.jfree.org/phpBB2/viewtopic.php?p=71791 | |
| //http://developers.itextpdf.com/examples/tables/cell-and-table-widths#1428-fullpagetable.java | |
| //http://developers.itextpdf.com/question/how-resize-image-fit-it-pdfpcell | |
| //http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfPTable.html | |
| //http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfPCell.html | |
| /* Now we need to create the writer to be able to output our | |
| * our document to the pdf format | |
| */ | |
| PdfWriter writer = null; | |
| try { | |
| writer = PdfWriter.getInstance(doc, new FileOutputStream("MyPDF.pdf")); | |
| doc.open(); | |
| } catch (DocumentException e) { | |
| // We need to handle the case where the document creation failed. | |
| e.printStackTrace(); | |
| } | |
| PdfContentByte rawData = writer.getDirectContent(); | |
| JFreeChart pieChart1 = null; | |
| DefaultPieDataset dataset = new DefaultPieDataset(); | |
| dataset.setValue("Multimedia", 1); | |
| dataset.setValue("Loisir", 1); | |
| dataset.setValue("Alimentation", 1); | |
| pieChart1 = ChartFactory.createPieChart(null, dataset); | |
| LegendTitle legend = (LegendTitle)pieChart1.getLegend(); | |
| legend.setItemFont(new Font("SansSerif", java.awt.Font.BOLD, 18)); | |
| pieChart1.removeLegend(); | |
| pieChart1.addLegend(legend); | |
| PiePlot plot = (PiePlot) pieChart1.getPlot(); | |
| plot.setLabelGenerator(null); | |
| pieChart1.setTitle(new TextTitle("Avril type de dépenses", new Font("SansSerif", java.awt.Font.BOLD, 24))); | |
| BufferedImage img1 = pieChart1.createBufferedImage(35*24, 35*32); | |
| ByteArrayOutputStream basImg1 = new ByteArrayOutputStream(); | |
| ImageIO.write(img1, "png", basImg1); | |
| byte[] rawImg1 = basImg1.toByteArray(); | |
| File out = new File("chart.png"); | |
| ImageIO.write(img1, "png", out); | |
| //pieChart1.setLabelFont() | |
| float pieChart1Width = (float)35*6; | |
| float pieChart1Height = (float)35*4; | |
| //pieChart1.setPreferredSize(new Dimension(pieChart1Width, pieChart1Height)); | |
| /*PdfTemplate pieChart1Template = rawData.createTemplate(pieChart1Width, pieChart1Height); | |
| Graphics2D g2d1 = new PdfGraphics2D(pieChart1Template, pieChart1Width, pieChart1Height); | |
| Rectangle2D r2d1 = new Rectangle2D.Double(0, 0, pieChart1Width, pieChart1Height); | |
| pieChart1.draw(g2d1, r2d1); | |
| g2d1.dispose(); | |
| rawData.addTemplate(pieChart1Template, 250, 250);*/ | |
| PdfPTable table = new PdfPTable(4); | |
| table.setWidthPercentage(100); | |
| table.setSpacingBefore(0f); | |
| table.setSpacingAfter(0f); | |
| PdfContentByte cb = writer.getDirectContent(); | |
| PdfPCell cell = null; | |
| //doc.add(new Chunk("")); | |
| try { | |
| cell = new PdfPCell(Image.getInstance(cb, img1, 1), true); | |
| } catch (BadElementException e1) { | |
| // TODO Auto-generated catch block | |
| e1.printStackTrace(); | |
| } | |
| try { | |
| //cell.setFixedHeight(35*4); | |
| table.addCell(Image.getInstance(cb, img1, 1)); | |
| table.addCell(Image.getInstance(cb, img1, 1)); | |
| table.addCell(Image.getInstance(cb, img1, 1)); | |
| table.completeRow(); | |
| System.out.println(table.getRows().size()); | |
| } catch (BadElementException e1) { | |
| // TODO Auto-generated catch block | |
| e1.printStackTrace(); | |
| } | |
| try { | |
| doc.add(table); | |
| } catch (DocumentException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| doc.close(); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment