Skip to content

Commit

Permalink
Added leftToRight parameter to AddStockChart
Browse files Browse the repository at this point in the history
  • Loading branch information
swmal committed Oct 25, 2023
1 parent 23f2399 commit 730d13d
Showing 1 changed file with 89 additions and 18 deletions.
107 changes: 89 additions & 18 deletions src/EPPlus/Drawing/ExcelDrawings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,61 +408,132 @@ public ExcelChartEx AddExtendedChart(string Name, eChartExType ChartType, ExcelP
/// </summary>
/// <param name="Name"></param>
/// <param name="ChartType">The Stock chart type</param>
/// <param name="Range">The category serie. A serie containng dates </param>
/// <param name="Range">The range containing all the series. Must match the stock chart type's expected ranges</param>
/// <param name="leftToRight">If true the range will be handled by rows, if false it will be handled by column</param>
/// <returns>The chart</returns>
public ExcelStockChart AddStockChart(string Name, eStockChartType ChartType, ExcelRangeBase Range)
public ExcelStockChart AddStockChart(string Name, eStockChartType ChartType, ExcelRangeBase Range, bool leftToRight = true)
{
var startRow = Range.Start.Row;
var startCol = Range.Start.Column;
var endRow = Range.End.Row;
var endCol = Range.End.Column;
var ws = Range.Worksheet;
switch (ChartType)
{
case eStockChartType.StockHLC:
if (Range.Columns != 4)
if (leftToRight)
{
throw (new InvalidOperationException("Range must contain 4 columns with the Category serie to the left and the High Price, Low Price and Close Price series"));
}
return AddStockChart(Name,
if (Range.Columns != 4)
{
throw (new InvalidOperationException("Range must contain 4 columns with the Category serie to the left of the High Price, Low Price and Close Price series"));
}
return AddStockChart(Name,
ws.Cells[startRow, startCol, endRow, startCol],
ws.Cells[startRow, startCol + 1, endRow, startCol + 1],
ws.Cells[startRow, startCol + 2, endRow, startCol + 2],
ws.Cells[startRow, startCol + 3, endRow, startCol + 3]);
case eStockChartType.StockOHLC:
if (Range.Columns != 5)
}
else
{
throw (new InvalidOperationException("Range must contain 5 columns with the Category serie to the left and the Opening Price, High Price, Low Price and Close Price series"));
if (Range.Rows != 4)
{
throw (new InvalidOperationException("Range must contain 4 columns with the Category serie above the High Price, Low Price and Close Price series"));
}
return AddStockChart(Name,
ws.Cells[startRow, startCol, startRow, endCol],
ws.Cells[startRow + 1, startCol, startRow + 1, endCol],
ws.Cells[startRow + 2, startCol, startRow + 2, endCol],
ws.Cells[startRow + 3, startCol, startRow + 3, endCol]);
}
return AddStockChart(Name,
case eStockChartType.StockOHLC:

if (leftToRight)
{
if (Range.Columns != 5)
{
throw (new InvalidOperationException("Range must contain 5 columns with the Category serie to the left of the Opening Price, High Price, Low Price and Close Price series"));
}
return AddStockChart(Name,
ws.Cells[startRow, startCol, endRow, startCol],
ws.Cells[startRow, startCol + 2, endRow, startCol + 2],
ws.Cells[startRow, startCol + 3, endRow, startCol + 3],
ws.Cells[startRow, startCol + 4, endRow, startCol + 4],
ws.Cells[startRow, startCol + 1, endRow, startCol + 1]);
case eStockChartType.StockVHLC:
if (Range.Columns != 5)
}
else
{
throw (new InvalidOperationException("Range must contain 5 columns with the Category serie to the left and the Volume, High Price, Low Price and Close Price series"));
if (Range.Rows != 5)
{
throw (new InvalidOperationException("Range must contain 5 rows with the Category serie above the Opening Price, High Price, Low Price and Close Price series"));
}
return AddStockChart(Name,
ws.Cells[startRow, startCol, endRow, startCol],
ws.Cells[startRow + 2, startCol, startRow + 2, endCol],
ws.Cells[startRow + 3, startCol, startRow + 3, endCol],
ws.Cells[startRow + 4, startCol, startRow + 4, endCol],
ws.Cells[startRow + 1, startCol, startRow + 1, endCol]);
}
return AddStockChart(Name,

case eStockChartType.StockVHLC:

if (leftToRight)
{
if (Range.Columns != 5)
{
throw (new InvalidOperationException("Range must contain 5 columns with the Category serie to the left of the Volume, High Price, Low Price and Close Price series"));
}
return AddStockChart(Name,
ws.Cells[startRow, startCol, endRow, startCol],
ws.Cells[startRow, startCol + 2, endRow, startCol + 2],
ws.Cells[startRow, startCol + 3, endRow, startCol + 3],
ws.Cells[startRow, startCol + 4, endRow, startCol + 4],
null,
ws.Cells[startRow, startCol + 1, endRow, startCol + 1]);
case eStockChartType.StockVOHLC:
if (Range.Columns != 6)
}
else
{
throw (new InvalidOperationException("Range must contain 6 columns with the Category serie to the left and the Volume, Opening Price, High Price, Low Price and Close Price series"));
if (Range.Rows != 5)
{
throw (new InvalidOperationException("Range must contain 5 rows with the Category serie above the Volume, High Price, Low Price and Close Price series"));
}
return AddStockChart(Name,
ws.Cells[startRow, startCol, startRow, endCol],
ws.Cells[startRow + 2, startCol, startRow + 2, endCol],
ws.Cells[startRow + 3, startCol, startRow + 3, endCol],
ws.Cells[startRow + 4, startCol, startRow + 4, endCol],
null,
ws.Cells[startRow + 1, startCol, startRow + 1, endCol]);
}
return AddStockChart(Name,
case eStockChartType.StockVOHLC:

if (leftToRight)
{
if (Range.Columns != 6)
{
throw (new InvalidOperationException("Range must contain 6 columns with the Category serie to the left of the Volume, Opening Price, High Price, Low Price and Close Price series"));
}
return AddStockChart(Name,
ws.Cells[startRow, startCol, endRow, startCol],
ws.Cells[startRow, startCol + 3, endRow, startCol + 3],
ws.Cells[startRow, startCol + 4, endRow, startCol + 4],
ws.Cells[startRow, startCol + 5, endRow, startCol + 5],
ws.Cells[startRow, startCol + 2, endRow, startCol + 2],
ws.Cells[startRow, startCol + 1, endRow, startCol + 1]);
}
else
{
if (Range.Rows != 6)
{
throw (new InvalidOperationException("Range must contain 6 rows with the Category serie to the left of the Volume, Opening Price, High Price, Low Price and Close Price series"));
}
return AddStockChart(Name,
ws.Cells[startRow, startCol, startRow, endCol],
ws.Cells[startRow + 3, startCol, startRow + 3, endCol],
ws.Cells[startRow + 4, startCol, startRow + 4, endCol],
ws.Cells[startRow + 5, startCol, startRow + 5, endCol],
ws.Cells[startRow + 2, startCol, startRow + 2, endCol],
ws.Cells[startRow + 1, startCol, startRow + 1, endCol]);
}
default:
throw new InvalidOperationException("Unknown eStockChartType");
}
Expand Down

0 comments on commit 730d13d

Please sign in to comment.