2024-10-30 14:26:40 +04:00
|
|
|
|
using Components.SaveToPdfHelpers;
|
|
|
|
|
using MigraDoc.DocumentObjectModel;
|
|
|
|
|
using MigraDoc.Rendering;
|
|
|
|
|
using OxyPlot.Series;
|
|
|
|
|
using OxyPlot;
|
|
|
|
|
using System;
|
2024-10-16 14:03:22 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2024-10-30 14:26:40 +04:00
|
|
|
|
using OxyPlot.WindowsForms;
|
|
|
|
|
using OxyPlot.Legends;
|
2024-10-16 14:03:22 +04:00
|
|
|
|
|
|
|
|
|
namespace Components.NonVisual
|
|
|
|
|
{
|
|
|
|
|
public partial class HistogramPDF : Component
|
|
|
|
|
{
|
|
|
|
|
public HistogramPDF()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2024-10-30 14:26:40 +04:00
|
|
|
|
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
2024-10-16 14:03:22 +04:00
|
|
|
|
}
|
|
|
|
|
public HistogramPDF(IContainer container)
|
|
|
|
|
{
|
|
|
|
|
container.Add(this);
|
|
|
|
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
2024-10-30 14:26:40 +04:00
|
|
|
|
|
|
|
|
|
public void CreateHistogramPdf(HistogramData histogramData)
|
|
|
|
|
{
|
2024-11-02 18:51:07 +04:00
|
|
|
|
if (string.IsNullOrEmpty(histogramData.FilePath))
|
2024-10-30 14:26:40 +04:00
|
|
|
|
throw new ArgumentException("Путь к файлу не может быть пустым.");
|
2024-11-02 18:51:07 +04:00
|
|
|
|
if (string.IsNullOrEmpty(histogramData.DocumentTitle))
|
2024-10-30 14:26:40 +04:00
|
|
|
|
throw new ArgumentException("Название документа не может быть пустым.");
|
2024-11-02 18:51:07 +04:00
|
|
|
|
if (string.IsNullOrEmpty(histogramData.ChartTitle))
|
2024-10-30 14:26:40 +04:00
|
|
|
|
throw new ArgumentException("Заголовок диаграммы не может быть пустым.");
|
2024-11-02 18:51:07 +04:00
|
|
|
|
if (histogramData.ChartData == null || histogramData.ChartData.Count == 0)
|
2024-10-30 14:26:40 +04:00
|
|
|
|
throw new ArgumentException("Набор данных не может быть пустым.");
|
|
|
|
|
|
2024-11-02 18:51:07 +04:00
|
|
|
|
foreach (var data in histogramData.ChartData)
|
2024-10-30 14:26:40 +04:00
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(data.SeriesName) || data.Data == null || data.Data.Count == 0)
|
|
|
|
|
throw new ArgumentException($"Набор данных для серии '{data.SeriesName}' некорректен.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// создание графика
|
2024-11-02 18:51:07 +04:00
|
|
|
|
var plotModel = new PlotModel { Title = histogramData.ChartTitle };
|
2024-10-30 14:26:40 +04:00
|
|
|
|
|
2024-11-02 18:51:07 +04:00
|
|
|
|
foreach (var data in histogramData.ChartData)
|
2024-10-30 14:26:40 +04:00
|
|
|
|
{
|
|
|
|
|
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-11-02 18:51:07 +04:00
|
|
|
|
AddLegend(plotModel, histogramData.LegendPosition);
|
2024-10-30 14:26:40 +04:00
|
|
|
|
|
|
|
|
|
// сохранение графика в изображение
|
|
|
|
|
var pngExporter = new PngExporter { Width = 600, Height = 400 };
|
|
|
|
|
using (var stream = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
pngExporter.Export(plotModel, stream);
|
|
|
|
|
File.WriteAllBytes("chart.png", stream.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// создание документа
|
|
|
|
|
Document document = new Document();
|
2024-11-02 18:51:07 +04:00
|
|
|
|
document.Info.Title = histogramData.DocumentTitle;
|
2024-10-30 14:26:40 +04:00
|
|
|
|
document.Info.Subject = "Гистограмма";
|
|
|
|
|
|
|
|
|
|
Section section = document.AddSection();
|
2024-11-02 18:51:07 +04:00
|
|
|
|
section.AddParagraph(histogramData.ChartTitle, "Heading1");
|
2024-10-30 14:26:40 +04:00
|
|
|
|
|
|
|
|
|
// вставка изображения в PDF
|
|
|
|
|
var image = section.AddImage("chart.png");
|
|
|
|
|
image.Width = Unit.FromCentimeter(15);
|
|
|
|
|
|
|
|
|
|
PdfDocumentRenderer renderer = new PdfDocumentRenderer(true) { Document = document };
|
|
|
|
|
renderer.RenderDocument();
|
2024-11-02 18:51:07 +04:00
|
|
|
|
renderer.PdfDocument.Save(histogramData.FilePath);
|
2024-10-30 14:26:40 +04:00
|
|
|
|
|
|
|
|
|
File.Delete("chart.png");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//добавление легенды
|
|
|
|
|
private void AddLegend(PlotModel plotModel, LegendPositions legendPosition)
|
|
|
|
|
{
|
|
|
|
|
// Создание легенды
|
|
|
|
|
var legend = new OxyPlot.Legends.Legend
|
|
|
|
|
{
|
|
|
|
|
LegendPlacement = LegendPlacement.Outside,
|
|
|
|
|
LegendPosition = ConvertLegendPosition(legendPosition),
|
|
|
|
|
LegendOrientation = LegendOrientation.Vertical
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
plotModel.Legends.Add(legend);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// конвертация позиции легенды
|
|
|
|
|
private OxyPlot.Legends.LegendPosition ConvertLegendPosition(LegendPositions legendPosition)
|
|
|
|
|
{
|
|
|
|
|
return legendPosition switch
|
|
|
|
|
{
|
|
|
|
|
LegendPositions.Top => OxyPlot.Legends.LegendPosition.TopCenter,
|
|
|
|
|
LegendPositions.Bottom => OxyPlot.Legends.LegendPosition.BottomCenter,
|
|
|
|
|
LegendPositions.Left => OxyPlot.Legends.LegendPosition.LeftTop,
|
|
|
|
|
LegendPositions.Right => OxyPlot.Legends.LegendPosition.RightTop,
|
|
|
|
|
_ => OxyPlot.Legends.LegendPosition.TopCenter, // Положение по умолчанию
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-10-16 14:03:22 +04:00
|
|
|
|
}
|
|
|
|
|
}
|