@@ -22,8 +22,10 @@ Date Author Change
2222using OfficeOpenXml . Drawing ;
2323using OfficeOpenXml . Drawing . Chart ;
2424using OfficeOpenXml . Drawing . Chart . Style ;
25+ using OfficeOpenXml . FormulaParsing . Excel . Functions . DateAndTime ;
2526using OfficeOpenXml . FormulaParsing . Excel . Functions . Logical ;
2627using OfficeOpenXml . FormulaParsing . Excel . Functions . MathFunctions ;
28+ using OfficeOpenXml . FormulaParsing . Utilities ;
2729using OfficeOpenXml . Style ;
2830using OfficeOpenXml . Style . XmlAccess ;
2931using OfficeOpenXml . Utils . String ;
@@ -218,6 +220,12 @@ public List<object> Values
218220 public double MinorUnit { get ; set ; }
219221 public eTimeUnit ? MajorDateUnit { get ; set ; }
220222 public eTextOrientation LabelOrientation { get ; set ; }
223+ public bool IsDateScale
224+ {
225+ get ;
226+ private set ;
227+ } = false ;
228+
221229 internal override void AppendRenderItems ( List < RenderItem > renderItems )
222230 {
223231 Title ? . AppendRenderItems ( renderItems ) ;
@@ -277,15 +285,11 @@ internal void AddTickmarksAndValues(List<RenderItem> DefItems)
277285 if ( Axis . HasMajorGridlines )
278286 {
279287 MajorGridlinePositions = AddGridlines ( MajorUnit , double . NaN , Axis . MajorGridlines , Chart . StyleManager . Style . GridlineMajor ) ;
280- //DefItems.Add(MajorGridlinePositions[0]);
281- //MajorGridlinePositions.RemoveAt(0);
282288 }
283289
284290 if ( ( Axis . HasMinorGridlines ) )
285291 {
286292 MinorGridlinePositions = AddGridlines ( MinorUnit , MajorUnit , Axis . MinorGridlines , Chart . StyleManager . Style . GridlineMinor ) ;
287- //DefItems.Add(MinorGridlinePositions[0]);
288- //MinorGridlinePositions.RemoveAt(0);
289293 }
290294
291295 if ( Axis . CrossBetween == eCrossBetween . MidCat )
@@ -497,42 +501,22 @@ private double GetAxisItemLeft(int i, OfficeOpenXml.Interfaces.Drawing.Text.Text
497501 {
498502 return Rectangle . Left ;
499503 }
500- //else if (Axis.AxisPosition == eAxisPosition.Right)
501- //{
502-
503- // return Rectangle.Left;
504- //}
505504 else
506505 {
507- //if ((Axis.AxisType == eAxisType.Cat || Axis.IsVertical==false) && LabelOrientation==eTextOrientation.Horizontal)
508- //{
509- // //Between tickmarks
510- // var majorWidth = Rectangle.Width / (AxisValues.Count);
511- // var majorTickStartingPosition = Rectangle.Left + majorWidth * i;
512- // //var middleOfBounds = majorTickStartingPosition + (majorWidth / 2);
513- // return majorTickStartingPosition;
514- //}
515- //else
516- //{
517- if ( Axis . AxisType == eAxisType . Cat || Axis . AxisType == eAxisType . Date )
518- {
519- var majorWidth = Rectangle . Width / AxisValues . Count ;
520- var majorTickStartingPosition = Rectangle . Left + majorWidth * i ;
521- if ( Axis . AxisType == eAxisType . Date )
522- {
523- return majorTickStartingPosition + majorWidth / 2 - m . Width / 2 ;
524- }
525- //var middleOfBounds = majorTickStartingPosition + (majorWidth / 2);
526- return majorTickStartingPosition ;
527- }
528- else
529- {
530- var min = ConvertUtil . GetValueDouble ( Values [ 0 ] ) ;
531- var max = ConvertUtil . GetValueDouble ( Values . Last ( ) ) ;
532- var v = ConvertUtil . GetValueDouble ( Values [ i ] ) ;
533- var majorWidth = Rectangle . Width * ( v - Min ) / ( Max - Min ) ;
534- return Rectangle . Left + majorWidth ;
535- }
506+ if ( Axis . AxisType == eAxisType . Cat || ( Axis . AxisType == eAxisType . Date && IsDateScale == false ) )
507+ {
508+ var majorWidth = Rectangle . Width / AxisValues . Count ;
509+ var majorTickStartingPosition = Rectangle . Left + majorWidth * i ;
510+ return majorTickStartingPosition + majorWidth / 2 - m . Width / 2 ;
511+ }
512+ else
513+ {
514+ var min = ConvertUtil . GetValueDouble ( Values [ 0 ] ) ;
515+ var max = ConvertUtil . GetValueDouble ( Values . Last ( ) ) ;
516+ var v = ConvertUtil . GetValueDouble ( Values [ i ] ) ;
517+ var majorWidth = Rectangle . Width * ( v - Min ) / ( Max - Min ) ;
518+ return Rectangle . Left + majorWidth ;
519+ }
536520 //}
537521 }
538522 }
@@ -865,7 +849,7 @@ internal double GetPositionInPlotarea(double val, bool startValue=false)
865849 else
866850 {
867851 if ( val < Min || val > Max ) return double . NaN ;
868- var diff = Max - Min + 1 ;
852+ var diff = Max - Min ;
869853 return ( Max - val ) / diff * SvgChart . Plotarea . Rectangle . Height ;
870854 }
871855 }
@@ -886,28 +870,28 @@ internal double GetPositionInPlotarea(double val, bool startValue=false)
886870 else
887871 {
888872 if ( val < Min || val > Max ) return double . NaN ;
889- var diff = Max - Min + 1 ;
873+ var diff = Max - Min ;
890874 return ( ( ( val - Min ) / diff * SvgChart . Plotarea . Rectangle . Width ) ) ;
891875 }
892876 }
893877 }
894878 protected List < object > GetAxisValue ( ExcelChartAxisStandard ax , RenderItem rect , out double ? min , out double ? max , out double ? majorUnit , out eTimeUnit ? dateUnit , out eTextOrientation orientation )
895879 {
896880 var values = ax . GetAxisValues ( out bool isCount ) ;
897-
881+ var isNumeric = values . Any ( x => x == null || x . IsNumeric ( ) ) ;
898882 var options = new AxisOptions
899883 {
900884 LockedMin = ax . MinValue ,
901885 LockedMax = ax . MaxValue ,
902886 LockedInterval = ax . MajorUnit ,
903887 LockedIntervalUnit = ax . MajorTimeUnit ,
904- AddPadding = ax . AxisType == eAxisType . Val , //(ax.AxisPosition == eAxisPosition.Left || ax.AxisPosition == eAxisPosition.Right ),
888+ AddPadding = ShouldHavePadding ( ) ,
905889 Axis = ax ,
906890 IsStacked100 = Chart . IsTypePercentStacked ( ) ,
907891 ChartSize = rect
908892 } ;
909893
910- if ( ax . AxisType == eAxisType . Cat &&
894+ if ( ( ax . AxisType == eAxisType . Cat || ( ax . IsDate && isNumeric == false ) ) &&
911895 isCount == false )
912896 {
913897 //min = 0;
@@ -970,17 +954,25 @@ protected List<object> GetAxisValue(ExcelChartAxisStandard ax, RenderItem rect,
970954 AxisScale res ;
971955 if ( ax . IsVertical )
972956 {
973- //res = DateAxisScaleCalculator.Calculate(min ?? 0, max ?? 0, length, options);
974- res = DateAxisScaleCalculator . CalculateByHeight ( min ?? 0D , max ?? 0D , SvgChart . TextMeasurer , options ) ;
957+ res = DateAxisScaleCalculator . CalculateByWidthHeight ( options . ChartSize . Bounds . Height , min ?? 0D , max ?? 0D , SvgChart . TextMeasurer , options ) ;
975958 }
976959 else
977960 {
978- res = DateAxisScaleCalculator . CalculateByWidth ( min ?? 0D , max ?? 0D , SvgChart . TextMeasurer , options ) ;
961+ if ( Chart . IsTypeBar ( ) )
962+ {
963+ res = DateAxisScaleCalculator . CalculateByWidthHeight ( options . ChartSize . Bounds . Width , min ?? 0D , max ?? 0D , SvgChart . TextMeasurer , options ) ;
964+ }
965+ else
966+ {
967+ res = DateAxisScaleCalculator . CalculateByWidthAllowDiagonal ( min ?? 0D , max ?? 0D , SvgChart . TextMeasurer , options ) ;
968+ }
979969 }
980970 orientation = res . TextOrientation ;
981971 dateUnit = res . MajorDateUnit ;
972+ majorUnit = res . MajorInterval ;
982973 var dt = DateTime . FromOADate ( res . Min ) ;
983974 var maxDt = DateTime . FromOADate ( res . Max ) ;
975+ IsDateScale = ( dateUnit != eTimeUnit . Days || majorUnit > 1 ) && values . Count > 31 ;
984976 while ( dt <= maxDt )
985977 {
986978 l . Add ( dt ) ;
@@ -1000,7 +992,6 @@ protected List<object> GetAxisValue(ExcelChartAxisStandard ax, RenderItem rect,
1000992
1001993 min = res . Min ;
1002994 max = res . Max ;
1003- majorUnit = res . MajorInterval ;
1004995 }
1005996 else
1006997 {
@@ -1019,5 +1010,10 @@ protected List<object> GetAxisValue(ExcelChartAxisStandard ax, RenderItem rect,
10191010
10201011 return l ;
10211012 }
1013+
1014+ private bool ShouldHavePadding ( )
1015+ {
1016+ return Axis . AxisType == eAxisType . Val || ( Chart . IsTypeLine ( ) && Axis . AxisType == eAxisType . Date ) ;
1017+ }
10221018 }
10231019}
0 commit comments