#160 Axis.drawHorizontalLabel() not used

open
David Gilbert
General (896)
5
2015-01-02
2003-04-22
m.hilpert
No

I tried to overwrite Axis.drawHorizontalLabel() on my
MyHorizontalDateAxis (subclass of HorizontalDatAxis)
and noticed that it is never called! Just copied the code
of Axis.drawHorizontalLabel() to my class and insert a
System.out.println() at the beginning doesn't print
anything (also doesn't do the code changes I did
afterwards). The Javadoc of Axis.drawHorizontalLabel()
says:

"A utility method intended for use by subclasses that
are 'horizontal' axes (for example,
HorizontalCategoryAxis, HorizontalNumberAxis and
HorizontalDateAxis."

So, we have to assume that we can overwrite this
method because it is called when the axis is drawn,
right?

Discussion

  • m.hilpert
    m.hilpert
    2003-04-22

    Logged In: YES
    user_id=667728

    Note: if this method would be used, it would be easy to
    implement tick label alignment:

    -------------
    protected double drawHorizontalLabel(String label,
    Graphics2D g2, Rectangle2D plotArea, Rectangle2D
    dataArea,
    int location, double cursorY) {
    if (label != null) {
    Font labelFont = getLabelFont();
    Insets labelInsets = getLabelInsets();
    g2.setFont(labelFont);
    g2.setPaint(getLabelPaint());
    FontRenderContext frc = g2.getFontRenderContext();
    Rectangle2D labelBounds =
    labelFont.getStringBounds(label, frc);
    LineMetrics lm = labelFont.getLineMetrics(label, frc);
    if (location == TOP) {
    cursorY = cursorY + labelInsets.top +
    lm.getAscent();
    } else {
    cursorY = cursorY - labelInsets.bottom -
    lm.getDescent() - lm.getLeading();
    }

    float labelx = 0;
    int alignment = SwingConstants.RIGHT; //test
    switch (alignment) {
    case SwingConstants.LEFT :
    labelx = (float) (dataArea.getX() +
    dataArea.getWidth() / 2 );
    break;
    case SwingConstants.RIGHT :
    labelx = (float) (dataArea.getX() +
    dataArea.getWidth() / 2 - labelBounds.getWidth());
    break;
    case SwingConstants.CENTER :
    default :
    labelx = (float) (dataArea.getX() +
    dataArea.getWidth() / 2 - labelBounds.getWidth() / 2);
    break;
    }
    float labely = (float) cursorY;
    g2.drawString(label, labelx, labely);
    if (location == TOP) {
    cursorY = cursorY + lm.getDescent() +
    lm.getLeading() + labelInsets.bottom;
    } else {
    cursorY = cursorY - labelInsets.top - lm.getAscent
    ();
    }
    }//else: no label available

    return cursorY;
    }//drawHorizontalLabel()
    ----------------

     
  • David Gilbert
    David Gilbert
    2003-04-22

    • labels: --> General
    • assigned_to: nobody --> mungady
     
  • David Gilbert
    David Gilbert
    2003-04-22

    Logged In: YES
    user_id=112975

    Thanks for the report. It is intended that the
    HorizontalDateAxis class should use that method, so I have
    updated the code for the next release (0.9.8).

    I tried out the alignment code, but I'm not sure that it
    helps since this method is only use for the axis label, not
    the tick labels. It won't hurt to put the code in, if you
    need it, since the default behaviour matches the existing
    code...let me know what you think.

    Regards,

    Dave Gilbert
    JFreeChart Project Leader

    P.S. I've read some of your other bug reports and
    enhancement requests. I'll respond to them as I get time,
    sorry for the delay.

     
  • m.hilpert
    m.hilpert
    2003-04-22

    Logged In: YES
    user_id=667728

    The same is of course applicable for the tick labels. Here's
    my code that implements tick label alignment:

    -------------------MyHorizontalDateAxis.java :
    ...
    /** Tick label alignment (LEFT, CENTER, RIGHT of
    SwingConstants). */
    private int tickLabelAlignment = SwingConstants.CENTER;
    ...

    /**
    * Overwrites super to implement tick label alignment.
    *
    * @param g2 the graphics device.
    * @param plotArea the area within which the axes and
    data should be drawn.
    * @param dataArea the area within which the data should
    be drawn.
    * @param location the location of the axis.
    */
    public void draw(Graphics2D g2, Rectangle2D plotArea,
    Rectangle2D dataArea, int location) {
    if (!isVisible()) {
    return;
    }

    double y = 0.0;
    if (location == TOP) {
    y = plotArea.getMinY();
    } else {
    y = plotArea.getMaxY();
    }

    // draw the axis label
    String label = getLabel();
    if (label != null) {
    Font labelFont = getLabelFont();
    Insets labelInsets = getLabelInsets();
    g2.setFont(labelFont);
    g2.setPaint(getLabelPaint());
    FontRenderContext frc = g2.getFontRenderContext();
    Rectangle2D labelBounds =
    labelFont.getStringBounds(label, frc);
    LineMetrics lm = labelFont.getLineMetrics(label, frc);
    if (location == TOP) {
    y = y + labelInsets.top + lm.getAscent();
    } else {
    y = y - labelInsets.bottom - lm.getDescent() -
    lm.getLeading();
    }
    float labelx = (float) (dataArea.getX() +
    dataArea.getWidth() / 2
    - labelBounds.getWidth
    () / 2);
    float labely = (float) y;
    g2.drawString(label, labelx, labely);
    if (location == TOP) {
    y = y + lm.getDescent() + lm.getLeading() +
    labelInsets.bottom;
    } else {
    y = y - labelInsets.bottom - labelInsets.top -
    lm.getHeight();
    }
    }

    // draw the tick labels and marks and gridlines
    refreshTicks(g2, plotArea, dataArea, location);

    double yy;
    double uu;
    double dd;
    if (location == TOP) {
    yy = dataArea.getMinY();
    uu = getTickMarkOutsideLength();
    dd = getTickMarkInsideLength();
    } else {
    yy = dataArea.getMaxY();
    uu = getTickMarkInsideLength();
    dd = getTickMarkOutsideLength();
    }
    g2.setFont(getTickLabelFont());

    Iterator iterator = getTicks().iterator();
    while (iterator.hasNext()) {
    Tick tick = (Tick) iterator.next();
    float xx = (float) translateValueToJava2D
    (tick.getNumericalValue(), dataArea);
    //labels:
    if (isTickLabelsVisible()) {
    g2.setPaint(getTickLabelPaint());
    if (getVerticalTickLabels()) { //draw tick labels
    vertically?
    RefineryUtilities.drawRotatedString(tick.getText
    (), g2, tick.getX(), tick.getY(), -Math.PI / 2);
    } else {
    //alignment of tick labels:
    String text = tick.getText();
    Font labelFont = getLabelFont();
    double textWidth = labelFont.getStringBounds
    (text, g2.getFontRenderContext()).getWidth();
    float x = tick.getX();
    switch (tickLabelAlignment) {
    case SwingConstants.LEFT :
    x += textWidth / 2;
    break;
    case SwingConstants.RIGHT :
    x -= textWidth / 2;
    break;
    case SwingConstants.CENTER :
    default :
    //default drawing is already CENTER
    break;
    }
    g2.drawString(tick.getText(), x, tick.getY());
    }
    }//else: tick labels not visible

    //marks:
    if (isTickMarksVisible()) {
    g2.setStroke(getTickMarkStroke());
    g2.setPaint(getTickMarkPaint());
    Line2D mark = new Line2D.Double(xx, yy - uu, xx,
    yy + dd);
    g2.draw(mark);
    }//else: tick marks not visible
    }//next tick
    }//draw()

    /**
    * Get alignment of tick labels. Alignment is relative to the
    tick mark.
    *
    * @return One of SwingConstants.LEFT,
    SwingConstants.CENTER or SwingConstants.RIGHT.
    */
    public int getTickLabelAlignment() {
    return tickLabelAlignment;
    }

    /**
    * Set alignment of tick labels. Alignment is relative to the
    tick mark.
    *
    * @param alignment One of SwingConstants.LEFT,
    SwingConstants.CENTER or SwingConstants.RIGHT.
    */
    public void setTickLabelAlignment(int alignment) {
    tickLabelAlignment = alignment;
    }
    ------------------

    Equivalent in a vertical axis with top, center, botton.