Chart correct size

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
luca
Posts: 2
Joined: Wed Oct 12, 2011 11:40 am
antibot: No, of course not.

Chart correct size

Post by luca » Wed Oct 12, 2011 11:48 am

Hi,

I'm able to create a graph using JFreeChart, but I need to save it to an image as smallest as possible.
How can I know what are the minimla width and height?
Or how can I know the correct width/height ratio?

Thank you
Luca

merlushatrash
Posts: 19
Joined: Mon Sep 26, 2011 4:48 pm
antibot: No, of course not.

Re: Chart correct size

Post by merlushatrash » Wed Oct 12, 2011 5:04 pm

Looking at source code of ChartPanel one can find answer:
public void doSaveAs() throws IOException {
2106:
2107: JFileChooser fileChooser = new JFileChooser();
2108: ExtensionFileFilter filter = new ExtensionFileFilter(
2109: localizationResources.getString("PNG_Image_Files"), ".png");
2110: fileChooser.addChoosableFileFilter(filter);
2111:
2112: int option = fileChooser.showSaveDialog(this);
2113: if (option == JFileChooser.APPROVE_OPTION) {
2114: String filename = fileChooser.getSelectedFile().getPath();
2115: if (isEnforceFileExtensions()) {
2116: if (!filename.endsWith(".png")) {
2117: filename = filename + ".png";
2118: }
2119: }
2120: ChartUtilities.saveChartAsPNG(new File(filename), this.chart,
2121: getWidth(), getHeight());
2122: }
2123:
2124: }
So saving is made by :
ChartUtilities.saveChartAsPNG(new File(filename), this.chart, getWidth(), getHeight());
and getWidth() and getHeight() are getting from superclass for ChartPanel - JPanel.
You can try to devide getWidth and getHeight by some ratio ,play with it and check what is required format for you.
Good idea will be to crease png files in cycle. Than,looking at result ,you can se what is good for you.

luca
Posts: 2
Joined: Wed Oct 12, 2011 11:40 am
antibot: No, of course not.

Re: Chart correct size

Post by luca » Thu Oct 13, 2011 8:30 am

Thank you, but maybe your answer is not good for me.
ChartPanel is a subclass of JPanel, I forgot to say that I'm not in a swing context: I'm using JFreeChart in a servlet.

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

Re: Chart correct size

Post by david.gilbert » Fri Oct 14, 2011 6:03 am

It was an early design decision that JFreeChart should not try to determine the "best" size for a chart, but instead would draw charts at whatever size is requested by the caller. In Swing applications, the caller is very often the ChartPanel, and the size will depend on how the user has resized the UI, the Swing layout manager etc - all things out of the control of JFreeChart.

In a web application, the caller will be your own code - so you need to make your own choice about the size of the charts. It would be worth inspecting the scaling code in the ChartPanel class, because in my experience for very small charts you get nicer results when the scaling is applied.
David Gilbert
JFreeChart Project Leader

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

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Chart correct size

Post by paradoxoff » Fri Oct 14, 2011 6:26 pm

Most parts of a JFreeChart have in fact a natural size, i.e. the axes, TextTitles, LegendTitle, and so on. This size is dependent on a large number of settings: font, insets, shape sizes, ....
The only thing that does not have a natural size is the data area. As a result, the data area can get squeezed when the a small overall chart area is combined with lots of axes or large titles. To handle these situations better, I have devlopped a patch 3 years ago.
This patch also allows to once set a preferred size for a chart that is stored in the chart itself. I have found that to be very useful if you not only want to show charts in a gui but also export them as a bitmap and want to get a consistent result between the gui and the bitmap.
The preferred size of the chart can also be used as a preferred size for the ChartPanel which should be recognized by at least some layout managers

Locked