done diagram component

This commit is contained in:
ShabOl 2024-09-25 00:46:40 +04:00
parent 05fd1f373b
commit a90c2b2cde
9 changed files with 198 additions and 1 deletions

View File

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

View File

@ -140,5 +140,21 @@ namespace FormsTesting
new List<string> { "LastName", "Id", "FirstName", "Age" },
Persons);
}
private void CreateHistogram_Click(object sender, EventArgs e)
{
var Data = new Dictionary<string, int[]>
{
{ "Ñåðèÿ 1", new[] { 10, 20, 30, 40 } },
{ "Ñåðèÿ 2", new[] { 15, 25, 35, 45 } }
};
DiagramComponent.CreateDocument(
@"C:\Comps\Doc3.pdf",
"Òåñòîâàÿ äèàãðàììà",
"Çàãîëîâîê",
ShabComponentsLibrary.Enums.LegendPosition.Bottom,
Data
);
}
}
}

View File

@ -123,4 +123,7 @@
<metadata name="ConfigurableTableComponent.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>233, 17</value>
</metadata>
<metadata name="DiagramComponent.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>447, 17</value>
</metadata>
</root>

View File

@ -0,0 +1,10 @@
namespace ShabComponentsLibrary.Enums
{
public enum LegendPosition
{
Top,
Bottom,
Left,
Right
}
}

View File

@ -0,0 +1,13 @@
using ShabComponentsLibrary.Enums;
namespace ShabComponentsLibrary.OfficePackage.HelperModels
{
internal class PdfChartParameters
{
public Dictionary<string, int[]> Data = new();
public string Title = string.Empty;
public LegendPosition LegendPosition = LegendPosition.Bottom;
}
}

View File

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

View File

@ -0,0 +1,36 @@
namespace ShabComponentsLibrary
{
partial class ShabDiagramComponent
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}

View File

@ -0,0 +1,56 @@
using ShabComponentsLibrary.OfficePackage.HelperEnums;
using ShabComponentsLibrary.OfficePackage.HelperModels;
using ShabComponentsLibrary.OfficePackage;
using System.ComponentModel;
using ShabComponentsLibrary.Enums;
namespace ShabComponentsLibrary
{
/// <summary>
/// Невизуальный компонент для создания документа с гистограммой
/// </summary>
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<string, int[]> 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);
}
}
}

View File

@ -23,6 +23,13 @@ namespace ShabComponentsLibrary
public void CreateDocument(string Filename, string Title, List<string[][]> 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