Share

JFreeChart

Tracker: Bugs

7 Strange label truncation - ID: 1410121
Last Update: Comment added ( minkis )

OS: Windows XP
JRE: 1.5.0_06
JFreeChart: 1.0.0

There is strange label truncation for category labels
along the bottom axis in a category bar chart. Long
labels that otherwise have room to display more of the
label are being truncated shorter than necessary.

In the attached example "FAZZY'S RESTAURANT, INC." is
truncated to "FRAZZY'S RESTAURAN..." but "AMANDA
QUICK/DORITOS" is truncated to just "AMAN...". The
other labels have also been truncated more than necessary.

Note that as the window is resized the truncation
usually sorts itself out but there are certain window
sizes that exhibit the problem (it almost looks like it
is truncating based on the length but then truncates
only the first word.)

A picture is attached and code is included below.

Sample code:
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

public class LableTruncTest
{
public static void main(String[] args) {
String series = "Series";

DefaultCategoryDataset dataset = new
DefaultCategoryDataset();
dataset.addValue(2.0, series, "SUSAN SMITHER/DORITOS");
dataset.addValue(1.0, series, "ACCOUNTS
PAYABLE/DORITOS");
dataset.addValue(5.0, series, "AMANDA QUICK/DORITOS");
dataset.addValue(4.0, series, "FAZZY'S RESTAURANT,
INC.");

JFreeChart chart =
ChartFactory.createBarChart("Funky
Labels",null,null,dataset,PlotOrientation.VERTICAL,true,true,false
);

chart.setAntiAlias(true);
CategoryPlot plot = chart.getCategoryPlot();
plot.getDomainAxis().setCategoryLabelPositions(

CategoryLabelPositions.createUpRotationLabelPositions(45.0*Math.PI/180.0)
);

ChartFrame frame = new ChartFrame("Funky Labels", chart);
frame.setSize(577,345);
frame.setVisible(true);
}

}


Aaron Siri ( as6o ) - 2006-01-19 18:52

7

Open

None

David Gilbert

General

1.0.x

Public


Comments ( 5 )

Date: 2009-03-31 20:58
Sender: minkis

I found it odd that createTextBlock replaced the last three characters of
the last word with "..." rather than placing "..." separately if there is
enough room. It's more of a JCommon fix than a JFreechart fix

Here is how I solved it.. It wraps normally on all but the last line,
otherwise it tries to fit as much as possible plus the "..."





Index: jcommon-1.0.15/source/org/jfree/text/TextUtilities.java
===================================================================
--- jcommon-1.0.15/source/org/jfree/text/TextUtilities.java (revision 28)
+++ jcommon-1.0.15/source/org/jfree/text/TextUtilities.java (working copy)
@@ -70,6 +70,10 @@
import java.text.BreakIterator;

import org.jfree.base.BaseBoot;
+import org.jfree.text.TextBlock;
+import org.jfree.text.TextFragment;
+import org.jfree.text.TextLine;
+import org.jfree.text.TextMeasurer;
import org.jfree.ui.TextAnchor;
import org.jfree.util.Log;
import org.jfree.util.LogContext;
@@ -224,7 +228,7 @@
public static TextBlock createTextBlock(final String text, final Font
font,
final Paint paint, final float maxWidth, final int maxLines,
final TextMeasurer measurer) {
-
+
final TextBlock result = new TextBlock();
final BreakIterator iterator = BreakIterator.getLineInstance();
iterator.setText(text);
@@ -232,33 +236,52 @@
int lines = 0;
final int length = text.length();
while (current < length && lines < maxLines) {
- final int next = nextLineBreak(text, current, maxWidth,
iterator,
+ int next = nextLineBreak(text, current, maxWidth, iterator,
measurer);
if (next == BreakIterator.DONE) {
result.addLine(text.substring(current), font, paint);
return result;
}
- result.addLine(text.substring(current, next), font, paint);
+
+ // If we're on the last line add as much as we can so we
don't wrap
+ if(lines == maxLines - 1)
+ {
+ // Keep adding words until we go past the maxWidth (and keep
it!)
+ while(next != BreakIterator.DONE &&
measurer.getStringWidth(text, current, next) < maxWidth)
+ next = iterator.next();
+
+ if(next == BreakIterator.DONE)
+ next = text.length();
+ }
+
+ result.addLine(text.substring(current, next), font, paint);
lines++;
current = next;
- while (current < text.length()&& text.charAt(current) ==
'\n') {
+ while (current < text.length()&& text.charAt(current) ==
'\n')
current++;
- }
}
- if (current < length) {
- final TextLine lastLine = result.getLastLine();
- final TextFragment lastFragment =
lastLine.getLastTextFragment();
- final String oldStr = lastFragment.getText();
- String newStr = "...";
- if (oldStr.length() > 3) {
- newStr = oldStr.substring(0, oldStr.length() - 3) +
"...";
- }
-
- lastLine.removeFragment(lastFragment);
- final TextFragment newFragment = new TextFragment(newStr,
- lastFragment.getFont(), lastFragment.getPaint());
- lastLine.addFragment(newFragment);
+
+
+ // Handle the last line and add "..."s as needed
+ final TextLine lastLine = result.getLastLine();
+ final TextFragment lastFragment = lastLine.getLastTextFragment();
+ final String oldStr = lastFragment.getText();
+
+ // Try to see how many characters exactly we can fit from the line
+ int offset = 4;
+ while(measurer.getStringWidth(oldStr.substring(0, offset), 0,
offset) < maxWidth)
+ ++offset;
+
+ if(offset != oldStr.length())
+ {
+ String newStr = oldStr.substring(0, offset - 3) + "...";
+
+ lastLine.removeFragment(lastFragment);
+ final TextFragment newFragment = new TextFragment(newStr,
lastFragment.getFont(), lastFragment.getPaint());
+ lastLine.addFragment(newFragment);
}
+
+
return result;
}




Date: 2009-02-06 15:11
Sender: jama22

Unfortunately, this defect has been reproduced with the latest JFreeChart
library (1.0.12) using java version 1.6.0_07


Date: 2008-01-18 12:55
Sender: vpsalgia


Can some body tell me if this bug has been fixed or not. If yes then which
version. - Thanks


Date: 2006-05-05 16:03
Sender: as6o

Logged In: YES
user_id=85900

Just wondering what the status is on this.


Date: 2006-01-20 14:40
Sender: mungadySourceForge.net SubscriberProject Admin

Logged In: YES
user_id=112975

Thanks for the report - I'll take a look.


Attached File ( 1 )

Filename Description Download
labeltrunc.png Snap shot of strange label truncation Download

Changes ( 3 )

Field Old Value Date By
priority 5 2006-01-20 14:40 mungady
assigned_to nobody 2006-01-20 14:40 mungady
File Added 164053: labeltrunc.png 2006-01-19 18:52 as6o