forked from yaunqiying/EasyCharts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsaveQualityChartImage.cs
146 lines (124 loc) · 5.18 KB
/
saveQualityChartImage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OfficeOpenXml.Drawing;
using OfficeOpenXml.Drawing.Chart;
using System.Drawing.Imaging;
using System.Windows.Forms.DataVisualization.Charting;
using System.Windows.Forms;
using OfficeOpenXml.Style;
using OfficeOpenXml;
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelAddIn_Graphics
{
class saveQualityChartImage
{
//Chart theChart = (Chart) Globals.ThisAddIn.Application.ActiveChart;
//ExcelWorksheet worksheet = null;
//ExcelChartSerie chartSerie = null;
Chart theChart;
System.Drawing.Font oldFont1 = new System.Drawing.Font("Trebuchet MS", 35F, System.Drawing.FontStyle.Bold);
System.Drawing.Font oldFont2 = new System.Drawing.Font("Trebuchet MS", 15F, System.Drawing.FontStyle.Bold);
System.Drawing.Font oldFont3 = new System.Drawing.Font("Trebuchet MS", 35F, System.Drawing.FontStyle.Bold);
System.Drawing.Font oldLegendFont = new System.Drawing.Font("Trebuchet MS", 35F, System.Drawing.FontStyle.Bold);
int oldLineWidth1;
int oldLineWidth2;
int oldLineWidth3;
int oldLineWidth4;
int oldWidth;
int oldHeight;
public saveQualityChartImage(Chart inputChart)
{
if (!(inputChart.Series.Count > 0))
{
return;
}
theChart = inputChart;
if (inputChart.Titles.Count > 0)
{
oldFont1 = inputChart.Titles[0].Font;
}
oldFont2 = inputChart.ChartAreas[0].AxisX.LabelStyle.Font;
oldFont3 = inputChart.ChartAreas[0].AxisX.TitleFont;
if (theChart.Legends.Count > 0)
{
oldLegendFont = theChart.Legends["Legend"].Font;
}
oldLineWidth1 = theChart.ChartAreas[0].AxisX.LineWidth;
oldLineWidth2 = theChart.ChartAreas[0].AxisX.MajorTickMark.LineWidth;
oldLineWidth3 = theChart.Series[0].BorderWidth;
oldLineWidth4 = theChart.ChartAreas[0].AxisY.MajorGrid.LineWidth;
oldWidth = theChart.Width;
oldHeight = theChart.Height;
saveimage();
}
public void saveimage()
{
theChart.Visible = false;
System.Drawing.Font chtFont = new System.Drawing.Font("Trebuchet MS", 35F, System.Drawing.FontStyle.Bold);
System.Drawing.Font smallFont = new System.Drawing.Font("Trebuchet MS", 15F, System.Drawing.FontStyle.Bold);
if (theChart.Titles.Count > 0)
{
theChart.Titles[0].Font = chtFont;
}
theChart.ChartAreas[0].AxisX.TitleFont = chtFont;
theChart.ChartAreas[0].AxisX.LineWidth = 3;
theChart.ChartAreas[0].AxisX.MajorGrid.LineWidth = 3;
theChart.ChartAreas[0].AxisX.LabelStyle.Font = smallFont;
theChart.ChartAreas[0].AxisX.MajorTickMark.LineWidth = 3;
theChart.ChartAreas[0].AxisY.TitleFont = chtFont;
theChart.ChartAreas[0].AxisY.LineWidth = 3;
theChart.ChartAreas[0].AxisY.MajorGrid.LineWidth = 3;
theChart.ChartAreas[0].AxisY.LabelStyle.Font = smallFont;
theChart.ChartAreas[0].AxisY.MajorTickMark.LineWidth = 3;
if (theChart.Legends.Count > 0)
{
theChart.Legends["Legend"].Font = smallFont;
}
foreach (Series series in theChart.Series)
{
series.BorderWidth = 3;
}
theChart.Width = 1800;
theChart.Height = 1200;
SaveFileDialog save = new SaveFileDialog();
save.DefaultExt = ".png";
if (save.ShowDialog() == DialogResult.OK)
{
theChart.SaveImage(save.FileName, ChartImageFormat.Png);
}
resetOldValues();
}
private void resetOldValues()
{
if (theChart.Titles.Count > 0)
{
theChart.Titles[0].Font = oldFont1;
}
theChart.ChartAreas[0].AxisX.TitleFont = oldFont3;
theChart.ChartAreas[0].AxisX.LineWidth = oldLineWidth1;
theChart.ChartAreas[0].AxisX.MajorGrid.LineWidth = oldLineWidth4;
theChart.ChartAreas[0].AxisX.LabelStyle.Font = oldFont2;
theChart.ChartAreas[0].AxisX.MajorTickMark.LineWidth = oldLineWidth2;
theChart.ChartAreas[0].AxisY.TitleFont = oldFont3;
theChart.ChartAreas[0].AxisY.LineWidth = oldLineWidth1;
theChart.ChartAreas[0].AxisY.MajorGrid.LineWidth = oldLineWidth4;
theChart.ChartAreas[0].AxisY.LabelStyle.Font = oldFont2;
theChart.ChartAreas[0].AxisY.MajorTickMark.LineWidth = oldLineWidth2;
if (theChart.Legends.Count > 0)
{
theChart.Legends["Legend"].Font = oldLegendFont;
}
foreach (Series series in theChart.Series)
{
series.BorderWidth = oldLineWidth3;
}
theChart.Width = oldWidth;
theChart.Height = oldHeight;
theChart.Visible = true;
}
}
}