Skip to content

Commit

Permalink
Added new sample for chart legend entries
Browse files Browse the repository at this point in the history
  • Loading branch information
JanKallman committed Oct 20, 2021
1 parent c18d1f7 commit ecdd13d
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 29 deletions.
9 changes: 5 additions & 4 deletions 08-SalesReport/SalesReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,22 @@ public static string Run(string connectionString)
// add the file path to the footer
worksheet.HeaderFooter.OddFooter.LeftAlignedText = ExcelHeaderFooter.FilePath + ExcelHeaderFooter.FileName;
}
// we had better add some document properties to the spreadsheet

// set some core property values
// We can also add some document properties to the spreadsheet

// Set some core property values
xlPackage.Workbook.Properties.Title = "Sales Report";
xlPackage.Workbook.Properties.Author = "Jan Källman";
xlPackage.Workbook.Properties.Subject = "Sales Report Samples";
xlPackage.Workbook.Properties.Keywords = "Office Open XML";
xlPackage.Workbook.Properties.Category = "Sales Report Samples";
xlPackage.Workbook.Properties.Comments = "This sample demonstrates how to create an Excel file from scratch using EPPlus";

// set some extended property values
// Set some extended property values
xlPackage.Workbook.Properties.Company = "Fiction Inc.";
xlPackage.Workbook.Properties.HyperlinkBase = new Uri("https://EPPlusSoftware.com");

// set some custom property values
// Set some custom property values
xlPackage.Workbook.Properties.SetCustomPropertyValue("Checked by", "Jan Källman");
xlPackage.Workbook.Properties.SetCustomPropertyValue("EmployeeID", "1");
xlPackage.Workbook.Properties.SetCustomPropertyValue("AssemblyName", "EPPlus");
Expand Down
11 changes: 7 additions & 4 deletions 15-ChartsAndThemes/ChartsAndThemesSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ public static async Task<string> RunAsync(string connectionString, FileInfo xlFi

//Adds a scatter chart with a moving average trendline.
ScatterChartSample.Add(package);


//Adds a column chart with a legend where we style and remove individual legend items.
await ColumnChartWithLegendSample.Add(connectionString, package);

//Adds a bubble-chartsheet
ChartWorksheetSample.Add(package);

Expand All @@ -79,13 +82,13 @@ public static async Task<string> RunAsync(string connectionString, FileInfo xlFi
//Adds a sunburst and a treemap chart
await SunburstAndTreemapChartSample.Add(connectionString, package);

//Add a box & whisker and a histogram chart
//Adds a box & whisker and a histogram chart
BoxWhiskerHistogramChartSample.Add(package);

// Add a waterfall chart
// Adds a waterfall chart
WaterfallChartSample.Add(package);

// Add a funnel chart
// Adds a funnel chart
FunnelChartSample.Add(package);

await RegionMapChartSample.Add(connectionString, package);
Expand Down
40 changes: 40 additions & 0 deletions 15-ChartsAndThemes/ColumnChartsWithLegendSample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using OfficeOpenXml;
using OfficeOpenXml.Drawing;
using OfficeOpenXml.Drawing.Chart;
using OfficeOpenXml.Drawing.Chart.Style;
using System.Drawing;
using System.Threading.Tasks;

namespace EPPlusSamples
{
public class ColumnChartWithLegendSample : ChartSampleBase
{
public static async Task Add(string connectionString, ExcelPackage package)
{
var ws = package.Workbook.Worksheets.Add("ColumnCharts");

var range = await LoadFromDatabase(connectionString, ws);

//Add a line chart
var chart = ws.Drawings.AddBarChart("ColumnChartWithLegend", eBarChartType.ColumnStacked);
var serie1 = chart.Series.Add(ws.Cells[2, 2, 16, 2], ws.Cells[2, 1, 16, 1]);
serie1.Header = "Order Value";
var serie2 = chart.Series.Add(ws.Cells[2, 3, 16, 3], ws.Cells[2, 1, 16, 1]);
serie2.Header = "Tax";
var serie3 = chart.Series.Add(ws.Cells[2, 4, 16, 4], ws.Cells[2, 1, 16, 1]);
serie3.Header = "Freight";
chart.SetPosition(0, 0, 6, 0);
chart.SetSize(1200, 400);
chart.Title.Text = "Column chart";

//Set style 10
chart.StyleManager.SetChartStyle(ePresetChartStyle.ColumnChartStyle10);

chart.Legend.Entries[0].Font.Fill.Color = Color.Red;
chart.Legend.Entries[1].Font.Fill.Color = Color.Green;
chart.Legend.Entries[2].Deleted = true;

range.AutoFitColumns(0);
}
}
}
2 changes: 1 addition & 1 deletion 17-FXReportFromDatabase/FXReportFromDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static string Run(string connectionString)
chart.Legend.Position = eLegendPosition.Bottom;

//Set Font bold on USD/EUR in the legend.
chart.Series[1].LegendEntry.Font.Bold = true;
chart.Legend.Entries[1].Font.Bold = true;

//Set the chart style
chart.StyleManager.SetChartStyle(236);
Expand Down
6 changes: 4 additions & 2 deletions 30-WorkingWithRanges/CopyRangeSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,18 @@ private static void CopyRange(ExcelPackage p, ExcelWorksheet sourceWs)

//Copy styles and merged cells, excluding values and hyperlinks.
sourceRange.Copy(ws.Cells["C45"], ExcelRangeCopyOptionFlags.ExcludeValues, ExcelRangeCopyOptionFlags.ExcludeHyperLinks);

ws.Cells.AutoFitColumns();
}
private static void CopyStyles(ExcelPackage p, ExcelWorksheet sourceWs)
{
var ws = p.Workbook.Worksheets.Add("CopyStyles");

//Create a new random report
FillRangeWithRandomData(ws);

//Copy the styles from the sales report.
//If the destination range is larger that the source range styles are filled down and right using the last column/row.
//If the destination range is larger that the source range styles are filled down and right using the last column/row the source range of the source range.
sourceWs.Cells["A1:G5"].CopyStyles(ws.Cells["A1:G50"]);

ws.Cells.AutoFitColumns();
Expand Down
57 changes: 39 additions & 18 deletions 30-WorkingWithRanges/FillRangeSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,27 @@ private static void FillNumber(ExcelPackage p)
{
var ws = p.Workbook.Worksheets.Add("FillNumber Samples");

ws.SetValue(1, 1, 50);
ws.SetValue(1, 1, "FillNumber Default");
ws.SetValue(2, 1, 50);
//Fill the range by adding 1 for each cell starting from the value in the top-left cell.
ws.Cells["A1:A20"].FillNumber();
ws.Cells["A2:A20"].FillNumber();

ws.SetValue(1, 2, "FillNumber, Start 30, Step -2");
//Fill the range by subtracting 2 from the initial value 30.
ws.Cells["B1:B20"].FillNumber(30, -2);
ws.Cells["B2:B20"].FillNumber(30, -2);

//Fill by starting with 100 and increase 5% for each cell. Fill by left by row
ws.Cells["D2:AA2"].FillNumber(x =>
ws.SetValue(1, 4, "FillNumber, Multiply add 5% row-wise");
//Fill by starting with 100 and increase 5% for each cell. Fill to the left by row
ws.Cells["E1:AA1"].FillNumber(x =>
{
x.CalculationMethod = eCalculationMethod.Multiply;
x.StartValue = 100;
x.StepValue = 1.05;
x.Direction = eFillDirection.Row;
});

ws.Cells["A1:D1"].Style.Font.Bold = true;
ws.Cells.AutoFitColumns();
}
/// <summary>
/// Several samples how to use the FillDate method
Expand All @@ -62,14 +68,18 @@ private static void FillDate(ExcelPackage p)
var ws = p.Workbook.Worksheets.Add("FillDateTime Samples");

//Fill dates starting from the value in the first cell. By default a 1 day increase is assumed and fill is performed per column downwards.
ws.SetValue("A1", "FillDateTime-Default");
ws.SetValue("A2", new DateTime(2021, 1, 1));
ws.Cells["A2:A60"].FillDateTime();

//Fill dates using the starting value from a fixed start value instead of using the first cell.
ws.Cells["B2:B60"].FillDateTime(new DateTime(2021, 6, 30));

ws.SetValue("B1", "FillDateTime-Step two months");
//Fill dates using the starting value from a fixed start value instead of using the first cell. Increase with 2 months.
ws.Cells["B2:B60"].FillDateTime(new DateTime(2021, 6, 30), eDateTimeUnit.Month, 2);

ws.Cells[2, 1, 60, 2].Style.Numberformat.Format = "yyyy-mm-dd";

ws.SetValue("C1", "FillDateTime-Month, exclude weekends and holidays");
//Fill dates per last day of the quarter. If the start value is the last day of the month, this is used for all dates in the fill.
//This sample exclude weekends and adds some holiday dates.
//Column C2 and D2 are used as start values.
Expand Down Expand Up @@ -100,21 +110,23 @@ private static void FillDate(ExcelPackage p)
);
});

ws.SetValue("F1", "FillDateTime-By Row from Right-to-Left");
//You can also fill row-wise and fill right-to-left.
//Note that as row 6 don't have a start value it's left blank in the fill.
//Note that as row 4 don't have a start value it's left blank in the fill.
//We also set an end value where the fill will stop.
ws.Cells["AA4"].Value = new DateTime(2030, 1, 1);
ws.Cells["AA5"].Value = new DateTime(2030, 1, 5);
ws.Cells["AA7"].Value = new DateTime(2030, 1, 10);
ws.Cells["F4:AA7"].FillDateTime(x =>
ws.Cells["AA1"].Value = new DateTime(2030, 1, 1);
ws.Cells["AA2"].Value = new DateTime(2030, 1, 5);
ws.Cells["AA4"].Value = new DateTime(2030, 1, 10);
ws.Cells["H1:AA4"].FillDateTime(x =>
{
x.Direction = eFillDirection.Row;
x.StartPosition = eFillStartPosition.BottomRight;
x.EndValue = new DateTime(2030, 1, 20);
x.NumberFormat = "yyyy-mm-dd";
});

ws.Cells["A1:F1"].Style.Font.Bold = true;
ws.Columns[1, 27].AutoFit();

}
/// <summary>
/// Several samples how to use the FillList method
Expand All @@ -125,23 +137,32 @@ private static void FillList(ExcelPackage p)
var ws = p.Workbook.Worksheets.Add("Fill List Samples");

var list = new[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
ws.Cells["A1:A20"].FillList(list);
ws.SetValue("A1", "FillList-Default");
//FillList repeates the list. Default starts from the first item at position 0
ws.Cells["A2:A20"].FillList(list);

ws.Cells["B1:B20"].FillList(list, x=> { x.StartIndex=6; });
ws.Cells["E1:AA1"].FillList(list, x =>
ws.SetValue("B1", "FillList-Start index 6");
//Starts at position 6 in the list
ws.Cells["B2:B20"].FillList(list, x=> { x.StartIndex=6; });

ws.SetValue("E1", "FillList-Per row");
//Fill per row
ws.Cells["F1:AA1"].FillList(list, x =>
{
x.Direction = eFillDirection.Row;
});

ws.SetValue("C1", "FillList-Primes, bottom up");
//Fill the list of primes starting from the bottom-up.
//We set the range to the size of the list so it's not repeated.
var primes = new List<int>{ 2,5,7,11,13,17,19,23,29,997,1009 };
ws.Cells[1,3,primes.Count, 3].FillList(primes, x =>
ws.Cells[2,3,primes.Count+1, 3].FillList(primes, x =>
{
x.NumberFormat = "#,##0";
x.StartPosition = eFillStartPosition.BottomRight;
});

ws.Cells["A1:E1"].Style.Font.Bold = true;
ws.Columns[1, 27].AutoFit();
}
}
}

0 comments on commit ecdd13d

Please sign in to comment.