VisualComponents/VisualComponentsLib/Components/ComponentWordHistogram.cs
2023-10-11 12:14:23 +04:00

105 lines
3.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Aspose.Words;
using Aspose.Words.Drawing;
using Aspose.Words.Drawing.Charts;
using System.ComponentModel;
using VisualComponentsLib.Components.SupportClasses;
namespace VisualComponentsLib.Components
{
public partial class ComponentWordHistogram : Component
{
public ComponentWordHistogram()
{
InitializeComponent();
}
public ComponentWordHistogram(IContainer container)
{
container.Add(this);
InitializeComponent();
}
//создание таблицы
public void AddHistogram(SimpleHistogram simpleHistogram)
{
if (!CheckData(simpleHistogram.DataList))
{
throw new Exception("Не данные заполнены");
}
// Initialize document
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify font formatting
Aspose.Words.Font font = builder.Font;
font.Size = 24;
font.Bold = true;
font.Color = System.Drawing.Color.Black;
font.Name = "Times New Roman";
// Specify paragraph formatting
ParagraphFormat paragraphFormat = builder.ParagraphFormat;
paragraphFormat.FirstLineIndent = 8;
paragraphFormat.SpaceAfter = 24;
paragraphFormat.Alignment = ParagraphAlignment.Center;
paragraphFormat.KeepTogether = true;
builder.Writeln(simpleHistogram.FileHeader);
// Добавьте диаграмму с данными по умолчанию. Вы можете указать различные типы и размеры диаграмм.
Shape shape = builder.InsertChart(ChartType.Column, 500, 270);
// Свойство диаграммы формы содержит все параметры, связанные с диаграммой.
Chart chart = shape.Chart;
chart.Title.Text = simpleHistogram.HistogramName;
// Получите коллекцию серий диаграмм.
ChartSeriesCollection seriesColl = chart.Series;
// Проверьте количество серий.
Console.WriteLine(seriesColl.Count);
// Удалить серию, сгенерированную по умолчанию.
seriesColl.Clear();
// Создайте массив имен категорий
string[] categories = new string[] { simpleHistogram.DataList[0].NameSeries };
// Добавление новых серий. Обратите внимание, что массивы данных не должны быть пустыми, а массивы должны быть одного размера.
foreach (var data in simpleHistogram.DataList)
{
seriesColl.Add(data.NameData, categories, new double[] { data.Data });
}
// Move the chart's legend to the top right corner.
ChartLegend legend = chart.Legend;
legend.Position = (LegendPosition)simpleHistogram.AreaLegend;
// Give other chart elements, such as the graph, more room by allowing them to overlap the legend.
legend.Overlay = true;
// Сохраните документ
doc.Save(simpleHistogram.FilePath);
}
//проверка заполненности входных значений
static bool CheckData(List<DataHistogram> data)
{
foreach (var _data in data)
{
if(string.IsNullOrEmpty(_data.NameSeries) || string.IsNullOrEmpty(_data.Data.ToString()))
{
return false;
}
}
return true;
}
}
}