diff --git a/COP/VisualCompLib/Components/SupportClasses/DataLineChart.cs b/COP/VisualCompLib/Components/SupportClasses/DataLineChart.cs new file mode 100644 index 0000000..b1570ad --- /dev/null +++ b/COP/VisualCompLib/Components/SupportClasses/DataLineChart.cs @@ -0,0 +1,18 @@ +namespace VisualCompLib.Components.SupportClasses +{ + public class DataLineChart + { + public string NameSeries { get; set; } = string.Empty; + + public string[] NameData { get; set; } + + public double[] Data { get; set; } + + public DataLineChart(string nameSeries, string[] nameData, double[] data) + { + NameSeries = nameSeries; + NameData = nameData; + Data = data; + } + } +} diff --git a/COP/VisualCompLib/Components/SupportClasses/SimpleLineChart.cs b/COP/VisualCompLib/Components/SupportClasses/SimpleLineChart.cs new file mode 100644 index 0000000..4650b54 --- /dev/null +++ b/COP/VisualCompLib/Components/SupportClasses/SimpleLineChart.cs @@ -0,0 +1,26 @@ +using VisualCompLib.Components.SupportClasses.Enums; + +namespace VisualCompLib.Components.SupportClasses +{ + public class SimpleLineChart + { + public string FilePath = string.Empty; + + public string FileHeader = string.Empty; + + public string LineChartName = string.Empty; + + public EnumAreaLegend AreaLegend; + + public List DataList = new(); + + public SimpleLineChart(string filePath, string fileHeader, string lineChartName, EnumAreaLegend areaLegend, List dataList) + { + FilePath = filePath; + FileHeader = fileHeader; + LineChartName = lineChartName; + AreaLegend = areaLegend; + DataList = dataList; + } + } +} diff --git a/COP/VisualCompLib/Components/WordLineChart.Designer.cs b/COP/VisualCompLib/Components/WordLineChart.Designer.cs new file mode 100644 index 0000000..151b374 --- /dev/null +++ b/COP/VisualCompLib/Components/WordLineChart.Designer.cs @@ -0,0 +1,36 @@ +namespace VisualCompLib.Components +{ + partial class WordLineChart + { + /// + /// Обязательная переменная конструктора. + /// + 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/VisualCompLib/Components/WordLineChart.cs b/COP/VisualCompLib/Components/WordLineChart.cs new file mode 100644 index 0000000..369c896 --- /dev/null +++ b/COP/VisualCompLib/Components/WordLineChart.cs @@ -0,0 +1,84 @@ +using Aspose.Words; +using Aspose.Words.Drawing; +using Aspose.Words.Drawing.Charts; +using System.ComponentModel; +using VisualCompLib.Components.SupportClasses; + +namespace VisualCompLib.Components +{ + public partial class WordLineChart : Component + { + public WordLineChart() + { + InitializeComponent(); + } + + public WordLineChart(IContainer container) + { + container.Add(this); + + InitializeComponent(); + } + + public void AddLineChart(SimpleLineChart simpleLineChart) + { + if (!CheckData(simpleLineChart.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 = "Times New Roman"; + + ParagraphFormat paragraphFormat = builder.ParagraphFormat; + paragraphFormat.FirstLineIndent = 8; + paragraphFormat.SpaceAfter = 24; + paragraphFormat.Alignment = ParagraphAlignment.Center; + paragraphFormat.KeepTogether = true; + + builder.Writeln(simpleLineChart.FileHeader); + + Shape shape = builder.InsertChart(ChartType.Line, 500, 270); + + Chart chart = shape.Chart; + + chart.Title.Text = simpleLineChart.LineChartName; + + ChartSeriesCollection seriesColl = chart.Series; + + Console.WriteLine(seriesColl.Count); + + seriesColl.Clear(); + + foreach (var data in simpleLineChart.DataList) + { + seriesColl.Add(data.NameSeries, data.NameData, data.Data ); + } + + ChartLegend legend = chart.Legend; + + legend.Position = (LegendPosition)simpleLineChart.AreaLegend; + + legend.Overlay = true; + + doc.Save(simpleLineChart.FilePath); + } + static bool CheckData(List data) + { + foreach (var _data in data) + { + if (string.IsNullOrEmpty(_data.NameSeries) || string.IsNullOrEmpty(_data.NameData.ToString()) || string.IsNullOrEmpty(_data.Data.ToString())) + { + return false; + } + } + + return true; + } + } +} diff --git a/COP/WinForms/FormWord.Designer.cs b/COP/WinForms/FormWord.Designer.cs index 1c04f5b..726bb77 100644 --- a/COP/WinForms/FormWord.Designer.cs +++ b/COP/WinForms/FormWord.Designer.cs @@ -32,7 +32,11 @@ wordText = new VisualCompLib.Components.WordText(components); groupBox1 = new GroupBox(); button1 = new Button(); + wordLineChart = new VisualCompLib.Components.WordLineChart(components); + groupBox3 = new GroupBox(); + button3 = new Button(); groupBox1.SuspendLayout(); + groupBox3.SuspendLayout(); SuspendLayout(); // // groupBox1 @@ -55,15 +59,37 @@ button1.UseVisualStyleBackColor = true; button1.Click += button1_Click; // + // groupBox3 + // + groupBox3.Controls.Add(button3); + groupBox3.Location = new Point(158, 12); + groupBox3.Name = "groupBox3"; + groupBox3.Size = new Size(120, 80); + groupBox3.TabIndex = 1; + groupBox3.TabStop = false; + groupBox3.Text = "Линейная диаграмма"; + // + // button3 + // + button3.Location = new Point(6, 45); + button3.Name = "button3"; + button3.Size = new Size(94, 29); + button3.TabIndex = 0; + button3.Text = "Создать"; + button3.UseVisualStyleBackColor = true; + button3.Click += button3_Click; + // // FormWord // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(320, 165); + Controls.Add(groupBox3); Controls.Add(groupBox1); Name = "FormWord"; Text = "Невизуальные компоненты"; groupBox1.ResumeLayout(false); + groupBox3.ResumeLayout(false); ResumeLayout(false); } @@ -72,5 +98,8 @@ private VisualCompLib.Components.WordText wordText; private GroupBox groupBox1; private Button button1; + private VisualCompLib.Components.WordLineChart wordLineChart; + private GroupBox groupBox3; + private Button button3; } } \ No newline at end of file diff --git a/COP/WinForms/FormWord.cs b/COP/WinForms/FormWord.cs index 25db246..1754efc 100644 --- a/COP/WinForms/FormWord.cs +++ b/COP/WinForms/FormWord.cs @@ -1,4 +1,6 @@ -using VisualCompLib.Components.SupportClasses; +using System.Diagnostics.Metrics; +using VisualCompLib.Components.SupportClasses; +using VisualCompLib.Components.SupportClasses.Enums; namespace WinForms { @@ -32,5 +34,36 @@ namespace WinForms } } } + + private void button3_Click(object sender, EventArgs e) + { + //фильтрация файлов для диалогового окна + using var dialog = new SaveFileDialog + { + Filter = "docx|*.docx" + }; + + if (dialog.ShowDialog() == DialogResult.OK) + { + try + { + string[] month = { "Январь", "Февраль", "Март" }; + double[] profit1 = { 300, 440, 270 }; + double[] profit2 = { 500, 620, 310 }; + double[] profit3 = { 420, 189, 430 }; + SimpleLineChart lineChart = new(dialog.FileName, "Третье задание", "График прибыли", EnumAreaLegend.Right, new List { + new DataLineChart("Компания 1", month, profit1), new DataLineChart("Компания 2", month, profit2), new DataLineChart("Компания 3", month, profit3), + }); + + wordLineChart.AddLineChart(lineChart); + + MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } diff --git a/COP/WinForms/FormWord.resx b/COP/WinForms/FormWord.resx index 5dda421..6501934 100644 --- a/COP/WinForms/FormWord.resx +++ b/COP/WinForms/FormWord.resx @@ -120,4 +120,7 @@ 17, 17 + + 134, 17 + \ No newline at end of file diff --git a/COP/WinForms/WinForms.csproj b/COP/WinForms/WinForms.csproj index 25a231c..234fae2 100644 --- a/COP/WinForms/WinForms.csproj +++ b/COP/WinForms/WinForms.csproj @@ -8,6 +8,10 @@ enable + + + +