-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChartImage.cs
198 lines (166 loc) · 7.25 KB
/
ChartImage.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
namespace RichEdit_InsertChart{
using System;
using System.Collections.Generic;
using System.Linq;
using DevExpress.XtraRichEdit.Fields;
using DevExpress.XtraRichEdit.Model;
using System.IO;
using DevExpress.XtraCharts;
using System.Drawing.Imaging;
using DevExpress.XtraRichEdit.Utils;
using System.Text;
using System.Xml;
using DevExpress.XtraRichEdit.API.Native;
public class ChartImage {
string _Input;
DevExpress.XtraCharts.ViewType _ViewType = DevExpress.XtraCharts.ViewType.Pie;
Nullable<int> _Width = null;
Nullable<int> _Height = null;
DevExpress.Utils.DefaultBoolean _ShowLegend = DevExpress.Utils.DefaultBoolean.False;
List<KeyValuePair<string, double>> _Data = new List<KeyValuePair<string, double>>();
public DevExpress.XtraCharts.ViewType ViewType { get { return this._ViewType; } }
public Nullable<int> Width { get { return this._Width; } }
public Nullable<int> Height { get { return this._Height; } }
public DevExpress.Utils.DefaultBoolean ShowLegend { get { return this._ShowLegend; } }
public List<KeyValuePair<string, double>> Data { get { return _Data; } }
public ChartImage(string input)
{
this._Input = input;
}
public void Initialize()
{
using (StringReader stringReader = new StringReader(_Input))
using (XmlTextReader reader = new XmlTextReader(stringReader)) {
reader.DtdProcessing = DtdProcessing.Ignore;
while (reader.Read()) {
if (reader.IsStartElement()) {
switch (reader.Name) {
case "type":
GetChartType(reader.ReadString());
break;
case "height":
_Height = new int?(int.Parse(reader.ReadString()));
break;
case "width":
_Width = new int?(int.Parse(reader.ReadString()));
break;
case "legend":
_ShowLegend = (reader.ReadString().ToLower() == "true") ? DevExpress.Utils.DefaultBoolean.True : DevExpress.Utils.DefaultBoolean.False;
break;
case "data":
GetData(reader.ReadString());
break;
}
}
}
}
}
private void GetData(string p)
{
foreach (string t in p.Split(',')) {
double value;
string[] pair = t.Split('|');
if (pair.Length == 0) {
continue;
}
if (pair.Length == 1) {
if (!double.TryParse(pair[0], out value)) {
_Data.Add(new KeyValuePair<string, double>(pair[0], 0));
}
else {
_Data.Add(new KeyValuePair<string, double>(String.Empty, value));
}
}
else
if (pair.Length == 2) {
if (!double.TryParse(pair[1], out value)) {
_Data.Add(new KeyValuePair<string, double>(pair[0], 0));
}
else {
_Data.Add(new KeyValuePair<string, double>(pair[0], value));
}
}
}
}
private void GetChartType(string p)
{
switch (p.ToLower()) {
case "bar":
_ViewType = DevExpress.XtraCharts.ViewType.Bar;
break;
case "line":
_ViewType = DevExpress.XtraCharts.ViewType.Line;
break;
case "pie3d":
_ViewType = DevExpress.XtraCharts.ViewType.Pie3D;
break;
case "pie":
default:
_ViewType = DevExpress.XtraCharts.ViewType.Pie;
break;
}
}
public Stream CreateChart() {
using (ChartControl chart = new ChartControl()) {
if (_Width.HasValue) {
chart.Width = _Width.Value;
}
if (_Height.HasValue) {
chart.Height = _Height.Value;
}
int undefined = 1;
MemoryStream stream = new MemoryStream();
try {
Series series = new Series("Chart", _ViewType);
try {
if (series.Label is DevExpress.XtraCharts.PieSeriesLabel) {
((DevExpress.XtraCharts.PieSeriesLabel)series.Label).Position = PieSeriesLabelPosition.Inside;
}
if (_ViewType == ViewType.Pie) {
series.Label.TextPattern = "{A}: {VP:P0}";
}
if (_Data == null || _Data.Count == 0) {
series.Points.Add(new SeriesPoint("Undefined", new double[] { 1 }));
series.Label.TextPattern = "{S}";
} else {
series.Label.TextPattern = "{A}: {V:F1}";
for (int i = 0; i < _Data.Count; i++) {
string argument = _Data[i].Key.Trim();
if (String.IsNullOrEmpty(argument)) {
argument = "Undefined " + undefined;
undefined++;
}
series.Points.Add(new SeriesPoint(argument, new double[] {
_Data[i].Value }));
}
}
chart.Legend.Visibility = _ShowLegend;
chart.BorderOptions.Visibility = DevExpress.Utils.DefaultBoolean.False;
chart.Series.AddRange(new Series[] { series });
series = null;
XYDiagram diagram = chart.Diagram as XYDiagram;
if (diagram != null && diagram.AxisX != null)
diagram.AxisX.Label.ResolveOverlappingOptions.AllowStagger = true; ;
chart.ExportToImage(stream, ImageFormat.Png);
stream.Position = 0;
return stream;
} catch {
if (series != null) {
series.Dispose();
}
throw;
}
} catch {
if (stream != null) {
stream.Dispose();
}
throw;
}
}
}
public DocumentImageSource CreateImage()
{
return DocumentImageSource.FromStream(CreateChart());
}
}
}