Program: 67983
Project: 67983
Buggy source:
/** 
 * @param xs
 * @param ax
 * @param dsi
 * @param iType
 * @param dStart
 * @param dEnd
 * @param axisOrigin
 * @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,AxisOrigin axisOrigin,RunTimeContext rtc,int direction,double zoomFactor,int iMarginPercent,PlotComputation plotComp) throws ChartException {
  final Scale scModel=ax.getModelAxis().getScale();
  final FormatSpecifier fs=ax.getFormatSpecifier();
  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=Double.NaN;
    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;
      }
    }
    ScaleInfo info=new ScaleInfo(plotComp,iType,rtc,fs,ax,direction,scModel.isAutoExpand()).dZoomFactor(zoomFactor).iMarginPercent(iMarginPercent).dPrecision(dPrecision).bStepFixed(true).dsiData(dsi).dFactor(factor);
    sc=new AutoScale(info);
    sc.setMinimum(Double.valueOf(0));
    sc.setMaximum(Double.valueOf(0));
    sc.setStep(new Double(dStep));
    sc.setStepNumber(oStepNumber);
    setStepToScale(sc,oStep,null,rtc);
    oMinValue=new Double(dMinValue);
    oMaxValue=new Double(dMaxValue);
    sc.setMinimum(oMinValue);
    sc.setMaximum(oMaxValue);
    sc.computeTicks(xs,la,iLabelLocation,iOrientation,dStart,dEnd,false,null);
    sc.setData(dsi);
    return sc;
  }
  if ((iType & TEXT) == TEXT || ax.isCategoryScale()) {
    ScaleInfo info=new ScaleInfo(plotComp,iType,rtc,fs,ax,direction,scModel.isAutoExpand()).dZoomFactor(zoomFactor).iMarginPercent(iMarginPercent);
    sc=new AutoScale(info);
    sc.setData(dsi);
    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=Double.NaN;
    ;
    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;
      }
    }
    ScaleInfo info=new ScaleInfo(plotComp,iType,rtc,fs,ax,direction,scModel.isAutoExpand()).dZoomFactor(zoomFactor).iMarginPercent(iMarginPercent).dPrecision(dPrecision);
    sc=new AutoScale(info);
    sc.setMaximum(Double.valueOf(0));
    sc.setMinimum(Double.valueOf(0));
    sc.setStep(new Double(dStep));
    sc.setStepNumber(oStepNumber);
    sc.setData(dsi);
    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;
      }
    }
    ScaleInfo info=new ScaleInfo(plotComp,iType,rtc,fs,ax,direction,scModel.isAutoExpand()).dZoomFactor(zoomFactor).iMarginPercent(iMarginPercent);
    sc=new AutoScale(info);
    sc.setMaximum(Double.valueOf(0));
    sc.setMinimum(Double.valueOf(0));
    sc.setStep(new Double(10));
    sc.setStepNumber(oStepNumber);
    sc.setData(dsi);
    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.info.bStepFixed(true);
      sc.info.bMaximumFixed(true);
      sc.info.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);
    ScaleInfo info=new ScaleInfo(plotComp,DATE_TIME,rtc,fs,ax,direction,scModel.isAutoExpand()).dZoomFactor(zoomFactor).iMarginPercent(iMarginPercent).iMinUnit(oMinValue.equals(oMaxValue) ? getUnitId(iUnit) : getMinUnitId(fs,rtc));
    sc=new AutoScale(info);
    sc.setMaximum(cdtMaxAxis);
    sc.setMinimum(cdtMinAxis);
    sc.setStep(Integer.valueOf(1));
    sc.setStepNumber(oStepNumber);
    sc.context.setUnit(Integer.valueOf(iUnit));
    if (oMinimum != null) {
      if (oMinimum instanceof DateTimeDataElement) {
        sc.setMinimum(((DateTimeDataElement)oMinimum).getValueAsCDateTime());
        sc.info.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.info.bMinimumFixed(true);
    }
    if (oMaximum != null) {
      if (oMaximum instanceof DateTimeDataElement) {
        sc.setMaximum(((DateTimeDataElement)oMaximum).getValueAsCDateTime());
        sc.info.oMaximumFixed(((DateTimeDataElement)oMaximum).getValueAsCDateTime());
      }
 else {
        throw new ChartException(ChartEnginePlugin.ID,ChartException.GENERATION,"String_Node_Str",new Object[]{sc.getMaximum(),ax.getModelAxis().getType().getName()},Messages.getResourceBundle(rtc.getULocale()));
      }
      sc.info.bMaximumFixed(true);
    }
    if (sc.info.bMaximumFixed && sc.info.bMinimumFixed) {
      if (((CDateTime)sc.getMinimum()).after(sc.getMaximum())) {
        throw new ChartException(ChartEnginePlugin.ID,ChartException.GENERATION,"String_Node_Str",new Object[]{sc.getMinimum(),sc.getMaximum()},Messages.getResourceBundle(rtc.getULocale()));
      }
    }
    setStepToScale(sc,oStep,oStepNumber,rtc);
    sc.updateAxisMinMax(oMinValue,oMaxValue);
  }
 else {
    oMinValue=null;
    oMaxValue=null;
  }
  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.info.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);
  }
  if (sc != null) {
    sc.setData(dsi);
  }
  return sc;
}

Fixed version (ground truth):
/** 
 * @param xs
 * @param ax
 * @param dsi
 * @param iType
 * @param dStart
 * @param dEnd
 * @param axisOrigin
 * @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,AxisOrigin axisOrigin,RunTimeContext rtc,int direction,double zoomFactor,int iMarginPercent,PlotComputation plotComp) throws ChartException {
  final Scale scModel=ax.getModelAxis().getScale();
  final FormatSpecifier fs=ax.getFormatSpecifier();
  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=Double.NaN;
    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;
      }
    }
    ScaleInfo info=new ScaleInfo(plotComp,iType,rtc,fs,ax,direction,scModel.isAutoExpand()).dZoomFactor(zoomFactor).iMarginPercent(iMarginPercent).dPrecision(dPrecision).bStepFixed(true).dsiData(dsi).dFactor(factor);
    sc=new AutoScale(info);
    sc.setMinimum(Double.valueOf(0));
    sc.setMaximum(Double.valueOf(0));
    sc.setStep(new Double(dStep));
    sc.setStepNumber(oStepNumber);
    setStepToScale(sc,oStep,null,rtc);
    oMinValue=new Double(dMinValue);
    oMaxValue=new Double(dMaxValue);
    sc.setMinimum(oMinValue);
    sc.setMaximum(oMaxValue);
    sc.computeTicks(xs,la,iLabelLocation,iOrientation,dStart,dEnd,false,null);
    sc.setData(dsi);
    return sc;
  }
  if ((iType & TEXT) == TEXT || ax.isCategoryScale()) {
    ScaleInfo info=new ScaleInfo(plotComp,iType,rtc,fs,ax,direction,scModel.isAutoExpand()).dZoomFactor(zoomFactor).iMarginPercent(iMarginPercent);
    sc=new AutoScale(info);
    sc.setData(dsi);
    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=Double.NaN;
    ;
    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;
      }
    }
    ScaleInfo info=new ScaleInfo(plotComp,iType,rtc,fs,ax,direction,scModel.isAutoExpand()).dZoomFactor(zoomFactor).iMarginPercent(iMarginPercent).dPrecision(dPrecision);
    sc=new AutoScale(info);
    sc.setMaximum(Double.valueOf(0));
    sc.setMinimum(Double.valueOf(0));
    sc.setStep(new Double(dStep));
    sc.setStepNumber(oStepNumber);
    sc.setData(dsi);
    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;
      }
    }
    ScaleInfo info=new ScaleInfo(plotComp,iType,rtc,fs,ax,direction,scModel.isAutoExpand()).dZoomFactor(zoomFactor).iMarginPercent(iMarginPercent);
    sc=new AutoScale(info);
    sc.setMaximum(Double.valueOf(0));
    sc.setMinimum(Double.valueOf(0));
    sc.setStep(new Double(10));
    sc.setStepNumber(oStepNumber);
    sc.setData(dsi);
    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.info.bStepFixed(true);
      sc.info.bMaximumFixed(true);
      sc.info.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);
    ScaleInfo info=new ScaleInfo(plotComp,DATE_TIME,rtc,fs,ax,direction,scModel.isAutoExpand()).dZoomFactor(zoomFactor).iMarginPercent(iMarginPercent).iMinUnit(oMinValue.equals(oMaxValue) ? getUnitId(iUnit) : getMinUnitId(fs,rtc));
    sc=new AutoScale(info);
    sc.setMaximum(cdtMaxAxis);
    sc.setMinimum(cdtMinAxis);
    sc.setStep(Integer.valueOf(1));
    sc.setStepNumber(oStepNumber);
    sc.context.setUnit(Integer.valueOf(iUnit));
    if (oMinimum != null) {
      if (oMinimum instanceof DateTimeDataElement) {
        sc.setMinimum(((DateTimeDataElement)oMinimum).getValueAsCDateTime());
        sc.info.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.info.bMinimumFixed(true);
    }
    if (oMaximum != null) {
      if (oMaximum instanceof DateTimeDataElement) {
        sc.setMaximum(((DateTimeDataElement)oMaximum).getValueAsCDateTime());
        sc.info.oMaximumFixed(((DateTimeDataElement)oMaximum).getValueAsCDateTime());
      }
 else {
        throw new ChartException(ChartEnginePlugin.ID,ChartException.GENERATION,"String_Node_Str",new Object[]{oMaximum,ax.getModelAxis().getType().getName()},Messages.getResourceBundle(rtc.getULocale()));
      }
      sc.info.bMaximumFixed(true);
    }
    if (sc.info.bMaximumFixed && sc.info.bMinimumFixed) {
      if (((CDateTime)sc.getMinimum()).after(sc.getMaximum())) {
        throw new ChartException(ChartEnginePlugin.ID,ChartException.GENERATION,"String_Node_Str",new Object[]{sc.getMinimum(),sc.getMaximum()},Messages.getResourceBundle(rtc.getULocale()));
      }
    }
    setStepToScale(sc,oStep,oStepNumber,rtc);
    sc.updateAxisMinMax(oMinValue,oMaxValue);
  }
 else {
    oMinValue=null;
    oMaxValue=null;
  }
  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.info.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);
  }
  if (sc != null) {
    sc.setData(dsi);
  }
  return sc;
}