2024-10-02 18:26:36 +04:00
|
|
|
|
using FormLibrary.HelperClasses;
|
|
|
|
|
using MigraDoc.DocumentObjectModel;
|
|
|
|
|
using MigraDoc.Rendering;
|
|
|
|
|
using OxyPlot.Series;
|
|
|
|
|
using OxyPlot;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using OxyPlot.WindowsForms;
|
2024-10-02 19:15:15 +04:00
|
|
|
|
using OxyPlot.Legends;
|
2024-10-02 19:39:58 +04:00
|
|
|
|
|
2024-10-02 18:26:36 +04:00
|
|
|
|
|
|
|
|
|
namespace FormLibrary
|
|
|
|
|
{
|
|
|
|
|
public partial class ComponentHistogramToPdf : Component
|
|
|
|
|
{
|
|
|
|
|
public ComponentHistogramToPdf()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2024-10-03 15:34:22 +04:00
|
|
|
|
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
2024-10-02 18:26:36 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ComponentHistogramToPdf(IContainer container)
|
|
|
|
|
{
|
|
|
|
|
container.Add(this);
|
|
|
|
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
2024-10-03 15:34:22 +04:00
|
|
|
|
public void CreateHistogramPdf(string filePath, string documentTitle, string chartTitle, LegendPosition legendPosition, List<ChartData> chartData)
|
2024-10-02 18:26:36 +04:00
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(filePath))
|
|
|
|
|
throw new ArgumentException("Путь к файлу не может быть пустым.");
|
|
|
|
|
if (string.IsNullOrEmpty(documentTitle))
|
|
|
|
|
throw new ArgumentException("Название документа не может быть пустым.");
|
|
|
|
|
if (string.IsNullOrEmpty(chartTitle))
|
|
|
|
|
throw new ArgumentException("Заголовок диаграммы не может быть пустым.");
|
|
|
|
|
if (chartData == null || chartData.Count == 0)
|
|
|
|
|
throw new ArgumentException("Набор данных не может быть пустым.");
|
|
|
|
|
|
|
|
|
|
foreach (var data in chartData)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(data.SeriesName) || data.Data == null || data.Data.Count == 0)
|
|
|
|
|
throw new ArgumentException($"Набор данных для серии '{data.SeriesName}' некорректен.");
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-02 19:15:15 +04:00
|
|
|
|
// создание графика
|
2024-10-02 18:26:36 +04:00
|
|
|
|
var plotModel = new PlotModel { Title = chartTitle };
|
2024-10-02 19:15:15 +04:00
|
|
|
|
|
2024-10-02 18:26:36 +04:00
|
|
|
|
foreach (var data in chartData)
|
|
|
|
|
{
|
|
|
|
|
var barSeries = new BarSeries { Title = data.SeriesName };
|
|
|
|
|
foreach (var item in data.Data)
|
|
|
|
|
{
|
|
|
|
|
barSeries.Items.Add(new BarItem(item.Value));
|
|
|
|
|
}
|
|
|
|
|
plotModel.Series.Add(barSeries);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-02 19:15:15 +04:00
|
|
|
|
// Добавление легенды
|
|
|
|
|
AddLegend(plotModel, legendPosition);
|
|
|
|
|
|
|
|
|
|
// сохранение графика в изображение
|
|
|
|
|
var pngExporter = new PngExporter { Width = 600, Height = 400 };
|
2024-10-02 18:26:36 +04:00
|
|
|
|
using (var stream = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
pngExporter.Export(plotModel, stream);
|
|
|
|
|
File.WriteAllBytes("chart.png", stream.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-02 19:15:15 +04:00
|
|
|
|
// создание документа
|
2024-10-02 18:26:36 +04:00
|
|
|
|
Document document = new Document();
|
|
|
|
|
document.Info.Title = documentTitle;
|
|
|
|
|
document.Info.Subject = "Гистограмма";
|
|
|
|
|
|
|
|
|
|
Section section = document.AddSection();
|
|
|
|
|
section.AddParagraph(chartTitle, "Heading1");
|
|
|
|
|
|
2024-10-02 19:15:15 +04:00
|
|
|
|
// вставка изображения в PDF
|
2024-10-02 18:26:36 +04:00
|
|
|
|
var image = section.AddImage("chart.png");
|
|
|
|
|
image.Width = Unit.FromCentimeter(15);
|
|
|
|
|
|
|
|
|
|
PdfDocumentRenderer renderer = new PdfDocumentRenderer(true) { Document = document };
|
|
|
|
|
renderer.RenderDocument();
|
|
|
|
|
renderer.PdfDocument.Save(filePath);
|
|
|
|
|
|
|
|
|
|
File.Delete("chart.png");
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-02 19:15:15 +04:00
|
|
|
|
//добавление легенды
|
2024-10-03 15:34:22 +04:00
|
|
|
|
private void AddLegend(PlotModel plotModel, LegendPosition legendPosition)
|
2024-10-02 19:15:15 +04:00
|
|
|
|
{
|
|
|
|
|
// Создание легенды
|
|
|
|
|
var legend = new OxyPlot.Legends.Legend
|
|
|
|
|
{
|
|
|
|
|
LegendPlacement = LegendPlacement.Outside,
|
2024-10-03 15:34:22 +04:00
|
|
|
|
LegendPosition = legendPosition,
|
2024-10-02 19:15:15 +04:00
|
|
|
|
LegendOrientation = LegendOrientation.Vertical
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
plotModel.Legends.Add(legend);
|
|
|
|
|
}
|
2024-10-02 18:26:36 +04:00
|
|
|
|
}
|
|
|
|
|
}
|