-
Notifications
You must be signed in to change notification settings - Fork 4
/
Charts.vb
325 lines (256 loc) · 10.4 KB
/
Charts.vb
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
Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Globalization
Imports DevExpress.Spreadsheet
Imports DevExpress.Spreadsheet.Charts
Imports DevExpress.Spreadsheet.Drawings
Imports DevExpress.Utils
Imports System.Windows.Forms
Namespace SpreadsheetChartAPIActions
Public NotInheritable Class Charts
Private Sub New()
End Sub
Private Shared Sub PieChart(ByVal workbook As IWorkbook)
' #Region "#PieChart"
Dim worksheet As Worksheet = workbook.Worksheets("chartTask1")
workbook.Worksheets.ActiveWorksheet = worksheet
' Create a chart and specify its location
Dim chart As 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
Dim dataLabels As DataLabelOptions = chart.Views(0).DataLabels
dataLabels.ShowCategoryName = True
dataLabels.ShowPercent = True
dataLabels.Separator = Constants.vbLf
' #End Region ' #PieChart
End Sub
Private Shared Sub BarChart(ByVal workbook As IWorkbook)
' #Region "#BarChart"
Dim worksheet As Worksheet = workbook.Worksheets("chartTask2")
workbook.Worksheets.ActiveWorksheet = worksheet
' Create a chart and specify its location
Dim chart As 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
' #End Region ' #BarChart
End Sub
Private Shared Sub ColumnChart(ByVal workbook As IWorkbook)
' #Region "#ColumnChart"
Dim worksheet As Worksheet = workbook.Worksheets("chartTask3")
workbook.Worksheets.ActiveWorksheet = worksheet
' Create a chart and specify its location
Dim chart As 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
Dim axis As 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
Dim view As ChartView = 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
' #End Region ' #ColumnChart
End Sub
Private Shared Sub ComplexChart(ByVal workbook As IWorkbook)
' #Region "#ComplexChart"
Dim worksheet As Worksheet = workbook.Worksheets("chartTask5")
workbook.Worksheets.ActiveWorksheet = worksheet
' Create a chart and specify its location
Dim chart As 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
' #End Region ' #ComplexChart
End Sub
Private Shared Sub DoughnutChart(ByVal workbook As IWorkbook)
' #Region "#DoughnutChart"
Dim worksheet As Worksheet = workbook.Worksheets("chartTask3")
workbook.Worksheets.ActiveWorksheet = worksheet
' Create a chart and specify its location
Dim chart As 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
' #End Region ' #DoughnutChart
End Sub
Private Shared Sub Pie3dChart(ByVal workbook As IWorkbook)
' #Region "#Pie3dChart"
Dim worksheet As Worksheet = workbook.Worksheets("chartTask3")
workbook.Worksheets.ActiveWorksheet = worksheet
' Create a chart and specify its location
Dim chart As 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
' #End Region ' #Pie3dChart
End Sub
Private Shared Sub ScatterChart(ByVal workbook As IWorkbook)
' #Region "#ScatterChart"
Dim worksheet As Worksheet = workbook.Worksheets("chartScatter")
workbook.Worksheets.ActiveWorksheet = worksheet
' Create a chart and specify its location
Dim chart As 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
Dim axis As 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
' #End Region ' #ScatterChart
End Sub
Private Shared Sub StockChart(ByVal workbook As IWorkbook)
' #Region "#StockChart"
Dim worksheet As Worksheet = workbook.Worksheets("chartStock")
workbook.Worksheets.ActiveWorksheet = worksheet
' Create a chart and specify its location
Dim chart As 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
Dim axis As 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")
' #End Region ' #StockChart
End Sub
Private Shared Sub BubbleChart(ByVal workbook As IWorkbook)
' #Region "#BubbleChart"
Dim worksheet As Worksheet = workbook.Worksheets("chartBubble")
workbook.Worksheets.ActiveWorksheet = worksheet
' Create a chart and specify its location
Dim chart As Chart = worksheet.Charts.Add(ChartType.Bubble3D)
chart.TopLeftCell = worksheet.Cells("F2")
chart.BottomRightCell = worksheet.Cells("L15")
Dim s1 As Series = chart.Series.Add(worksheet("A3"), worksheet("C3:C7"), worksheet("D3:D7"))
s1.BubbleSize = ChartData.FromRange(worksheet("E3:E7"))
Dim s2 As Series = 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
Dim dataLabels As DataLabelOptions = chart.Views(0).DataLabels
dataLabels.ShowBubbleSize = True
' Set the minimum and maximum values for the chart value axis.
Dim axis As Axis = chart.PrimaryAxes(1)
axis.Scaling.AutoMax = False
axis.Scaling.Max = 82
axis.Scaling.AutoMin = False
axis.Scaling.Min = 64
' #End Region ' #BubbleChart
End Sub
Private Shared Sub ChangeChartType(ByVal workbook As IWorkbook)
' #Region "#ChangeChartType"
Dim worksheet As 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;
Dim type1 As ChartType = ChartType.LineMarker
Dim type2 As ChartType = ChartType.ColumnClustered
' Create a chart and specify its location
Dim chart As 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 e As Exception
MessageBox.Show(e.Message, "Incompatible chart type")
chart.ChangeType(type2)
End Try
' #End Region ' #ChangeChartType
End Sub
End Class
End Namespace