Incanter charts can be saved as PNG files using the save
function, but I had a request earlier today to add the ability to save them as PDF documents.
So I’ve created a new function called save-pdf
in a new package called incanter.pdf
.
Here’s a basic example.
(use '(incanter core charts pdf)) (save-pdf (function-plot sin -4 4) "./pdf-chart.pdf")
Which outputs the following PDF file.
Man it was fast! Thanks, am gonna try it, and even add to the paper in the works!
Hi David,
This is good work. The generated PDF looks good, and I see you were able to save it as a true vector graphic, not a raster image, so it scales up and down nicely. Another great feature for Incanter.
Nice capability. I haven’t run the code, but it looks like there may be some missing characters from the example. Shouldn’t it be something like:
(use ‘(incanter core charts pdf)) ?
Fixed, thanks for catching that Steve!
The package isn’t available through lein / clojars.org yet, if I’m not mistaken? could you add it so it can be automatically installed by people using leiningen?
I’m afraid Incanter’s modular build structure isn’t supported in the Clojars repository. We are working on getting it deployed in Sonatype’s public repository (http://oss.sonatype.org/).
You can use Leiningen to build projects with Incanter as a dependency right now though, you just need to install it with Maven first (mvn install), then include the following dependency:
[org.incanter/incanter-full “1.2.0-SNAPSHOT”]
David
Hi,
I tried to save a chart to PDF, and the resulted chart seemed to be cropped in order to fit in the default width and height. Is there any way to scale the chart to fit in the output dimension instead of cropping?
This also happened when saving a chart to PNG.
Thank you,
– Cox
That’s very strange, I have never seen the cropping behavior, it normally scales by default. Try using the :width and :height options to save and save-pdf and see if that resolves the issue.
David
Hi, David,
Yes, it’s very strange. I haven’t change anything but now it works for me. Sorry for the false alarm.
I have another question now. I tried to add pointers to it chart and save it. But the pointers are all gone in the saved chart. Can you help me on this?
Hi,
Thanks for the great tool!
I wonder if it would be more convenient for save to infer output format out of file extension.
i.e.
generates pdf and(save (function-plot sin -4 4) "./pdf-chart.pdf"))
generates png(save (function-plot sin -4 4) "./png-chart.png"))
I’ve probably seen this approach implemented in some ruby lib (don’t remember exactly which).