write chart with default zoom

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
tchart
Posts: 3
Joined: Thu Oct 02, 2014 3:21 pm
antibot: No, of course not.

write chart with default zoom

Post by tchart » Thu Oct 02, 2014 3:33 pm

Hey there,

Problem:
I have a JFreeChart in a ChartPanel with some diagram. The user can save that chart as a PDF document (writing it as a PNG via ChartUtilities.writeChartAsPNG()). On the user interface the user can zoom in and zoom out the chart but I want the chart in the PDF to always have the default zoom (auto bounds).
The chart on the user interface should keep the customized zoom when exporting the chart.

My attempts:
Now, I can reset the bounds via restoreAutoBounds() but then of course the chart on the UI is reset. I did not found a way to clone / copy the ChartPanel, then reset the bounds of the copy and write it as PNG.
Has anyone any idea how to achieve my goal? Any help welcome!

Regards,
tchart

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: write chart with default zoom

Post by david.gilbert » Wed Oct 08, 2014 8:14 pm

You ought to be able to clone the chart rather than the ChartPanel. Then you can modify the axis bounds on the cloned chart, then call the chart's draw() method passing in a PDFGraphics2D instance (from Orson PDF, iText or FreeHEP, whichever you prefer). The original chart that is displayed in your UI will remain as is.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

tchart
Posts: 3
Joined: Thu Oct 02, 2014 3:21 pm
antibot: No, of course not.

Re: write chart with default zoom

Post by tchart » Thu Oct 09, 2014 9:59 am

This isn't quite working. :(

First of all, I get a NPE when invoking clone() on my JFreeChart because the DateFormat's calendar is null:
java.lang.NullPointerException
at java.text.DateFormat.clone(DateFormat.java:723)
at org.jfree.chart.axis.DateAxis.clone(DateAxis.java:1950)
at org.jfree.chart.plot.XYPlot.clone(XYPlot.java:5587)
at org.jfree.chart.JFreeChart.clone(JFreeChart.java:1732)

Secondly, I don't know how to reset the bounds on the JFreeChart. I only found a method on the ChartPanel: restoreAutoBounds().

tchart
Posts: 3
Joined: Thu Oct 02, 2014 3:21 pm
antibot: No, of course not.

Re: write chart with default zoom

Post by tchart » Fri Oct 24, 2014 4:05 pm

By the way, I solved the issue by simply creating the ChartPanel twice. One for the GUI and one (that the user cannot modify) for the PDF.

Locked