Class LabelLayout

java.lang.Object
com.codename1.ui.chart.layout.LabelLayout

public final class LabelLayout extends Object
Places text so no two labels overlap. Every label is offered a list of candidate positions around its anchor; the first that stays inside the bounds and clear of every label already placed wins, and a label with no free position is dropped rather than drawn over another. Callers place the most important labels first.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Candidate: centred above the anchor.
    static final int
    Candidate: centred below the anchor.
    static final int
    Candidate: centred on the anchor.
    static final int
    Candidate: left of the anchor, vertically centred.
    static final int
    Candidate: right of the anchor, vertically centred.
  • Constructor Summary

    Constructors
    Constructor
    Description
    LabelLayout(int x, int y, int w, int h)
    Creates a layout confined to a rectangle.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    ellipsize(Font f, String text, int maxWidth)
    Shortens text with a trailing ellipsis until it fits maxWidth.
    boolean
    fits(int x, int y, int w, int h)
    True when the box is inside the bounds and clear of every placed label.
    int[]
    getRect(int i)
    The bounds of the ith placed or reserved box, as {x, y, w, h}.
    boolean
    place(int ax, int ay, int w, int h, int offset, int candidates, int[] out)
    Tries to place a w by h box near an anchor.
    boolean
    placeAt(int x, int y, int w, int h)
    Places a box at an exact position if it is free.
    void
    reserve(int x, int y, int w, int h)
    Reserves an area (a legend, a marker) that labels must avoid.
    void
    setBounds(int x, int y, int w, int h)
    Changes the bounds and forgets every placed label.
    void
    setGap(int gap)
    The minimum clearance kept between labels, in pixels.
    int
    The number of labels placed so far.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • ABOVE

      public static final int ABOVE
      Candidate: centred above the anchor.
      See Also:
    • BELOW

      public static final int BELOW
      Candidate: centred below the anchor.
      See Also:
    • LEFT

      public static final int LEFT
      Candidate: left of the anchor, vertically centred.
      See Also:
    • CENTER

      public static final int CENTER
      Candidate: centred on the anchor.
      See Also:
  • Constructor Details

    • LabelLayout

      public LabelLayout(int x, int y, int w, int h)

      Creates a layout confined to a rectangle.

      Parameters
      • x: bounds left

      • y: bounds top

      • w: bounds width

      • h: bounds height

  • Method Details

    • setBounds

      public void setBounds(int x, int y, int w, int h)
      Changes the bounds and forgets every placed label.
    • setGap

      public void setGap(int gap)
      The minimum clearance kept between labels, in pixels.
    • size

      public int size()
      The number of labels placed so far.
    • getRect

      public int[] getRect(int i)

      The bounds of the ith placed or reserved box, as {x, y, w, h}.

      Parameters
      • i: an index below size()
      Returns

      a new array holding the box

    • reserve

      public void reserve(int x, int y, int w, int h)
      Reserves an area (a legend, a marker) that labels must avoid.
    • place

      public boolean place(int ax, int ay, int w, int h, int offset, int candidates, int[] out)

      Tries to place a w by h box near an anchor.

      Parameters
      • ax: anchor x

      • ay: anchor y

      • w: box width

      • h: box height

      • offset: distance kept from the anchor

      • candidates: a mask of ABOVE, BELOW, RIGHT, LEFT, CENTER, tried in that order

      • out: receives the chosen top-left {x, y}

      Returns

      true if placed, false if every candidate collided

    • placeAt

      public boolean placeAt(int x, int y, int w, int h)

      Places a box at an exact position if it is free.

      Returns

      true if the box was free and is now reserved

    • fits

      public boolean fits(int x, int y, int w, int h)
      True when the box is inside the bounds and clear of every placed label.
    • ellipsize

      public static String ellipsize(Font f, String text, int maxWidth)

      Shortens text with a trailing ellipsis until it fits maxWidth.

      Parameters
      • f: the font it is drawn in

      • text: the text

      • maxWidth: the space available

      Returns

      the text itself if it fits, a shortened copy, or an empty string when not even the ellipsis fits