c#的chart标题_C#之Chart篇
叕叕叕到周五了,時間總是走的如此之快,不免傷感(- -)。。。(傷感個毛線呀,再傷感16年就走了)12月就要結束了,趕緊來一篇充實一下生活。最近在項目中,做了個圖表程序,使用到了Chart,今天在這里做一個整理總結。
1.第一個Chart控件
1)先來熟悉一下chart,在前端做一個圖表可能會用到chart.js,在C#中可以用自帶的控件chart,感覺挺方便的。
2)創建一個項目,windows窗體應用程序。在工具箱的【數據】找到【 Chart】控件,并拖到窗體
3)右鍵chart【屬性】,在VS右側屬性【布局】下面找到【Dock】屬性設置為Fill,自己再調整一下大小
4)這里的操作是當加載窗體的時候顯示chart,所以有個窗體load事件。
5)雙擊后直接進入代碼,當在代碼中寫Series時會出現紅色波浪線,提示缺少相關命名空間之類的,點擊【Series】就可以看到所需要的,添加就ok了
6)代碼
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
40using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace MyChart
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//清除默認的series
chart1.Series.Clear();
//new 一個叫做【Strength】的系列
Series Strength = new Series("力量");
//設置chart的類型,這里為柱狀圖
Strength.ChartType = SeriesChartType.Column;
//給系列上的點進行賦值,分別對應橫坐標和縱坐標的值
Strength.Points.AddXY("A", "90");
Strength.Points.AddXY("B","88");
Strength.Points.AddXY("C", "60");
Strength.Points.AddXY("D", "93");
Strength.Points.AddXY("E", "79");
Strength.Points.AddXY("F", "85");
//把series添加到chart上
chart1.Series.Add(Strength);
}
}
}
7)效果圖
2.兩個Series
1)右擊項目名,【添加】一個windows窗體。然后的話步驟和前面一樣,這里就不多說了
2)簡單粗暴上代碼
using System;? using System.Collections.Generic;? using System.ComponentModel;? using System.Data;? using System.Drawing;? using System.Linq;? using System.Text;? using System.Windows.Forms;? using System.Windows.Forms.DataVisualization.Charting;
namespace MyChart? {? public partial class Form2 : Form? {? public Form2()? {? InitializeComponent();? }
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
49private void Form2_Load(object sender, EventArgs e)
{
chart1.Series.Clear();
Series Strength = new Series("力量");
Series Speed= new Series("速度");
Strength.ChartType = SeriesChartType.Column;
Strength.IsValueShownAsLabel = true;
Strength.Color = System.Drawing.Color.Cyan;
Speed.ChartType = SeriesChartType.Spline;
Speed.IsValueShownAsLabel = true;
chart1.ChartAreas[0].AxisX.MajorGrid.Interval =0.5;
chart1.ChartAreas[0].AxisX.MajorGrid.Enabled =true;
//chart1.ChartAreas[0].Area3DStyle.Enable3D = true;
chart1.ChartAreas[0].AxisX.IsMarginVisible = true;
chart1.ChartAreas[0].AxisX.Title = "英雄";
chart1.ChartAreas[0].AxisX.TitleForeColor = System.Drawing.Color.Crimson;
chart1.ChartAreas[0].AxisY.Title = "屬性";
chart1.ChartAreas[0].AxisY.TitleForeColor = System.Drawing.Color.Crimson;
chart1.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Horizontal;
Strength.LegendText = "力氣";
Strength.Points.AddXY("A", "90");
Strength.Points.AddXY("B", "88");
Strength.Points.AddXY("C", "60");
Strength.Points.AddXY("D", "93");
Strength.Points.AddXY("E", "79");
Strength.Points.AddXY("F", "85");
Speed.Points.AddXY("A", "120");
Speed.Points.AddXY("B", "133");
Speed.Points.AddXY("C", "100");
Speed.Points.AddXY("D", "98");
Speed.Points.AddXY("E", "126");
Speed.Points.AddXY("F", "89");
//把series添加到chart上
chart1.Series.Add(Speed);
chart1.Series.Add(Strength);
}
}
}
3)效果
4)熟悉常用屬性和方法
(1)Series對象
Series Strength = new Series("力量");? Series Speed= new Series("速度");
設置series類型
Strength.ChartType = SeriesChartType.Column;? Speed.ChartType = SeriesChartType.Spline;
是否把值當做標簽展示(默認false)
Speed.IsValueShownAsLabel = true;
設置series顏色
Strength.Color = System.Drawing.Color.Cyan;
給series上的點賦值
1
2
3Strength.Points.AddXY("A", "90");
Strength.Points.AddXY("B", "88");
Strength.Points.AddXY("C", "60");
(2)ChartArea(就是我們看到的區域)
以3D形式展示
chart1.ChartAreas[0].Area3DStyle.Enable3D = true;
設置坐標軸標題
1
2
3chart1.ChartAreas[0].AxisY.Title = "屬性";
chart1.ChartAreas[0].AxisY.TitleForeColor = System.Drawing.Color.Crimson;
chart1.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Horizontal;
設置網格間隔(這里設成0.5,看得更直觀一點)
1chart1.ChartAreas[0].AxisX.MajorGrid.Interval =0.5;
3.庫存波動
1)主代碼
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
75using Daisy.Common.McsClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace BIZWhOnhandQuery
{
public partial class MainForm : Mes.ControlsEx.ExtendForm.BaseForm
{
public string QuerySql01 = string.Empty;
public MainForm()
{
InitializeComponent();
}
private void navigatorEx1_OnQuery(object sender, Mes.Core.ApplicationObject.SystemNavigatorClickedEventArgs e)
{
try
{
QueryForm qf = new QueryForm();
qf.StartPosition = FormStartPosition.CenterScreen;
qf.ShowDialog();
if (qf.DialogResult == System.Windows.Forms.DialogResult.OK)
{
QuerySql01 = qf.QuerySql01;
this.chart1.Series.Clear();//先將series清除
//設置X/Y樣式
chart1.ChartAreas[0].AxisY.Title = Mes.Core.Utility.StrUtil.Translate("數量");
chart1.ChartAreas[0].AxisX.Title = Mes.Core.Utility.StrUtil.Translate("日期");
chart1.ChartAreas[0].AxisX.LabelStyle.Angle = 0;
chart1.ChartAreas[0].AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
chart1.ChartAreas[0].AxisY.IntervalAutoMode = IntervalAutoMode.VariableCount;
// chart1.ChartAreas[0].AxisX.Enabled = AxisEnabled.False;
// chart1.ChartAreas[0].AxisY.Enabled = AxisEnabled.False;
chart1.Titles[0].Text = "";
//設置char樣式
this.chart1.Series.Add(Mes.Core.Utility.StrUtil.Translate("數量"));
chart1.Series[Mes.Core.Utility.StrUtil.Translate("數量")].MarkerColor = Color.Black;//設置標志
chart1.Series[Mes.Core.Utility.StrUtil.Translate("數量")].MarkerSize = 1;
chart1.Series[Mes.Core.Utility.StrUtil.Translate("數量")].MarkerStyle = MarkerStyle.Square;
chart1.Series[Mes.Core.Utility.StrUtil.Translate("數量")].IsValueShownAsLabel = false;//是否顯示值
chart1.Series[Mes.Core.Utility.StrUtil.Translate("數量")].ChartType = SeriesChartType.Spline;//設置顯示樣式
chart1.Series[Mes.Core.Utility.StrUtil.Translate("數量")].BorderWidth = 1;
chart1.Series[Mes.Core.Utility.StrUtil.Translate("數量")].Color = Color.Blue;
chart1.Series[Mes.Core.Utility.StrUtil.Translate("數量")].ToolTip = Mes.Core.Utility.StrUtil.Translate("原材料數量");
chart1.Series[Mes.Core.Utility.StrUtil.Translate("數量")].YValueType = ChartValueType.Double;
McsDataReader reader = (McsDataReader)Mes.Core.Service.DatabaseAccessService.execute(qf.QuerySql01, Mes.Core.Service.ReturnType.RESULTSET);
if (reader.rowNumber > 0)
{
while (reader.Read())
{
chart1.Series[Mes.Core.Utility.StrUtil.Translate("數量")].Points.AddXY(reader.getString(1), reader.getString(0));
}
}
chart1.ChartAreas[0].AxisY.Minimum = 0;
chart1.ChartAreas[0].Axes[1].LabelStyle.Format = "N0";
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
}
2)效果圖
4.設備使用分析
查詢設備在某個時間范圍內的使用頻率(可按日或周或月),點擊一條記錄可以看到對應的曲線。該報表的設備包括有記錄已經維護基礎數據的設備,也包括有記錄還沒有維護基礎數據的設備
1)功能代碼結構
2)MainForm
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
324using Daisy.Common.McsClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace BIZDeviceUseAnalysis
{
public partial class MainForm : Mes.ControlsEx.ExtendForm.BaseForm
{
QueryForm form = null;
public MainForm()
{
InitializeComponent();
}
private void navigatorEx1_OnQuery(object sender, Mes.Core.ApplicationObject.SystemNavigatorClickedEventArgs e)
{
form = new QueryForm();
form.ShowDialog();
if (form.DialogResult == System.Windows.Forms.DialogResult.OK)
{
GetData();
}
}
void GetData()
{
try
{
AddColumns(); //獲取列名
AddDataInGridView();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void AddColumns()
{
try
{
List columnList = new List();
McsDataReader reader = (McsDataReader)Mes.Core.Service.DatabaseAccessService.execute(form.colsql, Mes.Core.Service.ReturnType.RESULTSET);
//McsDataReader reader01 = (McsDataReader)Mes.Core.Service.DatabaseAccessService.execute(form.QuerySql, Mes.Core.Service.ReturnType.RESULTSET);
while (reader.Read())
{
string calendar = reader.getString(0);
if (form.Kind == 1)
{
calendar = DateTime.Parse(calendar).ToString("yyyy-MM-dd");
}
//if (form.Kind == 2)
//{
// calendar = DateTime.Parse(calendar).ToString("yyyy-iw");
//}
//if (form.Kind == 3)
//{
// calendar = DateTime.Parse(calendar).ToString("yyyy-MM");
//}
columnList.Add(calendar);
}
if (columnList.Count == 0)
return;
int count = this.dataGridViewEx1.Columns.Count;
for (int j = count - 1; j > 2; j--)
{
this.dataGridViewEx1.Columns.RemoveAt(j);
}
for (int i = 0; i < columnList.Count; i++)
{
Mes.ControlsEx.DataGridViewTextBoxExColumn Column = new Mes.ControlsEx.DataGridViewTextBoxExColumn(this.components);
Column.DataType = Mes.Core.ApplicationObject.DataGridViewColumnDataType.NONE;
Column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.NotSet;
Column.SortMode = DataGridViewColumnSortMode.Automatic;
Column.HeaderText = Mes.Core.Utility.StrUtil.ValueToString(columnList[i]);
//Column.HeaderText = DateTime.Parse(Mes.Core.Utility.StrUtil.ValueToString(columnList[i])).ToString("yyyy-MM-dd");
Column.ToolTipText = Mes.Core.Utility.StrUtil.ValueToString(columnList[i]);
Column.IgnoreValueChanged = false;
Column.IndexOrder = 0;
Column.IsShowTimePick = false;
Column.LovParameter = null;
Column.MustBeInput = false;
Column.MustBeInputErrorMsg = "";
Column.Name = "Col" + Mes.Core.Utility.StrUtil.ValueToString(columnList[i]);
Column.Tag = columnList[i];
Column.PopType = Mes.Core.ApplicationObject.DataGridViewColumnPopType.NONE;
Column.RangeEndValue = "";
Column.RangeStartValue = "";
Column.ReadOnly = true;
Column.RegString = "";
Column.ValidationErrorMsg = "";
Column.ValidationType = Mes.Core.ApplicationObject.DataGridViewColumnValidationType.NONE;
Column.Width = 150;
Column.Resizable = DataGridViewTriState.False;
Column.SortMode = DataGridViewColumnSortMode.NotSortable;
this.dataGridViewEx1.Columns.Add(Column);
}
}
catch (Exception ex)
{
throw ex;
}
}
void AddDataInGridView()
{
try
{
this.dataGridViewEx1.Rows.Clear();
GetDataBySQL(form.QuerySql, 1);
this.statusStripBarEx1.ShowMessage(Mes.Core.Utility.StrUtil.Translate("共查詢到" + dataGridViewEx1.RowCount + "條數據"));
//FillChart();//填充圖表
}
catch (Exception ex)
{
throw ex;
}
}
void GetDataBySQL(string sql, int type)
{
try
{
int rowIndex = -1;
McsDataReader reader = (McsDataReader)Mes.Core.Service.DatabaseAccessService.execute(sql, Mes.Core.Service.ReturnType.RESULTSET);
while (reader.Read())
{
string calendar = reader.getString(2);
if (form.Kind == 1)
{
// calendar = DateTime.Parse(calendar).ToShortDateString();
calendar = DateTime.Parse(calendar).ToString("yyyy-MM-dd");
}
string mac= reader.getString(0);
string deviceCode = reader.getString(3);
string deviceName = reader.getString(4);
string value=reader.getString(1);
bool IsExist = false;
for (int i = 0; i < this.dataGridViewEx1.Rows.Count; i++)
{
string rowValue = Mes.Core.Utility.StrUtil.ValueToString(this.dataGridViewEx1.Rows[i].Cells[0].Value);
if (rowValue == mac)
{
rowIndex = i;
IsExist = true;
break;
}
}
if (IsExist)
{
FindCellForValue(rowIndex, value, calendar,deviceCode,deviceName);
}
else
{
string text = string.Empty;
if (type == 1)
text = mac;
// MessageBox.Show("mac:" + mac);
rowIndex = this.dataGridViewEx1.Rows.Add(text);
this.dataGridViewEx1.Rows[rowIndex].Cells[1].Value = deviceCode;
this.dataGridViewEx1.Rows[rowIndex].Cells[2].Value = deviceName;
FindCellForValue(rowIndex, value, calendar, deviceCode, deviceName);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
void FindCellForValue(int rowIndex, string Value, string ScanItem,string deviceCode,string deviceName)
{
for (int i = 3; i < this.dataGridViewEx1.Columns.Count; i++)
{
if (ScanItem == this.dataGridViewEx1.Columns[i].HeaderText)
{
this.dataGridViewEx1.Rows[rowIndex].Cells[i].Value =Value;
}
}
}
void FillChart(int rowIndex)
{
int FixColumnIndex = 1;
try
{
chart1.Series.Clear();
//chart1.ChartAreas[0].Axes[1].Maximum = 1.3;
chart1.ChartAreas[0].Axes[1].Minimum = 0;
//設置網格線
chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Black;
chart1.ChartAreas[0].AxisX.MajorGrid.Interval = 0;//網格間隔
chart1.ChartAreas[0].AxisX.MinorGrid.Interval = 0;
chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Black;
chart1.ChartAreas[0].AxisY.MajorGrid.Interval = 0;
chart1.ChartAreas[0].AxisY.MinorGrid.Interval = 0;
chart1.ChartAreas[0].AxisY.Title = Mes.Core.Utility.StrUtil.Translate("使用次數");
chart1.ChartAreas[0].AxisX.Title = Mes.Core.Utility.StrUtil.Translate("使用日期");
//折線圖
//for (int i = 0; i < this.datagridviewex1.rowcount; i++)
//{
int[] yValues1 = new int[this.dataGridViewEx1.Columns.Count - FixColumnIndex];
string[] xValues1 = new string[this.dataGridViewEx1.Columns.Count - FixColumnIndex];
//int[]yValues1=new int[0];
//string[] xValues1=new string[0] ;
chart1.Series.Add(Mes.Core.Utility.StrUtil.ValueToString(this.dataGridViewEx1.Rows[rowIndex].Cells[0].Value));
//設置圖片類型
chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
//設置默認軸
chart1.Series[0].IsVisibleInLegend = true;
//設置圖例顯示
chart1.Series[0].IsValueShownAsLabel = true;
//設置軸顯示
//chart1.Series[i].Label = "#VAL{P}";
//設置線條粗細
chart1.Series[0].BorderWidth = 3;
int count = 0;
for (int j = 3; j < this.dataGridViewEx1.ColumnCount; j++)
{
int _value = 0;
string value = Mes.Core.Utility.StrUtil.ValueToString(this.dataGridViewEx1.Rows[rowIndex].Cells[j].Value);
string text = Mes.Core.Utility.StrUtil.ValueToString(this.dataGridViewEx1.Columns[j].HeaderText);
xValues1[count] = text;
//柱狀圖數據添加
if (value!= string.Empty)
{
_value =Convert.ToInt32(value);
yValues1[count] = _value;
}
else
{
_value = 0;
yValues1[count] = _value;
}
chart1.Series[0].Points.DataBindXY(xValues1,yValues1);
count += 1;
// }
}
}
catch (Exception ex)
{
throw ex;
}
}
private void navigatorEx1_Load(object sender, EventArgs e)
{
this.navigatorEx1.AddButton(Mes.Core.Utility.StrUtil.Translate("設備基礎資料維護"), MAINTAIN_DEVICE);
}
//設備基礎資料維護窗口
private void MAINTAIN_DEVICE(object sender, EventArgs e)
{
BaseInfoForm bi = new BaseInfoForm();
bi.ShowDialog();
}
private void dataGridViewEx1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int currentIndex = e.RowIndex;
if (e.RowIndex < 0)
return;
if (this.dataGridViewEx1.Rows.Count > 0 && e.RowIndex < this.dataGridViewEx1.Rows.Count) {
FillChart(currentIndex);
}
}
}
}
3)QueryForm
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
183using Mes.ControlsEx;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace BIZDeviceUseAnalysis
{
public partial class QueryForm : Mes.ControlsEx.ExtendForm.QueryForm
{
public int Kind = 0;
public string colsql = string.Empty;
public QueryForm()
{
InitializeComponent();
}
private void QueryForm_Load(object sender, EventArgs e)
{
}
private void cbByDay_CheckedChanged(object sender, EventArgs e)
{
CheckChangeEvent(sender);
}
private void cbByWeek_CheckedChanged(object sender, EventArgs e)
{
CheckChangeEvent(sender);
}
private void cbByMonth_CheckedChanged(object sender, EventArgs e)
{
CheckChangeEvent(sender);
}
private void CheckChangeEvent(object sender)
{
try
{
if ((sender as CheckBoxEx).Checked == true)
{
foreach (CheckBoxEx chk in (sender as CheckBoxEx).Parent.Controls)
{
if (chk != sender)
{
chk.Checked = false;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void QueryForm_OnQuery(object sender, EventArgs e)
{
string startTimeStr = string.Empty;
string endTimeStr = string.Empty;
string condition = string.Empty;
string mac = string.Empty;
//Mac
string txtMac = this.tbMac.Text.Trim();
List macList = this.tbMac.MultirowValue;
if (macList != null & macList.Count > 0)
{
mac = " AND mac in (" + Mes.Core.Utility.StrUtil.BuildPara(macList) + ") ";
}
else
{
if (txtMac != string.Empty)
{
mac = " AND mac " + Mes.Core.Utility.StrUtil.ProcInput(txtMac, false);
}
}
//查詢日期從
string txtDailyWorkFrom = this.tbDateFrom.Text.Trim();
if (txtDailyWorkFrom == string.Empty)
{
MessageBox.Show(Mes.Core.Utility.StrUtil.Translate("查詢日期從不能為空"));
return;
}
else
{
startTimeStr = txtDailyWorkFrom;
}
//查詢日期到
string txtDailyWorkTo = this.tbDateTo.Text.Trim();
if (txtDailyWorkTo == string.Empty)
{
MessageBox.Show(Mes.Core.Utility.StrUtil.Translate("查詢日期到不能為空"));
return;
}
else
{
endTimeStr = txtDailyWorkTo;
}
TimeSpan ts;
try
{
DateTime startTime = DateTime.ParseExact(txtDailyWorkFrom, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture);
DateTime endTime = DateTime.ParseExact(txtDailyWorkTo, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture);
ts = endTime - startTime;
}
catch
{
MessageBox.Show(Mes.Core.Utility.StrUtil.Translate("時間格式不正確"));
return;
}
//日報
if (this.cbByDay.Checked)
{
if (ts.TotalDays > 30)
{
MessageBox.Show(Mes.Core.Utility.StrUtil.Translate("查詢日期從到查詢日期到不能超過一個月"));
return;
}
Kind = 1;
condition += "GROUP BY mac, to_char(bmr.creation_date, 'yyyy-mm-dd'), device_code,device_name";
condition += " ORDER BY mac, to_char(bmr.creation_date, 'yyyy-mm-dd') ,device_code,device_name ";
this.QuerySql = Sql.Core.GetMainSqlByDay(startTimeStr, endTimeStr,mac,condition);
colsql = Sql.Core.GetColumnByDay(startTimeStr, endTimeStr);
}
//周報
if (this.cbByWeek.Checked)
{
if (ts.TotalDays > 13 * 7)
{
MessageBox.Show(Mes.Core.Utility.StrUtil.Translate("查詢日期從到查詢日期到不能超過13周"));
return;
}
Kind = 2;
condition += "GROUP BY mac, to_char(trunc(bmr.creation_date),'yyyy-iw'), device_code,device_name";
condition += " ORDER BY mac, to_char(trunc(bmr.creation_date),'yyyy-iw') ,device_code,device_name ";
this.QuerySql = Sql.Core.GetMainSqlByWeek(startTimeStr, endTimeStr,mac, condition);
colsql = Sql.Core.GetColumnByWeek(startTimeStr, endTimeStr);
}
//月報
if (this.cbByMonth.Checked)
{
if (ts.TotalDays > 365 * 2)
{
MessageBox.Show(Mes.Core.Utility.StrUtil.Translate("查詢日期從到查詢日期到不能超過2年"));
return;
}
Kind = 3;
condition += "GROUP BY mac, to_char(trunc(bmr.creation_date),'yyyy-mm'), device_code,device_name";
condition += " ORDER BY mac, to_char(trunc(bmr.creation_date),'yyyy-mm') ,device_code,device_name ";
this.QuerySql = Sql.Core.GetMainSqlByMonth(startTimeStr, endTimeStr, mac,condition);
colsql = Sql.Core.GetColumnByMonth(startTimeStr, endTimeStr);
}
this.DialogResult = System.Windows.Forms.DialogResult.OK;
this.Close();
}
private void QueryForm_OnCancelQuery(object sender, EventArgs e)
{
this.tbMac.Text = this.tbDateFrom.Text = this.tbDateTo.Text = string.Empty;
}
}
}
5)BaseInfoForm
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
88using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace BIZDeviceUseAnalysis
{
public partial class BaseInfoForm : Mes.ControlsEx.ExtendForm.BaseForm
{
public BaseInfoForm()
{
InitializeComponent();
}
private void navigatorEx1_OnQuery(object sender, Mes.Core.ApplicationObject.SystemNavigatorClickedEventArgs e)
{
BaseInfoQueryForm bio = new BaseInfoQueryForm();
bio.StartPosition = FormStartPosition.CenterScreen;
bio.ShowDialog();
if (bio.DialogResult == System.Windows.Forms.DialogResult.OK) {
this.navigatorEx1.QuerySql = bio.QuerySql;
}
}
private void navigatorEx1_OnSave(object sender, Mes.Core.ApplicationObject.SystemNavigatorClickedEventArgs e)
{
if (this.dataGridViewEx1.AddedRows.Count > 0)//新增行大于0
{
this.navigatorEx1.InsertSqlList = this.SaveOrUpdate(this.dataGridViewEx1.AddedRows, true);//返回值為True
}
if (this.dataGridViewEx1.ChangedRows.Count > 0)//修改行大于0
{
this.navigatorEx1.UpdateSqlList = this.SaveOrUpdate(this.dataGridViewEx1.ChangedRows, false);//返回值為False
}
}
private List SaveOrUpdate(List list, bool flag)
{
List InsertOrUpdatelist = new List();
for (int i = 0; i < list.Count; i++)
{
int row_index = Convert.ToInt32(list[i]);
List lis = new List();
if (flag)//如果返回值為True
{
lis.Add(Mes.Core.Utility.StrUtil.ValueToString(dataGridViewEx1.Rows[row_index].Cells[this.ColDeviceCode.Name].Value));
lis.Add(Mes.Core.Utility.StrUtil.ValueToString(dataGridViewEx1.Rows[row_index].Cells[this.ColDeviceType.Name].Value));
lis.Add(Mes.Core.Utility.StrUtil.ValueToString(dataGridViewEx1.Rows[row_index].Cells[this.ColDeviceName.Name].Value));
lis.Add(Mes.Core.Utility.StrUtil.ValueToString(dataGridViewEx1.Rows[row_index].Cells[this.ColMacAddress.Name].Value));
lis.Add(Mes.Core.Utility.StrUtil.ValueToString(dataGridViewEx1.Rows[row_index].Cells[this.ColFloor.Name].Value));
lis.Add(Mes.Core.Utility.StrUtil.ValueToString(dataGridViewEx1.Rows[row_index].Cells[this.ColAdminDepartment.Name].Value));
lis.Add(Mes.Core.Utility.StrUtil.ValueToString(dataGridViewEx1.Rows[row_index].Cells[this.ColEnable.Name].Value));
lis.Add(Mes.Core.Utility.StrUtil.ValueToString(dataGridViewEx1.Rows[row_index].Cells[this.ColDescription.Name].Value));
string template = Mes.Core.Utility.StrUtil.BuildPara(lis);
string sql = "{? = call biz_device_pck.insert_rows_for_ui(" + template + ")}";
InsertOrUpdatelist.Add(sql);
}
else
{
// lis.Add(Mes.Core.Utility.StrUtil.ValueToString(dataGridViewEx1.Rows[row_index].Cells[0].Value));
lis.Add(Mes.Core.Utility.StrUtil.ValueToString(dataGridViewEx1.Rows[row_index].Cells[this.ColDeviceID.Name].Value));
lis.Add(Mes.Core.Utility.StrUtil.ValueToString(dataGridViewEx1.Rows[row_index].Cells[this.ColDeviceCode.Name].Value));
lis.Add(Mes.Core.Utility.StrUtil.ValueToString(dataGridViewEx1.Rows[row_index].Cells[this.ColDeviceType.Name].Value));
lis.Add(Mes.Core.Utility.StrUtil.ValueToString(dataGridViewEx1.Rows[row_index].Cells[this.ColDeviceName.Name].Value));
lis.Add(Mes.Core.Utility.StrUtil.ValueToString(dataGridViewEx1.Rows[row_index].Cells[this.ColMacAddress.Name].Value));
lis.Add(Mes.Core.Utility.StrUtil.ValueToString(dataGridViewEx1.Rows[row_index].Cells[this.ColFloor.Name].Value));
lis.Add(Mes.Core.Utility.StrUtil.ValueToString(dataGridViewEx1.Rows[row_index].Cells[this.ColAdminDepartment.Name].Value));
lis.Add(Mes.Core.Utility.StrUtil.ValueToString(dataGridViewEx1.Rows[row_index].Cells[this.ColEnable.Name].Value));
lis.Add(Mes.Core.Utility.StrUtil.ValueToString(dataGridViewEx1.Rows[row_index].Cells[this.ColDescription.Name].Value));
string template = Mes.Core.Utility.StrUtil.BuildPara(lis);
string sql = "{? = call biz_device_pck.update_rows_for_ui(" + template + ")}";
InsertOrUpdatelist.Add(sql);
}
}
return InsertOrUpdatelist;
}
}
}
}
6)BaseInfoQueryForm
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
92using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace BIZDeviceUseAnalysis
{
public partial class BaseInfoQueryForm : Mes.ControlsEx.ExtendForm.QueryForm
{
public BaseInfoQueryForm()
{
InitializeComponent();
}
private void BaseInfoQueryForm_OnQuery(object sender, EventArgs e)
{
this.QuerySql = Sql.Core.GetDeviceInfo();
// 設備編碼
string txtDeviceCode = this.tbDeviceCode.Text.Trim();
List deviceCodeList = this.tbDeviceCode.MultirowValue;
if (deviceCodeList != null & deviceCodeList.Count > 0)
{
this.QuerySql += " AND device_code in (" + Mes.Core.Utility.StrUtil.BuildPara(deviceCodeList) + ") ";
}
else
{
if (txtDeviceCode != string.Empty)
{
this.QuerySql += " AND device_code " + Mes.Core.Utility.StrUtil.ProcInput(txtDeviceCode, false);
}
}
//設備名稱
string txtDeviceName = this.tbDeviceName.Text.Trim();
List deviceNameList = this.tbDeviceName.MultirowValue;
if (deviceNameList != null & deviceNameList.Count > 0)
{
this.QuerySql += " AND device_name in (" + Mes.Core.Utility.StrUtil.BuildPara(deviceNameList) + ") ";
}
else
{
if (txtDeviceName != string.Empty)
{
this.QuerySql += " AND device_name " + Mes.Core.Utility.StrUtil.ProcInput(txtDeviceName, false);
}
}
//mac地址
string txtMac = this.tbMacAddress.Text.Trim();
List macList = this.tbMacAddress.MultirowValue;
if (macList != null & macList.Count > 0)
{
this.QuerySql += " AND mac_address in (" + Mes.Core.Utility.StrUtil.BuildPara(macList) + ") ";
}
else
{
if (txtMac != string.Empty)
{
this.QuerySql += " AND mac_address " + Mes.Core.Utility.StrUtil.ProcInput(txtMac, false);
}
}
//部門
string txtDepartment = this.tbDepartment.Text.Trim();
List departmentList = this.tbDepartment.MultirowValue;
if (departmentList != null & departmentList.Count > 0)
{
this.QuerySql += " AND admin_department in (" + Mes.Core.Utility.StrUtil.BuildPara(departmentList) + ") ";
}
else
{
if (txtDepartment != string.Empty)
{
this.QuerySql += " AND admin_department " + Mes.Core.Utility.StrUtil.ProcInput(txtDepartment, false);
}
}
this.QuerySql += " order by device_code,device_name";
this.DialogResult = System.Windows.Forms.DialogResult.OK;
this.Close();
}
private void BaseInfoQueryForm_OnCancelQuery(object sender, EventArgs e)
{
this.tbDeviceCode.Text = this.tbDeviceName.Text =
this.tbMacAddress.Text = this.tbDepartment.Text = string.Empty;
}
}
}
7)SQL
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
133using Mes.ControlsEx;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BIZDeviceUseAnalysis.Sql
{
class Core
{
//按天
public static string GetMainSqlByDay(string startTime, string endTime,string mac,string condition)
{
string sql = @"
WITH baseInfoRecord AS(SELECT mac, SUM(counting) total, to_char(bmr.creation_date, 'yyyy-mm-dd'), device_code,device_name FROM biz_mac_record bmr, (SELECT 1 AS counting FROM dual),biz_device_info bdi WHERE bmr.creation_date >= to_date('" + startTime + @"', 'yyyy-mm-dd') AND bmr.creation_date <= to_date('" + endTime + @"', 'yyyy-mm-dd') AND bmr.mac=bdi.mac_address "+mac+@" "+condition+@" ) , noBaseInfo AS(SELECT DISTINCT mac, SUM(counting) total, to_char(bmr.creation_date, 'yyyy-mm-dd'),''device_code,''device_name FROM biz_mac_record bmr, (SELECT 1 AS counting FROM dual),biz_device_info bdi WHERE bmr.creation_date >= to_date('" + startTime + @"', 'yyyy-mm-dd') AND bmr.creation_date <= to_date('" + endTime + @"', 'yyyy-mm-dd') AND bmr.mac NOT IN (SELECT mac_address FROM biz_device_info) " + mac + @" " + condition + @" ) SELECT * FROM baseInfoRecord UNION ALL SELECT * FROM noBaseInfo"; return sql; } public static string GetColumnByDay(string startTime, string endTime) { string sql = @"SELECT DISTINCT to_char(bmr.creation_date,'yyyy-mm-dd') FROM biz_mac_record bmr WHERE bmr.creation_date >= to_date('" + startTime+ @"', 'yyyy-mm-dd') AND bmr.creation_date <= to_date('" + endTime + @"', 'yyyy-mm-dd') ORDER BY to_char(bmr.creation_date,'yyyy-mm-dd')"; return sql; } //按周 public static string GetMainSqlByWeek(string startTime, string endTime, string mac, string condition) { string sql = @" WITH baseInfoRecord AS(SELECT mac, SUM(counting) total, to_char(trunc(bmr.creation_date),'yyyy-iw'), device_code,device_name FROM biz_mac_record bmr, (SELECT 1 AS counting FROM dual),biz_device_info bdi WHERE bmr.creation_date >= to_date('" + startTime + @"', 'yyyy-mm-dd') AND bmr.creation_date <= to_date('" + endTime + @"', 'yyyy-mm-dd') AND bmr.mac=bdi.mac_address " + mac + @" " + condition + @" ) , noBaseInfo AS(SELECT DISTINCT mac, SUM(counting) total, to_char(trunc(bmr.creation_date),'yyyy-iw'),''device_code,''device_name FROM biz_mac_record bmr, (SELECT 1 AS counting FROM dual),biz_device_info bdi WHERE bmr.creation_date >= to_date('" + startTime + @"', 'yyyy-mm-dd') AND bmr.creation_date <= to_date('" + endTime + @"', 'yyyy-mm-dd') AND bmr.mac NOT IN (SELECT mac_address FROM biz_device_info) " + mac + @" " + condition + @" ) SELECT * FROM baseInfoRecord UNION ALL SELECT * FROM noBaseInfo"; return sql; } public static string GetColumnByWeek(string startTime, string endTime) { string sql = @"SELECT DISTINCT to_char(bmr.creation_date,'yyyy-iw') FROM biz_mac_record bmr WHERE bmr.creation_date >= to_date('" + startTime + @"', 'yyyy-mm-dd') AND bmr.creation_date <= to_date('" + endTime + @"', 'yyyy-mm-dd') ORDER BY to_char(bmr.creation_date,'yyyy-iw')"; return sql; } //按月 public static string GetMainSqlByMonth(string startTime, string endTime, string mac, string condition) { string sql = @" WITH baseInfoRecord AS(SELECT mac, SUM(counting) total, to_char(trunc(bmr.creation_date),'yyyy-mm'), device_code,device_name FROM biz_mac_record bmr, (SELECT 1 AS counting FROM dual),biz_device_info bdi WHERE bmr.creation_date >= to_date('" + startTime + @"', 'yyyy-mm-dd') AND bmr.creation_date <= to_date('" + endTime + @"', 'yyyy-mm-dd') AND bmr.mac=bdi.mac_address " + mac + @" " + condition + @" ) , noBaseInfo AS(SELECT DISTINCT mac, SUM(counting) total, to_char(trunc(bmr.creation_date),'yyyy-mm'),''device_code,''device_name FROM biz_mac_record bmr, (SELECT 1 AS counting FROM dual),biz_device_info bdi WHERE bmr.creation_date >= to_date('" + startTime + @"', 'yyyy-mm-dd') AND bmr.creation_date <= to_date('" + endTime + @"', 'yyyy-mm-dd') AND bmr.mac NOT IN (SELECT mac_address FROM biz_device_info) " + mac + @" " + condition + @" ) SELECT * FROM baseInfoRecord UNION ALL SELECT * FROM noBaseInfo"; return sql; } public static string GetColumnByMonth(string startTime, string endTime) { string sql = @"SELECT DISTINCT to_char(bmr.creation_date,'yyyy-mm') FROM biz_mac_record bmr WHERE bmr.creation_date >= to_date('" + startTime + @"', 'yyyy-mm-dd') AND bmr.creation_date <= to_date('" + endTime + @"', 'yyyy-mm-dd') ORDER BY to_char(bmr.creation_date,'yyyy-mm')"; return sql; } //設備基礎信息 public static string GetDeviceInfo() { string sql = @"select * from biz_device_info where 1=1"; return sql; } } }
8)運行效果
按日
按周
總結
以上是生活随笔為你收集整理的c#的chart标题_C#之Chart篇的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于类的符号输入过程第三篇
- 下一篇: 三维GIS软件十九重唱