Принятая 2 лабораторная работа
This commit is contained in:
parent
2faa7bf7f9
commit
dc7bee82a0
@ -34,25 +34,22 @@ namespace Components.NonVisual
|
||||
if (settings == null ||
|
||||
string.IsNullOrEmpty(settings.FilePath) ||
|
||||
string.IsNullOrEmpty(settings.DocumentTitle) ||
|
||||
settings.HeaderTitles == null || settings.HeaderTitles.Count == 0 ||
|
||||
settings.ColumnWidths == null || settings.DataList == null ||
|
||||
settings.ColumnPropertyMappings == null)
|
||||
settings.Columns == null || settings.Columns.Count == 0 ||
|
||||
settings.DataList == null)
|
||||
throw new ArgumentException("Заполнены не все необходимые данные для генерации документа.");
|
||||
|
||||
if (settings.HeaderTitles.Count != settings.ColumnWidths.Count)
|
||||
throw new ArgumentException("Количество заголовков должно совпадать с количеством ширин столбцов.");
|
||||
|
||||
Document document = new Document();
|
||||
Section section = document.AddSection();
|
||||
section.AddParagraph(settings.DocumentTitle, "Heading1");
|
||||
Paragraph titleParagraph = section.AddParagraph(settings.DocumentTitle, "Heading1");
|
||||
titleParagraph.Format.Font.Bold = true;
|
||||
|
||||
Table table = new Table();
|
||||
table.Borders.Width = 0.75;
|
||||
|
||||
// столбцы
|
||||
for (int i = 0; i < settings.ColumnWidths.Count; i++)
|
||||
foreach (var (_, width, _, _) in settings.Columns)
|
||||
{
|
||||
Column column = table.AddColumn(Unit.FromCentimeter(settings.ColumnWidths[i]));
|
||||
Column column = table.AddColumn(Unit.FromCentimeter(width));
|
||||
column.Format.Alignment = ParagraphAlignment.Center;
|
||||
}
|
||||
|
||||
@ -60,9 +57,10 @@ namespace Components.NonVisual
|
||||
Row headerRow = table.AddRow();
|
||||
headerRow.Height = Unit.FromCentimeter(settings.HeaderRowHeight);
|
||||
|
||||
for (int i = 0; i < settings.HeaderTitles.Count; i++)
|
||||
for (int i = 0; i < settings.Columns.Count; i++)
|
||||
{
|
||||
headerRow.Cells[i].AddParagraph(settings.HeaderTitles[i]);
|
||||
var (headerTitle, _, _, _) = settings.Columns[i];
|
||||
headerRow.Cells[i].AddParagraph(headerTitle);
|
||||
headerRow.Cells[i].Format.Font.Bold = true;
|
||||
headerRow.Cells[i].Format.Alignment = ParagraphAlignment.Center;
|
||||
}
|
||||
@ -73,15 +71,20 @@ namespace Components.NonVisual
|
||||
Row row = table.AddRow();
|
||||
row.Height = Unit.FromCentimeter(settings.DataRowHeight);
|
||||
|
||||
foreach (var columnMapping in settings.ColumnPropertyMappings)
|
||||
for (int columnIndex = 0; columnIndex < settings.Columns.Count; columnIndex++)
|
||||
{
|
||||
PropertyInfo propertyInfo = typeof(T).GetProperty(columnMapping.Value);
|
||||
var (_, _, propertyName, _) = settings.Columns[columnIndex];
|
||||
|
||||
PropertyInfo propertyInfo = typeof(T).GetProperty(propertyName);
|
||||
if (propertyInfo == null)
|
||||
throw new ArgumentException($"Свойство {columnMapping.Value} не найдено в классе {typeof(T).Name}.");
|
||||
throw new ArgumentException($"Свойство {propertyName} не найдено в классе {typeof(T).Name}.");
|
||||
|
||||
object value = propertyInfo.GetValue(dataItem);
|
||||
row.Cells[columnMapping.Key].AddParagraph(value != null ? value.ToString() : "");
|
||||
if (columnIndex == 0)
|
||||
{
|
||||
row.Cells[columnIndex].Format.Font.Bold = true;
|
||||
}
|
||||
row.Cells[columnIndex].AddParagraph(value != null ? value.ToString() : "");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,25 +31,25 @@ namespace Components.NonVisual
|
||||
|
||||
public void CreateHistogramPdf(HistogramData histogramData)
|
||||
{
|
||||
if (string.IsNullOrEmpty(histogramData.filePath))
|
||||
if (string.IsNullOrEmpty(histogramData.FilePath))
|
||||
throw new ArgumentException("Путь к файлу не может быть пустым.");
|
||||
if (string.IsNullOrEmpty(histogramData.documentTitle))
|
||||
if (string.IsNullOrEmpty(histogramData.DocumentTitle))
|
||||
throw new ArgumentException("Название документа не может быть пустым.");
|
||||
if (string.IsNullOrEmpty(histogramData.chartTitle))
|
||||
if (string.IsNullOrEmpty(histogramData.ChartTitle))
|
||||
throw new ArgumentException("Заголовок диаграммы не может быть пустым.");
|
||||
if (histogramData.chartData == null || histogramData.chartData.Count == 0)
|
||||
if (histogramData.ChartData == null || histogramData.ChartData.Count == 0)
|
||||
throw new ArgumentException("Набор данных не может быть пустым.");
|
||||
|
||||
foreach (var data in histogramData.chartData)
|
||||
foreach (var data in histogramData.ChartData)
|
||||
{
|
||||
if (string.IsNullOrEmpty(data.SeriesName) || data.Data == null || data.Data.Count == 0)
|
||||
throw new ArgumentException($"Набор данных для серии '{data.SeriesName}' некорректен.");
|
||||
}
|
||||
|
||||
// создание графика
|
||||
var plotModel = new PlotModel { Title = histogramData.chartTitle };
|
||||
var plotModel = new PlotModel { Title = histogramData.ChartTitle };
|
||||
|
||||
foreach (var data in histogramData.chartData)
|
||||
foreach (var data in histogramData.ChartData)
|
||||
{
|
||||
var barSeries = new BarSeries { Title = data.SeriesName };
|
||||
foreach (var item in data.Data)
|
||||
@ -60,7 +60,7 @@ namespace Components.NonVisual
|
||||
}
|
||||
|
||||
// Добавление легенды
|
||||
AddLegend(plotModel, histogramData.legendPosition);
|
||||
AddLegend(plotModel, histogramData.LegendPosition);
|
||||
|
||||
// сохранение графика в изображение
|
||||
var pngExporter = new PngExporter { Width = 600, Height = 400 };
|
||||
@ -72,11 +72,11 @@ namespace Components.NonVisual
|
||||
|
||||
// создание документа
|
||||
Document document = new Document();
|
||||
document.Info.Title = histogramData.documentTitle;
|
||||
document.Info.Title = histogramData.DocumentTitle;
|
||||
document.Info.Subject = "Гистограмма";
|
||||
|
||||
Section section = document.AddSection();
|
||||
section.AddParagraph(histogramData.chartTitle, "Heading1");
|
||||
section.AddParagraph(histogramData.ChartTitle, "Heading1");
|
||||
|
||||
// вставка изображения в PDF
|
||||
var image = section.AddImage("chart.png");
|
||||
@ -84,7 +84,7 @@ namespace Components.NonVisual
|
||||
|
||||
PdfDocumentRenderer renderer = new PdfDocumentRenderer(true) { Document = document };
|
||||
renderer.RenderDocument();
|
||||
renderer.PdfDocument.Save(histogramData.filePath);
|
||||
renderer.PdfDocument.Save(histogramData.FilePath);
|
||||
|
||||
File.Delete("chart.png");
|
||||
}
|
||||
|
@ -8,10 +8,10 @@ namespace Components.SaveToPdfHelpers
|
||||
{
|
||||
public class HistogramData
|
||||
{
|
||||
public string filePath { get; set; } = string.Empty;
|
||||
public string documentTitle { get; set; } = string.Empty;
|
||||
public string chartTitle { get; set; } = string.Empty;
|
||||
public LegendPositions legendPosition { get; set; }
|
||||
public List<ChartData>? chartData { get; set; }
|
||||
public string FilePath { get; set; } = string.Empty;
|
||||
public string DocumentTitle { get; set; } = string.Empty;
|
||||
public string ChartTitle { get; set; } = string.Empty;
|
||||
public LegendPositions LegendPosition { get; set; }
|
||||
public List<ChartData>? ChartData { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -10,11 +10,9 @@ namespace Components.SaveToPdfHelpers
|
||||
{
|
||||
public string FilePath { get; set; } = string.Empty;
|
||||
public string DocumentTitle { get; set; } = string.Empty;
|
||||
public List<string> HeaderTitles { get; set; }
|
||||
public List<float> ColumnWidths { get; set; }
|
||||
public List<(string HeaderTitles, float Widht, string PropertyName, int ColumnIndex)> Columns { get; set; }
|
||||
public float HeaderRowHeight { get; set; }
|
||||
public float DataRowHeight { get; set; }
|
||||
public List<T>? DataList { get; set; }
|
||||
public Dictionary<int, string> ColumnPropertyMappings { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -46,10 +46,35 @@ namespace WinFormsTest
|
||||
customDataGridView1.FillData(data);
|
||||
}
|
||||
|
||||
private string FilePath()
|
||||
{
|
||||
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
|
||||
{
|
||||
saveFileDialog.InitialDirectory = "d:\\tmp";
|
||||
saveFileDialog.Filter = "Excel files (*.pdf)|*.pdf|All files (*.*)|*.*";
|
||||
saveFileDialog.FilterIndex = 1;
|
||||
saveFileDialog.RestoreDirectory = true;
|
||||
|
||||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
return saveFileDialog.FileName;
|
||||
}
|
||||
}
|
||||
return String.Empty;
|
||||
}
|
||||
|
||||
private void GeneratePdfButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
string filePath = FilePath();
|
||||
|
||||
if (filePath == String.Empty)
|
||||
{
|
||||
MessageBox.Show("Ïðîèçîøëà îøèáêà: íå âûáðàí ïóòü äëÿ ôàéëà");
|
||||
return;
|
||||
}
|
||||
|
||||
var settings = new PdfDocumentData(
|
||||
"F:\\Îò÷åò1.pdf",
|
||||
filePath,
|
||||
"Íàçâàíèå äîêóìåíòà",
|
||||
new List<string[,]>
|
||||
{
|
||||
@ -76,12 +101,18 @@ namespace WinFormsTest
|
||||
}
|
||||
private void btnGeneratePDF_Click(object sender, EventArgs e)
|
||||
{
|
||||
string filePath = FilePath();
|
||||
|
||||
if (filePath == String.Empty)
|
||||
{
|
||||
MessageBox.Show("Ïðîèçîøëà îøèáêà: íå âûáðàí ïóòü äëÿ ôàéëà");
|
||||
return;
|
||||
}
|
||||
|
||||
var settings = new PDFTableSettings<Person>
|
||||
{
|
||||
FilePath = "F:\\Îò÷åò2.pdf",
|
||||
DocumentTitle = "Îò÷åò ïî ñòóäåíòàì",
|
||||
HeaderTitles = new List<string> { "ÔÈÎ", "Âîçðàñò", "Ïî÷òà" },
|
||||
ColumnWidths = new List<float> { 6.0f, 2.0f, 6.0f },
|
||||
FilePath = filePath,
|
||||
DocumentTitle = "Îò÷åò",
|
||||
HeaderRowHeight = 1.0f,
|
||||
DataRowHeight = 1.0f,
|
||||
DataList = new List<Person>
|
||||
@ -90,11 +121,11 @@ namespace WinFormsTest
|
||||
new Person ( "Øåðëîê Õîëìñ", 25, "221B_Baker_Street@mail.com" ),
|
||||
new Person ("Ñòàñ Àñàôüåâ", 41, "Stas@gmail.com")
|
||||
},
|
||||
ColumnPropertyMappings = new Dictionary<int, string>
|
||||
Columns = new List<(string, float, string, int)>
|
||||
{
|
||||
{ 0, nameof(Person.Name) },
|
||||
{ 1, nameof(Person.Age) },
|
||||
{ 2, nameof(Person.Email) }
|
||||
("ÔÈÎ", 6.0f, nameof(Person.Name), 0),
|
||||
("Âîçðàñò", 2.0f, nameof(Person.Age), 1),
|
||||
("Ïî÷òà", 6.0f, nameof(Person.Email), 2)
|
||||
}
|
||||
};
|
||||
|
||||
@ -110,14 +141,22 @@ namespace WinFormsTest
|
||||
}
|
||||
private void btnGenerateHistogrammPdf_Click(object sender, EventArgs e)
|
||||
{
|
||||
string filePath = FilePath();
|
||||
|
||||
if (filePath == String.Empty)
|
||||
{
|
||||
MessageBox.Show("Ïðîèçîøëà îøèáêà: íå âûáðàí ïóòü äëÿ ôàéëà");
|
||||
return;
|
||||
}
|
||||
|
||||
var setting = new HistogramData
|
||||
{
|
||||
filePath = "F:\\Ãèñòîãðàììà.pdf",
|
||||
documentTitle = "Ãèñòîãðàììà",
|
||||
chartTitle = "Ïðèìåð ãèñòîãðàììû",
|
||||
legendPosition = LegendPositions.Bottom,
|
||||
FilePath = filePath,
|
||||
DocumentTitle = "Ãèñòîãðàììà",
|
||||
ChartTitle = "Ïðèìåð ãèñòîãðàììû",
|
||||
LegendPosition = LegendPositions.Bottom,
|
||||
|
||||
chartData = new List<ChartData>
|
||||
ChartData = new List<ChartData>
|
||||
{
|
||||
new ChartData { SeriesName = "Ñåðèÿ 1", Data = new Dictionary<string, double> { { "Êàòåãîðèÿ 1", 5 }, { "Êàòåãîðèÿ 2", 10 } } },
|
||||
new ChartData { SeriesName = "Ñåðèÿ 2", Data = new Dictionary<string, double> { { "Êàòåãîðèÿ 1", 3 }, { "Êàòåãîðèÿ 2", 8 } } },
|
||||
|
Loading…
Reference in New Issue
Block a user