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
write chart with default zoom
-
- 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
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
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
Re: write chart with default zoom
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().
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().
Re: write chart with default zoom
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.