PIbd-32_Artamonova_T.V._COP_2/COP/PieChart.cs

72 lines
2.3 KiB
C#
Raw Normal View History

2023-10-13 21:09:48 +04:00
using COP.Info;
using OfficeOpenXml;
using System.ComponentModel;
using LicenseContext = OfficeOpenXml.LicenseContext;
using OfficeOpenXml.Drawing.Chart;
namespace COP
{
public partial class PieChart : Component
{
public PieChart()
{
InitializeComponent();
}
public PieChart(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public void GenerateDocument(ExcelChartInfo info)
{
if (string.IsNullOrEmpty(info.filePath))
{
throw new ArgumentException("File path is null or empty.");
}
if (string.IsNullOrEmpty(info.chartTitle))
{
throw new ArgumentException("Chart title is null or empty.");
}
if (string.IsNullOrEmpty(info.documentTitle))
{
throw new ArgumentException("Document title is null or empty.");
}
if (info.data == null || info.data.Count == 0)
{
throw new ArgumentException("Data is null or empty.");
}
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using ExcelPackage excelPackage = new();
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Sheet 1");
worksheet.Cells["A1"].Value = info.documentTitle;
int row = 2;
int startCol = 1;
int endCol = 1;
foreach (var data in info.data)
{
worksheet.Cells[row, endCol].Value = data.Name;
worksheet.Cells[row + 1, endCol].Value = data.Value;
endCol++;
}
ExcelPieChart? pieChart = worksheet.Drawings.AddChart(info.chartTitle, eChartType.Pie) as ExcelPieChart;
pieChart.Title.Text = info.chartTitle;
pieChart.Series.Add(ExcelCellBase.GetAddress(row + 1, startCol, row + 1, endCol - 1),
2023-10-24 01:43:07 +04:00
ExcelCellBase.GetAddress(row, startCol, row, endCol - 1));
2023-10-13 21:09:48 +04:00
pieChart.Legend.Position = (eLegendPosition)(int)info.legendPosition;
pieChart.DataLabel.ShowPercent = true;
pieChart.SetPosition(1, 0, 0, 0);
FileInfo fi = new(info.filePath);
excelPackage.SaveAs(fi);
}
}
}