Program: 70650
Project: 70650
Buggy source:
/** 
 * @param xs
 * @param ax
 * @param dsi
 * @param iType
 * @param dStart
 * @param dEnd
 * @param scModel
 * @param axisOrigin
 * @param fs
 * @param rtc
 * @param direction
 * @param zoomFactor 1 is default factor
 * @param iMarginPercent the percentage of margin area for display some charts, such as bubble. 0 means no margin
 * @return AutoScale instance
 * @throws ChartException
 */
static final AutoScale computeScale(IDisplayServer xs,OneAxis ax,DataSetIterator dsi,int iType,double dStart,double dEnd,Scale scModel,AxisOrigin axisOrigin,FormatSpecifier fs,RunTimeContext rtc,int direction,double zoomFactor,int iMarginPercent) throws ChartException {
  final Label la=ax.getLabel();
  final int iLabelLocation=ax.getLabelPosition();
  final int iOrientation=ax.getOrientation();
  DataElement oMinimum=scModel.getMin();
  DataElement oMaximum=scModel.getMax();
  final Double oStep=scModel.isSetStep() ? new Double(scModel.getStep()) : null;
  final Integer oStepNumber=scModel.isSetStepNumber() ? Integer.valueOf(scModel.getStepNumber()) : null;
  AutoScale sc=null;
  AutoScale scCloned=null;
  final Object oMinValue, oMaxValue;
  final boolean bIsPercent=ax.getModelAxis().isPercent();
  if (scModel.isSetFactor() && (iType & LINEAR) == LINEAR && !ax.isCategoryScale()) {
    double factor=scModel.getFactor() * 72 / xs.getDpiResolution();
    Object oValue;
    double dValue, dMinValue=Double.MAX_VALUE, dMaxValue=-Double.MAX_VALUE;
    dsi.reset();
    double dPrecision=0;
    while (dsi.hasNext()) {
      oValue=dsi.next();
      if (oValue == null) {
        continue;
      }
      dValue=((Double)oValue).doubleValue();
      if (dValue < dMinValue)       dMinValue=dValue;
      if (dValue > dMaxValue)       dMaxValue=dValue;
      dPrecision=getPrecision(dPrecision,dValue,fs,rtc.getULocale(),bIsPercent);
    }
    if (dMinValue * dMaxValue > 0) {
      if (dMinValue > 0) {
        dMinValue=0d;
      }
 else {
        dMaxValue=0d;
      }
    }
    double length=Math.abs(dEnd - dStart);
    double valueLength=length * factor;
    dMaxValue=dMinValue + valueLength;
    double dStep=1;
    double dDelta=dMaxValue - dMinValue;
    if (dDelta == 0) {
      dStep=dPrecision;
    }
 else {
      dStep=Math.floor(Math.log(dDelta) / LOG_10);
      dStep=Math.pow(10,dStep);
      if (dStep < dPrecision) {
        dStep=dPrecision;
      }
    }
    sc=new AutoScale(iType,new Double(0),new Double(0));
    sc.setStep(new Double(dStep));
    sc.bStepFixed=true;
    sc.oStepNumber=oStepNumber;
    sc.setData(dsi);
    sc.setDirection(direction);
    sc.fs=fs;
    sc.rtc=rtc;
    sc.bAxisLabelStaggered=ax.isAxisLabelStaggered();
    sc.iLabelShowingInterval=ax.getLableShowingInterval();
    sc.bTickBetweenCategories=ax.isTickBwtweenCategories();
    sc.dZoomFactor=zoomFactor;
    sc.dPrecision=dPrecision;
    sc.iMarginPercent=iMarginPercent;
    sc.bExpandMinMax=scModel.isAutoExpand();
    sc.bAlignZero=ax.getModelAxis().isAligned();
    setStepToScale(sc,oStep,null,rtc);
    oMinValue=new Double(dMinValue);
    oMaxValue=new Double(dMaxValue);
    sc.setMinimum(oMinValue);
    sc.setMaximum(oMaxValue);
    sc.setFactor(factor);
    sc.computeTicks(xs,la,iLabelLocation,iOrientation,dStart,dEnd,false,null);
    sc.setData(dsi);
    return sc;
  }
  if ((iType & TEXT) == TEXT || ax.isCategoryScale()) {
    sc=new AutoScale(iType);
    sc.fs=fs;
    sc.rtc=rtc;
    sc.bCategoryScale=true;
    sc.bAxisLabelStaggered=ax.isAxisLabelStaggered();
    sc.iLabelShowingInterval=ax.getLableShowingInterval();
    sc.bTickBetweenCategories=ax.isTickBwtweenCategories();
    sc.dZoomFactor=zoomFactor;
    sc.iMarginPercent=iMarginPercent;
    sc.bExpandMinMax=scModel.isAutoExpand();
    sc.bAlignZero=ax.getModelAxis().isSetAligned();
    sc.setData(dsi);
    sc.setDirection(direction);
    sc.computeTicks(xs,ax.getLabel(),iLabelLocation,iOrientation,dStart,dEnd,false,null);
    oMinValue=null;
    oMaxValue=null;
  }
 else   if ((iType & LINEAR) == LINEAR) {
    Object oValue;
    double dValue, dMinValue=Double.MAX_VALUE, dMaxValue=-Double.MAX_VALUE;
    dsi.reset();
    double dPrecision=0;
    while (dsi.hasNext()) {
      oValue=dsi.next();
      if (oValue == null) {
        continue;
      }
      dValue=((Double)oValue).doubleValue();
      if (dValue < dMinValue)       dMinValue=dValue;
      if (dValue > dMaxValue)       dMaxValue=dValue;
      dPrecision=getPrecision(dPrecision,dValue,fs,rtc.getULocale(),bIsPercent);
    }
    if (axisOrigin != null && axisOrigin.getType().equals(IntersectionType.VALUE_LITERAL) && axisOrigin.getValue() instanceof NumberDataElement) {
      double origin=asDouble(axisOrigin.getValue()).doubleValue();
      if (oMinimum == null && origin < dMinValue) {
        oMinimum=axisOrigin.getValue();
      }
      if (oMaximum == null && origin > dMaxValue) {
        oMaximum=axisOrigin.getValue();
      }
    }
    final double dAbsMax=Math.abs(dMaxValue);
    final double dAbsMin=Math.abs(dMinValue);
    double dStep=Math.max(dAbsMax,dAbsMin);
    double dDelta=dMaxValue - dMinValue;
    if (dDelta == 0) {
      dStep=dPrecision;
    }
 else {
      dStep=Math.floor(Math.log(dDelta) / LOG_10);
      dStep=Math.pow(10,dStep);
      if (dStep < dPrecision) {
        dStep=dPrecision;
      }
    }
    sc=new AutoScale(iType,new Double(0),new Double(0));
    sc.setStep(new Double(dStep));
    sc.oStepNumber=oStepNumber;
    sc.setData(dsi);
    sc.setDirection(direction);
    sc.fs=fs;
    sc.rtc=rtc;
    sc.bAxisLabelStaggered=ax.isAxisLabelStaggered();
    sc.iLabelShowingInterval=ax.getLableShowingInterval();
    sc.bTickBetweenCategories=ax.isTickBwtweenCategories();
    sc.dZoomFactor=zoomFactor;
    sc.dPrecision=dPrecision;
    sc.iMarginPercent=iMarginPercent;
    sc.bExpandMinMax=scModel.isAutoExpand();
    sc.bAlignZero=ax.getModelAxis().isSetAligned();
    setNumberMinMaxToScale(sc,oMinimum,oMaximum,rtc,ax);
    setStepToScale(sc,oStep,oStepNumber,rtc);
    oMinValue=new Double(dMinValue);
    oMaxValue=new Double(dMaxValue);
    sc.updateAxisMinMax(oMinValue,oMaxValue);
  }
 else   if ((iType & LOGARITHMIC) == LOGARITHMIC) {
    Object oValue;
    double dValue, dMinValue=Double.MAX_VALUE, dMaxValue=-Double.MAX_VALUE;
    if ((iType & PERCENT) == PERCENT) {
      dMinValue=0;
      dMaxValue=100;
    }
 else {
      dsi.reset();
      while (dsi.hasNext()) {
        oValue=dsi.next();
        if (oValue == null) {
          continue;
        }
        dValue=((Double)oValue).doubleValue();
        if (dValue < dMinValue)         dMinValue=dValue;
        if (dValue > dMaxValue)         dMaxValue=dValue;
      }
      if (axisOrigin != null && axisOrigin.getType().equals(IntersectionType.VALUE_LITERAL) && axisOrigin.getValue() instanceof NumberDataElement) {
        double origin=asDouble(axisOrigin.getValue()).doubleValue();
        if (oMinimum == null && origin < dMinValue) {
          oMinimum=axisOrigin.getValue();
        }
        if (oMaximum == null && origin > dMaxValue) {
          oMaximum=axisOrigin.getValue();
        }
      }
      if (dMinValue == 0) {
        dMinValue=dMaxValue > 0 ? 1 : -1;
      }
    }
    sc=new AutoScale(iType,new Double(0),new Double(0));
    sc.setStep(new Double(10));
    sc.oStepNumber=oStepNumber;
    sc.fs=fs;
    sc.rtc=rtc;
    sc.bAxisLabelStaggered=ax.isAxisLabelStaggered();
    sc.iLabelShowingInterval=ax.getLableShowingInterval();
    sc.bTickBetweenCategories=ax.isTickBwtweenCategories();
    sc.dZoomFactor=zoomFactor;
    sc.iMarginPercent=iMarginPercent;
    sc.setData(dsi);
    sc.setDirection(direction);
    sc.bExpandMinMax=scModel.isAutoExpand();
    sc.bAlignZero=ax.getModelAxis().isSetAligned();
    setNumberMinMaxToScale(sc,oMinimum,oMaximum,rtc,ax);
    setStepToScale(sc,oStep,oStepNumber,rtc);
    oMinValue=new Double(dMinValue);
    oMaxValue=new Double(dMaxValue);
    sc.updateAxisMinMax(oMinValue,oMaxValue);
    if ((iType & PERCENT) == PERCENT) {
      sc.bStepFixed=true;
      sc.bMaximumFixed=true;
      sc.bMinimumFixed=true;
      sc.computeTicks(xs,ax.getLabel(),iLabelLocation,iOrientation,dStart,dEnd,false,null);
      return sc;
    }
  }
 else   if ((iType & DATE_TIME) == DATE_TIME) {
    Calendar cValue;
    Calendar caMin=null, caMax=null;
    dsi.reset();
    while (dsi.hasNext()) {
      cValue=(Calendar)dsi.next();
      if (cValue == null) {
        continue;
      }
      if (caMin == null) {
        caMin=cValue;
      }
      if (caMax == null) {
        caMax=cValue;
      }
      if (cValue.before(caMin))       caMin=cValue;
 else       if (cValue.after(caMax))       caMax=cValue;
    }
    oMinValue=new CDateTime(caMin);
    oMaxValue=new CDateTime(caMax);
    if (axisOrigin != null && axisOrigin.getType().equals(IntersectionType.VALUE_LITERAL) && axisOrigin.getValue() instanceof DateTimeDataElement) {
      CDateTime origin=asDateTime(axisOrigin.getValue());
      if (oMinimum == null && origin.before(oMinValue)) {
        oMinimum=axisOrigin.getValue();
      }
      if (oMaximum == null && origin.after(oMaxValue)) {
        oMaximum=axisOrigin.getValue();
      }
    }
    int iUnit;
    if (oStep != null || oStepNumber != null) {
      iUnit=ChartUtil.convertUnitTypeToCalendarConstant(scModel.getUnit());
    }
 else {
      iUnit=CDateTime.getPreferredUnit((CDateTime)oMinValue,(CDateTime)oMaxValue);
    }
    if (iUnit == 0)     iUnit=Calendar.SECOND;
    CDateTime cdtMinAxis=((CDateTime)oMinValue).backward(iUnit,1);
    CDateTime cdtMaxAxis=((CDateTime)oMaxValue).forward(iUnit,1);
    cdtMinAxis.clearBelow(iUnit);
    cdtMaxAxis.clearBelow(iUnit);
    sc=new AutoScale(DATE_TIME,cdtMinAxis,cdtMaxAxis);
    sc.setStep(Integer.valueOf(1));
    sc.oStepNumber=oStepNumber;
    sc.oUnit=Integer.valueOf(iUnit);
    sc.iMinUnit=oMinValue.equals(oMaxValue) ? getUnitId(iUnit) : getMinUnitId(fs,rtc);
    sc.setDirection(direction);
    sc.fs=fs;
    sc.rtc=rtc;
    sc.bAxisLabelStaggered=ax.isAxisLabelStaggered();
    sc.iLabelShowingInterval=ax.getLableShowingInterval();
    sc.bTickBetweenCategories=ax.isTickBwtweenCategories();
    sc.dZoomFactor=zoomFactor;
    sc.iMarginPercent=iMarginPercent;
    sc.bExpandMinMax=scModel.isAutoExpand();
    sc.bAlignZero=ax.getModelAxis().isSetAligned();
    if (oMinimum != null) {
      if (oMinimum instanceof DateTimeDataElement) {
        sc.oMinimum=((DateTimeDataElement)oMinimum).getValueAsCDateTime();
        sc.oMinimumFixed=((DateTimeDataElement)oMinimum).getValueAsCDateTime();
      }
 else {
        throw new ChartException(ChartEnginePlugin.ID,ChartException.GENERATION,"String_Node_Str",new Object[]{oMinimum,ax.getModelAxis().getType().getName()},Messages.getResourceBundle(rtc.getULocale()));
      }
      sc.bMinimumFixed=true;
    }
    if (oMaximum != null) {
      if (oMaximum instanceof DateTimeDataElement) {
        sc.oMaximum=((DateTimeDataElement)oMaximum).getValueAsCDateTime();
        sc.oMaximumFixed=((DateTimeDataElement)oMaximum).getValueAsCDateTime();
      }
 else {
        throw new ChartException(ChartEnginePlugin.ID,ChartException.GENERATION,"String_Node_Str",new Object[]{sc.oMaximum,ax.getModelAxis().getType().getName()},Messages.getResourceBundle(rtc.getULocale()));
      }
      sc.bMaximumFixed=true;
    }
    if (sc.bMaximumFixed && sc.bMinimumFixed) {
      if (((CDateTime)sc.oMinimum).after(sc.oMaximum)) {
        throw new ChartException(ChartEnginePlugin.ID,ChartException.GENERATION,"String_Node_Str",new Object[]{sc.oMinimum,sc.oMaximum},Messages.getResourceBundle(rtc.getULocale()));
      }
    }
    setStepToScale(sc,oStep,oStepNumber,rtc);
    sc.updateAxisMinMax(oMinValue,oMaxValue);
  }
 else {
    oMinValue=null;
    oMaxValue=null;
  }
  sc.bLabelWithinAxes=ax.getModelAxis().isLabelWithinAxes();
  if ((iType & TEXT) != TEXT && !ax.isCategoryScale()) {
    sc.computeTicks(xs,la,iLabelLocation,iOrientation,dStart,dEnd,false,null);
    dStart=sc.dStart;
    dEnd=sc.dEnd;
    boolean bFirstFit=sc.checkFit(xs,la,iLabelLocation);
    boolean bFits=bFirstFit;
    boolean bZoomSuccess=false;
    for (int i=0; bFits == bFirstFit && i < 50; i++) {
      bZoomSuccess=true;
      scCloned=(AutoScale)sc.clone();
      if (sc.bStepFixed || rtc.getSharedScale() != null && rtc.getSharedScale().isShared()) {
        break;
      }
      if (bFirstFit) {
        if (!bFits) {
          break;
        }
        bZoomSuccess=sc.zoomIn();
      }
 else {
        if (!bFits && sc.getTickCordinates().size() == 2) {
          break;
        }
        bZoomSuccess=sc.zoomOut();
      }
      if (!bZoomSuccess)       break;
      sc.updateAxisMinMax(oMinValue,oMaxValue);
      sc.computeTicks(xs,la,iLabelLocation,iOrientation,dStart,dEnd,false,null);
      bFits=sc.checkFit(xs,la,iLabelLocation);
      if (!bFits && sc.getTickCordinates().size() == 2) {
        sc=scCloned;
        break;
      }
    }
    if (scCloned != null && bFirstFit && bZoomSuccess) {
      sc=scCloned;
    }
    updateSharedScaleContext(rtc,iType,sc.tmpSC);
  }
  sc.setData(dsi);
  return sc;
}

Fixed version (ground truth):
/** 
 * @param xs
 * @param ax
 * @param dsi
 * @param iType
 * @param dStart
 * @param dEnd
 * @param scModel
 * @param axisOrigin
 * @param fs
 * @param rtc
 * @param direction
 * @param zoomFactor 1 is default factor
 * @param iMarginPercent the percentage of margin area for display some charts, such as bubble. 0 means no margin
 * @return AutoScale instance
 * @throws ChartException
 */
static final AutoScale computeScale(IDisplayServer xs,OneAxis ax,DataSetIterator dsi,int iType,double dStart,double dEnd,Scale scModel,AxisOrigin axisOrigin,FormatSpecifier fs,RunTimeContext rtc,int direction,double zoomFactor,int iMarginPercent) throws ChartException {
  final Label la=ax.getLabel();
  final int iLabelLocation=ax.getLabelPosition();
  final int iOrientation=ax.getOrientation();
  DataElement oMinimum=scModel.getMin();
  DataElement oMaximum=scModel.getMax();
  final Double oStep=scModel.isSetStep() ? new Double(scModel.getStep()) : null;
  final Integer oStepNumber=scModel.isSetStepNumber() ? Integer.valueOf(scModel.getStepNumber()) : null;
  AutoScale sc=null;
  AutoScale scCloned=null;
  final Object oMinValue, oMaxValue;
  final boolean bIsPercent=ax.getModelAxis().isPercent();
  if (scModel.isSetFactor() && (iType & LINEAR) == LINEAR && !ax.isCategoryScale()) {
    double factor=scModel.getFactor() * 72 / xs.getDpiResolution();
    Object oValue;
    double dValue, dMinValue=Double.MAX_VALUE, dMaxValue=-Double.MAX_VALUE;
    dsi.reset();
    double dPrecision=0;
    while (dsi.hasNext()) {
      oValue=dsi.next();
      if (oValue == null) {
        continue;
      }
      dValue=((Double)oValue).doubleValue();
      if (dValue < dMinValue)       dMinValue=dValue;
      if (dValue > dMaxValue)       dMaxValue=dValue;
      dPrecision=getPrecision(dPrecision,dValue,fs,rtc.getULocale(),bIsPercent);
    }
    if (oMinimum != null && oMinimum instanceof NumberDataElement) {
      dMinValue=((NumberDataElement)oMinimum).getValue();
    }
    double length=Math.abs(dEnd - dStart);
    double valueLength=length * factor;
    dMaxValue=dMinValue + valueLength;
    double dStep=1;
    double dDelta=dMaxValue - dMinValue;
    if (dDelta == 0) {
      dStep=dPrecision;
    }
 else {
      dStep=Math.floor(Math.log(dDelta) / LOG_10);
      dStep=Math.pow(10,dStep);
      if (dStep < dPrecision) {
        dStep=dPrecision;
      }
    }
    sc=new AutoScale(iType,new Double(0),new Double(0));
    sc.setStep(new Double(dStep));
    sc.bStepFixed=true;
    sc.oStepNumber=oStepNumber;
    sc.setData(dsi);
    sc.setDirection(direction);
    sc.fs=fs;
    sc.rtc=rtc;
    sc.bAxisLabelStaggered=ax.isAxisLabelStaggered();
    sc.iLabelShowingInterval=ax.getLableShowingInterval();
    sc.bTickBetweenCategories=ax.isTickBwtweenCategories();
    sc.dZoomFactor=zoomFactor;
    sc.dPrecision=dPrecision;
    sc.iMarginPercent=iMarginPercent;
    sc.bExpandMinMax=scModel.isAutoExpand();
    sc.bAlignZero=ax.getModelAxis().isAligned();
    setStepToScale(sc,oStep,null,rtc);
    oMinValue=new Double(dMinValue);
    oMaxValue=new Double(dMaxValue);
    sc.setMinimum(oMinValue);
    sc.setMaximum(oMaxValue);
    sc.setFactor(factor);
    sc.computeTicks(xs,la,iLabelLocation,iOrientation,dStart,dEnd,false,null);
    sc.setData(dsi);
    return sc;
  }
  if ((iType & TEXT) == TEXT || ax.isCategoryScale()) {
    sc=new AutoScale(iType);
    sc.fs=fs;
    sc.rtc=rtc;
    sc.bCategoryScale=true;
    sc.bAxisLabelStaggered=ax.isAxisLabelStaggered();
    sc.iLabelShowingInterval=ax.getLableShowingInterval();
    sc.bTickBetweenCategories=ax.isTickBwtweenCategories();
    sc.dZoomFactor=zoomFactor;
    sc.iMarginPercent=iMarginPercent;
    sc.bExpandMinMax=scModel.isAutoExpand();
    sc.bAlignZero=ax.getModelAxis().isSetAligned();
    sc.setData(dsi);
    sc.setDirection(direction);
    sc.computeTicks(xs,ax.getLabel(),iLabelLocation,iOrientation,dStart,dEnd,false,null);
    oMinValue=null;
    oMaxValue=null;
  }
 else   if ((iType & LINEAR) == LINEAR) {
    Object oValue;
    double dValue, dMinValue=Double.MAX_VALUE, dMaxValue=-Double.MAX_VALUE;
    dsi.reset();
    double dPrecision=0;
    while (dsi.hasNext()) {
      oValue=dsi.next();
      if (oValue == null) {
        continue;
      }
      dValue=((Double)oValue).doubleValue();
      if (dValue < dMinValue)       dMinValue=dValue;
      if (dValue > dMaxValue)       dMaxValue=dValue;
      dPrecision=getPrecision(dPrecision,dValue,fs,rtc.getULocale(),bIsPercent);
    }
    if (axisOrigin != null && axisOrigin.getType().equals(IntersectionType.VALUE_LITERAL) && axisOrigin.getValue() instanceof NumberDataElement) {
      double origin=asDouble(axisOrigin.getValue()).doubleValue();
      if (oMinimum == null && origin < dMinValue) {
        oMinimum=axisOrigin.getValue();
      }
      if (oMaximum == null && origin > dMaxValue) {
        oMaximum=axisOrigin.getValue();
      }
    }
    final double dAbsMax=Math.abs(dMaxValue);
    final double dAbsMin=Math.abs(dMinValue);
    double dStep=Math.max(dAbsMax,dAbsMin);
    double dDelta=dMaxValue - dMinValue;
    if (dDelta == 0) {
      dStep=dPrecision;
    }
 else {
      dStep=Math.floor(Math.log(dDelta) / LOG_10);
      dStep=Math.pow(10,dStep);
      if (dStep < dPrecision) {
        dStep=dPrecision;
      }
    }
    sc=new AutoScale(iType,new Double(0),new Double(0));
    sc.setStep(new Double(dStep));
    sc.oStepNumber=oStepNumber;
    sc.setData(dsi);
    sc.setDirection(direction);
    sc.fs=fs;
    sc.rtc=rtc;
    sc.bAxisLabelStaggered=ax.isAxisLabelStaggered();
    sc.iLabelShowingInterval=ax.getLableShowingInterval();
    sc.bTickBetweenCategories=ax.isTickBwtweenCategories();
    sc.dZoomFactor=zoomFactor;
    sc.dPrecision=dPrecision;
    sc.iMarginPercent=iMarginPercent;
    sc.bExpandMinMax=scModel.isAutoExpand();
    sc.bAlignZero=ax.getModelAxis().isSetAligned();
    setNumberMinMaxToScale(sc,oMinimum,oMaximum,rtc,ax);
    setStepToScale(sc,oStep,oStepNumber,rtc);
    oMinValue=new Double(dMinValue);
    oMaxValue=new Double(dMaxValue);
    sc.updateAxisMinMax(oMinValue,oMaxValue);
  }
 else   if ((iType & LOGARITHMIC) == LOGARITHMIC) {
    Object oValue;
    double dValue, dMinValue=Double.MAX_VALUE, dMaxValue=-Double.MAX_VALUE;
    if ((iType & PERCENT) == PERCENT) {
      dMinValue=0;
      dMaxValue=100;
    }
 else {
      dsi.reset();
      while (dsi.hasNext()) {
        oValue=dsi.next();
        if (oValue == null) {
          continue;
        }
        dValue=((Double)oValue).doubleValue();
        if (dValue < dMinValue)         dMinValue=dValue;
        if (dValue > dMaxValue)         dMaxValue=dValue;
      }
      if (axisOrigin != null && axisOrigin.getType().equals(IntersectionType.VALUE_LITERAL) && axisOrigin.getValue() instanceof NumberDataElement) {
        double origin=asDouble(axisOrigin.getValue()).doubleValue();
        if (oMinimum == null && origin < dMinValue) {
          oMinimum=axisOrigin.getValue();
        }
        if (oMaximum == null && origin > dMaxValue) {
          oMaximum=axisOrigin.getValue();
        }
      }
      if (dMinValue == 0) {
        dMinValue=dMaxValue > 0 ? 1 : -1;
      }
    }
    sc=new AutoScale(iType,new Double(0),new Double(0));
    sc.setStep(new Double(10));
    sc.oStepNumber=oStepNumber;
    sc.fs=fs;
    sc.rtc=rtc;
    sc.bAxisLabelStaggered=ax.isAxisLabelStaggered();
    sc.iLabelShowingInterval=ax.getLableShowingInterval();
    sc.bTickBetweenCategories=ax.isTickBwtweenCategories();
    sc.dZoomFactor=zoomFactor;
    sc.iMarginPercent=iMarginPercent;
    sc.setData(dsi);
    sc.setDirection(direction);
    sc.bExpandMinMax=scModel.isAutoExpand();
    sc.bAlignZero=ax.getModelAxis().isSetAligned();
    setNumberMinMaxToScale(sc,oMinimum,oMaximum,rtc,ax);
    setStepToScale(sc,oStep,oStepNumber,rtc);
    oMinValue=new Double(dMinValue);
    oMaxValue=new Double(dMaxValue);
    sc.updateAxisMinMax(oMinValue,oMaxValue);
    if ((iType & PERCENT) == PERCENT) {
      sc.bStepFixed=true;
      sc.bMaximumFixed=true;
      sc.bMinimumFixed=true;
      sc.computeTicks(xs,ax.getLabel(),iLabelLocation,iOrientation,dStart,dEnd,false,null);
      return sc;
    }
  }
 else   if ((iType & DATE_TIME) == DATE_TIME) {
    Calendar cValue;
    Calendar caMin=null, caMax=null;
    dsi.reset();
    while (dsi.hasNext()) {
      cValue=(Calendar)dsi.next();
      if (cValue == null) {
        continue;
      }
      if (caMin == null) {
        caMin=cValue;
      }
      if (caMax == null) {
        caMax=cValue;
      }
      if (cValue.before(caMin))       caMin=cValue;
 else       if (cValue.after(caMax))       caMax=cValue;
    }
    oMinValue=new CDateTime(caMin);
    oMaxValue=new CDateTime(caMax);
    if (axisOrigin != null && axisOrigin.getType().equals(IntersectionType.VALUE_LITERAL) && axisOrigin.getValue() instanceof DateTimeDataElement) {
      CDateTime origin=asDateTime(axisOrigin.getValue());
      if (oMinimum == null && origin.before(oMinValue)) {
        oMinimum=axisOrigin.getValue();
      }
      if (oMaximum == null && origin.after(oMaxValue)) {
        oMaximum=axisOrigin.getValue();
      }
    }
    int iUnit;
    if (oStep != null || oStepNumber != null) {
      iUnit=ChartUtil.convertUnitTypeToCalendarConstant(scModel.getUnit());
    }
 else {
      iUnit=CDateTime.getPreferredUnit((CDateTime)oMinValue,(CDateTime)oMaxValue);
    }
    if (iUnit == 0)     iUnit=Calendar.SECOND;
    CDateTime cdtMinAxis=((CDateTime)oMinValue).backward(iUnit,1);
    CDateTime cdtMaxAxis=((CDateTime)oMaxValue).forward(iUnit,1);
    cdtMinAxis.clearBelow(iUnit);
    cdtMaxAxis.clearBelow(iUnit);
    sc=new AutoScale(DATE_TIME,cdtMinAxis,cdtMaxAxis);
    sc.setStep(Integer.valueOf(1));
    sc.oStepNumber=oStepNumber;
    sc.oUnit=Integer.valueOf(iUnit);
    sc.iMinUnit=oMinValue.equals(oMaxValue) ? getUnitId(iUnit) : getMinUnitId(fs,rtc);
    sc.setDirection(direction);
    sc.fs=fs;
    sc.rtc=rtc;
    sc.bAxisLabelStaggered=ax.isAxisLabelStaggered();
    sc.iLabelShowingInterval=ax.getLableShowingInterval();
    sc.bTickBetweenCategories=ax.isTickBwtweenCategories();
    sc.dZoomFactor=zoomFactor;
    sc.iMarginPercent=iMarginPercent;
    sc.bExpandMinMax=scModel.isAutoExpand();
    sc.bAlignZero=ax.getModelAxis().isSetAligned();
    if (oMinimum != null) {
      if (oMinimum instanceof DateTimeDataElement) {
        sc.oMinimum=((DateTimeDataElement)oMinimum).getValueAsCDateTime();
        sc.oMinimumFixed=((DateTimeDataElement)oMinimum).getValueAsCDateTime();
      }
 else {
        throw new ChartException(ChartEnginePlugin.ID,ChartException.GENERATION,"String_Node_Str",new Object[]{oMinimum,ax.getModelAxis().getType().getName()},Messages.getResourceBundle(rtc.getULocale()));
      }
      sc.bMinimumFixed=true;
    }
    if (oMaximum != null) {
      if (oMaximum instanceof DateTimeDataElement) {
        sc.oMaximum=((DateTimeDataElement)oMaximum).getValueAsCDateTime();
        sc.oMaximumFixed=((DateTimeDataElement)oMaximum).getValueAsCDateTime();
      }
 else {
        throw new ChartException(ChartEnginePlugin.ID,ChartException.GENERATION,"String_Node_Str",new Object[]{sc.oMaximum,ax.getModelAxis().getType().getName()},Messages.getResourceBundle(rtc.getULocale()));
      }
      sc.bMaximumFixed=true;
    }
    if (sc.bMaximumFixed && sc.bMinimumFixed) {
      if (((CDateTime)sc.oMinimum).after(sc.oMaximum)) {
        throw new ChartException(ChartEnginePlugin.ID,ChartException.GENERATION,"String_Node_Str",new Object[]{sc.oMinimum,sc.oMaximum},Messages.getResourceBundle(rtc.getULocale()));
      }
    }
    setStepToScale(sc,oStep,oStepNumber,rtc);
    sc.updateAxisMinMax(oMinValue,oMaxValue);
  }
 else {
    oMinValue=null;
    oMaxValue=null;
  }
  sc.bLabelWithinAxes=ax.getModelAxis().isLabelWithinAxes();
  if ((iType & TEXT) != TEXT && !ax.isCategoryScale()) {
    sc.computeTicks(xs,la,iLabelLocation,iOrientation,dStart,dEnd,false,null);
    dStart=sc.dStart;
    dEnd=sc.dEnd;
    boolean bFirstFit=sc.checkFit(xs,la,iLabelLocation);
    boolean bFits=bFirstFit;
    boolean bZoomSuccess=false;
    for (int i=0; bFits == bFirstFit && i < 50; i++) {
      bZoomSuccess=true;
      scCloned=(AutoScale)sc.clone();
      if (sc.bStepFixed || rtc.getSharedScale() != null && rtc.getSharedScale().isShared()) {
        break;
      }
      if (bFirstFit) {
        if (!bFits) {
          break;
        }
        bZoomSuccess=sc.zoomIn();
      }
 else {
        if (!bFits && sc.getTickCordinates().size() == 2) {
          break;
        }
        bZoomSuccess=sc.zoomOut();
      }
      if (!bZoomSuccess)       break;
      sc.updateAxisMinMax(oMinValue,oMaxValue);
      sc.computeTicks(xs,la,iLabelLocation,iOrientation,dStart,dEnd,false,null);
      bFits=sc.checkFit(xs,la,iLabelLocation);
      if (!bFits && sc.getTickCordinates().size() == 2) {
        sc=scCloned;
        break;
      }
    }
    if (scCloned != null && bFirstFit && bZoomSuccess) {
      sc=scCloned;
    }
    updateSharedScaleContext(rtc,iType,sc.tmpSC);
  }
  sc.setData(dsi);
  return sc;
}