DateAxis on Domain with fixed ticks / labels !? (autorange)

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
mentox
Posts: 9
Joined: Thu Jan 03, 2008 11:02 am

DateAxis on Domain with fixed ticks / labels !? (autorange)

Post by mentox » Fri Jan 04, 2008 11:36 am

Hi,

i have an stacked area with dataaxis in domain an autorange on.

its great .. but i want that the first x ticks or lable is always on the 0 position. i mean left down of chart.

the value on axis can odd..


i have an sceenshot but in this forum i cant attache one :-(


i hope you know want i mean :-)


kind regards dominique

mentox
Posts: 9
Joined: Thu Jan 03, 2008 11:02 am

Post by mentox » Fri Jan 04, 2008 11:42 am

found an free upload center

Image

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Fri Jan 04, 2008 12:34 pm

There's no option you can set to achieve that. It's something that a number of people have asked for, and I'd like to implement it, but haven't found time to do it yet.

As a workaround, you could subclass the axis, and override the refreshTicks() method to return whatever ticks you require.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

mentox
Posts: 9
Joined: Thu Jan 03, 2008 11:02 am

Post by mentox » Mon Jan 07, 2008 9:28 am

have it ... totaly simple .. work for me :-)

Code: Select all

import java.util.Date;

import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;

public class FixDateAxis extends DateAxis {

    private static final long serialVersionUID = 1L;

    @Override
    /**
     * Calculates the value of the lowest visible tick on the axis.
     * 
     * @param unit
     *                date unit to use.
     * 
     * @return The value of the lowest visible tick on the axis.
     */
    public Date calculateLowestVisibleTickValue(DateTickUnit unit) {
        return getMinimumDate();
    }
}
thanks for help
greetings dominique

mentox
Posts: 9
Joined: Thu Jan 03, 2008 11:02 am

Fixed Number Axis for Vertical Axis

Post by mentox » Mon Jan 07, 2008 10:39 am

Hi,

i also made an Fixed Vertical Axis, because i have some charts and all should have the same width.

in this case you make an FixNuberAxis and set the FixWidth which are the number of charaters :-)

feel free to use them .. perhaps is insperete you (david.gilbert) to include it in jfreechart :-)

Code: Select all

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.font.LineMetrics;
import java.awt.geom.Rectangle2D;
import java.util.Iterator;
import java.util.List;

import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.Tick;
import org.jfree.text.TextUtilities;
import org.jfree.ui.RectangleInsets;

public class FixNumberAxis extends NumberAxis {

    private static final long serialVersionUID = 1L;

    private int fixWidth = -1;

    @Override
    /**
     * A utility method for determining the width of the widest tick label.
     * 
     * @param ticks
     *                the ticks.
     * @param g2
     *                the graphics device.
     * @param drawArea
     *                the area within which the plot and axes should be drawn.
     * @param vertical
     *                a flag that indicates whether or not the tick labels are
     *                'vertical'.
     * 
     * @return The width of the tallest tick label.
     */
    protected double findMaximumTickLabelWidth(List ticks, Graphics2D g2, Rectangle2D drawArea, boolean vertical) {

        RectangleInsets insets = getTickLabelInsets();
        Font font = getTickLabelFont();
        double maxWidth = 0.0;
        if (!vertical) {
            FontMetrics fm = g2.getFontMetrics(font);
            Iterator iterator = ticks.iterator();
            while (iterator.hasNext()) {
                Tick tick = (Tick) iterator.next();
                Rectangle2D labelBounds;
                if (fixWidth > 0) {
                    labelBounds = TextUtilities.getTextBounds(String.valueOf(new char[fixWidth]), g2, fm);
                } else {
                    labelBounds = TextUtilities.getTextBounds(tick.getText(), g2, fm);
                }
                if (labelBounds.getWidth() + insets.getLeft() + insets.getRight() > maxWidth) {
                    maxWidth = labelBounds.getWidth() + insets.getLeft() + insets.getRight();
                }
            }
        } else {
            LineMetrics metrics = font.getLineMetrics("ABCxyz", g2.getFontRenderContext());
            maxWidth = metrics.getHeight() + insets.getTop() + insets.getBottom();
        }
        return maxWidth;
    }

    public int getFixWidth() {
        return fixWidth;
    }

    public void setFixWidth(int fixWidth) {
        this.fixWidth = fixWidth;
    }
}

mentox
Posts: 9
Joined: Thu Jan 03, 2008 11:02 am

Post by mentox » Mon Jan 07, 2008 10:48 am

here is an other version...

the code is not so nice but the result is much more nicer :-)

Code: Select all

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.font.LineMetrics;
import java.awt.geom.Rectangle2D;
import java.util.Iterator;
import java.util.List;

import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.Tick;
import org.jfree.text.TextUtilities;
import org.jfree.ui.RectangleInsets;

public class FixNumberAxis extends NumberAxis {

    private static final long serialVersionUID = 1L;

    private int fixWidth = -1;

    @Override
    /**
     * A utility method for determining the width of the widest tick label.
     * 
     * @param ticks
     *                the ticks.
     * @param g2
     *                the graphics device.
     * @param drawArea
     *                the area within which the plot and axes should be drawn.
     * @param vertical
     *                a flag that indicates whether or not the tick labels are
     *                'vertical'.
     * 
     * @return The width of the tallest tick label.
     */
    protected double findMaximumTickLabelWidth(List ticks, Graphics2D g2, Rectangle2D drawArea, boolean vertical) {

        RectangleInsets insets = getTickLabelInsets();
        Font font = getTickLabelFont();
        double maxWidth = 0.0;
        if (!vertical) {
            FontMetrics fm = g2.getFontMetrics(font);
            Iterator iterator = ticks.iterator();
            while (iterator.hasNext()) {
                Tick tick = (Tick) iterator.next();
                Rectangle2D labelBounds;
                if (fixWidth > 0) {
                    labelBounds = TextUtilities.getTextBounds(createEmptyString(fixWidth), g2, fm);
                } else {
                    labelBounds = TextUtilities.getTextBounds(tick.getText(), g2, fm);
                }
                if (labelBounds.getWidth() + insets.getLeft() + insets.getRight() > maxWidth) {
                    maxWidth = labelBounds.getWidth() + insets.getLeft() + insets.getRight();
                }
            }
        } else {
            LineMetrics metrics = font.getLineMetrics("ABCxyz", g2.getFontRenderContext());
            maxWidth = metrics.getHeight() + insets.getTop() + insets.getBottom();
        }
        return maxWidth;
    }

    public int getFixWidth() {
        return fixWidth;
    }

    public void setFixWidth(int fixWidth) {
        this.fixWidth = fixWidth;
    }

    private String createEmptyString(int width) {
        String fixWidthString = "";
        for (int i = 0; i < width; i++) {
            fixWidthString += "0";
        }
        return fixWidthString;
    }
}

Locked