Multi Line Legend Item Problem

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
CarstenRogas
Posts: 2
Joined: Wed Oct 04, 2006 1:48 pm

Multi Line Legend Item Problem

Post by CarstenRogas » Wed Oct 04, 2006 1:59 pm

Hi,

i have the following Problem:
I have a DefaultCategoryDataset with the following Data:

Code: Select all

dataset.addValue(calc2*0.5, "Kosten i.v.m. het inentingsprogramma (midden)\nresp. nettobesparing (rechts)", "Savings");
For the legend i create a LegendTitle and everything works fine, just the long text with \n doesn't fromat as expected. The shorter part is aligned centered in relation to the longer one. This happens no matter if the shorter part is the first one or like the example.

I set the following properties with no success:

Code: Select all

legendTitle.setPosition(RectangleEdge.RIGHT);
legendTitle.setVerticalAlignment(VerticalAlignment.BOTTOM);
legendTitle.setHorizontalAlignment(HorizontalAlignment.LEFT);
Does anybody know how to solve?

Thanks in advance!
Carsten
P.S.: I just upgraded to V1.0.2

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 » Wed Oct 04, 2006 4:49 pm

This patch sets the alignment to LEFT:

Code: Select all

Index: source/org/jfree/chart/block/LabelBlock.java
===================================================================
RCS file: /cvsroot/jfreechart/jfreechart/source/org/jfree/chart/block/LabelBlock.java,v
retrieving revision 1.8.2.4
diff -u -r1.8.2.4 LabelBlock.java
--- source/org/jfree/chart/block/LabelBlock.java	4 Aug 2006 11:47:23 -0000	1.8.2.4
+++ source/org/jfree/chart/block/LabelBlock.java	4 Oct 2006 14:28:37 -0000
@@ -62,6 +62,7 @@
 import org.jfree.text.TextBlock;
 import org.jfree.text.TextBlockAnchor;
 import org.jfree.text.TextUtilities;
+import org.jfree.ui.HorizontalAlignment;
 import org.jfree.ui.Size2D;
 import org.jfree.util.ObjectUtilities;
 import org.jfree.util.PaintUtilities;
@@ -126,7 +127,8 @@
     public LabelBlock(String text, Font font, Paint paint) {        
         this.text = text;
         this.paint = paint; 
-        this.label = TextUtilities.createTextBlock(text, font, this.paint); 
+        this.label = TextUtilities.createTextBlock(text, font, this.paint);
+        this.label.setLineAlignment(HorizontalAlignment.LEFT);
         this.font = font;
         this.toolTipText = null;
         this.urlText = null;
@@ -152,6 +154,7 @@
         }
         this.font = font;
         this.label = TextUtilities.createTextBlock(this.text, font, this.paint);
+        this.label.setLineAlignment(HorizontalAlignment.LEFT);
     }
    
     /**
@@ -174,6 +177,7 @@
         }
         this.paint = paint;
         this.label = TextUtilities.createTextBlock(this.text, font, this.paint);
+        this.label.setLineAlignment(HorizontalAlignment.LEFT);
     }
 
     /**
I need to think about the best way to generalise this, but give it a try anyway.
David Gilbert
JFreeChart Project Leader

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

CarstenRogas
Posts: 2
Joined: Wed Oct 04, 2006 1:48 pm

Post by CarstenRogas » Thu Oct 05, 2006 10:36 am

Hi David!

Thanks for the patch, it works for me!

I changed it a little bit, but i don't know, if this will crash at other parts of jfreechart ( the diff is against the original 1.0.2 Version ):

Code: Select all

--- org/jfree/chart/block/LabelBlock.java	2006-08-25 15:16:44.000000000 +0200
+++ org/jfree/chart/block/LabelBlock.java	2006-10-05 11:10:39.374210600 +0200
@@ -62,6 +62,7 @@
 import org.jfree.text.TextBlock;
 import org.jfree.text.TextBlockAnchor;
 import org.jfree.text.TextUtilities;
+import org.jfree.ui.HorizontalAlignment;
 import org.jfree.ui.Size2D;
 import org.jfree.util.ObjectUtilities;
 import org.jfree.util.PaintUtilities;
@@ -97,6 +98,12 @@
     /** The paint. */
     private Paint paint;
     
+    
+    /**
+     * Horizontal Alignment
+     */
+    private HorizontalAlignment horizontalAlignment = HorizontalAlignment.RIGHT;
+
     /**
      * Creates a new label block.
      * 
@@ -127,6 +134,7 @@
         this.text = text;
         this.paint = paint; 
         this.label = TextUtilities.createTextBlock(text, font, this.paint); 
+		this.label.setLineAlignment(getHorizontalAlignment()); 
         this.font = font;
         this.toolTipText = null;
         this.urlText = null;
@@ -152,6 +160,7 @@
         }
         this.font = font;
         this.label = TextUtilities.createTextBlock(this.text, font, this.paint);
+		this.label.setLineAlignment(getHorizontalAlignment()); 
     }
    
     /**
@@ -174,6 +183,7 @@
         }
         this.paint = paint;
         this.label = TextUtilities.createTextBlock(this.text, font, this.paint);
+		this.label.setLineAlignment(getHorizontalAlignment()); 
     }
 
     /**
@@ -325,4 +335,14 @@
     public Object clone() throws CloneNotSupportedException {
         return super.clone();
     }
+
+	public HorizontalAlignment getHorizontalAlignment() {
+		return horizontalAlignment;
+	}
+
+	public void setHorizontalAlignment(HorizontalAlignment horizontalAlignment) {
+		this.horizontalAlignment = horizontalAlignment;
+		if(this.label != null)
+			this.label.setLineAlignment(getHorizontalAlignment()); 
+	}
 }

Code: Select all

--- org/jfree/chart/title/LegendTitle.java	2006-08-25 15:16:44.000000000 +0200
+++ org/jfree/chart/title/LegendTitle.java	2006-10-05 11:01:50.465967800 +0200
@@ -433,6 +433,7 @@
                 this.itemPaint);
         labelBlock.setPadding(this.itemLabelPadding);
         labelBlock.setToolTipText(item.getToolTipText());
+        labelBlock.setHorizontalAlignment(getHorizontalAlignment());
         legendItem.add(labelBlock);
         
         result = new BlockContainer(new CenterArrangement());
With these two Patches, you can set the HorizontalALignment according to the HorizontalAlignment of the LegendTitle. Please let me know which implementation strategy you chosse. I know, this one is not the best, but fast and it works for me :)

And thank you for the fast response!

Carsten

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 Oct 05, 2006 2:00 pm

I think your additions make sense.
David Gilbert
JFreeChart Project Leader

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

wgkorb
Posts: 1
Joined: Sun Jan 28, 2007 4:55 pm

Label alignment - category labels

Post by wgkorb » Sun Jan 28, 2007 5:15 pm

I have a similar issue. My category labels are fairly long (e.g. "Personnel Development") and I have called setMaximumCategoryLabelLines(2) to allow it to split across two lines. As was the case with Carsten and his legend labels, although my block is aligned the way I want it (right) the words within the block are centered. For example:

Code: Select all

 Personnel
Development
What I want to see is both the text block for my category labels aligned right and the words within the each category label text block aligned right. For example:

Code: Select all

  Personnel
Development
It looks like org.jfree.text.TextBlock.setLineAlignment() would allow me to do this, but I cannot find the appropriate method to get my hands on the TextBlock associated with my category labels. Moreover, I'd like to just set it once and have it apply to all of my category labels, and I'm thinking that even if I am able to get the TextBlock and set the alignment, I am going to have to do so for every instance of my category labels.

Am I correct in this assumption? Also, is it even possible to get the TextBlock for my category labels, and if so, how?

Thanks,
Bill

micmouse
Posts: 2
Joined: Wed Jan 23, 2008 2:18 pm

no idea

Post by micmouse » Wed Jan 23, 2008 2:20 pm

hi
sorry i have no idea on how to use that patch, can you give me closer information for that?

tnx Michael

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Post by matinh » Thu Jan 24, 2008 8:52 am

Hi!

This has nothing to do with JFreeChart but with applying patches in general. Have a look at http://www.cpqlinux.com/patch.html or use google for more details.

- martin

Locked