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
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GraphUserControl
{
public partial class GraphUC: UserControl
{
int x_margin=20;
int y_margin=20;
int x_in_margin=20;
int y_in_margin=20;
int font_size;
double y_scale=1;
int n_datpoints;// =4;
Color primaryLineColor = Color.DeepSkyBlue;
Color alternateLineColor = Color.Red;
Color baselineColor = Color.Black;
Color borderlineColor = Color.Black;
Color primaryFillColor = Color.DeepSkyBlue;
Color alternateFillColor = Color.Red;
Color primaryValueColor = Color.Blue;
Color alternateValueColor = Color.Red;
//Databinding-Start
public DataTable dataSource;
public string dataMember_Y;
public string dataMember_X;
//Databinding-End
public int Xmargin
{
get { return this.x_margin; }
set { this.x_margin = value; }
}
public int Ymargin
{
get { return this.y_margin; }
set { this.y_margin = value; }
}
public int Xinmargin
{
get { return this.x_in_margin; }
set { this.x_in_margin = value; }
}
public int Yinmargin
{
get { return this.y_in_margin; }
set { this.y_in_margin = value; }
}
public Color PrimaryLineColor
{
get { return this.primaryLineColor; }
set { this.primaryLineColor = value; }
}
public Color AlternateLineColor
{
get { return this.alternateLineColor; }
set { this.alternateLineColor = value; }
}
public Color BaselineColor
{
get { return this.baselineColor; }
set { this.baselineColor = value; }
}
public Color BorderlineColor
{
get { return this.borderlineColor; }
set { this.borderlineColor = value; }
}
public Color PrimaryFillColor
{
get { return this.primaryFillColor; }
set { this.primaryFillColor = value; }
}
public Color AlternateFillColor
{
get { return this.alternateFillColor; }
set { this.alternateFillColor = value; }
}
public Color PrimaryValueColor
{
get { return this.primaryValueColor; }
set { this.primaryValueColor = value; }
}
public Color AlternateValueColor
{
get { return this.alternateValueColor; }
set { this.alternateValueColor = value; }
}
//Data Binding - Start
[TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design")]
[Category("Data")]
[DefaultValue(null)]
public DataTable DataSource
{
get
{
return this.dataSource;
}
set
{
if (this.dataSource != value)
{
this.dataSource = value;
}
}
}
[Category("Data")]
[Editor("System.Windows.Forms.Design.DataMemberListEditor,System.Design", "System.Drawing.Design.UITypeEditor, System.Drawing")]
[DefaultValue("")]
public string DataMemberY
{
get
{
return this.dataMember_Y;
}
set
{
if (this.dataMember_Y != value)
{
this.dataMember_Y = value;
}
}
}
[Category("Data")]
[Editor("System.Windows.Forms.Design.DataMemberListEditor,System.Design", "System.Drawing.Design.UITypeEditor, System.Drawing")]
[DefaultValue("")]
public string DataMemberX
{
get
{
return this.dataMember_X;
}
set
{
if (this.dataMember_X != value)
{
this.dataMember_X = value;
}
}
}
//Data Binding - End
public GraphUC()
{
InitializeComponent();
}
private void DrawBarChart(Graphics gr)
{
try
{
// Create a new pen.
Pen PrimaryColorPen = new Pen(primaryLineColor);
Pen AlternateColorPen = new Pen(alternateLineColor);
Pen BaseLinePen = new Pen(baselineColor);
Pen BorderLinePen = new Pen(borderlineColor);
// Create new Solid Brush
SolidBrush PrimarysolidBrush = new SolidBrush(primaryFillColor);
SolidBrush AlternatesolidBrush = new SolidBrush(alternateFillColor);
SolidBrush PrimaryValueBrush = new SolidBrush(primaryValueColor);
SolidBrush AlternateValueBrush = new SolidBrush(alternateValueColor);
if (dataSource.Rows.Count != 0)
{
n_datpoints = dataSource.Rows.Count;
}
// label1.Text = Convert.ToString(n_datpoints);
float fbar_space = (float)(this.Width - 2 * (x_margin + x_in_margin)) / (2 * n_datpoints);
int bar_space = (int)Math.Round(fbar_space);
int bar_width = (int)Math.Floor(fbar_space);
font_size = (int)this.Font.Size;
//Calculate the scale
// y_scale = (this.Height - 2 * (y_margin + y_in_margin)) / double.Parse(listBox1.Compute("MAX(" + dataMember_Y + ")", "").ToString());
//Draw border
gr.DrawRectangle(BorderLinePen,
new Rectangle(x_margin, y_margin, this.Width - 2 * x_margin, this.Height - 2 * y_margin));
for (int i = 0; i < n_datpoints; i++)
{
if (i % 2 == 0)
{
gr.DrawRectangle(PrimaryColorPen,
new Rectangle(x_margin + x_in_margin + (i + 1) * bar_width + i * bar_space, this.Height - y_margin - y_in_margin - (int)(Convert.ToInt32(dataSource.Rows[i][dataMember_Y].ToString()) * y_scale), bar_width, (int)(Convert.ToInt32(dataSource.Rows[i][dataMember_Y].ToString()) * y_scale)));
gr.FillRectangle(PrimarysolidBrush, x_margin + x_in_margin + (i + 1) * bar_width + i * bar_space, this.Height - y_margin - y_in_margin - (int)(Convert.ToInt32(dataSource.Rows[i][dataMember_Y].ToString()) * y_scale), bar_width, (int)(Convert.ToInt32(dataSource.Rows[i][dataMember_Y].ToString()) * y_scale));
using (Font font = new Font(this.Font.Name, font_size, this.Font.Style, this.Font.Unit))
{
Point point1 = new Point(x_margin + x_in_margin + (i + 1) * bar_width + i * bar_space - 3, this.Height - y_margin - y_in_margin - (int)(Convert.ToInt32(dataSource.Rows[i][dataMember_Y].ToString()) * y_scale) - (int)font.Size - 5);
Point point2 = new Point(x_margin + x_in_margin + (i + 1) * bar_width + i * bar_space - 3, this.Height - y_margin - y_in_margin);
gr.DrawString(Convert.ToInt32(dataSource.Rows[i][dataMember_Y].ToString()).ToString(), font, PrimaryValueBrush, point1);
gr.DrawString(dataSource.Rows[i][dataMember_X].ToString().Substring(0, (bar_width / font_size) + 1), font, PrimaryValueBrush, point2);
}
}
else
{
gr.DrawRectangle(AlternateColorPen,
new Rectangle(x_margin + x_in_margin + (i + 1) * bar_width + i * bar_space, this.Height - y_margin - y_in_margin - (int)(Convert.ToInt32(dataSource.Rows[i][dataMember_Y].ToString()) * y_scale), bar_width, (int)(Convert.ToInt32(dataSource.Rows[i][dataMember_Y].ToString()) * y_scale)));
gr.FillRectangle(AlternatesolidBrush, x_margin + x_in_margin + (i + 1) * bar_width + i * bar_space, this.Height - y_margin - y_in_margin - (int)(Convert.ToInt32(dataSource.Rows[i][dataMember_Y].ToString()) * y_scale), bar_width, (int)(Convert.ToInt32(dataSource.Rows[i][dataMember_Y].ToString()) * y_scale));
using (Font font = new Font(this.Font.Name, font_size, this.Font.Style, this.Font.Unit))
{
Point point1 = new Point(x_margin + x_in_margin + (i + 1) * bar_width + i * bar_space - 3, this.Height - y_margin - y_in_margin - (int)(Convert.ToInt32(dataSource.Rows[i][dataMember_Y].ToString()) * y_scale) - (int)font.Size - 5);
Point point2 = new Point(x_margin + x_in_margin + (i + 1) * bar_width + i * bar_space - 3, this.Height - y_margin - y_in_margin);
gr.DrawString(Convert.ToInt32(dataSource.Rows[i][dataMember_Y].ToString()).ToString(), font, AlternateValueBrush, point1);
gr.DrawString(dataSource.Rows[i][dataMember_X].ToString().Substring(0, (bar_width / font_size) + 1), font, AlternateValueBrush, point2);
}
}
}
// Draw Baseline below bars
gr.DrawLine(BaseLinePen, x_margin + x_in_margin, this.Height - y_margin - y_in_margin, this.Width - x_margin - x_in_margin, this.Height - y_margin - y_in_margin);
// Draw Vertical Line
gr.DrawLine(BaseLinePen, x_margin + x_in_margin, this.Height - y_margin - y_in_margin, x_margin + x_in_margin, y_margin + y_in_margin);
//Dispose of Pens and Brushes.
PrimaryColorPen.Dispose();
AlternateColorPen.Dispose();
BaseLinePen.Dispose();
BorderLinePen.Dispose();
PrimarysolidBrush.Dispose();
AlternatesolidBrush.Dispose();
PrimaryValueBrush.Dispose();
AlternateValueBrush.Dispose();
}
catch (Exception ex)
{
string str = ex.Message;
}
}
private void GraphUC_Load(object sender, EventArgs e)
{
DrawBarChart(pictureBox1.CreateGraphics());
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
DrawBarChart(e.Graphics);
}
private void button1_Click(object sender, EventArgs e)
{
// Redraw.
using (Graphics gr = pictureBox1.CreateGraphics())
{
DrawBarChart(gr);
}
}
}
} |