Третий компонент

This commit is contained in:
Максим Яковлев 2024-09-22 23:39:25 +04:00
parent 554852a6b8
commit 5cf9f7d900
8 changed files with 161 additions and 0 deletions

View File

@ -0,0 +1,36 @@
namespace ComponentProgramming
{
partial class DiagramComponent
{
/// <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,98 @@
using ComponentProgramming.Components.Models;
using PdfSharp.Pdf;
using PdfSharp;
using System.ComponentModel;
using PdfSharp.Drawing;
using PdfSharp.Charting;
using PdfSharp.UniversalAccessibility.Drawing;
namespace ComponentProgramming
{
public partial class DiagramComponent : Component
{
public DiagramComponent()
{
InitializeComponent();
}
public DiagramComponent(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public void CreateLineDiagram(string docPath, string title, string header, Dictionary<string, List<Double>> data, LegendAlign legendAlign = LegendAlign.top)
{
if (string.IsNullOrEmpty(docPath))
{
throw new ArgumentNullException("Введите путь до файла!");
}
if (string.IsNullOrEmpty(title))
{
throw new ArgumentNullException("Введите заголовок");
}
if (string.IsNullOrEmpty(header))
{
throw new ArgumentNullException("Введите заголовок для диаграммы");
}
if (data.Count == 0)
{
throw new ArgumentException("Нету данных");
}
Chart chart = new Chart(ChartType.Line);
//Задание легенды
chart.Legend.Docking = (DockingType)legendAlign;
chart.Legend.LineFormat.Visible = true;
//Добавление серий
foreach (var item in data)
{
Series series = chart.SeriesCollection.AddSeries();
series.Name = item.Key;
double[] vals = new double[item.Value.Count];
for (int i = 0; i < item.Value.Count; i++)
{
vals.SetValue(Convert.ToDouble(item.Value[i]), i);
}
series.Add(vals);
}
//Объявление осей
chart.XAxis.MajorTickMark = TickMarkType.Outside;
chart.YAxis.MajorTickMark = TickMarkType.Outside;
chart.YAxis.HasMajorGridlines = true;
chart.PlotArea.LineFormat.Color = XColors.DarkGray;
chart.PlotArea.LineFormat.Width = 1;
chart.PlotArea.LineFormat.Visible = true;
chart.Legend.LineFormat.Visible = true;
ChartFrame chartFrame = new ChartFrame();
chartFrame.Location = new XPoint(50, 70);
chartFrame.Size = new XSize(500, 400);
chartFrame.Add(chart);
PdfDocument document = new PdfDocument(docPath);
PdfPage page = document.AddPage();
page.Size = PageSize.A4;
XGraphics gfx = XGraphics.FromPdfPage(page);
XFont font = new XFont("Times New Roman", 14);
gfx.DrawString(title, font, XBrushes.Black, new XRect(20, 20, page.Width, page.Height), XStringFormats.TopLeft);
gfx.DrawString(header, font, XBrushes.Black, new XRect(20, 40, page.Width, page.Height), XStringFormats.TopCenter);
chartFrame.Draw(gfx);
document.Close();
}
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComponentProgramming.Components.Models
{
public enum LegendAlign
{
top,
bottom,
left,
right
}
}

View File

@ -35,6 +35,7 @@
buttonEnter = new Button();
controlListBox = new ComponentProgramming.ControlListBox();
tableComponent = new ComponentProgramming.Components.TableComponent(components);
diagramComponent = new ComponentProgramming.DiagramComponent(components);
SuspendLayout();
//
// controlComboBox
@ -109,5 +110,6 @@
private Button buttonEnter;
private ComponentProgramming.ControlListBox controlListBox;
private ComponentProgramming.Components.TableComponent tableComponent;
private ComponentProgramming.DiagramComponent diagramComponent;
}
}

View File

@ -33,6 +33,12 @@ namespace Forms
};
tableComponent.CreateTable("C:\\Users\\shotb\\source\\repos\\KOP\\ComponentProgramming\\Forms\\table.pdf", "Çàãîëîâîê", mergeCells, colInfos, workers);
Dictionary<string, List<Double>> data = new Dictionary<string, List<Double>>();
data.Add("Çíà÷1", new List<double> { 0.5, 1, 2, 5, 2 });
data.Add("Çíà÷2", new List<double> { 3, 2, 1, 3, 6 });
data.Add("Çíà÷3", new List<double> { 7, 3, 1, 2, 5 });
diagramComponent.CreateLineDiagram("C:\\Users\\shotb\\source\\repos\\KOP\\ComponentProgramming\\Forms\\diagram.pdf", "Çàãîëîâîê", "Ëèíåéíàÿ äèàãðàììà", data, LegendAlign.bottom);
}
private void FillBox()

View File

@ -120,4 +120,7 @@
<metadata name="tableComponent.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="diagramComponent.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>161, 17</value>
</metadata>
</root>

Binary file not shown.

Binary file not shown.