From d0c502a6198ddf8cf4463ff40f12418d928f1c99 Mon Sep 17 00:00:00 2001 From: revengel66 Date: Mon, 28 Oct 2024 21:49:23 +0400 Subject: [PATCH] lab2 ready --- .../ComponentDiagram.Designer.cs | 36 +++++++ ComponentsLibrary/ComponentDiagram.cs | 100 ++++++++++++++++++ ComponentsLibrary/entities/DataLine.cs | 15 +++ ComponentsLibrary/entities/DocumentDiagram.cs | 32 ++++++ .../entities/enums/EnumAreaLegend.cs | 23 ++++ ComponentsView/FormComponents.Designer.cs | 17 ++- ComponentsView/FormComponents.cs | 47 +++++++- ComponentsView/FormComponents.resx | 2 +- 8 files changed, 267 insertions(+), 5 deletions(-) create mode 100644 ComponentsLibrary/ComponentDiagram.Designer.cs create mode 100644 ComponentsLibrary/ComponentDiagram.cs create mode 100644 ComponentsLibrary/entities/DataLine.cs create mode 100644 ComponentsLibrary/entities/DocumentDiagram.cs create mode 100644 ComponentsLibrary/entities/enums/EnumAreaLegend.cs diff --git a/ComponentsLibrary/ComponentDiagram.Designer.cs b/ComponentsLibrary/ComponentDiagram.Designer.cs new file mode 100644 index 0000000..6472d43 --- /dev/null +++ b/ComponentsLibrary/ComponentDiagram.Designer.cs @@ -0,0 +1,36 @@ +namespace ComponentsLibrary +{ + partial class ComponentDiagram + { + /// + /// Обязательная переменная конструктора. + /// + 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/ComponentsLibrary/ComponentDiagram.cs b/ComponentsLibrary/ComponentDiagram.cs new file mode 100644 index 0000000..d88fd89 --- /dev/null +++ b/ComponentsLibrary/ComponentDiagram.cs @@ -0,0 +1,100 @@ +using Aspose.Words; +using Aspose.Words.Drawing; +using Aspose.Words.Drawing.Charts; +using ComponentsLibrary.entities; +using System.ComponentModel; + +namespace ComponentsLibrary +{ + public partial class ComponentDiagram : Component + { + public ComponentDiagram() + { + InitializeComponent(); + } + + public ComponentDiagram(IContainer container) + { + container.Add(this); + + InitializeComponent(); + } + public void AddDiagram(DocumentDiagram diagramInfo) + { + if (!CheckData(diagramInfo.DataList)) + { + throw new Exception("Не заполнены данные"); + } + + //Создание документа и настройка шрифта + Document doc = new Document(); + DocumentBuilder builder = new DocumentBuilder(doc); + Aspose.Words.Font font = builder.Font; + font.Size = 24; + font.Bold = true; + font.Color = Color.Black; + font.Name = "Calibri"; + + //Настройка форматирования параграфа + ParagraphFormat paragraphFormat = builder.ParagraphFormat; + paragraphFormat.FirstLineIndent = 8; + paragraphFormat.SpaceAfter = 24; + paragraphFormat.Alignment = ParagraphAlignment.Center; + paragraphFormat.KeepTogether = true; + + //Добавление заголовка + builder.Writeln(diagramInfo.FileHeader); + + //Создание диаграммы + Shape shape = builder.InsertChart(ChartType.Line, 500, 270); + Chart chart = shape.Chart; + chart.Title.Text = diagramInfo.DiagramName; //Настройка заголовка диаграммы + ChartSeriesCollection seriesColl = chart.Series; + seriesColl.Clear(); + + string[] cats; //x - категории + double[] doubs; //y - данные + + //Добавление серий данных + foreach (var data in diagramInfo.DataList) + { + cats = new string[diagramInfo.DataList.Count]; + doubs = new double[diagramInfo.DataList.Count]; + int i = 0; + foreach (var (name, value) in data.LineData) + { + cats[i] = name; + doubs[i] = value; + i++; + } + seriesColl.Add(data.LineName, cats, doubs); + } + //Настройка легенды + ChartLegend legend = chart.Legend; + legend.Position = (LegendPosition)diagramInfo.AreaLegend; + legend.Overlay = true; + + doc.Save(diagramInfo.FileUrl); + } + + static bool CheckData(List data) + { + string[] cats = new string[data.Count]; + double[] doubs = new double[data.Count]; + + foreach (var _data in data) + { + int i = 0; + foreach (var (name, value) in _data.LineData) + { + cats[i] = name; + doubs[i] = value; + i++; + } + if (string.IsNullOrEmpty(_data.LineName) || cats.Length == 0 || doubs.Length == 0) + return false; + } + return true; + } + } +} diff --git a/ComponentsLibrary/entities/DataLine.cs b/ComponentsLibrary/entities/DataLine.cs new file mode 100644 index 0000000..e469732 --- /dev/null +++ b/ComponentsLibrary/entities/DataLine.cs @@ -0,0 +1,15 @@ +namespace ComponentsLibrary.entities +{ + public class DataLine + { + public string LineName { get; set; } = string.Empty; + + public (string, double)[] LineData { get; set; } + + public DataLine(string nameSeries, (string, double)[] lineData) + { + LineName = nameSeries; + LineData = lineData; + } + } +} diff --git a/ComponentsLibrary/entities/DocumentDiagram.cs b/ComponentsLibrary/entities/DocumentDiagram.cs new file mode 100644 index 0000000..3a97521 --- /dev/null +++ b/ComponentsLibrary/entities/DocumentDiagram.cs @@ -0,0 +1,32 @@ +using ComponentsLibrary.entities.enums; +using DocumentFormat.OpenXml.Drawing; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComponentsLibrary.entities +{ + public class DocumentDiagram + { + public string FileUrl = string.Empty; + + public string FileHeader = string.Empty; + + public string DiagramName = string.Empty; + + public EnumAreaLegend AreaLegend; + + public List DataList = new(); + + public DocumentDiagram(string fileUrl, string fileHeader, string diagramName, EnumAreaLegend areaLegend, List dataList) + { + FileUrl = fileUrl; + FileHeader = fileHeader; + DiagramName = diagramName; + AreaLegend = areaLegend; + DataList = dataList; + } + } +} diff --git a/ComponentsLibrary/entities/enums/EnumAreaLegend.cs b/ComponentsLibrary/entities/enums/EnumAreaLegend.cs new file mode 100644 index 0000000..b0bbe09 --- /dev/null +++ b/ComponentsLibrary/entities/enums/EnumAreaLegend.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComponentsLibrary.entities.enums +{ + public enum EnumAreaLegend + { + None, + + Left, + + Top, + + Right, + + Bottom, + + TopRight + } +} diff --git a/ComponentsView/FormComponents.Designer.cs b/ComponentsView/FormComponents.Designer.cs index 1e1e18c..f9cc219 100644 --- a/ComponentsView/FormComponents.Designer.cs +++ b/ComponentsView/FormComponents.Designer.cs @@ -37,6 +37,8 @@ buttonSaveTextWord = new Button(); componentTable = new ComponentsLibrary.ComponentTable(components); buttonSaveTable = new Button(); + buttonSaveDiagram = new Button(); + componentDiagram = new ComponentsLibrary.ComponentDiagram(components); SuspendLayout(); // // richTextBoxTest @@ -85,11 +87,22 @@ buttonSaveTable.UseVisualStyleBackColor = true; buttonSaveTable.Click += buttonSaveTable_Click; // + // buttonSaveDiagram + // + buttonSaveDiagram.Location = new Point(12, 190); + buttonSaveDiagram.Name = "buttonSaveDiagram"; + buttonSaveDiagram.Size = new Size(350, 41); + buttonSaveDiagram.TabIndex = 6; + buttonSaveDiagram.Text = "Cоздать документ с диаграмой"; + buttonSaveDiagram.UseVisualStyleBackColor = true; + buttonSaveDiagram.Click += buttonSaveDiagram_Click; + // // FormComponents // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(373, 244); + Controls.Add(buttonSaveDiagram); Controls.Add(buttonSaveTable); Controls.Add(buttonSaveTextWord); Controls.Add(richTextBoxWord); @@ -110,5 +123,7 @@ private Button buttonSaveTextWord; private ComponentsLibrary.ComponentTable componentTable; private Button buttonSaveTable; + private Button buttonSaveDiagram; + private ComponentsLibrary.ComponentDiagram componentDiagram; } } diff --git a/ComponentsView/FormComponents.cs b/ComponentsView/FormComponents.cs index d26f717..fa3a1f0 100644 --- a/ComponentsView/FormComponents.cs +++ b/ComponentsView/FormComponents.cs @@ -1,5 +1,6 @@ -using System.Windows.Forms; using ComponentsLibrary.entities; +using ComponentsLibrary.entities.enums; +using DocumentFormat.OpenXml.Drawing; namespace ComponentsView { @@ -37,7 +38,7 @@ namespace ComponentsView { try { - DocumentSymple doc = new(dialog.FileName, "- ?", richTextBoxWord.Lines); + DocumentSymple doc = new(dialog.FileName, " ", richTextBoxWord.Lines); componentBigText.CreateWordText(doc); MessageBox.Show(" ", "", MessageBoxButtons.OK, MessageBoxIcon.Information); @@ -96,7 +97,7 @@ namespace ComponentsView { try { - DocumentTable info = new(dialog.FileName, " 2", columns, employees, mergedColumns); + DocumentTable info = new(dialog.FileName, " ", columns, employees, mergedColumns); componentTable.CreateTable(info); MessageBox.Show(" ", "", @@ -109,5 +110,45 @@ namespace ComponentsView } } } + + private void buttonSaveDiagram_Click(object sender, EventArgs e) + { + using var dialog = new SaveFileDialog + { + Filter = "docx|*.docx" + }; + + if (dialog.ShowDialog() == DialogResult.OK) + { + try + { + string[] cathegories = { "", "", "", "April", "May" }; + (string, double)[] series1 = { (" 1", 100), (" 2", 300), (" 3", 400) }; + (string, double)[] series2 = { (" 1", 400), (" 2", 300), (" 3", 100) }; + (string, double)[] series3 = { (" 1", 200), (" 2", 500), (" 3", 900) }; + + string fileUrl = dialog.FileName; + string docHeader = " "; + string diagramName = " "; + List dataList = new List { + new DataLine(" 1", series1), + new DataLine(" 2", series2), + new DataLine(" 3", series3), + }; + + DocumentDiagram diagram = new(fileUrl, docHeader, diagramName, EnumAreaLegend.Right, dataList); + + componentDiagram.AddDiagram(diagram); + + MessageBox.Show(" ", "", + MessageBoxButtons.OK, MessageBoxIcon.Information); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "!", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } diff --git a/ComponentsView/FormComponents.resx b/ComponentsView/FormComponents.resx index 416d8db..890ee48 100644 --- a/ComponentsView/FormComponents.resx +++ b/ComponentsView/FormComponents.resx @@ -126,7 +126,7 @@ 308, 17 - + 451, 17 \ No newline at end of file