COP-17/WinFormsProject/WinFormsLibrary/CircleDiagram.cs

106 lines
3.3 KiB
C#
Raw Normal View History

using Aspose.Words.Drawing.Charts;
using Aspose.Words;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinFormsLibrary.SupportClasses;
using Aspose.Words.Drawing;
namespace WinFormsLibrary
{
public partial class CircleDiagram : Component
{
public CircleDiagram()
{
InitializeComponent();
}
public CircleDiagram(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public void AddCircleDiagram(SimpleCircleDiagram simpleCircleDiagram)
{
if (!CheckData(simpleCircleDiagram.DataList))
{
throw new Exception("Данные не заполнены");
}
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Aspose.Words.Font font = builder.Font;
font.Size = 24;
font.Bold = true;
font.Color = Color.Black;
font.Name = "Times New Roman";
ParagraphFormat paragraphFormat = builder.ParagraphFormat;
paragraphFormat.FirstLineIndent = 8;
paragraphFormat.SpaceAfter = 24;
paragraphFormat.Alignment = ParagraphAlignment.Center;
paragraphFormat.KeepTogether = true;
builder.Writeln(simpleCircleDiagram.FileHeader);
Shape shape = builder.InsertChart(ChartType.Pie, 500, 270);
Chart chart = shape.Chart;
chart.Title.Text = simpleCircleDiagram.CircleDiagramName;
ChartSeries series = chart.Series[0];
ChartSeriesCollection seriesColl = chart.Series;
Console.WriteLine(seriesColl.Count);
seriesColl.Clear();
foreach (var data in simpleCircleDiagram.DataList)
{
int count = Math.Min(simpleCircleDiagram.NameData.Length, data.Data.Length);
// Создаем временный массив для данных и имен, содержащий только необходимое количество элементов.
string seriesNames = data.NameSeries;
double[] seriesData = data.Data.Take(count).ToArray();
string[] categoryNames = simpleCircleDiagram.NameData.Take(count).ToArray();
// Добавляем только непустые серии с данными в график.
if (seriesData.Length > 0)
{
seriesColl.Add(seriesNames, categoryNames, seriesData);
}
}
ChartLegend legend = chart.Legend;
legend.Position = (LegendPosition)simpleCircleDiagram.AreaLegend;
legend.Overlay = true;
doc.Save(simpleCircleDiagram.FilePath);
}
static bool CheckData(List<DataCircleDiagram> data)
{
foreach (var _data in data)
{
2023-11-01 19:53:04 +04:00
if (string.IsNullOrEmpty(_data.NameSeries) || string.IsNullOrEmpty(_data.Data.ToString()))
{
return false;
}
}
return true;
}
}
}