-
Notifications
You must be signed in to change notification settings - Fork 4
/
Charts.cs
327 lines (258 loc) · 12.1 KB
/
Charts.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
using System;
using System.Drawing;
using System.Globalization;
using DevExpress.Spreadsheet;
using DevExpress.Spreadsheet.Charts;
using DevExpress.Spreadsheet.Drawings;
using DevExpress.Utils;
using System.Windows.Forms;
namespace SpreadsheetChartAPIActions {
public static class Charts {
static void PieChart(IWorkbook workbook) {
#region #PieChart
Worksheet worksheet = workbook.Worksheets["chartTask1"];
workbook.Worksheets.ActiveWorksheet = worksheet;
// Create a chart and specify its location
Chart chart = worksheet.Charts.Add(ChartType.PieExploded, worksheet["B2:C7"]);
chart.TopLeftCell = worksheet.Cells["E2"];
chart.BottomRightCell = worksheet.Cells["K15"];
// Set the chart style
chart.Style = ChartStyle.ColorGradient;
// Hide the legend
chart.Legend.Visible = false;
// Rotate the pie chart view
chart.Views[0].FirstSliceAngle = 100;
// Display data labels
DataLabelOptions dataLabels = chart.Views[0].DataLabels;
dataLabels.ShowCategoryName = true;
dataLabels.ShowPercent = true;
dataLabels.Separator = "\n";
#endregion #PieChart
}
static void BarChart(IWorkbook workbook) {
#region #BarChart
Worksheet worksheet = workbook.Worksheets["chartTask2"];
workbook.Worksheets.ActiveWorksheet = worksheet;
// Create a chart and specify its location
Chart chart = worksheet.Charts.Add(ChartType.BarFullStacked);
chart.TopLeftCell = worksheet.Cells["E3"];
chart.BottomRightCell = worksheet.Cells["K14"];
// Select chart data
chart.SelectData(worksheet["B3:C8"], ChartDataDirection.Row);
// Display the chart title
chart.Title.Visible = true;
chart.Title.SetReference(worksheet["B1"]);
// Change legend position
chart.Legend.Position = LegendPosition.Bottom;
// Hide the category axis
chart.PrimaryAxes[0].Visible = false;
// Set major unit of the value axis
chart.PrimaryAxes[1].MajorUnit = 0.2;
#endregion #BarChart
}
static void ColumnChart(IWorkbook workbook) {
#region #ColumnChart
Worksheet worksheet = workbook.Worksheets["chartTask3"];
workbook.Worksheets.ActiveWorksheet = worksheet;
// Create a chart and specify its location
Chart chart = worksheet.Charts.Add(ChartType.ColumnClustered);
chart.TopLeftCell = worksheet.Cells["H2"];
chart.BottomRightCell = worksheet.Cells["N14"];
// Add series
chart.Series.Add(worksheet["D2"], worksheet["B3:B6"], worksheet["D3:D6"]);
chart.Series.Add(worksheet["F2"], worksheet["B3:B6"], worksheet["F3:F6"]);
// Display the chart title
chart.Title.Visible = true;
chart.Title.SetValue("Mobile OS market share");
// Customize the appearance and scale of the axes
Axis axis = chart.PrimaryAxes[0];
axis.MajorTickMarks = AxisTickMarks.None;
axis = chart.PrimaryAxes[1];
axis.Outline.SetNoFill();
axis.MajorTickMarks = AxisTickMarks.None;
axis.NumberFormat.FormatCode = "0%";
axis.NumberFormat.IsSourceLinked = false;
axis.Scaling.AutoMax = false;
axis.Scaling.Max = 1;
axis.Scaling.AutoMin = false;
axis.Scaling.Min = 0;
// Set the gap width between data series
ChartView view = chart.Views[0];
view.GapWidth = 75;
// Display data labels
view.DataLabels.ShowValue = true;
view.DataLabels.NumberFormat.FormatCode = "0%";
view.DataLabels.NumberFormat.IsSourceLinked = false;
// Set the chart style
chart.Style = ChartStyle.ColorGradient;
#endregion #ColumnChart
}
static void ComplexChart(IWorkbook workbook) {
#region #ComplexChart
Worksheet worksheet = workbook.Worksheets["chartTask5"];
workbook.Worksheets.ActiveWorksheet = worksheet;
// Create a chart and specify its location
Chart chart = worksheet.Charts.Add(ChartType.ColumnClustered, worksheet["B2:D8"]);
chart.TopLeftCell = worksheet.Cells["F2"];
chart.BottomRightCell = worksheet.Cells["L15"];
// Change the chart type of the second series
chart.Series[1].ChangeType(ChartType.Line);
chart.Series[1].Smooth = true;
// Use secondary axes
chart.Series[1].AxisGroup = AxisGroup.Secondary;
// Specify the chart style
chart.Style = ChartStyle.ColorGradient;
// Set the position of the legend
chart.Legend.Position = LegendPosition.Bottom;
#endregion #ComplexChart
}
static void DoughnutChart(IWorkbook workbook) {
#region #DoughnutChart
Worksheet worksheet = workbook.Worksheets["chartTask3"];
workbook.Worksheets.ActiveWorksheet = worksheet;
// Create a chart and specify its location
Chart chart = worksheet.Charts.Add(ChartType.Doughnut);
chart.TopLeftCell = worksheet.Cells["H2"];
chart.BottomRightCell = worksheet.Cells["N14"];
// Add the data series
chart.Series.Add(worksheet["E2"], worksheet["B3:B6"], worksheet["E3:E6"]);
// Display the chart title
chart.Title.Visible = true;
chart.Title.SetValue("Mobile OS market share Q4'13");
// Change the hole size
chart.Views[0].HoleSize = 60;
// Display the data labels
chart.Views[0].DataLabels.ShowPercent = true;
#endregion #DoughnutChart
}
static void Pie3dChart(IWorkbook workbook) {
#region #Pie3dChart
Worksheet worksheet = workbook.Worksheets["chartTask3"];
workbook.Worksheets.ActiveWorksheet = worksheet;
// Create a chart and specify its location
Chart chart = worksheet.Charts.Add(ChartType.Pie3D);
chart.TopLeftCell = worksheet.Cells["H2"];
chart.BottomRightCell = worksheet.Cells["N14"];
// Add the data series
chart.Series.Add(worksheet["E2"], worksheet["B3:B6"], worksheet["E3:E6"]);
// Set the explosion value for the slice
chart.Series[0].CustomDataPoints.Add(2).Explosion = 25;
// Set the rotation of the 3-D chart view
chart.View3D.YRotation = 255;
// Set the chart style
chart.Style = ChartStyle.ColorGradient;
#endregion #Pie3dChart
}
static void ScatterChart(IWorkbook workbook) {
#region #ScatterChart
Worksheet worksheet = workbook.Worksheets["chartScatter"];
workbook.Worksheets.ActiveWorksheet = worksheet;
// Create a chart and specify its location
Chart chart = worksheet.Charts.Add(ChartType.ScatterLineMarkers, worksheet["C2:D52"]);
chart.TopLeftCell = worksheet.Cells["F2"];
chart.BottomRightCell = worksheet.Cells["L15"];
// Set the marker symbol
chart.Series[0].Marker.Symbol = MarkerStyle.Circle;
// Set appearance and scale of the X axis
Axis axis = chart.PrimaryAxes[0];
axis.Scaling.AutoMax = false;
axis.Scaling.AutoMin = false;
axis.Scaling.Max = 60.0;
axis.Scaling.Min = -60.0;
axis.MajorGridlines.Visible = true;
// Set appearance and scale of the Y axis
axis = chart.PrimaryAxes[1];
axis.Scaling.AutoMax = false;
axis.Scaling.AutoMin = false;
axis.Scaling.Max = 50.0;
axis.Scaling.Min = -50.0;
axis.MajorUnit = 10.0;
#endregion #ScatterChart
}
static void StockChart(IWorkbook workbook) {
#region #StockChart
Worksheet worksheet = workbook.Worksheets["chartStock"];
workbook.Worksheets.ActiveWorksheet = worksheet;
// Create a chart and specify its location
Chart chart = worksheet.Charts.Add(ChartType.StockOpenHighLowClose, worksheet["B2:F7"]);
chart.TopLeftCell = worksheet.Cells["H2"];
chart.BottomRightCell = worksheet.Cells["N15"];
// Display the chart title
chart.Title.Visible = true;
chart.Title.SetValue("NASDAQ:MSFT");
// Hide the legend
chart.Legend.Visible = false;
// Set appearance and scale of the value axis
Axis axis = chart.PrimaryAxes[1];
axis.Scaling.AutoMax = false;
axis.Scaling.Max = 40.5;
axis.Scaling.AutoMin = false;
axis.Scaling.Min = 38.5;
axis.MajorUnit = 0.25;
// Format the axis labels
axis.NumberFormat.FormatCode = "#0.00";
axis.NumberFormat.IsSourceLinked = false;
// Display the axis title
axis.Title.Visible = true;
axis.Title.SetValue("Price in USD");
#endregion #StockChart
}
static void BubbleChart(IWorkbook workbook)
{
#region #BubbleChart
Worksheet worksheet = workbook.Worksheets["chartBubble"];
workbook.Worksheets.ActiveWorksheet = worksheet;
// Create a chart and specify its location
Chart chart = worksheet.Charts.Add(ChartType.Bubble3D);
chart.TopLeftCell = worksheet.Cells["F2"];
chart.BottomRightCell = worksheet.Cells["L15"];
Series s1 = chart.Series.Add(worksheet["A3"], worksheet["C3:C7"], worksheet["D3:D7"]);
s1.BubbleSize = ChartData.FromRange(worksheet["E3:E7"]);
Series s2 = chart.Series.Add(worksheet["A9"], worksheet["C9:C13"], worksheet["D9:D13"]);
s2.BubbleSize = ChartData.FromRange(worksheet["E9:E13"]);
// Set the chart style
chart.Style = ChartStyle.ColorGradient;
// Set the bubble size 1.5x relative to the default setting.
chart.Views[0].BubbleScale = 150;
// Hide the legend
chart.Legend.Visible = false;
// Display data labels
DataLabelOptions dataLabels = chart.Views[0].DataLabels;
dataLabels.ShowBubbleSize = true;
// Set the minimum and maximum values for the chart value axis.
Axis axis = chart.PrimaryAxes[1];
axis.Scaling.AutoMax = false;
axis.Scaling.Max = 82;
axis.Scaling.AutoMin = false;
axis.Scaling.Min = 64;
#endregion #BubbleChart
}
static void ChangeChartType(IWorkbook workbook)
{
#region #ChangeChartType
Worksheet worksheet = workbook.Worksheets["chartTask1"];
workbook.Worksheets.ActiveWorksheet = worksheet;
// If a new chart type cannot be created with existing data, an exception is thrown.
//ChartType type1 = ChartType.StockHighLowClose;
ChartType type1 = ChartType.LineMarker;
ChartType type2 = ChartType.ColumnClustered;
// Create a chart and specify its location
Chart chart = worksheet.Charts.Add(ChartType.PieExploded, worksheet["B2:C7"]);
chart.TopLeftCell = worksheet.Cells["E2"];
chart.BottomRightCell = worksheet.Cells["K15"];
// Hide the legend.
chart.Legend.Visible = false;
// Change the chart type.
try
{
chart.ChangeType(type1);
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Incompatible chart type");
chart.ChangeType(type2);
}
#endregion #ChangeChartType
}
}
}