diff --git a/ExexForm2/Form1.Designer.cs b/ExexForm2/Form1.Designer.cs index 89cd569..6947beb 100644 --- a/ExexForm2/Form1.Designer.cs +++ b/ExexForm2/Form1.Designer.cs @@ -38,6 +38,8 @@ lblDirectory = new Label(); btnCrTb = new Button(); pdfColGroupTable1 = new Library14Petrushin.PdfColGroupTable(); + pdfCirclDiagr1 = new Library14Petrushin.PdfCirclDiagr(); + btnCircl = new Button(); SuspendLayout(); // // btnSelectImages @@ -104,11 +106,22 @@ btnCrTb.UseVisualStyleBackColor = true; btnCrTb.Click += btnCreatePdfTable_Click; // + // btnCircl + // + btnCircl.Location = new Point(606, 28); + btnCircl.Name = "btnCircl"; + btnCircl.Size = new Size(161, 29); + btnCircl.TabIndex = 7; + btnCircl.Text = "Создать диаграмму"; + btnCircl.UseVisualStyleBackColor = true; + btnCircl.Click += btnCreatePdfCircl_Click; + // // Form1 // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(btnCircl); Controls.Add(btnCrTb); Controls.Add(lblDirectory); Controls.Add(lstImages); @@ -133,5 +146,7 @@ private Label lblDirectory; private Button btnCrTb; private Library14Petrushin.PdfColGroupTable pdfColGroupTable1; + private Library14Petrushin.PdfCirclDiagr pdfCirclDiagr1; + private Button btnCircl; } } diff --git a/ExexForm2/Form1.cs b/ExexForm2/Form1.cs index 2c385c7..dd45fa6 100644 --- a/ExexForm2/Form1.cs +++ b/ExexForm2/Form1.cs @@ -1,4 +1,5 @@ using Library14Petrushin; +using Library14Petrushin.Classes; namespace ExexForm2 { @@ -8,6 +9,7 @@ namespace ExexForm2 private List selectedImages; private string selectedDirectory; private PdfColGroupTable pdfTable; + private PdfCirclDiagr pdfCirclDiagr; public Form1() { @@ -15,6 +17,7 @@ namespace ExexForm2 pdfImg = new PdfImg(); selectedImages = new List(); pdfTable = new PdfColGroupTable(); + pdfCirclDiagr = new PdfCirclDiagr(); } private void btnSelectImages_Click(object sender, EventArgs e) @@ -55,13 +58,13 @@ namespace ExexForm2 return; } - string fileName = Path.Combine(selectedDirectory, "output.pdf"); + string fileName = Path.Combine(selectedDirectory, "output1.pdf"); pdfImg.CreatePdfDocument(fileName, txtDocumentTitle.Text, selectedImages); MessageBox.Show("PDF document created successfully!"); } - // ------------------------------------------- + // 2 ------------------------------------------- private void btnCreatePdfTable_Click(object sender, EventArgs e) @@ -76,7 +79,7 @@ namespace ExexForm2 } } - string fileName = Path.Combine(selectDirTable, "output.pdf"); + string fileName = Path.Combine(selectDirTable, "output2.pdf"); var generator = new PdfColGroupTable(); @@ -98,5 +101,36 @@ namespace ExexForm2 generator.GeneratePdf(fileName, "Example Document", mergeCells, rowHeights, headers, commonHeaders, propertyNames, data1); } + + + // 3 ------------------------------------------- + + + private void btnCreatePdfCircl_Click(object sender, EventArgs e) + { + string selectDir = ""; + + using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog()) + { + if (folderBrowserDialog.ShowDialog() == DialogResult.OK) + { + selectDir = folderBrowserDialog.SelectedPath; + } + } + + string fileName = Path.Combine(selectDir, "output3.pdf"); + + var pdfCirclDiagr = new PdfCirclDiagr(); + + List chartData = new List + { + new ChartData { SeriesName = "Series 1", Value = 10 }, + new ChartData { SeriesName = "Series 2", Value = 20 }, + new ChartData { SeriesName = "Series 3", Value = 30 }, + new ChartData { SeriesName = "Series 4", Value = 40 } + }; + + pdfCirclDiagr.GeneratePdf(fileName, "Document Title", "Chart Title", LegendPosition.Right, chartData); + } } } \ No newline at end of file diff --git a/ExexForm2/Form1.resx b/ExexForm2/Form1.resx index 2cfbb8a..4e594b6 100644 --- a/ExexForm2/Form1.resx +++ b/ExexForm2/Form1.resx @@ -123,4 +123,7 @@ 130, 17 + + 315, 17 + \ No newline at end of file diff --git a/Library14Petrushin/Classes/ChartData.cs b/Library14Petrushin/Classes/ChartData.cs new file mode 100644 index 0000000..80b86f2 --- /dev/null +++ b/Library14Petrushin/Classes/ChartData.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Library14Petrushin.Classes +{ + public class ChartData + { + public string? SeriesName { get; set; } + public double Value { get; set; } + } +} diff --git a/Library14Petrushin/Classes/LegendPosition.cs b/Library14Petrushin/Classes/LegendPosition.cs new file mode 100644 index 0000000..475844b --- /dev/null +++ b/Library14Petrushin/Classes/LegendPosition.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Library14Petrushin.Classes +{ + public enum LegendPosition + { + Top, + Bottom, + Left, + Right + } +} diff --git a/Library14Petrushin/PdfCirclDiagr.Designer.cs b/Library14Petrushin/PdfCirclDiagr.Designer.cs new file mode 100644 index 0000000..26aa196 --- /dev/null +++ b/Library14Petrushin/PdfCirclDiagr.Designer.cs @@ -0,0 +1,36 @@ +namespace Library14Petrushin +{ + partial class PdfCirclDiagr + { + /// + /// Обязательная переменная конструктора. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Освободить все используемые ресурсы. + /// + /// истинно, если управляемый ресурс должен быть удален; иначе ложно. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Код, автоматически созданный конструктором компонентов + + /// + /// Требуемый метод для поддержки конструктора — не изменяйте + /// содержимое этого метода с помощью редактора кода. + /// + private void InitializeComponent() + { + components = new System.ComponentModel.Container(); + } + + #endregion + } +} diff --git a/Library14Petrushin/PdfCirclDiagr.cs b/Library14Petrushin/PdfCirclDiagr.cs new file mode 100644 index 0000000..1ee09f3 --- /dev/null +++ b/Library14Petrushin/PdfCirclDiagr.cs @@ -0,0 +1,96 @@ +using Library14Petrushin.Classes; +using PdfSharp.Drawing; +using PdfSharp.Pdf; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Library14Petrushin +{ + public partial class PdfCirclDiagr : Component + { + public void GeneratePdf( + string fileName, + string documentTitle, + string chartTitle, + LegendPosition legendPosition, + List chartData) + { + // Проверка на заполненность входных данных + if (string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(documentTitle) || + string.IsNullOrEmpty(chartTitle) || chartData == null || !chartData.Any()) + { + throw new ArgumentException("Все входные данные должны быть заполнены."); + } + + // Создание документа + PdfDocument document = new PdfDocument(); + PdfPage page = document.AddPage(); + XGraphics gfx = XGraphics.FromPdfPage(page); + XFont font = new XFont("Verdana", 10); + + // Отрисовка заголовка документа + gfx.DrawString(documentTitle, new XFont("Verdana", 16, XFontStyleEx.Bold), XBrushes.Black, new XRect(0, 0, page.Width, 50), XStringFormats.Center); + + // Отрисовка заголовка диаграммы + gfx.DrawString(chartTitle, font, XBrushes.Black, new XRect(50, 50, page.Width - 100, 30), XStringFormats.Center); + + // Отрисовка круговой диаграммы + DrawPieChart(gfx, new XRect(50, 100, 300, 300), chartData, legendPosition); + + // Сохранение документа + document.Save(fileName); + } + + private void DrawPieChart(XGraphics gfx, XRect rect, List chartData, LegendPosition legendPosition) + { + double total = chartData.Sum(d => d.Value); + double startAngle = 0; + int legendX = 0; + int legendY = 0; + + // Определение расположения легенды + switch (legendPosition) + { + case LegendPosition.Top: + legendX = (int)rect.X + (int)rect.Width / 2; + legendY = (int)rect.Y - 50; + break; + case LegendPosition.Bottom: + legendX = (int)rect.X + (int)rect.Width / 2; + legendY = (int)rect.Y + (int)rect.Height + 20; + break; + case LegendPosition.Left: + legendX = (int)rect.X - 100; + legendY = (int)rect.Y + (int)rect.Height / 2; + break; + case LegendPosition.Right: + legendX = (int)rect.X + (int)rect.Width + 20; + legendY = (int)rect.Y + (int)rect.Height / 2; + break; + } + + // Отрисовка секторов диаграммы + foreach (var data in chartData) + { + double sweepAngle = 360 * (data.Value / total); + gfx.DrawPie(GetRandomBrush(), rect, startAngle, sweepAngle); + startAngle += sweepAngle; + + // Отрисовка легенды + gfx.DrawString(data.SeriesName, new XFont("Verdana", 8), XBrushes.Black, new XPoint(legendX, legendY)); + legendY += 20; + } + } + + private XBrush GetRandomBrush() + { + Random random = new Random(); + return new XSolidBrush(XColor.FromArgb(random.Next(256), random.Next(256), random.Next(256))); + } + } +}