Higher Quality of JFreeChart charts
Asked Answered
N

3

6

I want to embed a PNG of a chart which I created with JFreeChart into a PDF document. The problem here is, that the quality of the chart is very poor. My goal is to have PNGs with 300 DPI. I googled for almost an hour now but I could not find a solution.

Is there a way to export the generated charts from JFreeCharts as a PNG (or a JPEG) with 300 DPI?

Noheminoil answered 3/10, 2011 at 17:39 Comment(4)
how do you export the charts?Beveridge
@Beveridge at the moment with ChartUtilities.saveChartAsPNG(new File("/Users/myuser/test.png"),demo.getChart(),1000,1000); But of course I am open to any suggestion which works with my requirements :)Noheminoil
Just save it at a bigger size? 1000x1000 should give you 300dpi for a 3in x 3 in image.Giulia
Saving with more pixels results in smaller fonts. So it's not a good solution.Tandy
T
1

Since version 1.4, JFreeChart uses the png encoder provided by ImageIO. As an alternative , you might look at com.keypoint.PngEncoder, which includes setDpi(), et al. See also org.jfree.chart.encoders.SunPNGEncoderAdapter and org.jfree.chart.encoders.KeypointPNGEncoderAdapter.

Thorma answered 5/10, 2011 at 4:29 Comment(0)
K
2

Also take a look at org.jfree.chart.ChartUtilities#writeScaledChartAsPNG

If you provide a scale factor of 2, your chart will be rendered at twice the resolution.

Kerikeriann answered 26/12, 2012 at 9:49 Comment(1)
Using a scale factor of 2 reduces the font size by 2. So it's not a complete solution.Tandy
T
1

Since version 1.4, JFreeChart uses the png encoder provided by ImageIO. As an alternative , you might look at com.keypoint.PngEncoder, which includes setDpi(), et al. See also org.jfree.chart.encoders.SunPNGEncoderAdapter and org.jfree.chart.encoders.KeypointPNGEncoderAdapter.

Thorma answered 5/10, 2011 at 4:29 Comment(0)
E
0

You could use XChart for this, as exporting Charts in high-res at a certain DPI is a new feature since version 2.2.0. The code would look something like this:

double[] xData = new double[] { 0.0, 1.0, 2.0 };
double[] yData = new double[] { 2.0, 1.0, 0.0 };

// Create Chart
Chart chart = QuickChart.getChart("Sample Chart", "X", "Y", "y(x)", xData, yData);

// Show it
new SwingWrapper(chart).displayChart();

// Save it
BitmapEncoder.savePNG(chart, "./Sample_Chart.png"); // default 72 DPI
BitmapEncoder.savePNGWithDPI(chart, "./Sample_Chart_300_DPI.png", 300);

Disclaimer: I'm the lead developer of XChart. I recently faced the exact same problem as you. I needed charts at 300 DPI for a scientific publication. Feedback is welcome!

Eversole answered 26/5, 2013 at 16:21 Comment(1)
Thanks herrtim, XCharts looks awesome. I will definitely consider using it for my next projects.Noheminoil

© 2022 - 2024 — McMap. All rights reserved.