124 lines
3.9 KiB
C#
124 lines
3.9 KiB
C#
using System.ComponentModel;
|
||
using Word = Microsoft.Office.Interop.Word;
|
||
using Excel = Microsoft.Office.Interop.Excel;
|
||
using WinFormsLibrary.NonVisualComponents.Helpers;
|
||
using WinFormsLibrary.NonVisualComponents.Enums;
|
||
|
||
namespace WinFormsLibrary.NonVisualComponents
|
||
{
|
||
public partial class WordDiagram : Component
|
||
{
|
||
public WordDiagram()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
|
||
public WordDiagram(IContainer container)
|
||
{
|
||
container.Add(this);
|
||
|
||
InitializeComponent();
|
||
}
|
||
|
||
public void CreateDiagramDocument(GraphicWordInfo diagramInfo)
|
||
{
|
||
ValidateDiagramInfo(diagramInfo);
|
||
|
||
var wordApp = new Word.Application();
|
||
var document = wordApp.Documents.Add();
|
||
|
||
var paragraph = document.Paragraphs.Add();
|
||
paragraph.Range.Text = diagramInfo.DocumentTitle;
|
||
paragraph.Range.Font.Size = 24;
|
||
paragraph.Range.Font.Bold = 1;
|
||
paragraph.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
|
||
paragraph.Range.InsertParagraphAfter();
|
||
|
||
AddChart(document, diagramInfo);
|
||
|
||
document.SaveAs2(diagramInfo.Path);
|
||
wordApp.Quit();
|
||
}
|
||
private void AddChart(Word.Document document, GraphicWordInfo diagramInfo)
|
||
{
|
||
var paragraph = document.Paragraphs.Add();
|
||
var chartShape = document.InlineShapes.AddChart2(-1, (Microsoft.Office.Core.XlChartType)Excel.XlChartType.xlLine, paragraph.Range);
|
||
var chart = chartShape.Chart;
|
||
|
||
chart.HasTitle = true;
|
||
chart.ChartTitle.Text = diagramInfo.DiagramTitle;
|
||
|
||
var chartSeriesCollection = chart.SeriesCollection();
|
||
for (int i = chartSeriesCollection.Count; i >= 1; i--)
|
||
{
|
||
chartSeriesCollection.Item(i).Delete();
|
||
}
|
||
|
||
var seriesX = diagramInfo.SeriesX.ToArray();
|
||
|
||
for (int i = 0; i < diagramInfo.SeriesParameters.Count; i++)
|
||
{
|
||
var seriesInfo = diagramInfo.SeriesParameters[i];
|
||
var series = chartSeriesCollection.NewSeries();
|
||
|
||
series.Name = seriesInfo.SeriesName;
|
||
series.Values = seriesInfo.ValuesY.ToArray();
|
||
series.XValues = seriesX;
|
||
|
||
SetSeriesColor(series, seriesInfo.Color);
|
||
|
||
series.MarkerStyle = Excel.XlMarkerStyle.xlMarkerStyleCircle;
|
||
series.MarkerSize = 5;
|
||
}
|
||
|
||
chart.HasLegend = true;
|
||
chart.Legend.Position = diagramInfo.LegendLayout switch
|
||
{
|
||
LegendLayoutEnum.Left => Word.XlLegendPosition.xlLegendPositionLeft,
|
||
LegendLayoutEnum.Top => Word.XlLegendPosition.xlLegendPositionTop,
|
||
LegendLayoutEnum.Right => Word.XlLegendPosition.xlLegendPositionRight,
|
||
LegendLayoutEnum.Bottom => Word.XlLegendPosition.xlLegendPositionBottom,
|
||
_ => Word.XlLegendPosition.xlLegendPositionBottom,
|
||
};
|
||
|
||
chart.ChartData.Workbook.Application.Quit();
|
||
}
|
||
|
||
private void SetSeriesColor(dynamic series, Color color)
|
||
{
|
||
series.Format.Line.ForeColor.RGB = ColorTranslator.ToOle(color);
|
||
series.Format.Fill.ForeColor.RGB = ColorTranslator.ToOle(color);
|
||
}
|
||
|
||
private void ValidateDiagramInfo(GraphicWordInfo diagramInfo)
|
||
{
|
||
if (string.IsNullOrEmpty(diagramInfo.Path) || string.IsNullOrEmpty(diagramInfo.DocumentTitle) ||
|
||
string.IsNullOrEmpty(diagramInfo.DiagramTitle) || diagramInfo.SeriesX == null ||
|
||
diagramInfo.SeriesX.Count == 0 || diagramInfo.SeriesParameters == null ||
|
||
diagramInfo.SeriesParameters.Count == 0)
|
||
{
|
||
throw new ArgumentException("Не все данные для диаграммы заполнены");
|
||
}
|
||
|
||
foreach (var series in diagramInfo.SeriesParameters)
|
||
{
|
||
if (string.IsNullOrEmpty(series.SeriesName))
|
||
{
|
||
throw new ArgumentException("Название серии не может быть пустым");
|
||
}
|
||
|
||
if (series.ValuesY == null || series.ValuesY.Count == 0)
|
||
{
|
||
throw new ArgumentException($"Список значений оси Y для серии '{series.SeriesName}' не заполнен");
|
||
}
|
||
|
||
if (diagramInfo.SeriesX.Count != series.ValuesY.Count)
|
||
{
|
||
throw new ArgumentException($"Количество данных оси X и значений оси Y для серии '{series.SeriesName}' не совпадает");
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|