99 lines
3.1 KiB
C#
99 lines
3.1 KiB
C#
|
using PdfSharp.Charting;
|
|||
|
using PdfSharp.Drawing;
|
|||
|
using PdfSharp.Pdf;
|
|||
|
using PdfSharp;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel;
|
|||
|
using System.Diagnostics;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Controls.Models;
|
|||
|
|
|||
|
namespace Controls
|
|||
|
{
|
|||
|
public partial class DiagramComponent : Component
|
|||
|
{
|
|||
|
public DiagramComponent()
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
}
|
|||
|
|
|||
|
public DiagramComponent(IContainer container)
|
|||
|
{
|
|||
|
container.Add(this);
|
|||
|
|
|||
|
InitializeComponent();
|
|||
|
}
|
|||
|
|
|||
|
public void CreateLineDiagram(string docPath, string title, string header, Dictionary<string, List<Double>> data, LegendAlign legendAlign = LegendAlign.top)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(docPath))
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Введите путь до файла!");
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(title))
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Введите заголовок");
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(header))
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Введите заголовок для диаграммы");
|
|||
|
}
|
|||
|
if (data.Count == 0)
|
|||
|
{
|
|||
|
throw new ArgumentException("Нету данных");
|
|||
|
}
|
|||
|
|
|||
|
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[] vals = new double[item.Value.Count];
|
|||
|
for (int i = 0; i < item.Value.Count; i++)
|
|||
|
{
|
|||
|
vals.SetValue(Convert.ToDouble(item.Value[i]), i);
|
|||
|
}
|
|||
|
|
|||
|
series.Add(vals);
|
|||
|
}
|
|||
|
|
|||
|
chart.XAxis.MajorTickMark = TickMarkType.Outside;
|
|||
|
chart.YAxis.MajorTickMark = TickMarkType.Outside;
|
|||
|
chart.YAxis.HasMajorGridlines = true;
|
|||
|
|
|||
|
chart.PlotArea.LineFormat.Color = XColors.DarkGray;
|
|||
|
chart.PlotArea.LineFormat.Width = 1;
|
|||
|
chart.PlotArea.LineFormat.Visible = true;
|
|||
|
|
|||
|
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(docPath);
|
|||
|
|
|||
|
PdfPage page = document.AddPage();
|
|||
|
page.Size = PageSize.A4;
|
|||
|
|
|||
|
XGraphics gfx = XGraphics.FromPdfPage(page);
|
|||
|
|
|||
|
XFont font = new XFont("Times New Roman", 14);
|
|||
|
|
|||
|
gfx.DrawString(title, font, XBrushes.Black, new XRect(20, 20, page.Width, page.Height), XStringFormats.TopLeft);
|
|||
|
gfx.DrawString(header, font, XBrushes.Black, new XRect(20, 40, page.Width, page.Height), XStringFormats.TopCenter);
|
|||
|
|
|||
|
chartFrame.Draw(gfx);
|
|||
|
document.Close();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|