From 989d867eda6736b93c2e4e1a820c7522251228e4 Mon Sep 17 00:00:00 2001 From: Yourdax Date: Thu, 3 Oct 2024 15:34:22 +0400 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B0=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormLibrary/ComponentHistogramToPdf.cs | 21 ++-------- .../HelperClasses/PDFTableSettings.cs | 4 +- KopLab1/FormLibrary/HelperClasses/Student.cs | 1 + KopLab1/FormLibrary/PDFTableCustom.cs | 37 ++++++++++-------- KopLab1/Forms/MainForm.Designer.cs | 12 +----- KopLab1/Forms/MainForm.cs | 39 +++++++++---------- KopLab1/Forms/MainForm.resx | 4 +- 7 files changed, 47 insertions(+), 71 deletions(-) diff --git a/KopLab1/FormLibrary/ComponentHistogramToPdf.cs b/KopLab1/FormLibrary/ComponentHistogramToPdf.cs index 0d50f1b..7a8bf8a 100644 --- a/KopLab1/FormLibrary/ComponentHistogramToPdf.cs +++ b/KopLab1/FormLibrary/ComponentHistogramToPdf.cs @@ -15,6 +15,7 @@ namespace FormLibrary public ComponentHistogramToPdf() { InitializeComponent(); + System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); } public ComponentHistogramToPdf(IContainer container) @@ -23,7 +24,7 @@ namespace FormLibrary InitializeComponent(); } - public void CreateHistogramPdf(string filePath, string documentTitle, string chartTitle, LegendPositions legendPosition, List chartData) + public void CreateHistogramPdf(string filePath, string documentTitle, string chartTitle, LegendPosition legendPosition, List chartData) { if (string.IsNullOrEmpty(filePath)) throw new ArgumentException("Путь к файлу не может быть пустым."); @@ -84,31 +85,17 @@ namespace FormLibrary } //добавление легенды - private void AddLegend(PlotModel plotModel, LegendPositions legendPosition) + private void AddLegend(PlotModel plotModel, LegendPosition legendPosition) { // Создание легенды var legend = new OxyPlot.Legends.Legend { LegendPlacement = LegendPlacement.Outside, - LegendPosition = ConvertLegendPosition(legendPosition), + LegendPosition = legendPosition, LegendOrientation = LegendOrientation.Vertical }; plotModel.Legends.Add(legend); } - - // конвертация позиции легенды - private OxyPlot.Legends.LegendPosition ConvertLegendPosition(LegendPositions legendPosition) - { - return legendPosition switch - { - LegendPositions.Top => OxyPlot.Legends.LegendPosition.TopCenter, - LegendPositions.Bottom => OxyPlot.Legends.LegendPosition.BottomCenter, - LegendPositions.Left => OxyPlot.Legends.LegendPosition.LeftTop, - LegendPositions.Right => OxyPlot.Legends.LegendPosition.RightTop, - _ => OxyPlot.Legends.LegendPosition.TopCenter, // Положение по умолчанию - }; - } - } } diff --git a/KopLab1/FormLibrary/HelperClasses/PDFTableSettings.cs b/KopLab1/FormLibrary/HelperClasses/PDFTableSettings.cs index 07ef99a..69188fa 100644 --- a/KopLab1/FormLibrary/HelperClasses/PDFTableSettings.cs +++ b/KopLab1/FormLibrary/HelperClasses/PDFTableSettings.cs @@ -10,12 +10,10 @@ namespace FormLibrary.HelperClasses { public string FilePath { get; set; } public string DocumentTitle { get; set; } - public List HeaderTitles { get; set; } - public List ColumnWidths { get; set; } + public List<(string HeaderTitle, float Width, string PropertyName, int ColumnIndex)> Columns { get; set; } public float HeaderRowHeight { get; set; } public float DataRowHeight { get; set; } public List DataList { get; set; } - public Dictionary ColumnPropertyMappings { get; set; } } } diff --git a/KopLab1/FormLibrary/HelperClasses/Student.cs b/KopLab1/FormLibrary/HelperClasses/Student.cs index b83d979..ef78de1 100644 --- a/KopLab1/FormLibrary/HelperClasses/Student.cs +++ b/KopLab1/FormLibrary/HelperClasses/Student.cs @@ -8,6 +8,7 @@ namespace FormLibrary.HelperClasses { public class Student { + public int Number { get; set; } public string Group { get; set; } public string FullName { get; set; } public int Course { get; set; } diff --git a/KopLab1/FormLibrary/PDFTableCustom.cs b/KopLab1/FormLibrary/PDFTableCustom.cs index 5786758..3a127ce 100644 --- a/KopLab1/FormLibrary/PDFTableCustom.cs +++ b/KopLab1/FormLibrary/PDFTableCustom.cs @@ -32,15 +32,10 @@ namespace FormLibrary if (settings == null || string.IsNullOrEmpty(settings.FilePath) || string.IsNullOrEmpty(settings.DocumentTitle) || - settings.HeaderTitles == null || settings.HeaderTitles.Count == 0 || - settings.ColumnWidths == null || settings.DataList == null || - settings.ColumnPropertyMappings == null) + settings.Columns == null || settings.Columns.Count == 0 || + settings.DataList == null) throw new ArgumentException("Заполнены не все необходимые данные для генерации документа."); - if (settings.HeaderTitles.Count != settings.ColumnWidths.Count) - throw new ArgumentException("Количество заголовков должно совпадать с количеством ширин столбцов."); - - Document document = new Document(); Section section = document.AddSection(); section.AddParagraph(settings.DocumentTitle, "Heading1"); @@ -49,20 +44,22 @@ namespace FormLibrary table.Borders.Width = 0.75; // столбцы - for (int i = 0; i < settings.ColumnWidths.Count; i++) + foreach (var (_, width, _, _) in settings.Columns) { - Column column = table.AddColumn(Unit.FromCentimeter(settings.ColumnWidths[i])); + Column column = table.AddColumn(Unit.FromCentimeter(width)); column.Format.Alignment = ParagraphAlignment.Center; } // заголовки Row headerRow = table.AddRow(); headerRow.Height = Unit.FromCentimeter(settings.HeaderRowHeight); - for (int i = 0; i < settings.HeaderTitles.Count; i++) + + for (int columnIndex = 0; columnIndex < settings.Columns.Count; columnIndex++) { - headerRow.Cells[i].AddParagraph(settings.HeaderTitles[i]); - headerRow.Cells[i].Format.Font.Bold = true; - headerRow.Cells[i].Format.Alignment = ParagraphAlignment.Center; + var (headerTitle, _, _, _) = settings.Columns[columnIndex]; + headerRow.Cells[columnIndex].AddParagraph(headerTitle); + headerRow.Cells[columnIndex].Format.Font.Bold = true; + headerRow.Cells[columnIndex].Format.Alignment = ParagraphAlignment.Center; } // данные @@ -71,14 +68,20 @@ namespace FormLibrary Row row = table.AddRow(); row.Height = Unit.FromCentimeter(settings.DataRowHeight); - foreach (var columnMapping in settings.ColumnPropertyMappings) + for (int columnIndex = 0; columnIndex < settings.Columns.Count; columnIndex++) { - PropertyInfo propertyInfo = typeof(T).GetProperty(columnMapping.Value); + var (_, _, propertyName, _) = settings.Columns[columnIndex]; + + PropertyInfo propertyInfo = typeof(T).GetProperty(propertyName); if (propertyInfo == null) - throw new ArgumentException($"Свойство {columnMapping.Value} не найдено в классе {typeof(T).Name}."); + throw new ArgumentException($"Свойство {propertyName} не найдено в классе {typeof(T).Name}."); object value = propertyInfo.GetValue(dataItem); - row.Cells[columnMapping.Key].AddParagraph(value != null ? value.ToString() : ""); + if (columnIndex == 0) + { + row.Cells[columnIndex].Format.Font.Bold = true; + } + row.Cells[columnIndex].AddParagraph(value != null ? value.ToString() : ""); } } diff --git a/KopLab1/Forms/MainForm.Designer.cs b/KopLab1/Forms/MainForm.Designer.cs index 0b792f3..50d9457 100644 --- a/KopLab1/Forms/MainForm.Designer.cs +++ b/KopLab1/Forms/MainForm.Designer.cs @@ -44,7 +44,6 @@ button8 = new Button(); pdfTableCustom1 = new FormLibrary.PDFTableCustom(components); button9 = new Button(); - button10 = new Button(); button11 = new Button(); componentHistogramToPdf1 = new FormLibrary.ComponentHistogramToPdf(components); SuspendLayout(); @@ -168,20 +167,13 @@ button9.UseVisualStyleBackColor = true; button9.Click += btnGeneratePDF_Click; // - // button10 - // - button10.Location = new Point(0, 0); - button10.Name = "button10"; - button10.Size = new Size(75, 23); - button10.TabIndex = 0; - // // button11 // button11.Location = new Point(347, 481); button11.Name = "button11"; button11.Size = new Size(139, 23); button11.TabIndex = 13; - button11.Text = "Create customPDF"; + button11.Text = "Create Histogram PDF"; button11.UseVisualStyleBackColor = true; button11.Click += btnGenerateHistogrammPdf_Click; // @@ -191,7 +183,6 @@ AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(944, 625); Controls.Add(button11); - Controls.Add(button10); Controls.Add(button9); Controls.Add(button8); Controls.Add(button7); @@ -228,7 +219,6 @@ private Button button8; private FormLibrary.PDFTableCustom pdfTableCustom1; private Button button9; - private Button button10; private Button button11; private FormLibrary.ComponentHistogramToPdf componentHistogramToPdf1; } diff --git a/KopLab1/Forms/MainForm.cs b/KopLab1/Forms/MainForm.cs index 433fd34..68be1b4 100644 --- a/KopLab1/Forms/MainForm.cs +++ b/KopLab1/Forms/MainForm.cs @@ -12,6 +12,7 @@ using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Forms.VisualStyles; using static System.Windows.Forms.VisualStyles.VisualStyleElement.Header; +using OxyPlot.Legends; namespace Forms { @@ -131,7 +132,7 @@ namespace Forms try { var pdfData = new PdfDocumentData( - "G:\\Отчет1.pdf", + "E:\\Отчет1.pdf", "Название документа", new List { @@ -159,27 +160,25 @@ namespace Forms } private void btnGeneratePDF_Click(object sender, EventArgs e) { - // Создание объекта настроек var settings = new PDFTableSettings { - FilePath = "G:\\Отчет2.pdf", + FilePath = "E:\\Отчет2.pdf", DocumentTitle = "Отчет по студентам", - HeaderTitles = new List { "Группа", "ФИО", "Курс" }, - ColumnWidths = new List { 4.0f, 6.0f, 2.0f }, HeaderRowHeight = 1.0f, DataRowHeight = 1.0f, DataList = new List - { - new Student { Group = "Пибд-33", FullName = "Иван Иванов", Course = 3 }, - new Student { Group = "Пибд-33", FullName = "Петр Петров", Course = 2 }, - new Student { Group = "Пибд-34", FullName = "Алексей Сидоров", Course = 4 } - }, - ColumnPropertyMappings = new Dictionary - { - { 0, nameof(Student.Group) }, - { 1, nameof(Student.FullName) }, - { 2, nameof(Student.Course) } - } + { + new Student { Number = 1, Group = "Пибд-33", FullName = "Иван Иванов", Course = 3 }, + new Student { Number = 2, Group = "Пибд-33", FullName = "Петр Петров", Course = 2 }, + new Student { Number = 3, Group = "Пибд-34", FullName = "Алексей Сидоров", Course = 4 } + }, + Columns = new List<(string, float, string, int)> + { + ("№", 1.0f, nameof(Student.Number), 0), + ("Группа", 4.0f, nameof(Student.Group), 1), + ("ФИО", 6.0f, nameof(Student.FullName), 2), + ("Курс", 2.0f, nameof(Student.Course), 3) + } }; try @@ -202,14 +201,12 @@ namespace Forms { new ChartData { SeriesName = "Серияd 1", Data = new Dictionary { { "Категорияz 1", 5 }, { "Категорияx 2", 10 } } }, new ChartData { SeriesName = "Серияs 2", Data = new Dictionary { { "Категорияa 1", 3 }, { "Категорияs 2", 8 } } }, - new ChartData { SeriesName = "Серияs 2", Data = new Dictionary { { "Категорияa 1", 3 }, { "Категорияs 2", 8 } } } + new ChartData { SeriesName = "Серияs 3", Data = new Dictionary { { "Категорияa 1", 3 }, { "Категорияs 2", 8 } } } }; - // Укажите путь, куда хотите сохранить PDF - string filePath = "G:\\Гистограмма.pdf"; + string filePath = "E:\\Гистограмма.pdf"; - // Генерация PDF - histogramGenerator.CreateHistogramPdf(filePath, "Название документа", "Заголовок диаграммы", LegendPositions.Bottom, chartData); + histogramGenerator.CreateHistogramPdf(filePath, "Название документа", "Заголовок гистограммы", LegendPosition.BottomCenter, chartData); MessageBox.Show("PDF успешно сгенерирован!", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information); } diff --git a/KopLab1/Forms/MainForm.resx b/KopLab1/Forms/MainForm.resx index 9c89390..3618ad6 100644 --- a/KopLab1/Forms/MainForm.resx +++ b/KopLab1/Forms/MainForm.resx @@ -1,7 +1,7 @@