using Components; using static System.Windows.Forms.VisualStyles.VisualStyleElement; using LegendPosition = Components.ComponentExcelWithPieDiagram.LegendPosition; namespace WinFormsApp { public partial class FormNoVisual : Form { private List _chartData; private double _chartDataValuesSum { get { return _chartData.Sum(x => x.SeriesValue); } } public FormNoVisual() { InitializeComponent(); _chartData = new List(); comboBoxLegendPosition.DataSource = Enum.GetValues(typeof(LegendPosition)); UpdateNumericUpDownSeriesValueMaximumValue(); } private void buttonCreateExcelFile_Click(object sender, EventArgs e) { string[] list = new string[listBoxImages.Items.Count]; for (int i = 0; i < listBoxImages.Items.Count; i++) { list[i] = listBoxImages.Items[i].ToString(); } try { componentExcelWithImage.CreateExcelWithImages(textBoxFilePath.Text, textBoxTitle.Text, list); MessageBox.Show("Файл успешно создан", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show($"Ошибка при создании файла:\n{ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void buttonSetFilePath_Click(object sender, EventArgs e) { var filePath = string.Empty; using (SaveFileDialog saveFileDialog = new SaveFileDialog()) { saveFileDialog.InitialDirectory = "d:\\tmp"; saveFileDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"; saveFileDialog.FilterIndex = 1; saveFileDialog.RestoreDirectory = true; if (saveFileDialog.ShowDialog() == DialogResult.OK) { filePath = saveFileDialog.FileName; } } if (!string.IsNullOrEmpty(filePath)) { textBoxFilePath.Text = filePath; } else { textBoxFilePath.Text = string.Empty; } } private void buttonAddImage_Click(object sender, EventArgs e) { var filePath = string.Empty; using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.InitialDirectory = "d:\\tmp"; openFileDialog.Filter = "Image files (*.jpeg;*.jpg;*.png)|*.jpeg;*.jpg;*.png|All files (*.*)|*.*"; openFileDialog.FilterIndex = 1; openFileDialog.RestoreDirectory = true; if (openFileDialog.ShowDialog() == DialogResult.OK) { filePath = openFileDialog.FileName; } } if (!string.IsNullOrEmpty(filePath)) { listBoxImages.Items.Add(filePath); } } private void buttonClearImages_Click(object sender, EventArgs e) { 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(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; } } }