Готов компонент с диаграммой
This commit is contained in:
parent
cbc470fe0f
commit
e1d5ffe478
@ -15,7 +15,6 @@ namespace Components
|
||||
container.Add(this);
|
||||
}
|
||||
|
||||
// Перечисление для расположения легенды
|
||||
public enum LegendPosition
|
||||
{
|
||||
Top,
|
||||
@ -24,13 +23,52 @@ namespace Components
|
||||
Right
|
||||
}
|
||||
|
||||
// Класс для хранения данных серии
|
||||
public class ChartData
|
||||
{
|
||||
[DisplayName("Серия")]
|
||||
public string SeriesName { get; set; }
|
||||
[DisplayName("Значение")]
|
||||
public double SeriesValue { get; set; }
|
||||
}
|
||||
|
||||
private void CheckInputValues(
|
||||
string filePath,
|
||||
string fileTitle,
|
||||
string chartTitle,
|
||||
LegendPosition legendPosition,
|
||||
List<ChartData> data)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filePath))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(filePath));
|
||||
}
|
||||
if (string.IsNullOrEmpty(fileTitle))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(fileTitle));
|
||||
}
|
||||
if (string.IsNullOrEmpty(chartTitle))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(chartTitle));
|
||||
}
|
||||
double chartSum = 0;
|
||||
foreach (var item in data)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(item.SeriesName)) { chartSum += item.SeriesValue; }
|
||||
else
|
||||
{
|
||||
throw new ArgumentNullException("Series name is null or empty");
|
||||
}
|
||||
if (item.SeriesValue == 0 || item.SeriesValue > 100)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("Series value is out of range");
|
||||
}
|
||||
}
|
||||
if (chartSum > 100)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("Series values sum is out of range");
|
||||
}
|
||||
}
|
||||
|
||||
public void CreateExcelWithPieChart(
|
||||
string filePath,
|
||||
string fileTitle,
|
||||
@ -38,41 +76,35 @@ namespace Components
|
||||
LegendPosition legendPosition,
|
||||
List<ChartData> data)
|
||||
{
|
||||
CheckInputValues(filePath, fileTitle, chartTitle, legendPosition, data);
|
||||
|
||||
Excel.Application excelApp = new Excel.Application();
|
||||
Excel.Workbook workbook = excelApp.Workbooks.Add();
|
||||
Excel.Worksheet worksheet = workbook.Sheets[1];
|
||||
|
||||
// Устанавливаем заголовок файла
|
||||
worksheet.Cells[1, 1] = fileTitle;
|
||||
|
||||
// Вставляем данные в таблицу для построения диаграммы
|
||||
int dataStartRow = 3; // Начальная строка для данных
|
||||
int dataStartRow = 3;
|
||||
for (int i = 0; i < data.Count; i++)
|
||||
{
|
||||
worksheet.Cells[dataStartRow + i, 1] = data[i].SeriesName;
|
||||
worksheet.Cells[dataStartRow + i, 2] = data[i].SeriesValue;
|
||||
}
|
||||
|
||||
// Создание круговой диаграммы
|
||||
Excel.ChartObjects chartObjects = (Excel.ChartObjects)worksheet.ChartObjects();
|
||||
Excel.ChartObject chartObject = chartObjects.Add(300, 50, 400, 300); // Размер и позиция диаграммы
|
||||
Excel.ChartObject chartObject = chartObjects.Add(300, 50, 400, 300);
|
||||
Excel.Chart chart = chartObject.Chart;
|
||||
|
||||
// Устанавливаем тип диаграммы "Круговая"
|
||||
chart.ChartType = Excel.XlChartType.xlPie;
|
||||
|
||||
// Устанавливаем диапазон данных для диаграммы
|
||||
Excel.Range chartRange = worksheet.get_Range("A3", $"B{dataStartRow + data.Count - 1}");
|
||||
chart.SetSourceData(chartRange);
|
||||
|
||||
// Устанавливаем заголовок диаграммы
|
||||
chart.HasTitle = true;
|
||||
chart.ChartTitle.Text = chartTitle;
|
||||
|
||||
// Устанавливаем расположение легенды
|
||||
SetLegendPosition(chart, legendPosition);
|
||||
|
||||
// Сохраняем файл
|
||||
workbook.SaveAs(filePath);
|
||||
workbook.Close();
|
||||
excelApp.Quit();
|
||||
@ -82,7 +114,6 @@ namespace Components
|
||||
ReleaseObject(excelApp);
|
||||
}
|
||||
|
||||
// Метод для установки позиции легенды на диаграмме
|
||||
private void SetLegendPosition(Excel.Chart chart, LegendPosition legendPosition)
|
||||
{
|
||||
chart.HasLegend = true;
|
||||
@ -104,7 +135,6 @@ namespace Components
|
||||
}
|
||||
}
|
||||
|
||||
// Освобождение объектов
|
||||
private void ReleaseObject(object obj)
|
||||
{
|
||||
try
|
||||
|
133
WinFormSolution/WinFormsApp/FormNoVisual.Designer.cs
generated
133
WinFormSolution/WinFormsApp/FormNoVisual.Designer.cs
generated
@ -37,6 +37,18 @@
|
||||
buttonCreateExcelFile = new Button();
|
||||
buttonAddImage = new Button();
|
||||
buttonClearImages = new Button();
|
||||
componentExcelWithPieDiagram = new Components.ComponentExcelWithPieDiagram(components);
|
||||
dataGridViewSeries = new DataGridView();
|
||||
textBoxSeriesName = new TextBox();
|
||||
numericUpDownSeriesValue = new NumericUpDown();
|
||||
buttonAddSeries = new Button();
|
||||
buttonClearSeries = new Button();
|
||||
buttonCreateExcelWithPieDiagram = new Button();
|
||||
comboBoxLegendPosition = new ComboBox();
|
||||
labelLegendPosition = new Label();
|
||||
textBoxDiagramTitle = new TextBox();
|
||||
((System.ComponentModel.ISupportInitialize)dataGridViewSeries).BeginInit();
|
||||
((System.ComponentModel.ISupportInitialize)numericUpDownSeriesValue).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
// textBoxFilePath
|
||||
@ -69,24 +81,24 @@
|
||||
// listBoxImages
|
||||
//
|
||||
listBoxImages.FormattingEnabled = true;
|
||||
listBoxImages.Location = new Point(12, 78);
|
||||
listBoxImages.Location = new Point(12, 127);
|
||||
listBoxImages.Name = "listBoxImages";
|
||||
listBoxImages.Size = new Size(357, 104);
|
||||
listBoxImages.TabIndex = 3;
|
||||
//
|
||||
// buttonCreateExcelFile
|
||||
//
|
||||
buttonCreateExcelFile.Location = new Point(12, 223);
|
||||
buttonCreateExcelFile.Location = new Point(12, 272);
|
||||
buttonCreateExcelFile.Name = "buttonCreateExcelFile";
|
||||
buttonCreateExcelFile.Size = new Size(357, 29);
|
||||
buttonCreateExcelFile.TabIndex = 4;
|
||||
buttonCreateExcelFile.Text = "Создать Excel файл";
|
||||
buttonCreateExcelFile.Text = "Создать Excel файл с картинкой";
|
||||
buttonCreateExcelFile.UseVisualStyleBackColor = true;
|
||||
buttonCreateExcelFile.Click += buttonCreateExcelFile_Click;
|
||||
//
|
||||
// buttonAddImage
|
||||
//
|
||||
buttonAddImage.Location = new Point(12, 188);
|
||||
buttonAddImage.Location = new Point(12, 237);
|
||||
buttonAddImage.Name = "buttonAddImage";
|
||||
buttonAddImage.Size = new Size(257, 29);
|
||||
buttonAddImage.TabIndex = 5;
|
||||
@ -96,7 +108,7 @@
|
||||
//
|
||||
// buttonClearImages
|
||||
//
|
||||
buttonClearImages.Location = new Point(275, 188);
|
||||
buttonClearImages.Location = new Point(275, 237);
|
||||
buttonClearImages.Name = "buttonClearImages";
|
||||
buttonClearImages.Size = new Size(94, 29);
|
||||
buttonClearImages.TabIndex = 6;
|
||||
@ -104,11 +116,108 @@
|
||||
buttonClearImages.UseVisualStyleBackColor = true;
|
||||
buttonClearImages.Click += buttonClearImages_Click;
|
||||
//
|
||||
// dataGridViewSeries
|
||||
//
|
||||
dataGridViewSeries.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
|
||||
dataGridViewSeries.Enabled = false;
|
||||
dataGridViewSeries.Location = new Point(406, 10);
|
||||
dataGridViewSeries.MultiSelect = false;
|
||||
dataGridViewSeries.Name = "dataGridViewSeries";
|
||||
dataGridViewSeries.ReadOnly = true;
|
||||
dataGridViewSeries.RowHeadersVisible = false;
|
||||
dataGridViewSeries.RowHeadersWidth = 51;
|
||||
dataGridViewSeries.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
||||
dataGridViewSeries.Size = new Size(300, 115);
|
||||
dataGridViewSeries.TabIndex = 7;
|
||||
//
|
||||
// textBoxSeriesName
|
||||
//
|
||||
textBoxSeriesName.Location = new Point(406, 166);
|
||||
textBoxSeriesName.Name = "textBoxSeriesName";
|
||||
textBoxSeriesName.PlaceholderText = "Название серии";
|
||||
textBoxSeriesName.Size = new Size(200, 27);
|
||||
textBoxSeriesName.TabIndex = 8;
|
||||
//
|
||||
// numericUpDownSeriesValue
|
||||
//
|
||||
numericUpDownSeriesValue.DecimalPlaces = 2;
|
||||
numericUpDownSeriesValue.Increment = new decimal(new int[] { 1, 0, 0, 65536 });
|
||||
numericUpDownSeriesValue.Location = new Point(406, 199);
|
||||
numericUpDownSeriesValue.Name = "numericUpDownSeriesValue";
|
||||
numericUpDownSeriesValue.Size = new Size(125, 27);
|
||||
numericUpDownSeriesValue.TabIndex = 9;
|
||||
numericUpDownSeriesValue.TextAlign = HorizontalAlignment.Right;
|
||||
//
|
||||
// buttonAddSeries
|
||||
//
|
||||
buttonAddSeries.Location = new Point(612, 166);
|
||||
buttonAddSeries.Name = "buttonAddSeries";
|
||||
buttonAddSeries.Size = new Size(94, 29);
|
||||
buttonAddSeries.TabIndex = 10;
|
||||
buttonAddSeries.Text = "Добавить";
|
||||
buttonAddSeries.UseVisualStyleBackColor = true;
|
||||
buttonAddSeries.Click += buttonAddSeries_Click;
|
||||
//
|
||||
// buttonClearSeries
|
||||
//
|
||||
buttonClearSeries.Location = new Point(537, 199);
|
||||
buttonClearSeries.Name = "buttonClearSeries";
|
||||
buttonClearSeries.Size = new Size(169, 29);
|
||||
buttonClearSeries.TabIndex = 11;
|
||||
buttonClearSeries.Text = "Очистить данные";
|
||||
buttonClearSeries.UseVisualStyleBackColor = true;
|
||||
buttonClearSeries.Click += buttonClearSeries_Click;
|
||||
//
|
||||
// buttonCreateExcelWithPieDiagram
|
||||
//
|
||||
buttonCreateExcelWithPieDiagram.Location = new Point(406, 272);
|
||||
buttonCreateExcelWithPieDiagram.Name = "buttonCreateExcelWithPieDiagram";
|
||||
buttonCreateExcelWithPieDiagram.Size = new Size(300, 29);
|
||||
buttonCreateExcelWithPieDiagram.TabIndex = 12;
|
||||
buttonCreateExcelWithPieDiagram.Text = "Создать Excel файл с диаграммой";
|
||||
buttonCreateExcelWithPieDiagram.UseVisualStyleBackColor = true;
|
||||
buttonCreateExcelWithPieDiagram.Click += buttonCreateExcelWithPieDiagram_Click;
|
||||
//
|
||||
// comboBoxLegendPosition
|
||||
//
|
||||
comboBoxLegendPosition.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxLegendPosition.FormattingEnabled = true;
|
||||
comboBoxLegendPosition.Location = new Point(581, 234);
|
||||
comboBoxLegendPosition.Name = "comboBoxLegendPosition";
|
||||
comboBoxLegendPosition.Size = new Size(125, 28);
|
||||
comboBoxLegendPosition.TabIndex = 13;
|
||||
//
|
||||
// labelLegendPosition
|
||||
//
|
||||
labelLegendPosition.AutoSize = true;
|
||||
labelLegendPosition.Location = new Point(406, 237);
|
||||
labelLegendPosition.Name = "labelLegendPosition";
|
||||
labelLegendPosition.Size = new Size(177, 20);
|
||||
labelLegendPosition.TabIndex = 14;
|
||||
labelLegendPosition.Text = "Расположение легенды:";
|
||||
//
|
||||
// textBoxDiagramTitle
|
||||
//
|
||||
textBoxDiagramTitle.Location = new Point(406, 131);
|
||||
textBoxDiagramTitle.Name = "textBoxDiagramTitle";
|
||||
textBoxDiagramTitle.PlaceholderText = "Заголовок диаграммы";
|
||||
textBoxDiagramTitle.Size = new Size(300, 27);
|
||||
textBoxDiagramTitle.TabIndex = 15;
|
||||
//
|
||||
// FormNoVisual
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(800, 446);
|
||||
ClientSize = new Size(1054, 446);
|
||||
Controls.Add(textBoxDiagramTitle);
|
||||
Controls.Add(labelLegendPosition);
|
||||
Controls.Add(comboBoxLegendPosition);
|
||||
Controls.Add(buttonCreateExcelWithPieDiagram);
|
||||
Controls.Add(buttonClearSeries);
|
||||
Controls.Add(buttonAddSeries);
|
||||
Controls.Add(numericUpDownSeriesValue);
|
||||
Controls.Add(textBoxSeriesName);
|
||||
Controls.Add(dataGridViewSeries);
|
||||
Controls.Add(buttonClearImages);
|
||||
Controls.Add(buttonAddImage);
|
||||
Controls.Add(buttonCreateExcelFile);
|
||||
@ -118,6 +227,8 @@
|
||||
Controls.Add(textBoxFilePath);
|
||||
Name = "FormNoVisual";
|
||||
Text = "FormNoVisual";
|
||||
((System.ComponentModel.ISupportInitialize)dataGridViewSeries).EndInit();
|
||||
((System.ComponentModel.ISupportInitialize)numericUpDownSeriesValue).EndInit();
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
}
|
||||
@ -132,5 +243,15 @@
|
||||
private Button buttonCreateExcelFile;
|
||||
private Button buttonAddImage;
|
||||
private Button buttonClearImages;
|
||||
private Components.ComponentExcelWithPieDiagram componentExcelWithPieDiagram;
|
||||
private DataGridView dataGridViewSeries;
|
||||
private TextBox textBoxSeriesName;
|
||||
private NumericUpDown numericUpDownSeriesValue;
|
||||
private Button buttonAddSeries;
|
||||
private Button buttonClearSeries;
|
||||
private Button buttonCreateExcelWithPieDiagram;
|
||||
private ComboBox comboBoxLegendPosition;
|
||||
private Label labelLegendPosition;
|
||||
private TextBox textBoxDiagramTitle;
|
||||
}
|
||||
}
|
@ -1,10 +1,26 @@
|
||||
namespace WinFormsApp
|
||||
using Components;
|
||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
||||
using LegendPosition = Components.ComponentExcelWithPieDiagram.LegendPosition;
|
||||
|
||||
namespace WinFormsApp
|
||||
{
|
||||
public partial class FormNoVisual : Form
|
||||
{
|
||||
private List<ComponentExcelWithPieDiagram.ChartData> _chartData;
|
||||
private double _chartDataValuesSum
|
||||
{
|
||||
get
|
||||
{
|
||||
return _chartData.Sum(x => x.SeriesValue);
|
||||
}
|
||||
}
|
||||
|
||||
public FormNoVisual()
|
||||
{
|
||||
InitializeComponent();
|
||||
_chartData = new List<ComponentExcelWithPieDiagram.ChartData>();
|
||||
comboBoxLegendPosition.DataSource = Enum.GetValues(typeof(LegendPosition));
|
||||
UpdateNumericUpDownSeriesValueMaximumValue();
|
||||
}
|
||||
|
||||
private void buttonCreateExcelFile_Click(object sender, EventArgs e)
|
||||
@ -79,5 +95,53 @@
|
||||
{
|
||||
listBoxImages.Items.Clear();
|
||||
}
|
||||
|
||||
private void buttonAddSeries_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (string.IsNullOrEmpty(textBoxSeriesName.Text) || numericUpDownSeriesValue.Value == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
ComponentExcelWithPieDiagram.ChartData chartData = new();
|
||||
chartData.SeriesName = textBoxSeriesName.Text;
|
||||
chartData.SeriesValue = (double)numericUpDownSeriesValue.Value;
|
||||
_chartData.Add(chartData);
|
||||
textBoxSeriesName.Text = string.Empty;
|
||||
UpdateDataGridViewChartData();
|
||||
UpdateNumericUpDownSeriesValueMaximumValue();
|
||||
}
|
||||
|
||||
private void buttonClearSeries_Click(object sender, EventArgs e)
|
||||
{
|
||||
_chartData.Clear();
|
||||
dataGridViewSeries.DataSource = null;
|
||||
}
|
||||
|
||||
private void buttonCreateExcelWithPieDiagram_Click(object sender, EventArgs e)
|
||||
{
|
||||
LegendPosition legendPosition;
|
||||
Enum.TryParse<LegendPosition>(comboBoxLegendPosition.SelectedValue.ToString(), out legendPosition);
|
||||
try
|
||||
{
|
||||
componentExcelWithPieDiagram.CreateExcelWithPieChart(textBoxFilePath.Text, textBoxTitle.Text, textBoxDiagramTitle.Text, legendPosition, _chartData);
|
||||
MessageBox.Show("Файл успешно создан", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"Ошибка при создании файла:\n{ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateDataGridViewChartData()
|
||||
{
|
||||
dataGridViewSeries.DataSource = null;
|
||||
dataGridViewSeries.DataSource = _chartData;
|
||||
}
|
||||
|
||||
private void UpdateNumericUpDownSeriesValueMaximumValue()
|
||||
{
|
||||
numericUpDownSeriesValue.Maximum = (decimal)(100 - _chartDataValuesSum);
|
||||
numericUpDownSeriesValue.Value = numericUpDownSeriesValue.Maximum;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -120,4 +120,7 @@
|
||||
<metadata name="componentExcelWithImage.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>17, 17</value>
|
||||
</metadata>
|
||||
<metadata name="componentExcelWithPieDiagram.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>256, 17</value>
|
||||
</metadata>
|
||||
</root>
|
Loading…
Reference in New Issue
Block a user