From 2d50eff66009070bce92863e79daf0758b59fdd2 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Mon, 28 Oct 2024 20:24:52 +0400 Subject: [PATCH] =?UTF-8?q?3=20=D0=BA=D0=BE=D0=BC=D0=BF=D0=BE=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/CustomPdfHistogram.Designer.cs | 36 ++++++++++ .../Components/CustomPdfHistogram.cs | 68 +++++++++++++++++++ .../Models/PdfHistigramInfo.cs | 35 ++++++++++ .../Models/PdfHistogramLineInfo.cs | 15 ++++ .../Enums/PdfLegendPosition.cs | 8 +-- .../Models/PieChartModel/PdfHistogramData.cs | 42 ++++++++++++ .../Models/PieChartModel/PdfHistogramModel.cs | 13 ++++ .../Models/PieChartModel/PdfPieChartModel.cs | 5 -- .../PIHelperSh.PdfCreater/PdfCreator.cs | 41 +++++++++++ .../Forms/Form2.Designer.cs | 15 ++++ .../TestCustomComponents/Forms/Form2.cs | 24 ++++++- .../TestCustomComponents/Forms/Form2.resx | 3 + 12 files changed, 292 insertions(+), 13 deletions(-) create mode 100644 Cop.Borovkov.Var3/Cop.Borovkov.Var3/Components/CustomPdfHistogram.Designer.cs create mode 100644 Cop.Borovkov.Var3/Cop.Borovkov.Var3/Components/CustomPdfHistogram.cs create mode 100644 Cop.Borovkov.Var3/Cop.Borovkov.Var3/Models/PdfHistigramInfo.cs create mode 100644 Cop.Borovkov.Var3/Cop.Borovkov.Var3/Models/PdfHistogramLineInfo.cs create mode 100644 Cop.Borovkov.Var3/PIHelperSh.PdfCreater/Models/PieChartModel/PdfHistogramData.cs create mode 100644 Cop.Borovkov.Var3/PIHelperSh.PdfCreater/Models/PieChartModel/PdfHistogramModel.cs diff --git a/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Components/CustomPdfHistogram.Designer.cs b/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Components/CustomPdfHistogram.Designer.cs new file mode 100644 index 0000000..eaed0e9 --- /dev/null +++ b/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Components/CustomPdfHistogram.Designer.cs @@ -0,0 +1,36 @@ +namespace Cop.Borovkov.Var3.Components +{ + partial class CustomPdfHistogram + { + /// + /// Обязательная переменная конструктора. + /// + 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/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Components/CustomPdfHistogram.cs b/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Components/CustomPdfHistogram.cs new file mode 100644 index 0000000..84bf12c --- /dev/null +++ b/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Components/CustomPdfHistogram.cs @@ -0,0 +1,68 @@ +using Cop.Borovkov.Var3.Models; +using PIHelperSh.PdfCreator; +using PIHelperSh.PdfCreator.Enums; +using PIHelperSh.PdfCreator.Models.PieChartModel; +using System.ComponentModel; +using static System.Runtime.InteropServices.Marshalling.IIUnknownCacheStrategy; + +namespace Cop.Borovkov.Var3.Components +{ + public partial class CustomPdfHistogram : Component + { + public CustomPdfHistogram() + { + InitializeComponent(); + } + + public CustomPdfHistogram(IContainer container) + { + container.Add(this); + + InitializeComponent(); + } + + /// + /// Сохранить гистограмму в пдф + /// + /// + public void SaveToPdf(PdfHistigramInfo histogramInfo) + { + if (string.IsNullOrEmpty(histogramInfo.FilePath)) + throw new ArgumentException("Путь к файлу не должен быть пустым."); + if (string.IsNullOrEmpty(histogramInfo.DocumentTitle)) + throw new ArgumentException("Название документа не должно быть пустым."); + if (string.IsNullOrEmpty(histogramInfo.HistogramTitle)) + throw new ArgumentException("Заголовок диаграммы не должен быть пустым."); + if (histogramInfo.Values == null || histogramInfo.Values.Count() == 0) + throw new ArgumentException("Набор данных не должен быть пустым."); + + foreach (var data in histogramInfo.Values) + { + if (string.IsNullOrEmpty(data.Name) || data.Values == null || data.Values.Count() == 0) + throw new ArgumentException($"Набор данных для серии '{data.Name}' некорректен."); + } + + PdfCreator creator = new PdfCreator(); + + creator.AddParagraph(new() + { + Style = PdfStyleType.Title, + Text = histogramInfo.DocumentTitle, + MarginAfter = PdfMargin.Smal, + }); + + creator.AddHistogram(new() + { + ChartName = histogramInfo.HistogramTitle, + LegendPosition = histogramInfo.LegendPosition, + DataSet = histogramInfo.Values.Select(l => new PdfHistogramData() + { + DisplayName = l.Name, + Value = l.Values + }).ToList() + }); + + creator.SavePdf(histogramInfo.FilePath); + } + } +} diff --git a/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Models/PdfHistigramInfo.cs b/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Models/PdfHistigramInfo.cs new file mode 100644 index 0000000..5b5f839 --- /dev/null +++ b/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Models/PdfHistigramInfo.cs @@ -0,0 +1,35 @@ +using PIHelperSh.PdfCreator.Enums; + +namespace Cop.Borovkov.Var3.Models +{ + /// + /// Параметры для создания линейной диограммы + /// + public record PdfHistigramInfo + { + /// + /// Имя файла (включая путь до файла) + /// + public string FilePath { get; init; } = @"C:\pdfTable.pdf"; + + /// + /// Заголовок документа + /// + public string DocumentTitle { get; init; } = "Гистограмма"; + + /// + /// Заголовок диограммы + /// + public string HistogramTitle { get; init; } = "Гистограмма"; + + /// + /// Расположение легенды + /// + public PdfLegendPosition LegendPosition { get; init; } = PdfLegendPosition.Bottom; + + /// + /// Значения + /// + public required IEnumerable Values { get; init; } +} +} diff --git a/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Models/PdfHistogramLineInfo.cs b/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Models/PdfHistogramLineInfo.cs new file mode 100644 index 0000000..bb04330 --- /dev/null +++ b/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Models/PdfHistogramLineInfo.cs @@ -0,0 +1,15 @@ +namespace Cop.Borovkov.Var3.Models +{ + public record PdfHistogramLineInfo + { + /// + /// Название графика + /// + public required string Name { get; init; } + + /// + /// Значения + /// + public required IEnumerable Values { get; init; } + } +} diff --git a/Cop.Borovkov.Var3/PIHelperSh.PdfCreater/Enums/PdfLegendPosition.cs b/Cop.Borovkov.Var3/PIHelperSh.PdfCreater/Enums/PdfLegendPosition.cs index 1923ccb..2e0c297 100644 --- a/Cop.Borovkov.Var3/PIHelperSh.PdfCreater/Enums/PdfLegendPosition.cs +++ b/Cop.Borovkov.Var3/PIHelperSh.PdfCreater/Enums/PdfLegendPosition.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace PIHelperSh.PdfCreator.Enums +namespace PIHelperSh.PdfCreator.Enums { /// /// Варианты расположения легенды diff --git a/Cop.Borovkov.Var3/PIHelperSh.PdfCreater/Models/PieChartModel/PdfHistogramData.cs b/Cop.Borovkov.Var3/PIHelperSh.PdfCreater/Models/PieChartModel/PdfHistogramData.cs new file mode 100644 index 0000000..faac94a --- /dev/null +++ b/Cop.Borovkov.Var3/PIHelperSh.PdfCreater/Models/PieChartModel/PdfHistogramData.cs @@ -0,0 +1,42 @@ +using System.Drawing; + +namespace PIHelperSh.PdfCreator.Models.PieChartModel +{ + /// + /// Элемент данных. Одно значение для диаграммы. + /// + public class PdfHistogramData + { + /// + /// Название варианта + /// + public string DisplayName { get; set; } = null!; + + /// + /// Значение варианта(по ним строят диаграмму) + /// + public IEnumerable Value { get; set; } + + /// + /// Цвет области на диаграме. При null будет использоватсся выдача цветов по умолчанию) + /// + public Color? Color { get; set; } = null; + + /// + /// + public PdfHistogramData() + { + Value = []; + } + + /// + /// + /// + /// + public PdfHistogramData(string displayName, IEnumerable value) + { + DisplayName = displayName; + Value = value; + } + } +} diff --git a/Cop.Borovkov.Var3/PIHelperSh.PdfCreater/Models/PieChartModel/PdfHistogramModel.cs b/Cop.Borovkov.Var3/PIHelperSh.PdfCreater/Models/PieChartModel/PdfHistogramModel.cs new file mode 100644 index 0000000..b6c7bb2 --- /dev/null +++ b/Cop.Borovkov.Var3/PIHelperSh.PdfCreater/Models/PieChartModel/PdfHistogramModel.cs @@ -0,0 +1,13 @@ +namespace PIHelperSh.PdfCreator.Models.PieChartModel +{ + /// + /// Модель линейной диограммы + /// + public class PdfHistogramModel : PdfPieChartModel + { + /// + /// Набор данных для создания диаграммы + /// + public new List DataSet { get; set; } = []; + } +} diff --git a/Cop.Borovkov.Var3/PIHelperSh.PdfCreater/Models/PieChartModel/PdfPieChartModel.cs b/Cop.Borovkov.Var3/PIHelperSh.PdfCreater/Models/PieChartModel/PdfPieChartModel.cs index 21968a1..93fd212 100644 --- a/Cop.Borovkov.Var3/PIHelperSh.PdfCreater/Models/PieChartModel/PdfPieChartModel.cs +++ b/Cop.Borovkov.Var3/PIHelperSh.PdfCreater/Models/PieChartModel/PdfPieChartModel.cs @@ -1,9 +1,4 @@ using PIHelperSh.PdfCreator.Enums; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace PIHelperSh.PdfCreator.Models.PieChartModel { diff --git a/Cop.Borovkov.Var3/PIHelperSh.PdfCreater/PdfCreator.cs b/Cop.Borovkov.Var3/PIHelperSh.PdfCreater/PdfCreator.cs index ea5110e..1457499 100644 --- a/Cop.Borovkov.Var3/PIHelperSh.PdfCreater/PdfCreator.cs +++ b/Cop.Borovkov.Var3/PIHelperSh.PdfCreater/PdfCreator.cs @@ -505,6 +505,47 @@ namespace PIHelperSh.PdfCreator _section?.Add(chart); } + /// + /// Создаёт линейную диаграмму. + /// + /// Модель для круговой диаграммы + public void AddHistogram(PdfHistogramModel pieHistogram) + { + if (_document == null) + { + return; + } + + Chart chart = new Chart(ChartType.Line); + ConfiguratePieChart(chart, pieHistogram); + + XSeries xseries = chart.XValues.AddXSeries(); + foreach (var item in pieHistogram.DataSet) + { + Series series = chart.SeriesCollection.AddSeries(); + series.Name = item.DisplayName; + + series.Add(item.Value.ToArray()); + xseries.Add(item.DisplayName); + + if (item.Color.HasValue) + { + series.FillFormat.Color = new Color((uint)item.Color.Value.ToArgb()); + } + } + + chart.XAxis.MajorTickMark = TickMarkType.Outside; + chart.XAxis.Title.Caption = "X"; + + chart.XAxis.Title.Alignment = HorizontalAlignment.Right; + + chart.YAxis.MajorTickMark = TickMarkType.Outside; + chart.YAxis.Title.Caption = "Y"; + chart.YAxis.HasMajorGridlines = true; + + _section?.Add(chart); + } + /// /// Добавляем на лист изображение. Можно по пути, можно по потоку, можно по Base64 строке /// diff --git a/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form2.Designer.cs b/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form2.Designer.cs index c080162..664dc65 100644 --- a/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form2.Designer.cs +++ b/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form2.Designer.cs @@ -33,6 +33,8 @@ button1 = new Button(); button2 = new Button(); customPdfTableWithGrouping1 = new Cop.Borovkov.Var3.Components.CustomPdfTableWithGrouping(components); + button3 = new Button(); + customPdfHistogram1 = new Cop.Borovkov.Var3.Components.CustomPdfHistogram(components); SuspendLayout(); // // button1 @@ -56,11 +58,22 @@ button2.UseVisualStyleBackColor = true; button2.Click += button2_Click; // + // button3 + // + button3.Location = new Point(206, 18); + button3.Name = "button3"; + button3.Size = new Size(94, 29); + button3.TabIndex = 2; + button3.Text = "button3"; + button3.UseVisualStyleBackColor = true; + button3.Click += button3_Click; + // // Form2 // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(914, 600); + Controls.Add(button3); Controls.Add(button2); Controls.Add(button1); Margin = new Padding(3, 4, 3, 4); @@ -75,5 +88,7 @@ private Button button1; private Button button2; private Cop.Borovkov.Var3.Components.CustomPdfTableWithGrouping customPdfTableWithGrouping1; + private Button button3; + private Cop.Borovkov.Var3.Components.CustomPdfHistogram customPdfHistogram1; } } \ No newline at end of file diff --git a/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form2.cs b/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form2.cs index 0b6b9a4..f4d1e3e 100644 --- a/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form2.cs +++ b/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form2.cs @@ -1,4 +1,6 @@ -using Newtonsoft.Json.Serialization; +using Cop.Borovkov.Var3.Components; +using Newtonsoft.Json.Serialization; +using PIHelperSh.PdfCreator.Enums; using TestCustomComponents.Models; namespace TestCustomComponents.Forms @@ -107,5 +109,25 @@ namespace TestCustomComponents.Forms HeaderHeight = 100f, }); } + + private void button3_Click(object sender, EventArgs e) + { + customPdfHistogram1.SaveToPdf(new() + { + FilePath = @"E:\test\test.pdf", + DocumentTitle = "Текст заголовка", + HistogramTitle = "Заголовок диограммы", + Values = [ + new(){ + Name = "Линия1", + Values = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89], + }, + new(){ + Name = "Линия1", + Values = [1, 100, 10, 80, 30, 60, 50], + } + ], + }); + } } } diff --git a/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form2.resx b/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form2.resx index f501082..805f130 100644 --- a/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form2.resx +++ b/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form2.resx @@ -123,4 +123,7 @@ 185, 17 + + 446, 17 + \ No newline at end of file