118 lines
3.0 KiB
C#
118 lines
3.0 KiB
C#
using MigraDoc.DocumentObjectModel.Shapes;
|
|
using MigraDoc.DocumentObjectModel;
|
|
using MigraDoc.Rendering;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using MigraDoc.DocumentObjectModel.Shapes.Charts;
|
|
using WinFormsLibrary1.Configs.Diagram;
|
|
|
|
namespace WinFormsLibrary1
|
|
{
|
|
public partial class PdfWithDiagram : Component
|
|
{
|
|
public PdfWithDiagram()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
public PdfWithDiagram(IContainer container)
|
|
{
|
|
container.Add(this);
|
|
|
|
InitializeComponent();
|
|
}
|
|
|
|
private Document _document;
|
|
private Chart _chart;
|
|
|
|
private Document Document
|
|
{
|
|
get
|
|
{
|
|
if (_document == null)
|
|
{
|
|
_document = new Document();
|
|
}
|
|
return _document;
|
|
}
|
|
}
|
|
|
|
public void CreateHeader(string header)
|
|
{
|
|
var section = Document.AddSection();
|
|
var paragraph = section.AddParagraph(header);
|
|
paragraph.Format.Font.Color = Colors.Black;
|
|
paragraph.Format.Font.Bold = true;
|
|
}
|
|
|
|
private void ConfigChart(PdfWithDiagramConfig config)
|
|
{
|
|
((Shape)_chart).Width = Unit.FromCentimeter(16.0);
|
|
((Shape)_chart).Height = Unit.FromCentimeter(12.0);
|
|
_chart.TopArea.AddParagraph(config.ChartTitle);
|
|
_chart.XAxis.MajorTickMark = (TickMarkType)2;
|
|
_chart.YAxis.MajorTickMark = (TickMarkType)2;
|
|
_chart.YAxis.HasMajorGridlines = true;
|
|
_chart.PlotArea.LineFormat.Width = new Unit(1);
|
|
_chart.PlotArea.LineFormat.Visible = true;
|
|
switch (config.LegendLocation)
|
|
{
|
|
case Location.Left:
|
|
_chart.LeftArea.AddLegend();
|
|
break;
|
|
case Location.Right:
|
|
_chart.RightArea.AddLegend();
|
|
break;
|
|
case Location.Top:
|
|
_chart.TopArea.AddLegend();
|
|
break;
|
|
case Location.Bottom:
|
|
_chart.BottomArea.AddLegend();
|
|
break;
|
|
}
|
|
}
|
|
|
|
public void CreatePieChart(PdfWithDiagramConfig config)
|
|
{
|
|
_chart = new Chart(ChartType.Pie2D);
|
|
foreach (var kvp in config.Data)
|
|
{
|
|
Series series = _chart.SeriesCollection.AddSeries();
|
|
series.Name = kvp.Key;
|
|
series.Add(kvp.Value.Select(x => x.Value).ToArray()); // Используем Value для данных
|
|
}
|
|
_chart.XValues.AddXSeries().Add(config.Data.First().Value.Select(x => x.Name).ToArray()); // Используем Date для оси X
|
|
ConfigChart(config);
|
|
}
|
|
|
|
public void SaveDoc(string filepath)
|
|
{
|
|
if (string.IsNullOrEmpty(filepath))
|
|
throw new ArgumentNullException(nameof(filepath), "File path cannot be null or empty.");
|
|
if (_document == null)
|
|
throw new ArgumentNullException(nameof(_document), "Document is not created.");
|
|
if (_chart != null)
|
|
_document.LastSection.Add(_chart);
|
|
PdfDocumentRenderer documentRenderer = new PdfDocumentRenderer(true)
|
|
{
|
|
Document = _document
|
|
};
|
|
documentRenderer.RenderDocument();
|
|
documentRenderer.PdfDocument.Save(filepath);
|
|
}
|
|
|
|
public void CreateDoc(PdfWithDiagramConfig config)
|
|
{
|
|
config.CheckFields();
|
|
CreateHeader(config.Header);
|
|
CreatePieChart(config);
|
|
SaveDoc(config.FilePath);
|
|
}
|
|
}
|
|
}
|