2.3 component + refactoring
This commit is contained in:
parent
f1591601e5
commit
52ad12538d
@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
|||||||
# Visual Studio Version 17
|
# Visual Studio Version 17
|
||||||
VisualStudioVersion = 17.4.33110.190
|
VisualStudioVersion = 17.4.33110.190
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinFormsLibrary", "WinFormsLibrary\WinFormsLibrary.csproj", "{FA2C000C-0815-44C4-BBA7-FB37C33DE121}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinFormsLibraryRazubaev", "WinFormsLibrary\WinFormsLibraryRazubaev.csproj", "{FA2C000C-0815-44C4-BBA7-FB37C33DE121}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinForms", "WinForms\WinForms.csproj", "{AA566691-FB51-4E4C-BB7B-9BA77AD20FD1}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinForms", "WinForms\WinForms.csproj", "{AA566691-FB51-4E4C-BB7B-9BA77AD20FD1}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
3
WinForms/FormComponents.Designer.cs
generated
3
WinForms/FormComponents.Designer.cs
generated
@ -49,6 +49,7 @@
|
|||||||
this.buttonWordText = new System.Windows.Forms.Button();
|
this.buttonWordText = new System.Windows.Forms.Button();
|
||||||
this.wordTextComponent = new WinFormsLibrary.WordText(this.components);
|
this.wordTextComponent = new WinFormsLibrary.WordText(this.components);
|
||||||
this.wordTableComponent = new WinFormsLibrary.NonVisualComponents.WordTable(this.components);
|
this.wordTableComponent = new WinFormsLibrary.NonVisualComponents.WordTable(this.components);
|
||||||
|
this.wordDiagramComponent = new WinFormsLibrary.NonVisualComponents.WordDiagram(this.components);
|
||||||
this.panel1.SuspendLayout();
|
this.panel1.SuspendLayout();
|
||||||
this.SuspendLayout();
|
this.SuspendLayout();
|
||||||
//
|
//
|
||||||
@ -202,6 +203,7 @@
|
|||||||
this.buttonWordDiagram.TabIndex = 15;
|
this.buttonWordDiagram.TabIndex = 15;
|
||||||
this.buttonWordDiagram.Text = "Word (диаграмма)";
|
this.buttonWordDiagram.Text = "Word (диаграмма)";
|
||||||
this.buttonWordDiagram.UseVisualStyleBackColor = true;
|
this.buttonWordDiagram.UseVisualStyleBackColor = true;
|
||||||
|
this.buttonWordDiagram.Click += new System.EventHandler(this.buttonWordDiagram_Click);
|
||||||
//
|
//
|
||||||
// buttonWordTable
|
// buttonWordTable
|
||||||
//
|
//
|
||||||
@ -275,5 +277,6 @@
|
|||||||
private Button buttonWordText;
|
private Button buttonWordText;
|
||||||
private WinFormsLibrary.WordText wordTextComponent;
|
private WinFormsLibrary.WordText wordTextComponent;
|
||||||
private WinFormsLibrary.NonVisualComponents.WordTable wordTableComponent;
|
private WinFormsLibrary.NonVisualComponents.WordTable wordTableComponent;
|
||||||
|
private WinFormsLibrary.NonVisualComponents.WordDiagram wordDiagramComponent;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,5 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using WinFormsLibrary;
|
using WinFormsLibrary;
|
||||||
|
using WinFormsLibrary.NonVisualComponents.Enums;
|
||||||
using WinFormsLibrary.NonVisualComponents.Helpers;
|
using WinFormsLibrary.NonVisualComponents.Helpers;
|
||||||
using WinFormsLibrary.Object;
|
using WinFormsLibrary.Object;
|
||||||
|
|
||||||
@ -165,5 +166,54 @@ namespace WinForms
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void buttonWordDiagram_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
using var dialog = new SaveFileDialog
|
||||||
|
{
|
||||||
|
Filter = "docx|*.docx"
|
||||||
|
};
|
||||||
|
if (dialog.ShowDialog() == DialogResult.OK)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var series = new List<SeriesParameters>
|
||||||
|
{
|
||||||
|
new() {
|
||||||
|
SeriesName = "Номер 1",
|
||||||
|
ValuesY = new List<double> { 14, 22, 13 },
|
||||||
|
Color = Color.FromArgb(255, 165, 0)
|
||||||
|
},
|
||||||
|
new() {
|
||||||
|
SeriesName = "Номер 2",
|
||||||
|
ValuesY = new List<double> { 87, 44, 51 },
|
||||||
|
Color = Color.FromArgb(145, 145, 145)
|
||||||
|
},
|
||||||
|
new() {
|
||||||
|
SeriesName = "Номер 3",
|
||||||
|
ValuesY = new List<double> { 29, 15, 7 },
|
||||||
|
Color = Color.FromArgb(255, 255, 0)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
wordDiagramComponent.CreateDiagramDocument(new GraphicWordInfo()
|
||||||
|
{
|
||||||
|
Path = dialog.FileName,
|
||||||
|
DocumentTitle = "Диаграмма в ворде",
|
||||||
|
DiagramTitle = "Ниже показана диаграмма об исследовании...",
|
||||||
|
LegendLayout = LegendLayoutEnum.Bottom,
|
||||||
|
SeriesX = new List<string> { "Пример", "Пример", "Пример" },
|
||||||
|
SeriesParameters = series
|
||||||
|
});
|
||||||
|
|
||||||
|
MessageBox.Show("Готово!");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
MessageBox.Show("Произошла ошибка: " + ex.Message, "А", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -63,4 +63,7 @@
|
|||||||
<metadata name="wordTableComponent.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
<metadata name="wordTableComponent.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||||
<value>156, 17</value>
|
<value>156, 17</value>
|
||||||
</metadata>
|
</metadata>
|
||||||
|
<metadata name="wordDiagramComponent.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||||
|
<value>392, 17</value>
|
||||||
|
</metadata>
|
||||||
</root>
|
</root>
|
@ -9,7 +9,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\WinFormsLibrary\WinFormsLibrary.csproj" />
|
<ProjectReference Include="..\WinFormsLibrary\WinFormsLibraryRazubaev.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
@ -0,0 +1,16 @@
|
|||||||
|
namespace WinFormsLibrary.NonVisualComponents.Enums
|
||||||
|
{
|
||||||
|
public enum LegendLayoutEnum
|
||||||
|
{
|
||||||
|
None,
|
||||||
|
|
||||||
|
Left,
|
||||||
|
|
||||||
|
Top,
|
||||||
|
|
||||||
|
Right,
|
||||||
|
|
||||||
|
Bottom
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
using WinFormsLibrary.NonVisualComponents.Enums;
|
||||||
|
|
||||||
|
namespace WinFormsLibrary.NonVisualComponents.Helpers
|
||||||
|
{
|
||||||
|
public class GraphicWordInfo
|
||||||
|
{
|
||||||
|
public string Path { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
public string DocumentTitle { get; set; } = string.Empty;
|
||||||
|
public string DiagramTitle { get; set; } = string.Empty;
|
||||||
|
public LegendLayoutEnum LegendLayout { get; set; }
|
||||||
|
public List<string> SeriesX { get; set; }
|
||||||
|
public List<SeriesParameters> SeriesParameters { get; set; } = new();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
namespace WinFormsLibrary.NonVisualComponents.Helpers
|
||||||
|
{
|
||||||
|
public class SeriesParameters
|
||||||
|
{
|
||||||
|
public string SeriesName { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
public List<double> ValuesY { get; set; } = new();
|
||||||
|
|
||||||
|
public Color Color { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
36
WinFormsLibrary/NonVisualComponents/WordDiagram.Designer.cs
generated
Normal file
36
WinFormsLibrary/NonVisualComponents/WordDiagram.Designer.cs
generated
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
namespace WinFormsLibrary.NonVisualComponents
|
||||||
|
{
|
||||||
|
partial class WordDiagram
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Обязательная переменная конструктора.
|
||||||
|
/// </summary>
|
||||||
|
private System.ComponentModel.IContainer components = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Освободить все используемые ресурсы.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
|
||||||
|
protected override void Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
if (disposing && (components != null))
|
||||||
|
{
|
||||||
|
components.Dispose();
|
||||||
|
}
|
||||||
|
base.Dispose(disposing);
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Код, автоматически созданный конструктором компонентов
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Требуемый метод для поддержки конструктора — не изменяйте
|
||||||
|
/// содержимое этого метода с помощью редактора кода.
|
||||||
|
/// </summary>
|
||||||
|
private void InitializeComponent()
|
||||||
|
{
|
||||||
|
components = new System.ComponentModel.Container();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
123
WinFormsLibrary/NonVisualComponents/WordDiagram.cs
Normal file
123
WinFormsLibrary/NonVisualComponents/WordDiagram.cs
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
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}' не совпадает");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -37,8 +37,4 @@
|
|||||||
</COMReference>
|
</COMReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="NonVisualComponents\Enums\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
Loading…
Reference in New Issue
Block a user