diff --git a/FormsTesting/Form1.Designer.cs b/FormsTesting/Form1.Designer.cs index 1c2bbaf..80e09e8 100644 --- a/FormsTesting/Form1.Designer.cs +++ b/FormsTesting/Form1.Designer.cs @@ -42,6 +42,8 @@ DocumentContextComponent = new ShabComponentsLibrary.ShabDocumentContextComponent(components); ConfigurableTableComponent = new ShabComponentsLibrary.ShabConfigurableTableComponent(components); CreateConfigurableTable = new Button(); + CreateHistogram = new Button(); + DiagramComponent = new ShabComponentsLibrary.ShabDiagramComponent(components); groupBox1.SuspendLayout(); groupBox2.SuspendLayout(); groupBox3.SuspendLayout(); @@ -157,11 +159,22 @@ CreateConfigurableTable.UseVisualStyleBackColor = true; CreateConfigurableTable.Click += CreateConfigurableTable_Click; // + // CreateHistogram + // + CreateHistogram.Location = new Point(410, 592); + CreateHistogram.Name = "CreateHistogram"; + CreateHistogram.Size = new Size(172, 42); + CreateHistogram.TabIndex = 8; + CreateHistogram.Text = "Документ с гистограммой"; + CreateHistogram.UseVisualStyleBackColor = true; + CreateHistogram.Click += CreateHistogram_Click; + // // Form1 // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(637, 660); + Controls.Add(CreateHistogram); Controls.Add(CreateConfigurableTable); Controls.Add(CreateDocumentWithTables); Controls.Add(groupBox3); @@ -190,5 +203,7 @@ private ShabComponentsLibrary.ShabDocumentContextComponent DocumentContextComponent; private ShabComponentsLibrary.ShabConfigurableTableComponent ConfigurableTableComponent; private Button CreateConfigurableTable; + private Button CreateHistogram; + private ShabComponentsLibrary.ShabDiagramComponent DiagramComponent; } } diff --git a/FormsTesting/Form1.cs b/FormsTesting/Form1.cs index d890b0d..1512cee 100644 --- a/FormsTesting/Form1.cs +++ b/FormsTesting/Form1.cs @@ -140,5 +140,21 @@ namespace FormsTesting new List { "LastName", "Id", "FirstName", "Age" }, Persons); } + + private void CreateHistogram_Click(object sender, EventArgs e) + { + var Data = new Dictionary + { + { " 1", new[] { 10, 20, 30, 40 } }, + { " 2", new[] { 15, 25, 35, 45 } } + }; + DiagramComponent.CreateDocument( + @"C:\Comps\Doc3.pdf", + " ", + "", + ShabComponentsLibrary.Enums.LegendPosition.Bottom, + Data + ); + } } } diff --git a/FormsTesting/Form1.resx b/FormsTesting/Form1.resx index 25bd66e..fea90e5 100644 --- a/FormsTesting/Form1.resx +++ b/FormsTesting/Form1.resx @@ -123,4 +123,7 @@ 233, 17 + + 447, 17 + \ No newline at end of file diff --git a/ShabComponentsLibrary/Enums/LegendPosition.cs b/ShabComponentsLibrary/Enums/LegendPosition.cs new file mode 100644 index 0000000..aecb492 --- /dev/null +++ b/ShabComponentsLibrary/Enums/LegendPosition.cs @@ -0,0 +1,10 @@ +namespace ShabComponentsLibrary.Enums +{ + public enum LegendPosition + { + Top, + Bottom, + Left, + Right + } +} diff --git a/ShabComponentsLibrary/OfficePackage/HelperModels/PdfChartParameters.cs b/ShabComponentsLibrary/OfficePackage/HelperModels/PdfChartParameters.cs new file mode 100644 index 0000000..0480129 --- /dev/null +++ b/ShabComponentsLibrary/OfficePackage/HelperModels/PdfChartParameters.cs @@ -0,0 +1,13 @@ +using ShabComponentsLibrary.Enums; + +namespace ShabComponentsLibrary.OfficePackage.HelperModels +{ + internal class PdfChartParameters + { + public Dictionary Data = new(); + + public string Title = string.Empty; + + public LegendPosition LegendPosition = LegendPosition.Bottom; + } +} diff --git a/ShabComponentsLibrary/OfficePackage/SaveToPdf.cs b/ShabComponentsLibrary/OfficePackage/SaveToPdf.cs index 83dea25..6450016 100644 --- a/ShabComponentsLibrary/OfficePackage/SaveToPdf.cs +++ b/ShabComponentsLibrary/OfficePackage/SaveToPdf.cs @@ -1,6 +1,8 @@ using MigraDoc.DocumentObjectModel; +using MigraDoc.DocumentObjectModel.Shapes.Charts; using MigraDoc.DocumentObjectModel.Tables; using MigraDoc.Rendering; +using ShabComponentsLibrary.Enums; using ShabComponentsLibrary.OfficePackage.HelperEnums; using ShabComponentsLibrary.OfficePackage.HelperModels; @@ -104,11 +106,50 @@ namespace ShabComponentsLibrary.OfficePackage Row.Cells[i].Borders.Right.Width = borderWidth; Row.Cells[i].Borders.Top.Width = borderWidth; Row.Cells[i].Borders.Bottom.Width = borderWidth; - // Row.Cells[i].Format.Alignment = GetParagraphAlignment(RowParameters.ParagraphAlignment); Row.Cells[i].VerticalAlignment = VerticalAlignment.Center; } } + public void CreateChart(PdfChartParameters ChartParameters) + { + if (_section == null) + return; + + Chart chart = new Chart(ChartType.Bar2D); + chart.Width = "15cm"; + chart.Height = "8cm"; + + foreach (var dataSeries in ChartParameters.Data) + { + Series series = chart.SeriesCollection.AddSeries(); + series.Name = dataSeries.Key; + series.Add(dataSeries.Value.Select(x => (double)x).ToArray()); + } + + //var xseries = chart.XValues.AddXSeries(); + //xseries.Add(new string[] { "1", "2", "3" }); + + chart.TopArea.AddParagraph(ChartParameters.Title); + + chart.XAxis.MajorTickMark = TickMarkType.Outside; + chart.YAxis.MajorTickMark = TickMarkType.Outside; + chart.YAxis.HasMajorGridlines = true; + + _ = ChartParameters.LegendPosition switch + { + LegendPosition.Top => chart.TopArea.AddLegend(), + LegendPosition.Right => chart.RightArea.AddLegend(), + LegendPosition.Bottom => chart.BottomArea.AddLegend(), + LegendPosition.Left => chart.LeftArea.AddLegend(), + _ => chart.BottomArea.AddLegend(), + }; + + chart.PlotArea.LineFormat.Width = 1; + chart.PlotArea.LineFormat.Visible = true; + + _section.Add(chart); + } + public void SavePdf(string Filename) { var Renderer = new PdfDocumentRenderer(true) diff --git a/ShabComponentsLibrary/ShabDiagramComponent.Designer.cs b/ShabComponentsLibrary/ShabDiagramComponent.Designer.cs new file mode 100644 index 0000000..6b4a638 --- /dev/null +++ b/ShabComponentsLibrary/ShabDiagramComponent.Designer.cs @@ -0,0 +1,36 @@ +namespace ShabComponentsLibrary +{ + partial class ShabDiagramComponent + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Component Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + components = new System.ComponentModel.Container(); + } + + #endregion + } +} diff --git a/ShabComponentsLibrary/ShabDiagramComponent.cs b/ShabComponentsLibrary/ShabDiagramComponent.cs new file mode 100644 index 0000000..956e7f1 --- /dev/null +++ b/ShabComponentsLibrary/ShabDiagramComponent.cs @@ -0,0 +1,56 @@ +using ShabComponentsLibrary.OfficePackage.HelperEnums; +using ShabComponentsLibrary.OfficePackage.HelperModels; +using ShabComponentsLibrary.OfficePackage; +using System.ComponentModel; +using ShabComponentsLibrary.Enums; + +namespace ShabComponentsLibrary +{ + /// + /// Невизуальный компонент для создания документа с гистограммой + /// + public partial class ShabDiagramComponent : Component + { + public ShabDiagramComponent() + { + InitializeComponent(); + } + + public ShabDiagramComponent(IContainer Container) + { + Container.Add(this); + InitializeComponent(); + } + + public void CreateDocument(string Filename, string Title, string ChartTitle, LegendPosition LegendPosition, + Dictionary Data) + { + if (string.IsNullOrEmpty(Filename)) + throw new ArgumentException("Filename cannot be empty"); + if (string.IsNullOrEmpty(Title)) + throw new ArgumentException("Title cannot be empty"); + if (string.IsNullOrEmpty(ChartTitle)) + throw new ArgumentException("Chart title cannot be empty"); + if (Data.Count == 0) + throw new ArgumentException("Data cannot be empty"); + + SaveToPdf Pdf = new SaveToPdf(); + Pdf.CreatePdf(); + Pdf.CreateParagraph(new PdfParagraph + { + Text = Title, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + + Pdf.CreateChart(new PdfChartParameters + { + Data = Data, + Title = ChartTitle, + LegendPosition = LegendPosition, + }); + + Pdf.SavePdf(Filename); + } + } +} diff --git a/ShabComponentsLibrary/ShabDocumentContextComponent.cs b/ShabComponentsLibrary/ShabDocumentContextComponent.cs index eae8b6c..4a5ec78 100644 --- a/ShabComponentsLibrary/ShabDocumentContextComponent.cs +++ b/ShabComponentsLibrary/ShabDocumentContextComponent.cs @@ -23,6 +23,13 @@ namespace ShabComponentsLibrary public void CreateDocument(string Filename, string Title, List Tables) { + if (string.IsNullOrEmpty(Filename)) + throw new ArgumentException("Filename cannot be empty"); + if (string.IsNullOrEmpty(Title)) + throw new ArgumentException("Title cannot be empty"); + if (Tables.Count == 0) + throw new ArgumentException("Data cannot be empty"); + SaveToPdf Pdf = new SaveToPdf(); Pdf.CreatePdf(); Pdf.CreateParagraph(new PdfParagraph