lab2 ready

This commit is contained in:
revengel66 2024-10-28 21:49:23 +04:00
parent 0880abd42e
commit d0c502a619
8 changed files with 267 additions and 5 deletions

View File

@ -0,0 +1,36 @@
namespace ComponentsLibrary
{
partial class ComponentDiagram
{
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
/// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Код, автоматически созданный конструктором компонентов
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}

View File

@ -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<DataLine> 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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -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<DataLine> DataList = new();
public DocumentDiagram(string fileUrl, string fileHeader, string diagramName, EnumAreaLegend areaLegend, List<DataLine> dataList)
{
FileUrl = fileUrl;
FileHeader = fileHeader;
DiagramName = diagramName;
AreaLegend = areaLegend;
DataList = dataList;
}
}
}

View File

@ -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
}
}

View File

@ -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;
}
}

View File

@ -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<Employee> info = new(dialog.FileName, "Çàäàíèå 2", columns, employees, mergedColumns);
DocumentTable<Employee> 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<DataLine> dataList = new List<DataLine> {
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);
}
}
}
}
}

View File

@ -126,7 +126,7 @@
<metadata name="componentTable.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>308, 17</value>
</metadata>
<metadata name="componentTable2.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<metadata name="componentDiagram.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>451, 17</value>
</metadata>
</root>