#595 Strange label truncation

1.0.x
open
General (896)
7
2014-08-26
2006-01-19
Aaron Siri
No

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\);
\}

}

Discussion

  • Aaron Siri

    Aaron Siri - 2006-01-19

    Snap shot of strange label truncation

     
  • David Gilbert

    David Gilbert - 2006-01-20
    • priority: 5 --> 7
    • assigned_to: nobody --> mungady
     
  • David Gilbert

    David Gilbert - 2006-01-20

    Logged In: YES
    user_id=112975

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

     
  • Aaron Siri

    Aaron Siri - 2006-05-05

    Logged In: YES
    user_id=85900

    Just wondering what the status is on this.

     
  • Vishwa Salgia

    Vishwa Salgia - 2008-01-18

    Logged In: YES
    user_id=1935191
    Originator: NO

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

     
  • James Ma

    James Ma - 2009-02-06

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

     
  • Marek

    Marek - 2009-03-31

    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;
    }

     
  • Willem Duminy

    Willem Duminy - 2010-08-05

    I have also noticed this problem on version 1.0.13.

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks