label positioning

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
dpop
Posts: 20
Joined: Thu Jun 03, 2004 4:05 pm
Contact:

label positioning

Post by dpop » Thu Nov 11, 2004 2:45 pm

I think I'm not the only one having trouble with the way labels display (at least in the so trivial bar chart) .
Sometimes they are truncated (at chart edges), other times overlapping (large numbers with thin bars) and it is really hard to get them positioned where you want them.
I have managed to display labels above the top of the bars in a two-series bar chart. All I wanted is to rotate them 45 degrees (or 90) so that large numbers don't overlap and instead of it being a 5 minutes job, testing included, I have already spent 2 hours. :cry:
So my question is: how does one use setPositive/Negative ItemLabelPosition() and ItemLabelPosition ? (actually I think the latter is the one that needs extensive explaining)
What are those anchors and how do they work? why do many combinations of ItemLabelAnchors and TextAnchors result in no labels displaying? Why do my labels appear when I have an angle = 0 or PI but they are not visible when angle is PI/2 ? (using same anchors)

thank you at least for reading this :wink:

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 Nov 11, 2004 3:48 pm

The first thing to understand is how the TextAnchor is used to align a String to a fixed point on the screen/chart. A good way to see this in action is to run the org.jfree.demo.DrawStringDemo application (included in the JCommon download).

The same demo application will help you to see how the text can be rotated about another point that is also specified using a TextAnchor (the "rotation anchor"). Often, the text anchor and the rotation anchor are the same, which gives the most intuitive result - but other combinations are possible. Experiment.

The next piece of the puzzle is to determine the point on the chart that the text will be aligned with - that's what the ItemLabelAnchor class represents, and it allows you to specify points relative to the data item, arranged roughly along the lines of the hours (1 to 12) on a clock face. We use two sets of values, INSIDE1-12 and OUTSIDE1-12, think of this as two clock faces around a data point, one small (INSIDE) and one large (OUTSIDE). A slight variation is used for the bar charts, since the numbers 1 to 12 need to map to points on the rectangle - the INSIDE values get offset slightly within the bar, the OUTSIDE values get offset slightly outside the bar.

One last trick for the BarRenderer - if a label uses an INSIDE item label anchor, but the text won't fit within the bar, the renderer will check the getPositiveItemLabelPositionFallback() method (or getNegativeItemLabelPositionFallback()) for a new position for the label, if the fallback position is null then you won't see the label at all. This allows you, for example, to put values inside bars, but then for very small bars have the values appear above the bars.

None of the above is simple or intuitive, but it does allow a lot of useful options once you understand how it works.
David Gilbert
JFreeChart Project Leader

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

dpop
Posts: 20
Joined: Thu Jun 03, 2004 4:05 pm
Contact:

... but it still shows weird results

Post by dpop » Wed Nov 24, 2004 11:01 am

First of all thank you for the reply.

Now I have experimented with the application you suggested, and found the combination to produce the desired result: a vertical label positioning

Code: Select all

		ItemLabelPosition lp = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,  TextAnchor.TOP_LEFT, TextAnchor.CENTER_LEFT, -Math.PI/2.0);
		renderer.setPositiveItemLabelPosition(lp);
		renderer.setNegativeItemLabelPosition(new ItemLabelPosition (ItemLabelAnchor.OUTSIDE12,  TextAnchor.TOP_LEFT, TextAnchor.CENTER_LEFT, -Math.PI/2.0));

So now the labels seem to have the correct alignment as you can see in this picture:
Image

but they are not correct (see the values on the left) : instead of 50 000 or 100 000 i get a 5 and a 1. 150 000, 0 or negative values don't even show up. what's wrong with it?
Can this be a problem of the IE's SVG plugin?

dpop
Posts: 20
Joined: Thu Jun 03, 2004 4:05 pm
Contact:

does anybody know?

Post by dpop » Thu Nov 25, 2004 12:19 pm

anybody, any suggestions, please?

Locked