Skip to content

Commit 1be9324

Browse files
committed
WIP:Fixed axis positioning and bar/column drawing
1 parent add9357 commit 1be9324

3 files changed

Lines changed: 45 additions & 38 deletions

File tree

src/EPPlus.Export.ImageRenderer.Test/Chart/ColumnChartTests.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ public void GenerateSvgForColumnCharts2()
4242
var ws = p.Workbook.Worksheets[1];
4343
var renderer = new EPPlusImageRenderer.ImageRenderer();
4444

45-
//var ix = 2;
46-
//var c = ws.Drawings[ix];
47-
//var svg = renderer.RenderDrawingToSvg(c);
48-
//SaveTextFileToWorkbook($"svg\\ChartForSvg_ind{ix++}.svg", svg);
45+
var ix = 2;
46+
var c = ws.Drawings[ix];
47+
var svg = renderer.RenderDrawingToSvg(c);
48+
SaveTextFileToWorkbook($"svg\\ChartForSvg_ind{ix++}.svg", svg);
4949

50-
var ix = 0;
51-
foreach (ExcelChart c in ws.Drawings)
52-
{
53-
var svg = renderer.RenderDrawingToSvg(c);
54-
SaveTextFileToWorkbook($"svg\\ColumnChartForSvg_sheet2_{ix++}.svg", svg);
55-
}
50+
//var ix = 0;
51+
//foreach (ExcelChart c in ws.Drawings)
52+
//{
53+
// var svg = renderer.RenderDrawingToSvg(c);
54+
// SaveTextFileToWorkbook($"svg\\ColumnChartForSvg_sheet2_{ix++}.svg", svg);
55+
//}
5656
}
5757
}
5858
}

src/EPPlus.Export.ImageRenderer/Svg/Chart/ChartTypeDrawers/BarColumnChartTypeDrawer.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private void AddBar(ExcelBarChart chartType, ExcelBarChartSerie serie, List<List
122122
double yAxisStart;
123123
if (yAxis.Axis.Crosses == eCrosses.AutoZero)
124124
{
125-
yAxisStart = valAx.GetPositionInPlotarea(valAx.Min <= 0 ? 0D : yAxis.Min, true);
125+
yAxisStart = valAx.GetPositionInPlotarea(valAx.Min <= 0 ? 0D : valAx.Min, true);
126126
}
127127
else if (yAxis.Axis.Crosses == eCrosses.Min)
128128
{
@@ -149,18 +149,20 @@ private void AddBar(ExcelBarChart chartType, ExcelBarChartSerie serie, List<List
149149

150150
var y = ConvertUtil.GetValueDouble(valValues[i], false, true);
151151

152-
var xPos = catAx.GetPositionInPlotarea(x, true) + halfGap + position * barWidth * step;
152+
var rect = new SvgRenderRectItem(ChartRenderer, _svgChart.Plotarea.Rectangle.Bounds);
153153
var yPos = valAx.GetPositionInPlotarea(y);
154154

155-
var rect = new SvgRenderRectItem(ChartRenderer, _svgChart.Plotarea.Rectangle.Bounds);
156155
if (isColumn)
157156
{
157+
var xPos = catAx.GetPositionInPlotarea(x, true) + halfGap + position * barWidth * step;
158158
rect.Left = xPos;
159159
rect.Width = barWidth;
160160
}
161161
else
162162
{
163-
rect.Top = yWidth - xPos - barWidth;
163+
//var xPos = catAx.GetPositionInPlotarea(x, true) - halfGap - (position + 1) * barWidth * step;
164+
var xPos = catAx.GetPositionInPlotarea(x, true) + halfGap + position * barWidth * step;
165+
rect.Top = xPos;
164166
rect.Height = barWidth;
165167
}
166168

src/EPPlus.Export.ImageRenderer/Svg/Chart/SvgChartAxis.cs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,11 @@ internal void AddTickmarksAndValues(List<RenderItem> DefItems)
292292
{
293293

294294
}
295+
295296
if (AxisValues != null && AxisValues.Count > 0 && Axis.Deleted==false && Axis.LabelPosition != eTickLabelPosition.None)
296297
{
297298
Textboxes = new SvgAxisTextBoxes(SvgChart);
298-
Textboxes.TextBoxes = GetAxisValueTextBoxes();
299+
Textboxes.TextBoxes = GetAxisValueTextBoxes();
299300
}
300301
}
301302

@@ -343,7 +344,7 @@ private List<SvgTextBox> GetAxisValueTextBoxes()
343344
double x, y;
344345
if(LabelOrientation==eTextOrientation.Horizontal)
345346
{
346-
if (Axis.AxisType == eAxisType.Cat)
347+
if (Axis.AxisType == eAxisType.Cat || Axis.AxisType==eAxisType.Date)
347348
{
348349
x = ticMarkX;
349350
y = ticMarkY;
@@ -364,7 +365,7 @@ private List<SvgTextBox> GetAxisValueTextBoxes()
364365
}
365366
else
366367
{
367-
x = ticMarkX; // - width / 2;
368+
x = ticMarkX - width / 2;
368369
y = ticMarkY;
369370
}
370371
}
@@ -485,31 +486,35 @@ private List<SvgTextBox> GetAxisValueTextBoxes()
485486

486487
private double GetAxisItemLeft(int i, OfficeOpenXml.Interfaces.Drawing.Text.TextMeasurement m)
487488
{
488-
if (Axis.AxisPosition == eAxisPosition.Left)
489+
if (Axis.IsVertical)
489490
{
490491
return Rectangle.Left;
491492
}
492-
else if (Axis.AxisPosition == eAxisPosition.Right)
493-
{
493+
//else if (Axis.AxisPosition == eAxisPosition.Right)
494+
//{
494495

495-
return Rectangle.Left;
496-
}
496+
// return Rectangle.Left;
497+
//}
497498
else
498499
{
499-
if ((Axis.AxisType == eAxisType.Cat || Axis.IsVertical==false) && LabelOrientation==eTextOrientation.Horizontal)
500-
{
501-
//Between tickmarks
502-
var majorWidth = Rectangle.Width / AxisValues.Count;
503-
var majorTickStartingPosition = Rectangle.Left + majorWidth * i;
504-
//var middleOfBounds = majorTickStartingPosition + (majorWidth / 2);
505-
return majorTickStartingPosition;
506-
}
507-
else
508-
{
509-
if(Axis.AxisType == eAxisType.Cat)
500+
//if ((Axis.AxisType == eAxisType.Cat || Axis.IsVertical==false) && LabelOrientation==eTextOrientation.Horizontal)
501+
//{
502+
// //Between tickmarks
503+
// var majorWidth = Rectangle.Width / (AxisValues.Count);
504+
// var majorTickStartingPosition = Rectangle.Left + majorWidth * i;
505+
// //var middleOfBounds = majorTickStartingPosition + (majorWidth / 2);
506+
// return majorTickStartingPosition;
507+
//}
508+
//else
509+
//{
510+
if(Axis.AxisType == eAxisType.Cat || Axis.AxisType==eAxisType.Date)
510511
{
511512
var majorWidth = Rectangle.Width / AxisValues.Count;
512513
var majorTickStartingPosition = Rectangle.Left + majorWidth * i;
514+
if(Axis.AxisType == eAxisType.Date)
515+
{
516+
return majorTickStartingPosition + majorWidth / 2 - m.Width / 2;
517+
}
513518
//var middleOfBounds = majorTickStartingPosition + (majorWidth / 2);
514519
return majorTickStartingPosition;
515520
}
@@ -521,7 +526,7 @@ private double GetAxisItemLeft(int i, OfficeOpenXml.Interfaces.Drawing.Text.Text
521526
var majorWidth = Rectangle.Width * (v - Min) / (Max - Min);
522527
return Rectangle.Left + majorWidth;
523528
}
524-
}
529+
//}
525530
}
526531
}
527532

@@ -577,15 +582,15 @@ private double GetAxisItemTop(int i, OfficeOpenXml.Interfaces.Drawing.Text.TextM
577582
}
578583
else
579584
{
580-
var majorHeight = Rectangle.Height / (AxisValues.Count);
585+
var majorHeight = Rectangle.Height / (AxisValues.Count - 1);
581586
if (Axis.AxisType == eAxisType.Cat || Axis.AxisType == eAxisType.Date)
582587
{
583588
return Rectangle.Top + majorHeight * (AxisValues.Count - i - 1) + (majorHeight / 2) - m.Height / 2;
584589
}
585590
else
586-
{
587-
//return Rectangle.Top + majorHeight * (AxisValues.Count - i - 1) - m.Height / 2;
591+
{
588592
return Rectangle.Top + majorHeight * (AxisValues.Count - i - 1);
593+
//return Rectangle.Top + majorHeight * (AxisValues.Count - i - 1);
589594
}
590595
}
591596

@@ -854,7 +859,7 @@ internal double GetPositionInPlotarea(double val, bool startValue=false)
854859
{
855860
if (val < Min || val > Max) return double.NaN;
856861
var diff = Max - Min + 1;
857-
return (((Max-val) / diff * SvgChart.Plotarea.Rectangle.Height));
862+
return (Max - val) / diff * SvgChart.Plotarea.Rectangle.Height;
858863
}
859864
}
860865
else

0 commit comments

Comments
 (0)