diff --git a/src/EPPlus/Drawing/Chart/ExcelChartAxisStandard.cs b/src/EPPlus/Drawing/Chart/ExcelChartAxisStandard.cs index 902735a30..1cda8be04 100644 --- a/src/EPPlus/Drawing/Chart/ExcelChartAxisStandard.cs +++ b/src/EPPlus/Drawing/Chart/ExcelChartAxisStandard.cs @@ -79,14 +79,14 @@ public override eAxisTickMark MajorTickMark var v = GetXmlNodeString(_majorTickMark); if (string.IsNullOrEmpty(v)) { - return eAxisTickMark.Cross; + //return eAxisTickMark.Cross; //On Epplus generated charts e.g. the test "HtmlExportWithColumnGradient" //Excel displays cross even without v or MajorTickmarkParent - //if (ExistsNode(_majorTickMarkParent)) - //{ - // return eAxisTickMark.Cross; - //} - //return eAxisTickMark.Out; + if (ExistsNode(_majorTickMarkParent)) + { + return eAxisTickMark.Cross; + } + return eAxisTickMark.Out; } else { @@ -1096,6 +1096,7 @@ internal void GetSeriesValues(out bool isCount, out bool isMultiLevel, out List< isMultiLevel = false; foreach (var ct in _chart.PlotArea.ChartTypes) { + var showHidden = ct.ShowHiddenData; foreach (var serie in ct.Series) { var l = new List(); @@ -1110,7 +1111,7 @@ internal void GetSeriesValues(out bool isCount, out bool isMultiLevel, out List< { if (ct.XAxis.Id == Id || ct.YAxis.Id == Id) { - serie.AddFromSerie(l, true, true, new string[] { serie.HeaderAddress?.Address, serie.GetHeaderText(ix) }, out isMultiLevel); + serie.AddFromSerie(l, true, true, new string[] { serie.HeaderAddress?.Address, serie.GetHeaderText(ix) }, showHidden, out isMultiLevel); } else { @@ -1124,11 +1125,11 @@ internal void GetSeriesValues(out bool isCount, out bool isMultiLevel, out List< if (ct.YAxis.Id == Id) { //AddFromSerie(l, serie.Series, serie.NumberLiteralsY, serie.StringLiteralsY, false, null, pl); - serie.AddFromSerie(l, false, false, null, out isMultiLevel); + serie.AddFromSerie(l, false, false, null, showHidden, out isMultiLevel); } else if (ct.XAxis.Id == Id) { - serie.AddFromSerie(l, true, false, null, out isMultiLevel); + serie.AddFromSerie(l, true, false, null, showHidden, out isMultiLevel); } else { diff --git a/src/EPPlus/Drawing/Chart/ExcelChartSerie.cs b/src/EPPlus/Drawing/Chart/ExcelChartSerie.cs index 3c380a008..145ab4535 100644 --- a/src/EPPlus/Drawing/Chart/ExcelChartSerie.cs +++ b/src/EPPlus/Drawing/Chart/ExcelChartSerie.cs @@ -329,13 +329,13 @@ private string GetAddressValue(ExcelAddressBase address) return ExcelErrorValue.Values.Ref; } - internal List GetValues(bool useX, bool useString, out bool isMultiLevel) + internal List GetValues(bool useX, bool useString, bool showHidden, out bool isMultiLevel) { var l = new List(); - AddFromSerie(l, useX, useString, null, out isMultiLevel); + AddFromSerie(l, useX, useString, null, showHidden, out isMultiLevel); return l; } - internal void AddFromSerie(List list, bool useX, bool useText, string[] headerInfo, out bool isMultiLevel, bool doSort=false) + internal void AddFromSerie(List list, bool useX, bool useText, string[] headerInfo, bool showHidden, out bool isMultiLevel, bool doSort=false) { isMultiLevel = false; var values = new List[] {new List(), new List()}; @@ -344,13 +344,13 @@ internal void AddFromSerie(List list, bool useX, bool useText, string[] { if (useX) { - var max = GetCount(Series, NumberLiteralsY, StringLiteralsY)??0; - AddSerieValues(list, useText, headerInfo, ref isMultiLevel, XSeries, NumberLiteralsX, StringLiteralsX, max); + var max = GetCount(Series, NumberLiteralsY, StringLiteralsY) ?? 0; + AddSerieValues(list, useText, headerInfo, ref isMultiLevel, XSeries, NumberLiteralsX, StringLiteralsX, max, showHidden); } else { - var max = GetCount(XSeries, NumberLiteralsX, StringLiteralsX) ?? 0; - AddSerieValues(list, useText, headerInfo, ref isMultiLevel, Series, NumberLiteralsY, StringLiteralsY, max); + var max = GetCount(XSeries, NumberLiteralsX, StringLiteralsX) ?? GetCount(Series, NumberLiteralsY, StringLiteralsY)??0; + AddSerieValues(list, useText, headerInfo, ref isMultiLevel, Series, NumberLiteralsY, StringLiteralsY, max, showHidden); } } else @@ -358,8 +358,8 @@ internal void AddFromSerie(List list, bool useX, bool useText, string[] bool isMultiX=false, isMultiY=false; var maxY = GetCount(Series, NumberLiteralsY, StringLiteralsY) ?? 0; var maxX = GetCount(XSeries, NumberLiteralsX, StringLiteralsX) ?? 0; - AddSerieValues(values[1], useText, headerInfo, ref isMultiX, Series, NumberLiteralsY, StringLiteralsY, maxX); - AddSerieValues(values[0], useText, headerInfo, ref isMultiY, XSeries, NumberLiteralsX, StringLiteralsX, maxY); + AddSerieValues(values[1], useText, headerInfo, ref isMultiX, Series, NumberLiteralsY, StringLiteralsY, maxX, showHidden); + AddSerieValues(values[0], useText, headerInfo, ref isMultiY, XSeries, NumberLiteralsX, StringLiteralsX, maxY, showHidden); var sorted = values[0] .Zip(values[1], (k, v) => (Key: k, Value: v)) @@ -392,10 +392,8 @@ private object GetValue(object v) } } - private void AddSerieValues(List list, bool useText, string[] headerInfo, ref bool isMultiLevel, string address, double[] numberLiterals, string[] stringLiterals, int maxOtherSeries) + private void AddSerieValues(List list, bool useText, string[] headerInfo, ref bool isMultiLevel, string address, double[] numberLiterals, string[] stringLiterals, int maxOtherSeries, bool showHidden) { - var isStacked = false;//_chart.IsTypeStacked() && prevList != null; - var count = Math.Min(GetCount(address, numberLiterals, stringLiterals) ?? maxOtherSeries, maxOtherSeries); if (numberLiterals?.Length > 0) @@ -430,7 +428,7 @@ private void AddSerieValues(List list, bool useText, string[] headerInfo } else { - GetInternalValues(list, useText, headerInfo, a, count, out isMultiLevel); + GetInternalValues(list, useText, headerInfo, a, count, showHidden, out isMultiLevel); } } } @@ -453,12 +451,12 @@ private void AddSerieValues(List list, bool useText, string[] headerInfo return null; } - private void GetInternalValues(List list, bool useText, string[] headerInfo, ExcelAddressBase a, int? count, out bool isMultiLevel) + private void GetInternalValues(List list, bool useText, string[] headerInfo, ExcelAddressBase a, int? count, bool showHidden, out bool isMultiLevel) { var ws = _chart.WorkSheet.Workbook.Worksheets[a.WorkSheetName]; if (ws != null) { - GetValuesFromWorksheet(list, useText, headerInfo, a, ws, count, out isMultiLevel); + GetValuesFromWorksheet(list, useText, headerInfo, a, ws, count, showHidden, out isMultiLevel); } else { @@ -466,9 +464,14 @@ private void GetInternalValues(List list, bool useText, string[] headerI } } - private void GetValuesFromWorksheet(List list, bool useText, string[] headerInfo, ExcelAddressBase a, ExcelWorksheet ws, int? count, out bool isMultiLevel) + private void GetValuesFromWorksheet(List list, bool useText, string[] headerInfo, ExcelAddressBase a, ExcelWorksheet ws, int? count, bool showHidden, out bool isMultiLevel) { var range = ws.Cells[a.Address]; + if (showHidden==false && range.IsHidden) + { + isMultiLevel = false; + return; + } var i = 0; isMultiLevel = range.Rows > 1 && range.Columns > 1; object o; @@ -487,6 +490,10 @@ private void GetValuesFromWorksheet(List list, bool useText, string[] he for (var c = 0; c < range.Columns; c++) { var v = range.Offset(r, c, 1, 1); + if(showHidden==false && v.IsHidden) + { + continue; + } if (useText) { if (headerInfo == null) diff --git a/src/EPPlus/Drawing/ExcelDrawing.cs b/src/EPPlus/Drawing/ExcelDrawing.cs index ecab9561c..30eb134e2 100644 --- a/src/EPPlus/Drawing/ExcelDrawing.cs +++ b/src/EPPlus/Drawing/ExcelDrawing.cs @@ -2610,7 +2610,7 @@ public string ToSvg() { return ToSvg(null); } - /// git statu + /// /// Converts the drawing to a SVG image. /// This is currently only supported for shapes, line-, column-, bar- and pie- charts. /// @@ -2640,7 +2640,6 @@ public string ToSvg(Action options) } throw new NotSupportedException("Only line-, column-, bar- and pie charts and shapes can be rendered to svg"); } - } } \ No newline at end of file diff --git a/src/EPPlus/Drawing/Renderer/Chart/Axis/CategoryAxisScaleCalculator.cs b/src/EPPlus/Drawing/Renderer/Chart/Axis/CategoryAxisScaleCalculator.cs index af07ab121..8ae624732 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/Axis/CategoryAxisScaleCalculator.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/Axis/CategoryAxisScaleCalculator.cs @@ -60,7 +60,7 @@ internal static AxisScale CalculateHorizontalAxisByWidth(ref List values //if (FitAsVerticalDiagonalText(displayValues.Count, interval, width, margin, plotAreaWidth)) //Check diagonal //{ var width = fontSize * Math.Sqrt(2); - var margin = fontSize * 0.5; + var margin = fontSize * 0.2; if (FitAsVerticalDiagonalText(displayValues.Count, interval, width, margin, plotAreaWidth)) //Check diagonal { return new AxisScale() @@ -90,7 +90,7 @@ internal static AxisScale CalculateHorizontalAxisByWidth(ref List values else { var width = fontSize * Math.Sqrt(2); - var margin = fontSize * 0.5; + var margin = fontSize * 0.2; if (options.IsMulti || FitAsVerticalDiagonalText(displayValues.Count, interval, width, margin, plotAreaWidth)) //Check diagonal { if (FitAsHorizontalText(displayValues, interval, shaper, fontSize, plotAreaWidth, options.IsMulti)) //Check horizontal @@ -197,7 +197,7 @@ internal static AxisScale CalculateVerticalAxisByHeight(ref List values, private static bool FitAsHorizontalText(List displayValues, int interval, ITextShaper shaper, float fontSize, double plotAreaWidth, bool isMultLevel) { - var margin = fontSize * 0.3; + var margin = fontSize * 0.1; double maxWidth; var pos = interval; if (isMultLevel) diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs index c154f8625..03f04820b 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs @@ -17,6 +17,7 @@ Date Author Change using EPPlus.Export.Renderer; using EPPlus.Fonts.OpenType.Integration; using EPPlus.Fonts.OpenType.Integration.DataHolders; +using EPPlus.Fonts.OpenType.TextShaping; using EPPlus.Fonts.OpenType.Utils; using EPPlus.Graphics; using EPPlusImageRenderer.RenderItems; @@ -107,7 +108,7 @@ internal ChartAxisRenderer(ChartRenderer sc, ExcelChartAxisStandard ax) : base(s internal void UpdateValueScale() { - Values = GetAxisValue(Axis, ChartRenderer.ChartArea.Rectangle, out double? min, out double? max, out double? majorUnit, out eTimeUnit? dateUnit, out eTextOrientation orientation); + Values = GetAxisValue(Axis, ChartRenderer.Plotarea.Rectangle, out double? min, out double? max, out double? majorUnit, out eTimeUnit? dateUnit, out eTextOrientation orientation); if (_isMulti) { AxisValues = null; @@ -196,11 +197,6 @@ internal void UpdateValueScale() } } } - - //if (Axis.TextBody.Rotation != 0) - //{ - // Rectangle.Width += - //} } } @@ -439,7 +435,10 @@ private double GetTextWidest(ExcelChartAxisStandard ax, bool isMulti) widest = w.Width; } } - widest += (float)(AxisValuesMulti.Count - 1 * shaper.GetLineHeightInPoints(mf.Size) * 1.5); + if (ax.IsVertical) + { + widest += (float)((AxisValuesMulti[0].Length - 1) * shaper.GetLineHeightInPoints(mf.Size) * 1.5); + } } else { @@ -454,16 +453,6 @@ private double GetTextWidest(ExcelChartAxisStandard ax, bool isMulti) } return widest; } - //internal GetAxisWidthForPlotarea() - //{ - - //} - //internal double GetAxisHeightForPlotarea() - //{ - // var pa = ChartRenderer.Plotarea; - - // var - //} public List Values { get; @@ -522,7 +511,6 @@ public override void AppendRenderItems(List renderItems) { axisGroup.ChildObjects.Add(TickMarkGroup); } - axisGroup.ChildObjects.AddRange(MultiCategoryLineRenderItems); //renderItems.Add(axisGroup); plotareaGroup.AddChildItem(axisGroup); @@ -611,7 +599,7 @@ internal void AddTickmarksAndValues(List DefItems) } else if (ChartRenderer.HorizontalAxis == this || ChartRenderer.HorizontalAxis.Axis.AxisTextboxPosition != Axis.AxisTextboxPosition) { - Rectangle.Top -= Rectangle.Height; + //Rectangle.Top -= Rectangle.Height; } else if (Axis.AxisTextboxPosition == eActualAxisPosition.Top) { @@ -649,7 +637,7 @@ internal void AddTickmarksAndValues(List DefItems) } } - List MultiCategoryLineRenderItems { get; } = new List(); + internal List MultiCategoryLineRenderItems { get; } = new List(); private List GetAxisValueTextBoxes(out double widest, out double highest) { @@ -698,10 +686,21 @@ private List GetAxisValueTextBoxes(out double widest, out } } + if(Axis.AxisType==eAxisType.Val) + { + maxWidth = Rectangle.Width; //We dont want to wrap value axis labels as they are numeric. + } List ret; if (AxisValues==null) { - return AddTextboxesMulti(shaper, fontSize, lineHeight, maxWidth, maxHeight, out widest, out highest); + if(Axis.IsVertical) + { + return AddTextboxesMultiVertical(shaper, fontSize, lineHeight, maxWidth, maxHeight, out widest, out highest); + } + else + { + return AddTextboxesMultiHorizontal(shaper, fontSize, lineHeight, maxWidth, maxHeight, out widest, out highest); + } } else { @@ -749,13 +748,13 @@ private List GetAxisValueTextBoxes(out double widest, out { switch (lblAlignment) { - case OfficeOpenXml.eAxisLabelAlignment.Left: + case eAxisLabelAlignment.Left: tb.Left -= (tb.Width + majorWidth) / 2; break; - case OfficeOpenXml.eAxisLabelAlignment.Center: + case eAxisLabelAlignment.Center: tb.Left -= tb.Width / 2; break; - case OfficeOpenXml.eAxisLabelAlignment.Right: + case eAxisLabelAlignment.Right: tb.Left += (tb.Width + majorWidth) / 2; break; } @@ -768,28 +767,14 @@ private List GetAxisValueTextBoxes(out double widest, out switch (lblAlignment) { - case OfficeOpenXml.eAxisLabelAlignment.Left: + case eAxisLabelAlignment.Left: break; - case OfficeOpenXml.eAxisLabelAlignment.Center: + case eAxisLabelAlignment.Center: tb.Left += majorWidth / 2 - tb.Width / 2; break; case OfficeOpenXml.eAxisLabelAlignment.Right: - //tb.TextBody.MaxWidth = 50000d; - //foreach (var item in tb.TextBody.Paragraphs) - //{ - // //Re-calculates wrap (so that it's one line) - // item.HorizontalAlignment = item.HorizontalAlignment; - //} - - tb.Left += majorWidth - tb.Width; - //foreach (var item in tb.TextBody.Paragraphs) - //{ - // //Re-calculates wrap (so that it's one line) - // item.HorizontalAlignment = item.HorizontalAlignment; - //} - break; } } @@ -811,10 +796,10 @@ private List GetAxisValueTextBoxes(out double widest, out { majorWidth = Rectangle.Width / count; } - foreach (var tb in ret) - { - //tb.Left += majorWidth / 2; - } + //foreach (var tb in ret) + //{ + // //tb.Left += majorWidth / 2; + //} } } else if(LabelOrientation == eTextOrientation.Custom) @@ -884,7 +869,7 @@ private List AddTextboxes(ITextShaper shaper, float fontSi else { //There is only one axisValue as we are somehow both the first and last value - majorTickMarkWidth = 0; + majorTickMarkWidth = maxWidth; } } @@ -905,7 +890,7 @@ private List AddTextboxes(ITextShaper shaper, float fontSi double x, y; double availableTextLength; - if (Axis.IsVertical) + if (Axis.IsVertical || AutoAxisType==eAxisType.Val) { availableTextLength = maxWidth; } @@ -927,7 +912,8 @@ private List AddTextboxes(ITextShaper shaper, float fontSi if (Axis.IsVertical) { x = ticMarkX; - if (ChartRenderer.Chart.IsTypeBar()) + var ct = ChartRenderer.Chart.PlotArea.ChartTypes.First(x => x.XAxis?.Id == Axis.Id || x.YAxis?.Id == Axis.Id); + if ((ct??ChartRenderer.Chart).IsTypeBar()) { y = ticMarkY; } @@ -957,9 +943,16 @@ private List AddTextboxes(ITextShaper shaper, float fontSi if (rot == 90 || rot == -90 || LabelOrientation == eTextOrientation.Vertical) { - if(Axis.IsVertical == false) + if (Axis.IsVertical == false) { - x = xPosToMiddle - (width/2); + if (Axis.AxisType == eAxisType.Cat) + { + x = xPosToMiddle - (width / 2); + } + else + { + x = ticMarkX - (width / 2); + } y = ticMarkY + descent; } else @@ -1159,7 +1152,7 @@ private List AddTextboxes(ITextShaper shaper, float fontSi } return ret; } - private List AddTextboxesMulti(ITextShaper shaper, float fontSize, float lineHeight, double maxWidth, double maxHeight, out double widest, out double highest) + private List AddTextboxesMultiHorizontal(ITextShaper shaper, float fontSize, float lineHeight, double maxWidth, double maxHeight, out double widest, out double highest) { widest = highest = 0; var axisStyle = GetAxisStyleEntry(); @@ -1169,7 +1162,7 @@ private List AddTextboxesMulti(ITextShaper shaper, float f var firstItem = (string[])AxisValuesMulti.First(); var isTopRow = true; var heightAdd = 0D; - var majorWidth = Rectangle.Width / AxisValuesMulti.Count; + var majorSize = Rectangle.Width / AxisValuesMulti.Count; var margin = fontSize * 0.5; //margin between lines for (int j = firstItem.Length - 1; j >= 0; j--) { @@ -1187,46 +1180,23 @@ private List AddTextboxesMulti(ITextShaper shaper, float f } else if (isTopRow == false && tb != null) { - //center the text box in the middle of the empty cells. - var left = GetAxisItemLeft(i - emptyCells - 1); - if (i + 1 == AxisValuesMulti.Count && string.IsNullOrEmpty(AxisValuesMulti[i][j])) + if (AdjustEmptyCellsHorizontal(tb, i, j, emptyCells, majorSize) == false) { - switch (Axis.LabelAlignment) - { - case eAxisLabelAlignment.Left: - tb.Left = left; - break; - case eAxisLabelAlignment.Center: - tb.Left = left + (majorWidth * (emptyCells + 2) / 2) - (tb.Width / 2); - break; - case eAxisLabelAlignment.Right: - tb.Left = left + majorWidth * (emptyCells + 2) - tb.Width; - break; - } continue; } - else - { - switch (Axis.LabelAlignment) - { - case eAxisLabelAlignment.Left: - tb.Left = left; - break; - case eAxisLabelAlignment.Center: - tb.Left = left + (majorWidth * (emptyCells + 1) / 2) - (tb.Width / 2); - break; - case eAxisLabelAlignment.Right: - tb.Left = left + majorWidth * (emptyCells + 1) - tb.Width; - break; - } - } emptyCells = 0; } var v = ((object[])Values[i])[j]; var t = AxisValuesMulti[i][j]; - var ticMarkX = GetAxisItemLeft(i); + //An empty label has no size, as with ITextMeasurer. + float width = string.IsNullOrEmpty(t) ? 0f : shaper.Shape(t).GetWidthInPoints(fontSize); + float height = string.IsNullOrEmpty(t) ? 0f : lineHeight; + + double ticMarkX, ticMarkY; + double x, y; + ticMarkX = GetAxisItemLeft(i); double majorTickMarkWidth; if (i + 1 < Values.Count) { @@ -1246,13 +1216,8 @@ private List AddTextboxesMulti(ITextShaper shaper, float f majorTickMarkWidth = 0; } } + ticMarkY = GetAxisItemTop(i, width, height) + heightAdd; - //An empty label has no size, as with ITextMeasurer. - float width = string.IsNullOrEmpty(t) ? 0f : shaper.Shape(t).GetWidthInPoints(fontSize); - float height = string.IsNullOrEmpty(t) ? 0f : lineHeight; - - var ticMarkY = GetAxisItemTop(i, width, height) + heightAdd; - double x, y; if (LabelOrientation == eTextOrientation.Horizontal || isTopRow == false) { x = ticMarkX; @@ -1309,61 +1274,26 @@ private List AddTextboxesMulti(ITextShaper shaper, float f } if (isTopRow) { - AddGroupLines(x, heightFirstRow + margin); + AddGroupLinesVertical(x, heightFirstRow + margin); switch (Axis.LabelAlignment) { case eAxisLabelAlignment.Left: break; case eAxisLabelAlignment.Center: - x += (majorWidth / 2) - (width / 2); + x += (majorSize / 2) - (width / 2); break; case eAxisLabelAlignment.Right: - x += majorWidth - tb.Width; + x += majorSize - tb.Width; break; } } else { - AddGroupLines(x, lineHeight + margin + heightAdd); + AddGroupLinesVertical(x, lineHeight + margin + heightAdd); } tb = new DrawingTextContainer(ChartRenderer.RenderContext, Chart, Rectangle, DefaultFonts2007Style.Axis, x, y, width, height, GetMaxWidthForEmptyRows(maxWidth, isTopRow, i, j), maxHeight); tb.SetRotation(EPPlus.DrawingRenderer.RenderItems.Textbox.RotationAnchor.Middle); - //if (isTopRow) - //{ - // if (LabelOrientation == eTextOrientation.Diagonal) - // { - // tb.Rotation = -45; - // if (Axis.ActualAxisPosition == eAxisPosition.Bottom) - // { - // tb.TextAnchor = eTextAnchor.End; - // } - // } - // else if (LabelOrientation == eTextOrientation.Vertical) - // { - // if (Axis.TextBody.VerticalText == eTextVerticalType.EastAsianVertical) - // { - // tb.Rotation = 90; - // } - // else - // { - // tb.Rotation = -90; - // } - // if (Axis.ActualAxisPosition == eAxisPosition.Bottom) - // { - // tb.TextAnchor = eTextAnchor.End; - // } - // } - // else if (LabelOrientation == eTextOrientation.Custom) - // { - // tb.Rotation = Axis.TextBody?.RotationAdjustedForTextVertical ?? 0D; - // if (Axis.ActualAxisPosition == eAxisPosition.Bottom) - // { - // tb.TextAnchor = eTextAnchor.End; - // } - // } - //} - var p = Axis.TextBody.Paragraphs.FirstOrDefault(); if (p != null) @@ -1392,7 +1322,6 @@ private List AddTextboxesMulti(ITextShaper shaper, float f highest = tb.Height; } ret.Add(tb); - } if(isTopRow) { @@ -1404,24 +1333,212 @@ private List AddTextboxesMulti(ITextShaper shaper, float f heightAdd += lineHeight + margin; } } - AddGroupLines(Rectangle.Width, heightAdd); + AddGroupLinesVertical(Rectangle.Width, heightAdd); + return ret; + } + private List AddTextboxesMultiVertical(ITextShaper shaper, float fontSize, float lineHeight, double maxWidth, double maxHeight, out double widest, out double highest) + { + var axisStyle = GetAxisStyleEntry(); + var ret = new List(); + int labelSkip = Axis.AxisType == eAxisType.Cat ? (int)(Axis.MajorUnit ?? 1) : 1; + var heightFirstRow = Rectangle.Height - (lineHeight * AxisValuesMulti[0].Length - 1); + var firstItem = (string[])AxisValuesMulti.First(); + var isTopRow = true; + var isVertical = Axis.IsVertical; + var majorSize = (isVertical ? Rectangle.Height : Rectangle.Width) / AxisValuesMulti.Count; + var margin = fontSize * 0.5; //margin between lines + + widest = GetFirstColTextWidest(shaper, fontSize, AxisValuesMulti); + var widthAdd = widest+RightMargin; + highest = 0; + for (int j = firstItem.Length - 1; j >= 0; j--) + { + var emptyCells = 0; + DrawingTextContainer tb = null; + for (var i = 0; i < AxisValuesMulti.Count; i++) + { + if (i % labelSkip != 0) continue; + var items = AxisValuesMulti[i]; + + if (isTopRow == false && i + 1 < AxisValuesMulti.Count && string.IsNullOrEmpty(AxisValuesMulti[i][j])) + { + emptyCells++; + continue; + } + else if (isTopRow == false && tb != null) + { + if (AdjustEmptyCellsVertical(tb, i, j, emptyCells, majorSize, widest) == false) + { + continue; + } + emptyCells = 0; + } + + var v = ((object[])Values[i])[j]; + var t = AxisValuesMulti[i][j]; + + //An empty label has no size, as with ITextMeasurer. + float width = string.IsNullOrEmpty(t) ? 0f : shaper.Shape(t).GetWidthInPoints(fontSize); + float height = string.IsNullOrEmpty(t) ? 0f : lineHeight; + + double ticMarkX, ticMarkY; + double x, y; + ticMarkY = GetAxisItemTop(i, width, height); + ticMarkX = j * (lineHeight + LeftMargin); + x = ticMarkX; + y = ticMarkY; + + tb = new DrawingTextContainer(ChartRenderer.RenderContext, Chart, Rectangle, DefaultFonts2007Style.Axis, x, y, width, height, GetMaxWidthForEmptyRows(maxWidth, isTopRow, i, j), maxHeight); + tb.SetRotation(EPPlus.DrawingRenderer.RenderItems.Textbox.RotationAnchor.Middle); + if (isTopRow == false) + { + tb.TextBody.Rotation = -90; + } + + var p = Axis.TextBody.Paragraphs.FirstOrDefault(); + + if (p != null) + { + if (p.HorizontalAlignment != eTextAlignment.Center && Axis.AxisType != eAxisType.Val && (Axis.AxisPosition == eAxisPosition.Bottom || Axis.AxisPosition == eAxisPosition.Top)) + { + //Horizontal axises are always center aligned visually + //Should be broken out as input to ImportParagraph instead of changing the base item + p.HorizontalAlignment = eTextAlignment.Center; + } + tb.ImportParagraph(p, 0, t); + } + else + { + tb.AddText(t); + } + + tb.GetStyleBg().SetDrawingPropertiesFill(ChartRenderer.Theme, Axis.Fill, axisStyle?.FillReference.Color, UserSpaceSettings.UserSpaceOnUse_Global, DefaultFillColor); + ret.Add(tb); + AddGroupLinesHorizontal(y+majorSize, widthAdd); + } + widthAdd += lineHeight + LeftMargin; + isTopRow = false; + } + AddGroupLinesHorizontal(0, widthAdd- lineHeight - LeftMargin); return ret; } - private void AddGroupLines(double x, double length) + private double GetFirstColTextWidest(ITextShaper shaper, float fontSize, List axisValuesMulti) { - if (Axis.IsVertical) + var widest = 0D; + foreach(string[] v in AxisValuesMulti) + { + var t = v[v.Length - 1]; + var w = shaper.MeasureTextInPoints(t, fontSize); + if(w > widest) + { + widest = w; + } + } + return widest; + } + + private bool AdjustEmptyCellsHorizontal(DrawingTextContainer tb, int i, int j, int emptyCells, double majorWidth) + { + //center the text box in the middle of the empty cells. + var left = GetAxisItemLeft(i - emptyCells - 1); + if (i + 1 == AxisValuesMulti.Count && string.IsNullOrEmpty(AxisValuesMulti[i][j])) + { + switch (Axis.LabelAlignment) + { + case eAxisLabelAlignment.Left: + tb.Left = left; + break; + case eAxisLabelAlignment.Center: + tb.Left = left + (majorWidth * (emptyCells + 2) / 2) - (tb.Width / 2); + break; + case eAxisLabelAlignment.Right: + tb.Left = left + majorWidth * (emptyCells + 2) - tb.Width; + break; + } + return false; + } + else { + switch (Axis.LabelAlignment) + { + case eAxisLabelAlignment.Left: + tb.Left = left; + break; + case eAxisLabelAlignment.Center: + tb.Left = left + (majorWidth * (emptyCells + 1) / 2) - (tb.Width / 2); + break; + case eAxisLabelAlignment.Right: + tb.Left = left + majorWidth * (emptyCells + 1) - tb.Width; + break; + } + } + return true; + } + private bool AdjustEmptyCellsVertical(DrawingTextContainer tb, int i, int j, int emptyCells, double majorHeight, double widest) + { + //center the text box in the middle of the empty cells. + var top = majorHeight * (AxisValuesMulti.Count-i); //GetAxisItemTop(i - emptyCells - 1, tb.Width, tb.Height); + if (i + 1 == AxisValuesMulti.Count && string.IsNullOrEmpty(AxisValuesMulti[i][j])) + { + top -= majorHeight; + switch (Axis.LabelAlignment) + { + case eAxisLabelAlignment.Left: + tb.Top = top + majorHeight * (emptyCells + 2); + break; + case eAxisLabelAlignment.Center: + tb.Top = top + (majorHeight * (emptyCells + 2) / 2) + (tb.TextBody.RotatedHeight / 2); + break; + case eAxisLabelAlignment.Right: + tb.Top = top + tb.TextBody.RotatedHeight; + break; + } + return false; } else { - var li = new LineRenderItem(Rectangle) { X1 = x, X2 = x, Y1 = 0, Y2 = length }; - li.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, Axis.Border, ChartRenderer.Chart.StyleManager.Style?.Title.BorderReference.Color, Axis.Border.IsEmpty == true || Axis.Border.Fill.Style != eFillStyle.NoFill, GetDefaultBorderColor, 0.75); - MultiCategoryLineRenderItems.Add(li); + switch (Axis.LabelAlignment) + { + case eAxisLabelAlignment.Left: + tb.Top = top + majorHeight * (emptyCells + 1); + break; + case eAxisLabelAlignment.Center: + tb.Top = top + (majorHeight * (emptyCells + 1) / 2) + (tb.TextBody.RotatedHeight / 2); + break; + case eAxisLabelAlignment.Right: + tb.Top = top + majorHeight * (emptyCells + 1) - tb.TextBody.RotatedHeight; + break; + } } + + return true; } + private void AddGroupLinesVertical(double x, double length) + { + var li = new LineRenderItem(Rectangle) { X1 = x, X2 = x, Y1 = 0, Y2 = length }; + li.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, Axis.Border, ChartRenderer.Chart.StyleManager.Style?.Title.BorderReference.Color, Axis.Border.IsEmpty == true || Axis.Border.Fill.Style != eFillStyle.NoFill, GetDefaultBorderColor, 0.75); + MultiCategoryLineRenderItems.Add(li); + } + private void AddGroupLinesHorizontal(double y, double length) + { + double x1, x2; + if(Axis.ActualAxisPosition==eAxisPosition.Left) + { + x1 = Rectangle.Width - length; + x2 = Rectangle.Width; + } + else + { + x1 = 0; + x2 = length; + } + var li = new LineRenderItem(Rectangle) { X1 = x1, X2 = x2, Y1 = y, Y2 = y }; + li.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, Axis.Border, ChartRenderer.Chart.StyleManager.Style?.Title.BorderReference.Color, Axis.Border.IsEmpty == true || Axis.Border.Fill.Style != eFillStyle.NoFill, GetDefaultBorderColor, 0.75); + MultiCategoryLineRenderItems.Add(li); + } private double GetMaxWidthForEmptyRows(double maxWidth, bool isTopRow, int startIndex, int col) { if (isTopRow) return maxWidth; @@ -2526,5 +2643,40 @@ internal eTimeUnit GetBaseUnit() } return eTimeUnit.Days; } + + internal double GetAxisPosition(double cv) + { + if(cv <= Min) + { + if (Axis.CrossingAxis.ActualAxisPosition == eAxisPosition.Bottom) + { + return ChartRenderer.Plotarea.Rectangle.Height; + } + else if (Axis.CrossingAxis.ActualAxisPosition == eAxisPosition.Right) + { + return ChartRenderer.Plotarea.Rectangle.Width; + } + else + { + return 0; + } + } + else if (cv >= Max) + { + if (Axis.ActualAxisPosition == eAxisPosition.Top) + { + return ChartRenderer.Plotarea.Rectangle.Height; + } + else if (Axis.ActualAxisPosition == eAxisPosition.Left) + { + return ChartRenderer.Plotarea.Rectangle.Width; + } + else + { + return 0; + } + } + return GetPositionInPlotarea(cv); + } } } \ No newline at end of file diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartAxisTextBoxes.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartAxisTextBoxes.cs index a9da2990e..54cbdb969 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartAxisTextBoxes.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartAxisTextBoxes.cs @@ -57,6 +57,7 @@ public override void AppendRenderItems(List renderItems) { tb.Parent = axisTxtBoxGroup; } + axisTxtBoxGroup.ChildObjects.AddRange(_axis.MultiCategoryLineRenderItems); renderItems.Add(axisTxtBoxGroup); } diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartDataTableRenderer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartDataTableRenderer.cs index fde5c40d9..66bb854a5 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartDataTableRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartDataTableRenderer.cs @@ -186,7 +186,7 @@ internal ChartDataTableRenderer(ChartRenderer svgChart) : base(svgChart) } var rows = AddSerieValues(svgChart, _maxWidth, _maxHeight, serie); pSls = sls; - var formattedValues = serie.GetValues(false, true, out bool isMultiLevel); + var formattedValues = serie.GetValues(false, true, ct.ShowHiddenData, out bool isMultiLevel); var l=new List(); foreach (var v in formattedValues) { diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartLegendRenderer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartLegendRenderer.cs index 2d2ec6e5e..930c394f3 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartLegendRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartLegendRenderer.cs @@ -430,7 +430,7 @@ internal DrawingLegendSerie SetLegendSeries(double entryWidth, double entryHeigh } else { - var totSeriesCount = Chart.PlotArea.ChartTypes.Sum(x => x.Series.Count)-1; + //var totSeriesCount = Chart.PlotArea.ChartTypes.Sum(x => x.Series.Count)-1; int entryIx = 0; int prevCtSeriesCount = 0; index = 0; @@ -443,7 +443,7 @@ internal DrawingLegendSerie SetLegendSeries(double entryWidth, double entryHeigh { ix = ct.Series.Count - 1; end = -1; - index = totSeriesCount-prevCtSeriesCount; + index = ix+prevCtSeriesCount; } else { diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartPlotareaRenderer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartPlotareaRenderer.cs index 540af941f..59a6c0959 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartPlotareaRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartPlotareaRenderer.cs @@ -94,7 +94,7 @@ internal double GetPlotAreaHeight(RectRenderItem rect) { var bottomAxis = GetAxisActualByPosition(eActualAxisPosition.Bottom, 0); double vaHeight = 0; - if (bottomAxis!=null) + if (bottomAxis!=null && bottomAxis.Axis.LabelPosition != eTickLabelPosition.None) { var bottomSecondAxis = GetAxisActualByPosition(eActualAxisPosition.Bottom, 1); if(bottomSecondAxis==null) @@ -105,7 +105,7 @@ internal double GetPlotAreaHeight(RectRenderItem rect) vaHeight = secAxis.Title?.Rectangle?.Height??0D; } } - + vaHeight += (bottomAxis.Rectangle?.Height ?? 0D) + (bottomAxis.Title?.GetHeightOverlay(0) ?? 0D) + (bottomAxis.DisplayUnitTitle?.TextBox.GetActualHeight() ?? 0D) + (bottomSecondAxis?.Rectangle?.Height ?? 0D); } else @@ -133,7 +133,7 @@ internal double GetPlotAreaWidth(RectRenderItem rect) var rightSecondAxis = GetAxisActualByPosition(eActualAxisPosition.Right, 1); var lp = ChartRenderer.Chart.Legend?.Position; var right = ((lp == eLegendPosition.Right || lp == eLegendPosition.TopRight) && ChartRenderer.Legend != null && ChartRenderer.Legend.Overlay==false ? - ChartRenderer.Legend.Rectangle.GlobalLeft - RightMargin : + ChartRenderer.ChartArea.Rectangle.Width - ChartRenderer.Legend.Rectangle.Width - RightMargin : ChartRenderer.ChartArea.Rectangle.Width - RightMargin); var leftAxis = GetAxisActualByPosition(eActualAxisPosition.Left, 0); diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartTitleRenderer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartTitleRenderer.cs index 3ae7e0e9e..7b4448b50 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartTitleRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartTitleRenderer.cs @@ -214,9 +214,9 @@ internal void InitTextBox(double maxWidth, double maxHeight, MeasurementFont def { TextBox = new DrawingTextBox(ChartRenderer.RenderContext, _svgChart.Drawing, _svgChart.ChartArea.Rectangle, defaultFont, maxWidth, maxHeight); TextBox.TextBody.Style.FillColor = "#" + ChartRenderer.InbuiltStyleFontColor.Value.ToColorString(); - if (_title.Rotation != 0) + if (_title.TextBody.Rotation.HasValue && _title.TextBody.Rotation != 0) { - TextBox.Rotation = _title.Rotation; + TextBox.Rotation = _title.TextBody.Rotation.Value; } if (_title.TextBody.Paragraphs.Count > 0) { diff --git a/src/EPPlus/Drawing/Renderer/ChartRenderer.cs b/src/EPPlus/Drawing/Renderer/ChartRenderer.cs index 53b587874..b8809d848 100644 --- a/src/EPPlus/Drawing/Renderer/ChartRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/ChartRenderer.cs @@ -216,11 +216,11 @@ private void PlaceHorizontalAxis(ChartAxisRenderer horizontalAxis, bool isSecond var cv = ax.CrossesAt.Value; if (isSecondary) { - axisY = SecondVerticalAxis.GetPositionInPlotarea(Math.Max(Math.Min(cv, SecondHorizontalAxis.Min), SecondVerticalAxis.Max)); + axisY = SecondVerticalAxis.GetAxisPosition(cv); } else { - axisY = VerticalAxis.GetPositionInPlotarea(Math.Min(Math.Max(cv, VerticalAxis.Min), VerticalAxis.Max)); + axisY = VerticalAxis.GetAxisPosition(cv); } horizontalAxis.Line.Y1 = horizontalAxis.Line.Y2 = axisY; } @@ -231,7 +231,7 @@ private void PlaceHorizontalAxis(ChartAxisRenderer horizontalAxis, bool isSecond { if(VerticalAxis.Min < 0) { - pos = VerticalAxis.GetPositionInPlotarea(0); + pos = VerticalAxis.GetAxisPosition(0); } else { @@ -297,12 +297,12 @@ private void PlaceHorizontalAxis(ChartAxisRenderer horizontalAxis, bool isSecond case eTickLabelPosition.High: //if (ax.AxisPosition == eAxisPosition.Bottom) //{ - horizontalAxis.Rectangle.Top = Plotarea.Group.GlobalTop; + horizontalAxis.Rectangle.Top = Plotarea.Group.GlobalTop - horizontalAxis.Rectangle.Height; horizontalAxis.AdjustPosBySize = true; //} //else //{ - // horizontalAxis.Rectangle.Top = Plotarea.Group.GlobalTop+Plotarea.Rectangle.Height; + // horizontalAxis.Rectangle.Top = Plotarea.Group.GlobalBottom; // horizontalAxis.AdjustPosBySize = false; //} break; @@ -348,7 +348,6 @@ private void PlaceVerticalAxis(ChartAxisRenderer verticalAxis, bool isSecondary) verticalAxis.Line?.Y2 = (float)verticalAxis.Rectangle.Height; var axisPos = verticalAxis.Axis.ActualAxisPosition; var ax = verticalAxis.Axis; - if (ax.CrossesAt.HasValue) { double axisX; @@ -486,7 +485,14 @@ private void PlaceVerticalAxisTitle(ChartAxisRenderer verticalAxis) { var sinRot = Math.Abs(Math.Sin(MathHelper.Radians(verticalAxis.Title.TextBox.Rotation))); var cosRot = Math.Abs(Math.Cos(MathHelper.Radians(verticalAxis.Title.TextBox.Rotation))); - verticalAxis.Title.TextBox.Top = Plotarea.Group.Top + (Plotarea.Rectangle.Height / 2) - ((verticalAxis.Title.TextBox.Height * cosRot + verticalAxis.Title.TextBox.Width * sinRot) / 2); + + if(verticalAxis.Title.TextBox.Rotation < 0) + { + verticalAxis.Title.TextBox.Top = Plotarea.Group.Top + (Plotarea.Rectangle.Height / 2) - ((verticalAxis.Title.TextBox.Height * cosRot + verticalAxis.Title.TextBox.Width * sinRot) / 2); + } + { + verticalAxis.Title.TextBox.Top = Plotarea.Group.Top + (Plotarea.Rectangle.Height / 2) + ((verticalAxis.Title.TextBox.Height * cosRot + verticalAxis.Title.TextBox.Width * sinRot) / 2); + } if (verticalAxis.Axis.AxisPosition == eAxisPosition.Left) { @@ -683,11 +689,6 @@ public bool AppendItems() { Plotarea?.AppendRenderItems(RenderItems); - HorizontalAxis?.AppendRenderItems(RenderItems); - VerticalAxis?.AppendRenderItems(RenderItems); - SecondHorizontalAxis?.AppendRenderItems(RenderItems); - SecondVerticalAxis?.AppendRenderItems(RenderItems); - if (Plotarea != null) { //TODO: Add typeDrawers group? @@ -698,6 +699,10 @@ public bool AppendItems() drawer.AppendRenderItems(RenderItems); } } + HorizontalAxis?.AppendRenderItems(RenderItems); + VerticalAxis?.AppendRenderItems(RenderItems); + SecondHorizontalAxis?.AppendRenderItems(RenderItems); + SecondVerticalAxis?.AppendRenderItems(RenderItems); HorizontalAxis?.Textboxes?.AppendRenderItems(RenderItems); VerticalAxis?.Textboxes?.AppendRenderItems(RenderItems); diff --git a/src/EPPlus/ExcelRange.cs b/src/EPPlus/ExcelRange.cs index d5676cb4f..cfe69f697 100644 --- a/src/EPPlus/ExcelRange.cs +++ b/src/EPPlus/ExcelRange.cs @@ -10,6 +10,9 @@ Date Author Change ************************************************************************************************* 01/27/2020 EPPlus Software AB Initial release EPPlus 5 *************************************************************************************************/ +using OfficeOpenXml.Core.CellStore; +using OfficeOpenXml.FormulaParsing.Excel.Functions.DateAndTime; +using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup; using System; using System.Xml.Serialization; diff --git a/src/EPPlus/ExcelRangeBase.cs b/src/EPPlus/ExcelRangeBase.cs index 868ea0437..5006bb6fa 100644 --- a/src/EPPlus/ExcelRangeBase.cs +++ b/src/EPPlus/ExcelRangeBase.cs @@ -2953,5 +2953,56 @@ internal ExcelTable GetIntersectingTable() } return _worksheet.Tables.GetIntersectingRanges(this).Select(x => x.Value).FirstOrDefault(); } + internal bool IsHidden + { + get + { + if (_fromRow == _toRow) + { + if (IsRowHidden) + { + return true; + } + return IsColumnHidden; + } + else + { + return IsColumnHidden || IsRowHidden; + } + } + } + + private bool IsColumnHidden + { + get + { + ExcelValue ev = default; + for (int col = _fromCol; col <= _toCol; col++) + { + if (Worksheet._values.Exists(0, col, ref ev) == false || ((ExcelColumn)ev._value).Hidden == false) + { + return false; + } + } + + return true; + } + } + private bool IsRowHidden + { + get + { + ExcelValue ev = default; + for (int row = _fromRow; row <= _toRow; row++) + { + if (Worksheet._values.Exists(row, 0, ref ev) == false || ((RowInternal)ev._value).Hidden == false) + { + return false; + } + } + + return true; + } + } } }