Howto: Fixed height for the chart area?
Howto: Fixed height for the chart area?
Hello everybody,
Im currently trying to combine and put together several bar charts on a web page, each bar chart being in its own image file. The height of the chart area (the limited area wher the bars are plotted) has to be the same in all the graphs, since the charts to be created are closely related to each other and my goal is to position them in a horisontal row one after another. Additionally, the bar item labels on the domain axis are positioned vertically and have different widths, a note that is worth taking into account when trying to understand my problem.
The chart area height seems primarily to depend on two factors, the calculated height of the domain axis labels as well as the total height in pixels declared when creating the image (when calling for example the method saveChartAsPNG) Therefore, if the height of the image is declared 500 pixels and the height of the highest label is calculated to be 40 pixels, the height of the chart area is 460 pixels. This is not really true, since for example the borders and margins affect the result of the calculations but in my case their corresponding values all the same in all the graphs. Only the label strings differ greatly.
....and now comes the problem:The calculation explained above is done by the JFreeChart library and I dont know how to control it by myself. As the domain axis labels can be of different heights, the chart height is going to vary from graph to graph. How to declare a thrustworthy fixed percentual value for the height of either the area of the labels or the area designated for the chart itself? In this way, the chart area dimensions would be the same in all the generated graphs.
In the case I did not explain myself clearly enough, please ask me for further details.
Regards,
Thomas
Im currently trying to combine and put together several bar charts on a web page, each bar chart being in its own image file. The height of the chart area (the limited area wher the bars are plotted) has to be the same in all the graphs, since the charts to be created are closely related to each other and my goal is to position them in a horisontal row one after another. Additionally, the bar item labels on the domain axis are positioned vertically and have different widths, a note that is worth taking into account when trying to understand my problem.
The chart area height seems primarily to depend on two factors, the calculated height of the domain axis labels as well as the total height in pixels declared when creating the image (when calling for example the method saveChartAsPNG) Therefore, if the height of the image is declared 500 pixels and the height of the highest label is calculated to be 40 pixels, the height of the chart area is 460 pixels. This is not really true, since for example the borders and margins affect the result of the calculations but in my case their corresponding values all the same in all the graphs. Only the label strings differ greatly.
....and now comes the problem:The calculation explained above is done by the JFreeChart library and I dont know how to control it by myself. As the domain axis labels can be of different heights, the chart height is going to vary from graph to graph. How to declare a thrustworthy fixed percentual value for the height of either the area of the labels or the area designated for the chart itself? In this way, the chart area dimensions would be the same in all the generated graphs.
In the case I did not explain myself clearly enough, please ask me for further details.
Regards,
Thomas
image support...
Angel, you are damn right. Though, since Im lacking a web server where to distribute my images, I decided to stick to the following compromiseangel wrote:There is a saying "a picture is worth a thousand words"
Code: Select all
GRAPH 1 GRAPH 2
!-----------------------------------! ! ------------------------------- !
! ----------------------------- ! ! -------------------------- !
! ! !x! CHART AREA ! ! ! ! CHART AREA ! !
! ! ! ! ! ! ! ! !x! ! !
! ! ! ! !y! ! ! ! ! ! ! ! !
! ! ! ! ! ! ! ! ! ! ! ! !y! ! !
! ! ! ! ! ! !z! ! ! ! ! ! ! ! ! ! !
! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !z! ! !
! ---------------------------- ! ! ! ! ! ! ! ! ! ! !
! a bb c ! ! ! ! ! ! ! ! ! ! !
! a bb c ! ! ------------------------ !
! b ! ! e f g ! <---- LABEL AREA
! b ! ! g !
! ------------------------------- ! !-------------------------------- !
-the letters a through g represent bar axis label strings
-the letters x,y and z are imaginary bar Ids
-CHART AREA and LABEL AREA... I think the names themselves are descriptive enough

I am trying to illustrate the problem by putting two similar graphs beside each other.
In the two examples, the graphs are supposed to have the same values for the margins, borders and other variables. Even the amount of bars and their respective values are identical. Only the label strings differ.
Now, here again comes the problem: since the label string 'b' for bar y is very long in GRAPH 1 but short in GRAPH 2, the resulting height of CHART AREA is longer in GRAPH 1. By default, JFreeChart squeezes the CHART AREA when there are very long bar axis label strings present.
ASSUMED THAT THE TOTAL PIXEL HEIGHT OF THE TWO GRAPHS IS ALWAYS THE SAME, HOW CAN I DEFINE A FIXED PERCENTUAL HEIGHT FOR THE CHART AREA THAT IS NOT AFFECTED BY THE LABELS? If the labels dont fit into the space left for them, they should be shortened or separated between several lines.
Best regards and thank's in advance...
Thomas
Perhaps it will help when you limit the size of the labels?
Code: Select all
CategoryPlot plot = chart.getCategoryPlot();
plot.getDomainAxis().setMaximumCategoryLabelLines(2);
If having the labels consist of one single word with varying lengths, one can see that declaring the limit you mentioned is not enough. In my opinion, the module that assigns space for the labels is TOO smart and dynamic
It should be implemented a more robust way, for example declaring a percentual height for the label area or the chart area itself...

-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
I don't think there is a reliable way to get the result you are looking for without modifying the JFreeChart source code. Right now, the CategoryAxis class completely ignores the setFixedDimension(double) setting, but if that was fixed you could combine that with an appropriate CategoryLabelPosition setting (for wrapping long labels) to get a constant height for the category axes.
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


-
- Posts: 3
- Joined: Thu Oct 14, 2004 4:03 pm
How to get plot insets?
I have a related problem - it's not important to me that the insets (from the ChartPanel border to where the white plot area begins) are constant, but I want to know what they are (I want to scale another component to be in line with the plot). Is there a way to get these from a ChartPanel?
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
There is a convenience method in the ChartPanel class that should return this:
Code: Select all
public Rectangle2D getScreenDataArea();
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader

