Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

saving multiple charts to a single pdf #285

Closed
macrinalobo opened this issue Oct 24, 2018 · 8 comments
Closed

saving multiple charts to a single pdf #285

macrinalobo opened this issue Oct 24, 2018 · 8 comments

Comments

@macrinalobo
Copy link

Is it possible to save
ArrayList(numCharts) to a single pdf (or even jpeg) ? Maybe this could be added or could someone point me in the direction of how this could be done ? Thanks a lot.

@timmolter
Copy link
Member

It exists for bitmaps (BitmapEncoder.java), but not yet PDFs ( VectorGraphicsEncoder.java).

@Mr14huashao
Copy link
Collaborator

// Found that this method is very slow to execute
VectorGraphicsEncoder.saveVectorGraphic(chart, "./Sample_Chart", VectorGraphicsFormat.PDF);

Add dependency

<dependency>
  <groupId>com.lowagie</groupId>
  <artifactId>itext</artifactId>
  <version>4.2.1</version>
</dependency>
public static void testCreatePdf() {
    List<Chart> charts = new ArrayList<>();
    charts.add(new AreaChart01().getChart());
    charts.add(new BarChart01().getChart());
    charts.add(new LineChart01().getChart());
    charts.add(new PieChart01().getChart());
    charts.add(new OHLCChart02().getChart());
    Rectangle pageSize = new Rectangle(800, 600);
    createPdf(charts, "/root/xchart/chats", pageSize);
  }

  public static void createPdf(List<Chart> charts, String fileName, Rectangle pageSize) {
    Document document = new Document(pageSize);
    OutputStream os = null;
    try {
      os = new FileOutputStream(fileName);
      PdfWriter writer = PdfWriter.getInstance(document, os);
      document.open();
      PdfContentByte pb = writer.getDirectContent();
      Graphics2D g2 = null;
      Chart chart = null;
      for (int i = 0; i < charts.size(); i++) {
        g2 = new PdfGraphics2D(pb, pageSize.getWidth(), pageSize.getHeight());
        chart = charts.get(i);
        chart.paint(g2, chart.getWidth(), chart.getHeight());
        g2.dispose();
        if (i < charts.size() - 1) {
          document.newPage();
        }
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (DocumentException e) {
      e.printStackTrace();
    } finally {
      document.close();
      if (os != null) {
        try {
          os.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }

The speed is several times faster than the above, the effect is as follows:
Screenshot_2019-11-16_16-53-19

@Mr14huashao
Copy link
Collaborator

Mr14huashao commented Nov 28, 2019

@timmolter
I have mentioned the issue on vectorgraphics2d, link: eseifert/vectorgraphics2d#73,vectorgraphics2d does not support importing multiple Charts into one PDF file, and the export of PDF files is slow. , Will add new dependencies to export PDF files, I think itext is very good, above I have submitted the test code.
I hope you take the time to see it.

@timmolter
Copy link
Member

iText looks good and I'd switch to it if it's faster, but the license is more restricted than the Apache License. Maybe there's a newer lib for SVG and PDF that's MIT or Apache License???

@Mr14huashao
Copy link
Collaborator

Mr14huashao commented Dec 2, 2019

@timmolter
itext: AGPL license requirements are very strict and unfriendly
pdfbox-graphics2d: Apache License Version 2.0, supports saving pdf files, not saving EPS, SVG

pdfbox-graphics2d saves a single chart as a pdf file, a hundred times faster than erichseifert.vectorgraphics2d, and pdfbox-graphics2d can save multiple charts into a pdf file

Can I replace erichseifert.vectorgraphics2d with pdfbox-graphics2d to save pdf files?
If you think so, I can mention a PR.

@Mr14huashao
Copy link
Collaborator

@timmolter
Is it possible to add pdfbox-graphics2d dependency?

GitHub address: https://github.com/rototor/pdfbox-graphics2d

@timmolter
Copy link
Member

yes, please replace the erichseifert PDF stuff with the pdfbox-graphics2d lib. Thanks!

@Mr14huashao
Copy link
Collaborator

Ok

Mr14huashao added a commit to Mr14huashao/XChart that referenced this issue Dec 16, 2019
Mr14huashao added a commit to Mr14huashao/XChart that referenced this issue Dec 23, 2019
timmolter added a commit that referenced this issue Dec 25, 2019
[resolves #285] saving multiple charts to a single pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants