2024-10-16 13:59:02 +03:00

88 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BarsukovComponents.NotVisualComponents.Configs;
using PdfSharp.Charting;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
namespace BarsukovComponents.NotVisualComponents
{
public partial class PdfDiagram : Component
{
public PdfDiagram()
{
InitializeComponent();
}
public PdfDiagram(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public void CreateDiagram(string filename, string title, string diagramName, Dictionary<string, List<double>> data, LengendAlign legendAlign = LengendAlign.bottom)
{
if (string.IsNullOrEmpty(filename))
{
throw new ArgumentNullException("Enter filemale");
}
if (string.IsNullOrEmpty(title))
{
throw new ArgumentNullException("Enter title");
}
if (data.Count == 0)
{
throw new ArgumentNullException("Enter data");
}
Chart chart = new Chart(ChartType.Line);
chart.Legend.Docking = (DockingType)legendAlign;
chart.Legend.LineFormat.Visible = true;
foreach (var item in data)
{
Series series = chart.SeriesCollection.AddSeries();
series.Name = item.Key;
double[] values = new double[item.Value.Count];
for (int i = 0; i < item.Value.Count; i++)
{
values.SetValue(Convert.ToDouble(item.Value[i]), i);
}
series.Add(values);
}
chart.XAxis.MajorTickMark = TickMarkType.Outside;
chart.YAxis.MajorTickMark = TickMarkType.Cross;
chart.PlotArea.LineFormat.Color = XColors.Black;
chart.PlotArea.LineFormat.Visible = true;
chart.PlotArea.FillFormat.Color = XColors.White;
chart.Legend.LineFormat.Visible = true;
ChartFrame chartFrame = new ChartFrame();
chartFrame.Location = new XPoint(50, 70);
chartFrame.Size = new XSize(500, 400);
chartFrame.Add(chart);
PdfDocument document = new PdfDocument(filename);
PdfPage page = document.AddPage();
page.Size = PdfSharp.PageSize.A4;
XGraphics gfx = XGraphics.FromPdfPage(page);
gfx.DrawString(title, new XFont("Times new Roman", 20, XFontStyleEx.Bold), XBrushes.Black, new XRect(20, 20, page.Width, page.Height), XStringFormats.TopCenter);
gfx.DrawString(diagramName, new XFont("Times new Roman", 14), XBrushes.Black, new XRect(20, 40, page.Width, page.Height), XStringFormats.TopCenter);
chartFrame.DrawChart(gfx);
document.Close();
}
}
}