Removing beginning and end gap in range

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Chee Wah

Removing beginning and end gap in range

Post by Chee Wah » Thu Feb 20, 2003 12:26 pm

The default axis range leaves a gap in both the beginning and end of the chart, is there a way to remove the gap?

For example, if my x-axis dataset contains dates from 12-12-1950 to 12-12-1960, the default x-axis created will start with a date slightly before 12-12-1950 and ends slightly after 12-12-1960. Is there any simple way to make the axis starts and stops exactly on the dates itself?

Thanks in advance for any help.

I have also set up a simple JFreeChart applet to plot financial charts of Singapore market. Please feel free to take a look :)

http://www.shareking.com/modules.php?name=Java_Chart

Cheers!

morpheus

Re: Removing beginning and end gap in range

Post by morpheus » Thu Feb 20, 2003 1:30 pm

Hi,

The calculateLowestVisibleThick method in DateAxis Class round up Dates, that's why the start tick mark and end tick mark are not displayed. I had the same problem, by extending class HorizontalDateAxis and overriding method refreshTicks u can solve it... Here it is :

public void refreshTicks(Graphics2D g2,
Rectangle2D plotArea, Rectangle2D dataArea,
int location) {

getTicks().clear();

Font tickLabelFont = getTickLabelFont();
g2.setFont(tickLabelFont);

if (isAutoTickUnitSelection()) {
selectAutoTickUnit(g2, plotArea, dataArea);
}

Rectangle2D labelBounds = null;
DateTickUnit tickUnit = getTickUnit();

// Date tickDate = calculateLowestVisibleTickValue(tickUnit); // Old

Date tickDate = getMinimumDate(); // New

Date upperDate = getMaximumDate();

// while (tickDate.before(upperDate)) { // Old

boolean loop = ( tickDate.compareTo(upperDate) <= 0 ); // New
boolean upper = false; // New

while ( loop ) { // New
// work out the value, label and position
double xx = translateDateToJava2D(tickDate, dataArea);
String tickLabel;
DateFormat formatter = getDateFormatOverride();
if (formatter != null) {
tickLabel = formatter.format(tickDate);
}
else {
tickLabel = tickUnit.dateToString(tickDate);
}
FontRenderContext frc = g2.getFontRenderContext();
Rectangle2D tickLabelBounds = tickLabelFont.getStringBounds(tickLabel, frc);
LineMetrics metrics = tickLabelFont.getLineMetrics(tickLabel, frc);
float x = 0.0f;
float y = 0.0f;
Insets tickLabelInsets = getTickLabelInsets();
if (this.verticalTickLabels) {
x = (float) (xx + tickLabelBounds.getHeight() / 2 - metrics.getDescent());
if (location == TOP) {
y = (float) (dataArea.getMinY() - tickLabelInsets.bottom
- tickLabelBounds.getWidth());
}
else {
y = (float) (dataArea.getMaxY() + tickLabelInsets.top
+ tickLabelBounds.getWidth());
}
}
else {
x = (float) (xx - tickLabelBounds.getWidth() / 2);
if (location == TOP) {
y = (float) (dataArea.getMinY() - tickLabelInsets.bottom);
}
else {
y = (float) (dataArea.getMaxY() + tickLabelInsets.top
+ tickLabelBounds.getHeight());
}
}
Tick tick = new Tick(tickDate, tickLabel, x, y);
getTicks().add(tick);
tickDate = tickUnit.addToDate(tickDate);

// New
if( !upper ) {
if( tickDate.compareTo( upperDate ) >= 0 ) {
tickDate = upperDate;
upper = false;
}
}
else {
loop = false;
}
// New
}

}

Then add this call in the code where u create the chart :

plot.setDomainAxis( instance_of_ur_class_which_extends_HorizontalDateAxis );


Hope this works for u. In my case I didn't extend , I modified the code source directly :o)

Regards
Morpheus

morpheus

Re: Removing beginning and end gap in range

Post by morpheus » Thu Feb 20, 2003 2:20 pm

Correction :

Variable verticalTickLabels is private member of HorizontalDateAxis, so u have to get it by getVerticalTickLabels().

Another problem u could have ( I'm not sure ) is that maximum date tick label steps on the date before tick label. I recommend to set AutoTickUnitSelection to false and compute unit value urself by making the difference beetween max date and min date and dividing by the number of ticks u want to display...

Regards
Morpheus

morpheus

Re: Removing beginning and end gap in range

Post by morpheus » Thu Feb 20, 2003 2:26 pm

Another correction :o) in refreshTicks


// New
if( !upper ) {
if( tickDate.compareTo( upperDate ) >= 0 ) {
tickDate = upperDate;
upper = true; // it was false!
}
}
else {
loop = false;
}
// New

Regards
Morpheus

Cheewah

Re: Removing beginning and end gap in range

Post by Cheewah » Thu Feb 20, 2003 4:52 pm

Hi Morpheus,

Thanks for the detailed explanation and codes! Will test it out soon :)

David Gilbert

Re: Removing beginning and end gap in range

Post by David Gilbert » Fri Feb 21, 2003 12:33 pm

Hi Chee Wah,

You can use the setLowerMargin(...) and setUpperMargin(...) methods in the axis to control how much margin is added at the start and end of the axis (this applies only when the axis range is auto-calculated). You probably want to set both these to 0.0.

Regards,

Dave Gilbert

Locked