Howto: Fixed height for the chart area?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Thomas Isomaki

Howto: Fixed height for the chart area?

Post by Thomas Isomaki » Thu Aug 25, 2005 7:06 pm

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

angel
Posts: 899
Joined: Thu Jan 15, 2004 12:07 am
Location: Germany - Palatinate

Post by angel » Fri Aug 26, 2005 7:37 am

There is a saying "a picture is worth a thousand words" :wink:

Thomas Isomaki

image support...

Post by Thomas Isomaki » Fri Aug 26, 2005 11:54 pm

angel wrote:There is a saying "a picture is worth a thousand words" :wink:
Angel, you are damn right. Though, since Im lacking a web server where to distribute my images, I decided to stick to the following compromise

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       ! 
!  -------------------------------  !     !-------------------------------- !
where

-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 :wink:

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

angel
Posts: 899
Joined: Thu Jan 15, 2004 12:07 am
Location: Germany - Palatinate

Post by angel » Tue Aug 30, 2005 10:00 am

Perhaps it will help when you limit the size of the labels?

Code: Select all

CategoryPlot plot = chart.getCategoryPlot();
plot.getDomainAxis().setMaximumCategoryLabelLines(2);

Thomas Isomaki

Post by Thomas Isomaki » Wed Aug 31, 2005 3:59 pm

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 :wink: It should be implemented a more robust way, for example declaring a percentual height for the label area or the chart area itself...

angel
Posts: 899
Joined: Thu Jan 15, 2004 12:07 am
Location: Germany - Palatinate

Post by angel » Thu Sep 01, 2005 8:38 am

Ok, how about a combined axis chart? I don't know if your axes have the same values. You can look at a few of them in the demos.

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

Post by david.gilbert » Thu Sep 01, 2005 9:06 am

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

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

mortenalver
Posts: 3
Joined: Thu Oct 14, 2004 4:03 pm

How to get plot insets?

Post by mortenalver » Thu Sep 01, 2005 10:56 am

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?

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

Post by david.gilbert » Thu Sep 01, 2005 11:03 am

There is a convenience method in the ChartPanel class that should return this:

Code: Select all

public Rectangle2D getScreenDataArea();
David Gilbert
JFreeChart Project Leader

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

mortenalver
Posts: 3
Joined: Thu Oct 14, 2004 4:03 pm

Post by mortenalver » Thu Sep 01, 2005 1:01 pm

Thanks, I'll look into that!

Thomas Isomaki

Post by Thomas Isomaki » Tue Sep 06, 2005 10:28 pm

...and I on my behalf will stick to hacking the source code :wink: Thank's to all of you for your answers!

regards,
Thomas

vijay
Posts: 13
Joined: Thu Nov 17, 2005 3:20 pm
Location: India

Post by vijay » Tue Nov 29, 2005 9:56 am

Thomas Isomaki,
The problem i have in hand is identical to that of yours. Do you have any tips that you can let me know, so that i can start tweaking the code as well.


Thanks,
Vijay

Locked