package prefuse.render;_x000D_
_x000D_
import java.awt.FontMetrics;_x000D_
import java.awt.Graphics2D;_x000D_
import java.awt.Shape;_x000D_
import java.awt.geom.AffineTransform;_x000D_
import java.awt.geom.Line2D;_x000D_
import java.awt.geom.Point2D;_x000D_
import java.awt.geom.Rectangle2D;_x000D_
_x000D_
import prefuse.Constants;_x000D_
import prefuse.util.ColorLib;_x000D_
import prefuse.util.GraphicsLib;_x000D_
import prefuse.visual.VisualItem;_x000D_
_x000D_
/**_x000D_
 * Renderer for drawing an axis tick mark and label._x000D_
 * _x000D_
 * @author <a href="http://jheer.org">jeffrey heer</a>_x000D_
 * @author jgood_x000D_
 */_x000D_
public class AxisRenderer extends AbstractShapeRenderer {_x000D_
_x000D_
    private Line2D      m_line = new Line2D.Double();_x000D_
    private Rectangle2D m_box  = new Rectangle2D.Double();_x000D_
    _x000D_
    private int m_xalign;_x000D_
    private int m_yalign;_x000D_
    private int m_ascent;_x000D_
    _x000D_
    /**_x000D_
     * Create a new AxisRenderer. By default, axis labels are drawn along the_x000D_
     * left edge and underneath the tick marks._x000D_
     */_x000D_
    public AxisRenderer() {_x000D_
        this(Constants.LEFT, Constants.BOTTOM);_x000D_
    }_x000D_
    _x000D_
    /**_x000D_
     * Create a new AxisRenderer._x000D_
     * @param xalign the horizontal alignment for the axis label. One of_x000D_
     * {@link prefuse.Constants#LEFT}, {@link prefuse.Constants#RIGHT},_x000D_
     * or {@link prefuse.Constants#CENTER}._x000D_
     * @param yalign the vertical alignment for the axis label. One of_x000D_
     * {@link prefuse.Constants#TOP}, {@link prefuse.Constants#BOTTOM},_x000D_
     * or {@link prefuse.Constants#CENTER}._x000D_
     */_x000D_
    public AxisRenderer(int xalign, int yalign) {_x000D_
        m_xalign = xalign;_x000D_
        m_yalign = yalign;_x000D_
    }_x000D_
    _x000D_
    /**_x000D_
     * Set the horizontal alignment of axis labels._x000D_
     * @param xalign the horizontal alignment for the axis label. One of_x000D_
     * {@link prefuse.Constants#LEFT}, {@link prefuse.Constants#RIGHT},_x000D_
     * or {@link prefuse.Constants#CENTER}._x000D_
     */_x000D_
    public void setHorizontalAlignment(int xalign) {_x000D_
        m_xalign = xalign;_x000D_
    }_x000D_
    _x000D_
    /**_x000D_
     * Set the vertical alignment of axis labels._x000D_
     * @param yalign the vertical alignment for the axis label. One of_x000D_
     * {@link prefuse.Constants#TOP}, {@link prefuse.Constants#BOTTOM},_x000D_
     * or {@link prefuse.Constants#CENTER}._x000D_
     */_x000D_
    public void setVerticalAlignment(int yalign) {_x000D_
        m_yalign = yalign;_x000D_
    }_x000D_
    _x000D_
    /**_x000D_
     * @see prefuse.render.AbstractShapeRenderer#getRawShape(prefuse.visual.VisualItem)_x000D_
     */_x000D_
    protected Shape getRawShape(VisualItem item) {_x000D_
        double x1 = item.getDouble(VisualItem.X);_x000D_
        double y1 = item.getDouble(VisualItem.Y);_x000D_
        double x2 = item.getDouble(VisualItem.X2);_x000D_
        double y2 = item.getDouble(VisualItem.Y2);_x000D_
        m_line.setLine(x1,y1,x2,y2);_x000D_
        _x000D_
        if ( !item.canGetString(VisualItem.LABEL) )_x000D_
            return m_line;_x000D_
        _x000D_
        String label = item.getString(VisualItem.LABEL);_x000D_
        if ( label == null ) return m_line;_x000D_
        _x000D_
        FontMetrics fm = DEFAULT_GRAPHICS.getFontMetrics(item.getFont());_x000D_
        m_ascent = fm.getAscent();_x000D_
        int h = fm.getHeight();_x000D_
        int w = fm.stringWidth(label);_x000D_
        _x000D_
        double tx, ty;_x000D_
        _x000D_
        // get text x-coord_x000D_
        switch ( m_xalign ) {_x000D_
        case Constants.FAR_RIGHT:_x000D_
            tx = x2 + 2;_x000D_
            break;_x000D_
        case Constants.FAR_LEFT:_x000D_
            tx = x1 - w - 2;_x000D_
            break;_x000D_
        case Constants.CENTER:_x000D_
            tx = x1 + (x2-x1)/2 - w/2;_x000D_
            break;_x000D_
        case Constants.RIGHT:_x000D_
            tx = x2 - w;_x000D_
            break;_x000D_
        case Constants.LEFT:_x000D_
        default:_x000D_
            tx = x1;_x000D_
        }_x000D_
        // get text y-coord_x000D_
        switch ( m_yalign ) {_x000D_
        case Constants.FAR_TOP:_x000D_
            ty = y1-h;_x000D_
            break;_x000D_
        case Constants.FAR_BOTTOM:_x000D_
            ty = y2;_x000D_
            break;_x000D_
        case Constants.CENTER:_x000D_
            ty = y1 + (y2-y1)/2 - h/2;_x000D_
            break;_x000D_
        case Constants.TOP:_x000D_
            ty = y1;_x000D_
            break;_x000D_
        case Constants.BOTTOM:_x000D_
        default:_x000D_
            ty = y2-h; _x000D_
        }_x000D_
        m_box.setFrame(tx,ty,w,h);_x000D_
        return m_box;_x000D_
    }_x000D_
    _x000D_
    /**_x000D_
     * @see prefuse.render.Renderer#render(java.awt.Graphics2D, prefuse.visual.VisualItem)_x000D_
     */_x000D_
    public void render(Graphics2D g, VisualItem item) { _x000D_
    	Shape s = getShape(item); _x000D_
    	GraphicsLib.paint(g, item, m_line, getStroke(item), getRenderType(item)); _x000D_
    	 _x000D_
    	// check if we have a text label, if so, render it _x000D_
    	if ( item.canGetString(VisualItem.LABEL) ) { _x000D_
	    	float x = (float)m_box.getMinX(); _x000D_
	    	float y = (float)m_box.getMinY() + m_ascent; _x000D_
	    	 _x000D_
	    	// draw label background _x000D_
	    	GraphicsLib.paint(g, item, s, null, RENDER_TYPE_FILL); _x000D_
	    	 _x000D_
	    	String str = item.getString(VisualItem.LABEL); _x000D_
	    	AffineTransform origTransform = g.getTransform();_x000D_
	    	AffineTransform transform = this.getTransform(item);_x000D_
	    	if ( transform != null ) g.setTransform(transform);_x000D_
	    		_x000D_
	    	g.setFont(item.getFont()); _x000D_
	    	g.setColor(ColorLib.getColor(item.getTextColor())); _x000D_
	    	g.drawString(str, x, y);_x000D_
	    		_x000D_
	    	if ( transform != null ) g.setTransform(origTransform); _x000D_
    	}_x000D_
	}_x000D_
_x000D_
    /**_x000D_
     * @see prefuse.render.Renderer#locatePoint(java.awt.geom.Point2D, prefuse.visual.VisualItem)_x000D_
     */_x000D_
    public boolean locatePoint(Point2D p, VisualItem item) {_x000D_
        Shape s = getShape(item);_x000D_
        if ( s == null ) {_x000D_
            return false;_x000D_
        } else if ( s == m_box && m_box.contains(p) ) {_x000D_
            return true;_x000D_
        } else {_x000D_
            double width = Math.max(2, item.getSize());_x000D_
            double halfWidth = width/2.0;_x000D_
            return s.intersects(p.getX()-halfWidth,_x000D_
                                p.getY()-halfWidth,_x000D_
                                width,width);_x000D_
        }_x000D_
    }_x000D_
_x000D_
    /**_x000D_
     * @see prefuse.render.Renderer#setBounds(prefuse.visual.VisualItem)_x000D_
     */_x000D_
    public void setBounds(VisualItem item) {_x000D_
        if ( !m_manageBounds ) return;_x000D_
        Shape shape = getShape(item);_x000D_
        if ( shape == null ) {_x000D_
            item.setBounds(item.getX(), item.getY(), 0, 0);_x000D_
        } else if ( shape == m_line ) {_x000D_
            GraphicsLib.setBounds(item, shape, getStroke(item));_x000D_
        } else {_x000D_
            m_box.add(m_line.getX1(),m_line.getY1());_x000D_
            m_box.add(m_line.getX2(),m_line.getY2());_x000D_
            item.setBounds(m_box.getMinX(), m_box.getMinY(),_x000D_
                           m_box.getWidth(), m_box.getHeight());_x000D_
        }_x000D_
    }_x000D_
    _x000D_
} // end of class AxisRenderer_x000D_

// get text x-coord_x000D_
package prefuse.render;_x000D_
_x000D_
import java.awt.FontMetrics;_x000D_
import java.awt.Graphics2D;_x000D_
import java.awt.Shape;_x000D_
import java.awt.geom.AffineTransform;_x000D_
import java.awt.geom.Line2D;_x000D_
import java.awt.geom.Point2D;_x000D_
import java.awt.geom.Rectangle2D;_x000D_
_x000D_
import prefuse.Constants;_x000D_
import prefuse.util.ColorLib;_x000D_
import prefuse.util.GraphicsLib;_x000D_
import prefuse.visual.VisualItem;_x000D_
_x000D_
/**_x000D_
 * Renderer for drawing an axis tick mark and label._x000D_
 * _x000D_
 * @author <a href="http://jheer.org">jeffrey heer</a>_x000D_
 * @author jgood_x000D_
 */_x000D_
public class AxisRenderer extends AbstractShapeRenderer {_x000D_
_x000D_
    private Line2D      m_line = new Line2D.Double();_x000D_
    private Rectangle2D m_box  = new Rectangle2D.Double();_x000D_
    _x000D_
    private int m_xalign;_x000D_
    private int m_yalign;_x000D_
    private int m_ascent;_x000D_
    _x000D_
    /**_x000D_
     * Create a new AxisRenderer. By default, axis labels are drawn along the_x000D_
     * left edge and underneath the tick marks._x000D_
     */_x000D_
    public AxisRenderer() {_x000D_
        this(Constants.LEFT, Constants.BOTTOM);_x000D_
    }_x000D_
    _x000D_
    /**_x000D_
     * Create a new AxisRenderer._x000D_
     * @param xalign the horizontal alignment for the axis label. One of_x000D_
     * {@link prefuse.Constants#LEFT}, {@link prefuse.Constants#RIGHT},_x000D_
     * or {@link prefuse.Constants#CENTER}._x000D_
     * @param yalign the vertical alignment for the axis label. One of_x000D_
     * {@link prefuse.Constants#TOP}, {@link prefuse.Constants#BOTTOM},_x000D_
     * or {@link prefuse.Constants#CENTER}._x000D_
     */_x000D_
    public AxisRenderer(int xalign, int yalign) {_x000D_
        m_xalign = xalign;_x000D_
        m_yalign = yalign;_x000D_
    }_x000D_
    _x000D_
    /**_x000D_
     * Set the horizontal alignment of axis labels._x000D_
     * @param xalign the horizontal alignment for the axis label. One of_x000D_
     * {@link prefuse.Constants#LEFT}, {@link prefuse.Constants#RIGHT},_x000D_
     * or {@link prefuse.Constants#CENTER}._x000D_
     */_x000D_
    public void setHorizontalAlignment(int xalign) {_x000D_
        m_xalign = xalign;_x000D_
    }_x000D_
    _x000D_
    /**_x000D_
     * Set the vertical alignment of axis labels._x000D_
     * @param yalign the vertical alignment for the axis label. One of_x000D_
     * {@link prefuse.Constants#TOP}, {@link prefuse.Constants#BOTTOM},_x000D_
     * or {@link prefuse.Constants#CENTER}._x000D_
     */_x000D_
    public void setVerticalAlignment(int yalign) {_x000D_
        m_yalign = yalign;_x000D_
    }_x000D_
    _x000D_
    /**_x000D_
     * @see prefuse.render.AbstractShapeRenderer#getRawShape(prefuse.visual.VisualItem)_x000D_
     */_x000D_
    protected Shape getRawShape(VisualItem item) {_x000D_
        double x1 = item.getDouble(VisualItem.X);_x000D_
        double y1 = item.getDouble(VisualItem.Y);_x000D_
        double x2 = item.getDouble(VisualItem.X2);_x000D_
        double y2 = item.getDouble(VisualItem.Y2);_x000D_
        m_line.setLine(x1,y1,x2,y2);_x000D_
        _x000D_
        if ( !item.canGetString(VisualItem.LABEL) )_x000D_
            return m_line;_x000D_
        _x000D_
        String label = item.getString(VisualItem.LABEL);_x000D_
        if ( label == null ) return m_line;_x000D_
        _x000D_
        FontMetrics fm = DEFAULT_GRAPHICS.getFontMetrics(item.getFont());_x000D_
        m_ascent = fm.getAscent();_x000D_
        int h = fm.getHeight();_x000D_
        int w = fm.stringWidth(label);_x000D_
        _x000D_
        double tx, ty;_x000D_
        _x000D_
        // get text<SUF>
        switch ( m_xalign ) {_x000D_
        case Constants.FAR_RIGHT:_x000D_
            tx = x2 + 2;_x000D_
            break;_x000D_
        case Constants.FAR_LEFT:_x000D_
            tx = x1 - w - 2;_x000D_
            break;_x000D_
        case Constants.CENTER:_x000D_
            tx = x1 + (x2-x1)/2 - w/2;_x000D_
            break;_x000D_
        case Constants.RIGHT:_x000D_
            tx = x2 - w;_x000D_
            break;_x000D_
        case Constants.LEFT:_x000D_
        default:_x000D_
            tx = x1;_x000D_
        }_x000D_
        // get text y-coord_x000D_
        switch ( m_yalign ) {_x000D_
        case Constants.FAR_TOP:_x000D_
            ty = y1-h;_x000D_
            break;_x000D_
        case Constants.FAR_BOTTOM:_x000D_
            ty = y2;_x000D_
            break;_x000D_
        case Constants.CENTER:_x000D_
            ty = y1 + (y2-y1)/2 - h/2;_x000D_
            break;_x000D_
        case Constants.TOP:_x000D_
            ty = y1;_x000D_
            break;_x000D_
        case Constants.BOTTOM:_x000D_
        default:_x000D_
            ty = y2-h; _x000D_
        }_x000D_
        m_box.setFrame(tx,ty,w,h);_x000D_
        return m_box;_x000D_
    }_x000D_
    _x000D_
    /**_x000D_
     * @see prefuse.render.Renderer#render(java.awt.Graphics2D, prefuse.visual.VisualItem)_x000D_
     */_x000D_
    public void render(Graphics2D g, VisualItem item) { _x000D_
    	Shape s = getShape(item); _x000D_
    	GraphicsLib.paint(g, item, m_line, getStroke(item), getRenderType(item)); _x000D_
    	 _x000D_
    	// check if we have a text label, if so, render it _x000D_
    	if ( item.canGetString(VisualItem.LABEL) ) { _x000D_
	    	float x = (float)m_box.getMinX(); _x000D_
	    	float y = (float)m_box.getMinY() + m_ascent; _x000D_
	    	 _x000D_
	    	// draw label background _x000D_
	    	GraphicsLib.paint(g, item, s, null, RENDER_TYPE_FILL); _x000D_
	    	 _x000D_
	    	String str = item.getString(VisualItem.LABEL); _x000D_
	    	AffineTransform origTransform = g.getTransform();_x000D_
	    	AffineTransform transform = this.getTransform(item);_x000D_
	    	if ( transform != null ) g.setTransform(transform);_x000D_
	    		_x000D_
	    	g.setFont(item.getFont()); _x000D_
	    	g.setColor(ColorLib.getColor(item.getTextColor())); _x000D_
	    	g.drawString(str, x, y);_x000D_
	    		_x000D_
	    	if ( transform != null ) g.setTransform(origTransform); _x000D_
    	}_x000D_
	}_x000D_
_x000D_
    /**_x000D_
     * @see prefuse.render.Renderer#locatePoint(java.awt.geom.Point2D, prefuse.visual.VisualItem)_x000D_
     */_x000D_
    public boolean locatePoint(Point2D p, VisualItem item) {_x000D_
        Shape s = getShape(item);_x000D_
        if ( s == null ) {_x000D_
            return false;_x000D_
        } else if ( s == m_box && m_box.contains(p) ) {_x000D_
            return true;_x000D_
        } else {_x000D_
            double width = Math.max(2, item.getSize());_x000D_
            double halfWidth = width/2.0;_x000D_
            return s.intersects(p.getX()-halfWidth,_x000D_
                                p.getY()-halfWidth,_x000D_
                                width,width);_x000D_
        }_x000D_
    }_x000D_
_x000D_
    /**_x000D_
     * @see prefuse.render.Renderer#setBounds(prefuse.visual.VisualItem)_x000D_
     */_x000D_
    public void setBounds(VisualItem item) {_x000D_
        if ( !m_manageBounds ) return;_x000D_
        Shape shape = getShape(item);_x000D_
        if ( shape == null ) {_x000D_
            item.setBounds(item.getX(), item.getY(), 0, 0);_x000D_
        } else if ( shape == m_line ) {_x000D_
            GraphicsLib.setBounds(item, shape, getStroke(item));_x000D_
        } else {_x000D_
            m_box.add(m_line.getX1(),m_line.getY1());_x000D_
            m_box.add(m_line.getX2(),m_line.getY2());_x000D_
            item.setBounds(m_box.getMinX(), m_box.getMinY(),_x000D_
                           m_box.getWidth(), m_box.getHeight());_x000D_
        }_x000D_
    }_x000D_
    _x000D_
} // end of class AxisRenderer_x000D_
