Index: trunk/Ohana/src/kapa2/include/prototypes.h
===================================================================
--- trunk/Ohana/src/kapa2/include/prototypes.h	(revision 38553)
+++ trunk/Ohana/src/kapa2/include/prototypes.h	(revision 38986)
@@ -40,5 +40,5 @@
 void          DrawYErrors         PROTO((KapaGraphWidget *graph, Gobjects *objects));
 void	      DrawTick		  PROTO((Graphic *graphic, Axis *axis, int P, TickMarkData *tick, int naxis));
-void	      AxisTickScale	  PROTO((Axis *axis, double *major, double *minor, int *nsignif));
+void	      AxisTickScale	  PROTO((Axis *axis, double *range, double *major, double *minor, int *nsignif));
 TickMarkData *CreateAxisTicks     PROTO((Axis *axis, int *nticks));
 int           PrintTick           PROTO((char *string, double value, double min, double max, int nsignif));
Index: trunk/Ohana/src/kapa2/include/structures.h
===================================================================
--- trunk/Ohana/src/kapa2/include/structures.h	(revision 38553)
+++ trunk/Ohana/src/kapa2/include/structures.h	(revision 38986)
@@ -143,4 +143,6 @@
   double fx, dfx, fy, dfy;  /* axis location on graphic */
   double lweight;
+  double fLabelRange;
+  double fMinor;
   int color;
 } Axis;
Index: trunk/Ohana/src/kapa2/src/DrawFrame.c
===================================================================
--- trunk/Ohana/src/kapa2/src/DrawFrame.c	(revision 38553)
+++ trunk/Ohana/src/kapa2/src/DrawFrame.c	(revision 38986)
@@ -116,8 +116,9 @@
 
   if ((naxis == 0) || (naxis == 2)) {
-    x = MAX(x, fx);
-  }
-  //   y = MAX(y, fy);
-  // }
+    x = MIN(MAX(x, fx),fx+dfx);
+  }
+  if ((naxis == 1) || (naxis == 3)) {
+    y = MAX(MIN(y, fy),fy+dfy);
+  }
 
   dir = ((naxis == 0) || (naxis == 1)) ? -1 : +1;
@@ -137,9 +138,9 @@
     
     /* temporarily assume rectilinear axes */
-    if (naxis == 0) { dx = 0; dy = +1.0*pad; pos = 7; }
-    if (naxis == 2) { dx = 0; dy = -1.0*pad; pos = 1; }
-
-    if (naxis == 1) { dy = 0; dx = -0.5*pad; pos = 3; }
-    if (naxis == 3) { dy = 0; dx = +0.5*pad; pos = 5; }
+    if (naxis == 0) { dx = 0; dy = +1.0*pad; pos = 7; } // center, down justified
+    if (naxis == 2) { dx = 0; dy = -1.0*pad; pos = 1; } // center, up justified
+
+    if (naxis == 1) { dy = 0; dx = -0.5*pad; pos = 3; } // left, center justified
+    if (naxis == 3) { dy = 0; dx = +0.5*pad; pos = 5; } // right, center justified
 
     xt = fx + (value-min)*dfx/(max - min) + dx;
@@ -154,14 +155,17 @@
 # define MIN_RANGE 1e-30
 
-void AxisTickScale (Axis *axis, double *major, double *minor, int *nsignif) {
-
-  double range, lrange, factor, mantis, fmantis, power;
-
-  range = axis[0].max - axis[0].min;
-  if (fabs(range) < MIN_RANGE) {
-    range = (range < 0) ? -MIN_RANGE : +MIN_RANGE;
-  }
-
-  lrange = log10(MAX(fabs(range), MIN_RANGE));
+void AxisTickScale (Axis *axis, double *range, double *major, double *minor, int *nsignif) {
+
+  double lrange, factor, mantis, fmantis, power;
+
+  *range = axis->fLabelRange*(axis[0].max - axis[0].min);
+  if (fabs(*range) < MIN_RANGE) {
+    *range = (*range < 0) ? -MIN_RANGE : +MIN_RANGE;
+  }
+
+  lrange = log10(MAX(fabs(*range), MIN_RANGE));
+  double truncRange = ((int) ((lrange + 0.005) * 1000)) / 1000.0;
+  lrange = truncRange;
+
   mantis = modf (lrange, &factor);
   if (mantis < 0.0) {
@@ -179,29 +183,35 @@
   if ((fmantis >= 1.0) && (fmantis <  1.999)) {
     *major = 0.5 * power;
-    *minor = 0.1 * power;
     *nsignif = factor - 1;
     if (axis[0].areticks == 1) {
       *major = 0.25 * power;
-      *minor = 0.05 * power;
       *nsignif = factor - 2;
+    }	  
+    if (axis[0].areticks == 3) {
+      *major = 1.0 * power;
+      *nsignif = factor - 1;
     }	  
   }
   if ((fmantis >= 1.999) && (fmantis <  3.999)) {
     *major = 1.0 * power;
-    *minor = 0.2 * power;
     *nsignif = factor;
     if (axis[0].areticks == 1) {
       *major = 0.5 * power;
-      *minor = 0.1 * power;
       *nsignif = factor - 1;
+    }	  
+    if (axis[0].areticks == 3) {
+      *major = 2.0 * power;
+      *nsignif = factor;
     }	  
   }
   if ((fmantis >= 3.999) && (fmantis <  5.999)) {
     *major = 1.0 * power;
-    *minor = 0.5 * power;
     *nsignif = factor;
     if (axis[0].areticks == 1) {
       *major = 1.0 * power;
-      *minor = 0.2 * power;
+      *nsignif = factor;
+    }	  
+    if (axis[0].areticks == 3) {
+      *major = 2.0 * power;
       *nsignif = factor;
     }	  
@@ -209,9 +219,11 @@
   if ((fmantis >= 5.999) && (fmantis <   7.999)) {
     *major = 2.0 * power;
-    *minor = 0.5 * power;
     *nsignif = factor;
     if (axis[0].areticks == 1) {
       *major = 1.0 * power;
-      *minor = 0.5 * power;
+      *nsignif = factor;
+    }	  
+    if (axis[0].areticks == 3) {
+      *major = 4.0 * power;
       *nsignif = factor;
     }	  
@@ -219,12 +231,15 @@
   if ((fmantis >= 7.999) && (fmantis <  10.000)) {
     *major = 2.5 * power;
-    *minor = 0.5 * power;
-      *nsignif = factor - 1;
+    *nsignif = factor - 1;
     if (axis[0].areticks == 1) {
       *major = 2.0 * power;
-      *minor = 0.5 * power;
-      *nsignif = factor;
-    }	  
-  }
+      *nsignif = factor;
+    }	  
+    if (axis[0].areticks == 3) {
+      *major = 4.0 * power;
+      *nsignif = factor;
+    }	  
+  }
+  *minor = *major / axis->fMinor;
 }
 
@@ -233,5 +248,5 @@
   TickMarkData *ticks;
   double range, major, minor, first, value;
-  int i, NTICKS, done, ifirst, nsignif;
+  int i, NTICKS, done, nsignif;
 
   *nticks = 0;
@@ -240,18 +255,9 @@
   if (isnan(axis->max) || isinf(axis->max)) return NULL;
   
-  range = axis->max - axis->min;
-
-  // not sure why I skip out here instead of using the MIN_RANGE value
-  if (fabs(range) < MIN_RANGE) return NULL;
-  
-  if (fabs(range) < MIN_RANGE) {
-    range = (range < 0) ? -MIN_RANGE : +MIN_RANGE;
-  }
-
   // length of the axis in pixels
   // int nPixels = hypot(axis->dfx, axis->dfy);
   // double dPixels = nPixels / range; // axis pixel-scale
 
-  AxisTickScale (axis, &major, &minor, &nsignif);
+  AxisTickScale (axis, &range, &major, &minor, &nsignif);
 
   // be a little generous 
@@ -259,20 +265,19 @@
   ALLOCATE (ticks, TickMarkData, NTICKS);
 
-  // value of the tick mark
-  ifirst = (axis->min < 0.0) ? (axis->min/minor - 0.05) : (axis->min/minor + 0.05);
+  // value of the first tick mark (near axis->min)
+  double minValue = axis->min / minor;
+  int ifirst = (int) minValue;
+
+  if ((axis->min > 0.0) && (minValue - ifirst > +0.1)) {
+    ifirst ++;
+  } 
+  if ((axis->min < 0.0) && (minValue - ifirst < -0.1)) {
+    ifirst --;
+  }
   first = minor*ifirst;
   
-  // allow undershoot by 1 pixel
-  // double overshoot = (axis->min - first) * dPixels;
-  // if (overshoot > 0.5) {
-  //   if (range > 0) {
-  //     first += minor;
-  //   } else {
-  //     first -= minor;
-  //   }
-  // }
-
-  // XXX need to fix first and last value for printing label: currently prints first label
-  // too early & fails to do last label if too close
+  // only write the labels for the inner fRange of the axies
+  double minLabelValue = 0.5*(1.0 - 1.001*axis->fLabelRange)*(axis->max - axis->min) + axis->min;
+  double maxLabelValue = 0.5*(1.0 + 1.001*axis->fLabelRange)*(axis->max - axis->min) + axis->min;
 
   // loop to find the ticks
@@ -284,5 +289,6 @@
     ticks[i].IsMajor |= (fabs((int)((value + 0.5*minor)/major) - (value/major)) < 0.5*(minor/major));
     ticks[i].IsMajor |= (fabs((int)((value - 0.5*minor)/major) - (value/major)) < 0.5*(minor/major));
-    ticks[i].IsLabel = (ticks[i].IsMajor && axis->islabel);
+    int inLabelRange = (range > 0) ? (value >= minLabelValue) && (value <= maxLabelValue) : (value <= minLabelValue) && (value >= maxLabelValue);
+    ticks[i].IsLabel = inLabelRange && ticks[i].IsMajor && axis->islabel;
     ticks[i].value = value;
     ticks[i].nsignif = nsignif;
@@ -292,6 +298,7 @@
       value -= minor;
     
-    done |= (range > 0) && (value > axis->max);
-    done |= (range < 0) && (value < axis->max);
+    done |= (range > 0) && (value > axis->max + 0.1*minor);
+    done |= (range < 0) && (value < axis->max - 0.1*minor);
+    // fprintf (stderr, "%d : %f %f %f %f\n", done, range, value, axis->max, 0.1*minor);
   }
 
Index: trunk/Ohana/src/kapa2/src/Graphs.c
===================================================================
--- trunk/Ohana/src/kapa2/src/Graphs.c	(revision 38553)
+++ trunk/Ohana/src/kapa2/src/Graphs.c	(revision 38986)
@@ -23,4 +23,6 @@
     graph[0].axis[i].labelPad = NAN;
     graph[0].axis[i].pad = NAN;
+    graph[0].axis[i].fLabelRange = 1.0;
+    graph[0].axis[i].fMinor = 5.0;
   }    
   graph[0].data.ticktextPad = NAN;
@@ -35,4 +37,14 @@
   graph[0].data.padYm = NAN;
   graph[0].data.padYp = NAN;
+
+  graph[0].data.fLabelRangeXm = 1.0;
+  graph[0].data.fLabelRangeXp = 1.0;
+  graph[0].data.fLabelRangeYm = 1.0;
+  graph[0].data.fLabelRangeYp = 1.0;
+
+  graph[0].data.fMinorXm = 5.0;
+  graph[0].data.fMinorXp = 5.0;
+  graph[0].data.fMinorYm = 5.0;
+  graph[0].data.fMinorYp = 5.0;
 
   for (i = 0; i < 8; i++) {
Index: trunk/Ohana/src/kapa2/src/LoadFrame.c
===================================================================
--- trunk/Ohana/src/kapa2/src/LoadFrame.c	(revision 38553)
+++ trunk/Ohana/src/kapa2/src/LoadFrame.c	(revision 38986)
@@ -37,4 +37,14 @@
   graph[0].axis[3].labelPad = graph[0].data.labelPadYp;
 
+  graph[0].axis[0].fLabelRange = graph[0].data.fLabelRangeXm;
+  graph[0].axis[1].fLabelRange = graph[0].data.fLabelRangeYm;
+  graph[0].axis[2].fLabelRange = graph[0].data.fLabelRangeXp;
+  graph[0].axis[3].fLabelRange = graph[0].data.fLabelRangeYp;
+
+  graph[0].axis[0].fMinor = graph[0].data.fMinorXm;
+  graph[0].axis[1].fMinor = graph[0].data.fMinorYm;
+  graph[0].axis[2].fMinor = graph[0].data.fMinorXp;
+  graph[0].axis[3].fMinor = graph[0].data.fMinorYp;
+
   for (i = 0; i < 4; i++) {
     graph[0].axis[i].lweight = graph[0].data.lweight;
@@ -63,4 +73,7 @@
     case '2':
       graph[0].axis[i].areticks = 2;
+      break;
+    case '3':
+      graph[0].axis[i].areticks = 3;
       break;
     }
Index: trunk/Ohana/src/kapa2/src/PSFrame.c
===================================================================
--- trunk/Ohana/src/kapa2/src/PSFrame.c	(revision 38553)
+++ trunk/Ohana/src/kapa2/src/PSFrame.c	(revision 38986)
@@ -81,4 +81,11 @@
   y = fy + (value-min)*dfy/(max - min);
 
+  if ((naxis == 0) || (naxis == 2)) {
+    x = MIN(MAX(x, fx),fx+dfx);
+  }
+  if ((naxis == 1) || (naxis == 3)) {
+    y = MIN(MAX(y, fy),fy+dfy);
+  }
+
   dir = ((naxis == 0) || (naxis == 1)) ? +1 : -1;
   dx = dir*size*dfy*n;	
Index: trunk/Ohana/src/kapa2/src/SetGraphData.c
===================================================================
--- trunk/Ohana/src/kapa2/src/SetGraphData.c	(revision 38553)
+++ trunk/Ohana/src/kapa2/src/SetGraphData.c	(revision 38986)
@@ -27,4 +27,14 @@
   graph[0].axis[2].labelPad = graph[0].data.labelPadXp;
   graph[0].axis[3].labelPad = graph[0].data.labelPadYp;
+
+  graph[0].axis[0].fLabelRange = graph[0].data.fLabelRangeXm;
+  graph[0].axis[1].fLabelRange = graph[0].data.fLabelRangeYm;
+  graph[0].axis[2].fLabelRange = graph[0].data.fLabelRangeXp;
+  graph[0].axis[3].fLabelRange = graph[0].data.fLabelRangeYp;
+
+  graph[0].axis[0].fMinor = graph[0].data.fMinorXm;
+  graph[0].axis[1].fMinor = graph[0].data.fMinorYm;
+  graph[0].axis[2].fMinor = graph[0].data.fMinorXp;
+  graph[0].axis[3].fMinor = graph[0].data.fMinorYp;
 
   graph[0].axis[0].pad = graph[0].data.padXm;
Index: trunk/Ohana/src/kapa2/src/bDrawFrame.c
===================================================================
--- trunk/Ohana/src/kapa2/src/bDrawFrame.c	(revision 38553)
+++ trunk/Ohana/src/kapa2/src/bDrawFrame.c	(revision 38986)
@@ -84,4 +84,11 @@
   y = fy + (value-min)*dfy/(max - min);
 
+  if ((naxis == 0) || (naxis == 2)) {
+    x = MIN(MAX(x, fx),fx+dfx);
+  }
+  if ((naxis == 1) || (naxis == 3)) {
+    y = MAX(MIN(y, fy),fy+dfy);
+  }
+
   dir = ((naxis == 0) || (naxis == 1)) ? -1 : +1;
   dx = dir*size*dfy*n;	
