-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChartImage.vb
205 lines (171 loc) · 5.33 KB
/
ChartImage.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
Imports DevExpress.XtraRichEdit.API.Native
Imports System.Xml
Imports System.Text
Imports DevExpress.XtraRichEdit.Utils
Imports System.Drawing.Imaging
Imports DevExpress.XtraCharts
Imports System.IO
Imports DevExpress.XtraRichEdit.Model
Imports DevExpress.XtraRichEdit.Fields
Imports System.Linq
Imports System.Collections.Generic
Imports System
Imports DevExpress.Utils
Namespace RichEdit_InsertChart
Public Class ChartImage
Private _Input As String
Private _ViewType As DevExpress.XtraCharts.ViewType = DevExpress.XtraCharts.ViewType.Pie
Private _Width? As Integer = Nothing
Private _Height? As Integer = Nothing
Private _ShowLegend As DefaultBoolean = DefaultBoolean.False
Private _Data As New List(Of KeyValuePair(Of String, Double))()
Public ReadOnly Property ViewType() As DevExpress.XtraCharts.ViewType
Get
Return Me._ViewType
End Get
End Property
Public ReadOnly Property Width() As Integer?
Get
Return Me._Width
End Get
End Property
Public ReadOnly Property Height() As Integer?
Get
Return Me._Height
End Get
End Property
Public ReadOnly Property ShowLegend() As DefaultBoolean
Get
Return Me._ShowLegend
End Get
End Property
Public ReadOnly Property Data() As List(Of KeyValuePair(Of String, Double))
Get
Return _Data
End Get
End Property
Public Sub New(ByVal input As String)
Me._Input = input
End Sub
Public Sub Initialize()
Using stringReader As New StringReader(_Input)
Using reader As New XmlTextReader(stringReader)
reader.DtdProcessing = DtdProcessing.Ignore
Do While reader.Read()
If reader.IsStartElement() Then
Select Case reader.Name
Case "type"
GetChartType(reader.ReadString())
Case "height"
_Height = New Integer?(Integer.Parse(reader.ReadString()))
Case "width"
_Width = New Integer?(Integer.Parse(reader.ReadString()))
Case "legend"
_ShowLegend = If((reader.ReadString().ToLower() = "true"), DefaultBoolean.True, DefaultBoolean.False)
Case "data"
GetData(reader.ReadString())
End Select
End If
Loop
End Using
End Using
End Sub
Private Sub GetData(ByVal p As String)
For Each t As String In p.Split(","c)
Dim value As Double
Dim pair() As String = t.Split("|"c)
If pair.Length = 0 Then
Continue For
End If
If pair.Length = 1 Then
If Not Double.TryParse(pair(0), value) Then
_Data.Add(New KeyValuePair(Of String, Double)(pair(0), 0))
Else
_Data.Add(New KeyValuePair(Of String, Double)(String.Empty, value))
End If
Else
If pair.Length = 2 Then
If Not Double.TryParse(pair(1), value) Then
_Data.Add(New KeyValuePair(Of String, Double)(pair(0), 0))
Else
_Data.Add(New KeyValuePair(Of String, Double)(pair(0), value))
End If
End If
End If
Next t
End Sub
Private Sub GetChartType(ByVal p As String)
Select Case p.ToLower()
Case "bar"
_ViewType = DevExpress.XtraCharts.ViewType.Bar
Case "line"
_ViewType = DevExpress.XtraCharts.ViewType.Line
Case "pie3d"
_ViewType = DevExpress.XtraCharts.ViewType.Pie3D
Case Else
_ViewType = DevExpress.XtraCharts.ViewType.Pie
End Select
End Sub
Public Function CreateChart() As Stream
Using chart As New ChartControl()
If _Width.HasValue Then
chart.Width = _Width.Value
End If
If _Height.HasValue Then
chart.Height = _Height.Value
End If
Dim undefined As Integer = 1
Dim stream As New MemoryStream()
Try
Dim series As New Series("Chart", _ViewType)
Try
If TypeOf series.Label Is DevExpress.XtraCharts.PieSeriesLabel Then
CType(series.Label, DevExpress.XtraCharts.PieSeriesLabel).Position = PieSeriesLabelPosition.Inside
End If
If _ViewType = ViewType.Pie Then
series.Label.TextPattern = "{A}: {VP:P0}"
End If
If _Data Is Nothing OrElse _Data.Count = 0 Then
series.Points.Add(New SeriesPoint("Undefined", New Double() {1}))
series.Label.TextPattern = "{S}"
Else
series.Label.TextPattern = "{A}: {V:F1}"
For i As Integer = 0 To _Data.Count - 1
Dim argument As String = _Data(i).Key.Trim()
If String.IsNullOrEmpty(argument) Then
argument = "Undefined " & undefined
undefined += 1
End If
series.Points.Add(New SeriesPoint(argument, New Double() {_Data(i).Value}))
Next i
End If
chart.Legend.Visibility = _ShowLegend
chart.BorderOptions.Visibility = DevExpress.Utils.DefaultBoolean.False
chart.Series.AddRange(New Series() {series})
series = Nothing
Dim diagram As XYDiagram = TryCast(chart.Diagram, XYDiagram)
If diagram IsNot Nothing AndAlso diagram.AxisX IsNot Nothing Then
diagram.AxisX.Label.ResolveOverlappingOptions.AllowStagger = True
End If
chart.ExportToImage(stream, ImageFormat.Png)
stream.Position = 0
Return stream
Catch
If series IsNot Nothing Then
series.Dispose()
End If
Throw
End Try
Catch
If stream IsNot Nothing Then
stream.Dispose()
End If
Throw
End Try
End Using
End Function
Public Function CreateImage() As DocumentImageSource
Return DocumentImageSource.FromStream(CreateChart())
End Function
End Class
End Namespace